manuskript/manuskript/ui/cheatSheet.py

175 lines
6 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
from PyQt5.QtGui import QBrush, QCursor, QPalette, QFontMetrics
from PyQt5.QtWidgets import QWidget, QListWidgetItem, QToolTip, QStyledItemDelegate, QStyle
from manuskript.enums import Perso
from manuskript.enums import Plot
from manuskript.functions import lightBlue
from manuskript.functions import mainWindow
from manuskript.ui.cheatSheet_ui import Ui_cheatSheet
from manuskript.models import references as Ref
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)
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
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)
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-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
self.outlineModel = None
self.persoModel = 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.persoModel = mw.mdlPersos
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.persoModel.dataChanged.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
2015-06-30 22:27:43 +12:00
def populate(self):
if self.persoModel:
d = []
2016-02-07 00:34:22 +13:00
2015-06-30 22:27:43 +12:00
for r in range(self.persoModel.rowCount()):
name = self.persoModel.item(r, Perso.name.value).text()
ID = self.persoModel.item(r, Perso.ID.value).text()
imp = self.persoModel.item(r, Perso.importance.value).text()
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-03 03:33:05 +12:00
self.data[(self.tr("Characters"), Ref.PersoLetter)] = 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()):
name = self.plotModel.item(r, Plot.name.value).text()
ID = self.plotModel.item(r, Plot.ID.value).text()
imp = self.plotModel.item(r, Plot.importance.value).text()
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)
item.setBackground(QBrush(lightBlue()))
item.setForeground(QBrush(Qt.darkBlue))
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:
filtered = [i for i in self.data[cat] if self.txtFilter.text().lower() in i[0].lower()]
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):
i = self.list.currentItem()
ref = i.data(Qt.UserRole)
if ref:
2015-07-03 03:33:05 +12:00
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()