manuskript/manuskript/ui/cheatSheet.py

196 lines
6.8 KiB
Python
Raw Normal View History

2015-06-30 22:27:43 +12:00
#!/usr/bin/env python
2016-02-07 00:34:22 +13:00
# --!-- coding: utf8 --!--
from PyQt5.QtCore import pyqtSignal, Qt, QTimer, QRect
2017-11-15 02:48:28 +13:00
from PyQt5.QtGui import QBrush, QCursor, QPalette, QFontMetrics, QColor
2016-02-07 00:34:22 +13:00
from PyQt5.QtWidgets import QWidget, QListWidgetItem, QToolTip, QStyledItemDelegate, QStyle
from manuskript.enums import Character
2016-02-07 00:34:22 +13:00
from manuskript.enums import Plot
from manuskript.functions import mainWindow
2017-11-15 02:48:28 +13:00
from manuskript.ui import style as S
2016-02-07 00:34:22 +13:00
from manuskript.ui.cheatSheet_ui import Ui_cheatSheet
from manuskript.models import references as Ref
2016-02-08 22:52:25 +13:00
from manuskript.ui.editors.completer import completer
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
class cheatSheet(QWidget, Ui_cheatSheet):
activated = pyqtSignal(str)
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setupUi(self)
2017-11-15 02:48:28 +13:00
self.txtFilter.setStyleSheet(S.lineEditSS())
2015-07-01 01:38:14 +12:00
self.splitter.setStretchFactor(0, 5)
2015-06-30 22:27:43 +12:00
self.splitter.setStretchFactor(1, 70)
2016-02-07 00:34:22 +13:00
2016-02-08 22:52:25 +13:00
self.txtFilter.textChanged.connect(self.textChanged)
2015-06-30 22:27:43 +12:00
self.txtFilter.textChanged.connect(self.updateListFromData)
self.txtFilter.returnPressed.connect(self.showInfos)
self.listDelegate = listCompleterDelegate(self)
self.list.setItemDelegate(self.listDelegate)
self.list.itemActivated.connect(self.showInfos)
self.list.itemClicked.connect(self.showInfos)
2016-02-10 04:47:17 +13:00
self.hideList()
2015-07-03 03:33:05 +12:00
self.list.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
2015-06-30 22:27:43 +12:00
self.view.linkActivated.connect(self.openLink)
2015-07-01 00:01:32 +12:00
self.view.linkHovered.connect(self.linkHovered)
2016-02-10 04:47:17 +13:00
self.btnShowList.toggled.connect(self.list.setVisible)
self.line.hide()
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
self.outlineModel = None
self.characterModel = None
2015-07-06 19:45:28 +12:00
self.plotModel = None
self.worldModel = None
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
self.populateTimer = QTimer(self)
self.populateTimer.setSingleShot(True)
self.populateTimer.setInterval(500)
self.populateTimer.timeout.connect(self.populate)
self.populateTimer.stop()
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
self.data = {}
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
self.populate()
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
def setModels(self):
mw = mainWindow()
self.outlineModel = mw.mdlOutline
self.characterModel = mw.mdlCharacter
self.plotModel = mw.mdlPlots
self.worldModel = mw.mdlWorld
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
self.outlineModel.dataChanged.connect(self.populateTimer.start)
self.characterModel.dataChanged.connect(self.populateTimer.start)
self.characterModel.rowsInserted.connect(self.populateTimer.start)
self.characterModel.rowsRemoved.connect(self.populateTimer.start)
2015-07-06 19:45:28 +12:00
self.plotModel.dataChanged.connect(self.populateTimer.start)
self.worldModel.dataChanged.connect(self.populateTimer.start)
2016-02-07 00:34:22 +13:00
2015-07-03 03:33:05 +12:00
self.populate()
2016-02-07 00:34:22 +13:00
2016-02-08 22:52:25 +13:00
def textChanged(self, text):
if not text:
2016-02-10 04:47:17 +13:00
self.hideList()
self.list.clear()
self.view.setText("")
2016-02-08 22:52:25 +13:00
else:
self.list.show()
2016-02-10 04:47:17 +13:00
def hideList(self):
if not self.btnShowList.isChecked():
self.list.hide()
2015-06-30 22:27:43 +12:00
def populate(self):
if self.characterModel:
2015-06-30 22:27:43 +12:00
d = []
2016-02-07 00:34:22 +13:00
for c in self.characterModel.characters:
imp = [self.tr("Minor"), self.tr("Secondary"), self.tr("Main")][int(c.importance())]
d.append((c.name(), c.ID(), imp))
2016-02-07 00:34:22 +13:00
self.data[(self.tr("Characters"), Ref.CharacterLetter)] = d
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
if self.outlineModel:
d = []
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
def addChildren(item):
for c in item.children():
d.append((c.title(), c.ID(), c.path()))
addChildren(c)
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
r = self.outlineModel.rootItem
addChildren(r)
2016-02-07 00:34:22 +13:00
2015-07-03 03:33:05 +12:00
self.data[(self.tr("Texts"), Ref.TextLetter)] = d
2016-02-07 00:34:22 +13:00
2015-07-06 19:45:28 +12:00
if self.plotModel:
d = []
2016-02-07 00:34:22 +13:00
2015-07-06 19:45:28 +12:00
for r in range(self.plotModel.rowCount()):
2017-11-16 09:05:48 +13:00
name = self.plotModel.item(r, Plot.name).text()
ID = self.plotModel.item(r, Plot.ID).text()
imp = self.plotModel.item(r, Plot.importance).text()
2015-07-06 19:45:28 +12:00
imp = [self.tr("Minor"), self.tr("Secondary"), self.tr("Main")][int(imp)]
d.append((name, ID, imp))
2016-02-07 00:34:22 +13:00
2015-07-06 19:45:28 +12:00
self.data[(self.tr("Plots"), Ref.PlotLetter)] = d
2016-02-07 00:34:22 +13:00
if self.worldModel:
d = self.worldModel.listAll()
self.data[(self.tr("World"), Ref.WorldLetter)] = d
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
self.updateListFromData()
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +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-30 22:27:43 +12:00
item.setFlags(Qt.ItemIsEnabled)
f = item.font()
f.setBold(True)
item.setFont(f)
self.list.addItem(item)
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
def updateListFromData(self):
self.list.clear()
for cat in self.data:
2016-02-10 04:47:17 +13:00
filtered = [i for i in self.data[cat] if self.txtFilter.text().lower() in i[0].lower() or
self.txtFilter.text().lower() in cat[0].lower()]
2015-06-30 22:27:43 +12:00
if filtered:
self.addCategory(cat[0])
for item in filtered:
i = QListWidgetItem(item[0])
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-30 22:27:43 +12:00
self.list.addItem(i)
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
self.list.setCurrentRow(1)
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
def showInfos(self):
2016-02-10 04:47:17 +13:00
self.hideList()
if self.list and len(self.txtFilter.text()) != 0:
i = self.list.currentItem()
ref = i.data(Qt.UserRole)
if ref:
self.view.setText(Ref.infos(ref))
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
def openLink(self, link):
2015-07-03 03:33:05 +12:00
Ref.open(link)
2016-02-07 00:34:22 +13:00
2015-07-01 00:01:32 +12:00
def linkHovered(self, link):
if link:
2015-07-03 03:33:05 +12:00
QToolTip.showText(QCursor.pos(), Ref.tooltip(link))
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
def keyPressEvent(self, event):
if event.key() in [Qt.Key_Up, Qt.Key_Down]:
self.list.keyPressEvent(event)
else:
QWidget.keyPressEvent(self, event)
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
class listCompleterDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
QStyledItemDelegate.__init__(self, parent)
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
def paint(self, painter, option, index):
2016-02-07 00:34:22 +13:00
extra = index.data(Qt.UserRole + 1)
2015-06-30 22:27:43 +12:00
if not extra:
return QStyledItemDelegate.paint(self, painter, option, index)
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +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-30 22:27:43 +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-30 22:27:43 +12:00
fm = QFontMetrics(option.font)
w = fm.width(title)
r = QRect(option.rect)
r.setLeft(r.left() + w)
painter.save()
painter.setPen(Qt.gray)
painter.drawText(r, Qt.AlignLeft, extra)
2016-02-07 00:34:22 +13:00
painter.restore()