2015-06-27 10:22:46 +12:00
|
|
|
#!/usr/bin/env python
|
2016-02-07 00:34:22 +13:00
|
|
|
# --!-- coding: utf8 --!--
|
|
|
|
from PyQt5.QtCore import pyqtSignal, Qt, QRect
|
2017-11-15 02:48:28 +13:00
|
|
|
from PyQt5.QtGui import QBrush, QFontMetrics, QPalette, QColor
|
2016-02-07 00:34:22 +13:00
|
|
|
from PyQt5.QtWidgets import QWidget, QListWidgetItem, QStyledItemDelegate, QStyle
|
|
|
|
|
|
|
|
from manuskript.functions import mainWindow
|
|
|
|
from manuskript.ui.editors.completer_ui import Ui_completer
|
|
|
|
from manuskript.models import references as Ref
|
2017-11-15 02:48:28 +13:00
|
|
|
from manuskript.ui import style as S
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 10:22:46 +12:00
|
|
|
|
|
|
|
class completer(QWidget, Ui_completer):
|
|
|
|
activated = pyqtSignal(str)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 10:22:46 +12:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
QWidget.__init__(self, parent)
|
|
|
|
self.setupUi(self)
|
|
|
|
self.setWindowFlags(Qt.Popup)
|
2015-06-27 20:27:52 +12:00
|
|
|
self.text.textChanged.connect(self.updateListFromData)
|
2015-06-27 10:22:46 +12:00
|
|
|
self.text.returnPressed.connect(self.submit)
|
2015-06-27 20:27:52 +12:00
|
|
|
self.listDelegate = listCompleterDelegate(self)
|
|
|
|
self.list.setItemDelegate(self.listDelegate)
|
2017-05-24 03:02:04 +12:00
|
|
|
self.list.itemClicked.connect(self.submit)
|
2015-06-30 22:27:43 +12:00
|
|
|
self.list.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
2015-06-27 10:22:46 +12:00
|
|
|
self.hide()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 20:27:52 +12:00
|
|
|
def popup(self, completion=""):
|
2015-06-30 22:27:43 +12:00
|
|
|
self.updateListFromData()
|
2015-06-27 20:27:52 +12:00
|
|
|
self.text.setText(completion)
|
2015-06-27 10:22:46 +12:00
|
|
|
self.text.setFocus(Qt.PopupFocusReason)
|
|
|
|
self.show()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 10:22:46 +12:00
|
|
|
def addCategory(self, title):
|
|
|
|
item = QListWidgetItem(title)
|
2017-11-15 02:48:28 +13:00
|
|
|
item.setBackground(QBrush(QColor(S.highlightLight)))
|
|
|
|
item.setForeground(QBrush(QColor(S.highlightedTextDark)))
|
2015-06-27 10:22:46 +12:00
|
|
|
item.setFlags(Qt.ItemIsEnabled)
|
|
|
|
self.list.addItem(item)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 10:22:46 +12:00
|
|
|
def updateListFromData(self):
|
2015-06-30 22:27:43 +12:00
|
|
|
data = mainWindow().cheatSheet.data
|
2015-06-27 10:22:46 +12:00
|
|
|
self.list.clear()
|
2015-06-30 22:27:43 +12:00
|
|
|
for cat in data:
|
|
|
|
filtered = [i for i in data[cat] if self.text.text().lower() in i[0].lower()]
|
2015-06-27 20:27:52 +12:00
|
|
|
if filtered:
|
|
|
|
self.addCategory(cat[0])
|
|
|
|
for item in filtered:
|
|
|
|
i = QListWidgetItem(item[0])
|
2015-07-09 08:06:00 +12:00
|
|
|
i.setData(Qt.UserRole, Ref.EmptyRef.format(cat[1], item[1], item[0]))
|
2016-02-07 00:34:22 +13:00
|
|
|
i.setData(Qt.UserRole + 1, item[2])
|
2015-06-27 20:27:52 +12:00
|
|
|
self.list.addItem(i)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 10:22:46 +12:00
|
|
|
self.list.setCurrentRow(1)
|
|
|
|
self.text.setFocus(Qt.PopupFocusReason)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 10:22:46 +12:00
|
|
|
def submit(self):
|
|
|
|
i = self.list.currentItem()
|
|
|
|
self.activated.emit(i.data(Qt.UserRole))
|
|
|
|
self.hide()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 10:22:46 +12:00
|
|
|
def keyPressEvent(self, event):
|
|
|
|
if event.key() in [Qt.Key_Up, Qt.Key_Down]:
|
|
|
|
self.list.keyPressEvent(event)
|
|
|
|
else:
|
2015-06-27 20:27:52 +12:00
|
|
|
QWidget.keyPressEvent(self, event)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
|
|
|
|
2015-06-27 20:27:52 +12:00
|
|
|
class listCompleterDelegate(QStyledItemDelegate):
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
QStyledItemDelegate.__init__(self, parent)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 20:27:52 +12:00
|
|
|
def paint(self, painter, option, index):
|
2016-02-07 00:34:22 +13:00
|
|
|
extra = index.data(Qt.UserRole + 1)
|
2015-06-27 20:27:52 +12:00
|
|
|
if not extra:
|
|
|
|
return QStyledItemDelegate.paint(self, painter, option, index)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 20:27:52 +12:00
|
|
|
else:
|
|
|
|
if option.state & QStyle.State_Selected:
|
|
|
|
painter.fillRect(option.rect, option.palette.color(QPalette.Inactive, QPalette.Highlight))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 20:27:52 +12:00
|
|
|
title = index.data()
|
|
|
|
extra = " - {}".format(extra)
|
|
|
|
painter.drawText(option.rect, Qt.AlignLeft, title)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-27 20:27:52 +12:00
|
|
|
fm = QFontMetrics(option.font)
|
|
|
|
w = fm.width(title)
|
|
|
|
r = QRect(option.rect)
|
|
|
|
r.setLeft(r.left() + w)
|
|
|
|
painter.save()
|
2017-11-15 02:48:28 +13:00
|
|
|
if option.state & QStyle.State_Selected:
|
|
|
|
painter.setPen(QColor(S.highlightedTextLight))
|
|
|
|
else:
|
|
|
|
painter.setPen(QColor(S.textLight))
|
|
|
|
|
2015-06-27 20:27:52 +12:00
|
|
|
painter.drawText(r, Qt.AlignLeft, extra)
|
2016-02-07 00:34:22 +13:00
|
|
|
painter.restore()
|