2015-06-29 09:46:51 +12:00
|
|
|
#!/usr/bin/env python
|
2016-02-07 00:34:22 +13:00
|
|
|
# --!-- coding: utf8 --!--
|
|
|
|
from PyQt5.QtCore import QSize, QModelIndex, Qt
|
|
|
|
from PyQt5.QtGui import QPixmap, QColor, QIcon, QBrush
|
2016-03-04 04:38:38 +13:00
|
|
|
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QColorDialog
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
from manuskript.enums import Character
|
|
|
|
from manuskript.functions import iconColor, mainWindow
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
class characterTreeView(QTreeWidget):
|
|
|
|
"""
|
|
|
|
A QTreeWidget that displays characters from a characterModel in respect of their importance.
|
|
|
|
"""
|
2015-06-29 09:46:51 +12:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
QTreeWidget.__init__(self, parent)
|
|
|
|
self._model = None
|
|
|
|
self._catRow = [-1, -1, -1]
|
|
|
|
self._filter = ""
|
|
|
|
self._lastID = -1
|
|
|
|
self._updating = False
|
|
|
|
self.setRootIsDecorated(False)
|
|
|
|
self.setIndentation(10)
|
2015-06-29 20:22:18 +12:00
|
|
|
self.setHeaderHidden(True)
|
|
|
|
self.setIconSize(QSize(24, 24))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
self.setColumnCount(1)
|
|
|
|
self._rootItem = QTreeWidgetItem()
|
|
|
|
self.insertTopLevelItem(0, self._rootItem)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
def setCharactersModel(self, model):
|
2015-06-29 09:46:51 +12:00
|
|
|
self._model = model
|
|
|
|
self._model.dataChanged.connect(self.updateMaybe)
|
|
|
|
self._model.rowsInserted.connect(self.updateMaybe2)
|
|
|
|
self._model.rowsRemoved.connect(self.updateMaybe2)
|
|
|
|
self.updateItems()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
def setFilter(self, text):
|
|
|
|
self._filter = text
|
|
|
|
self.updateItems()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
def updateMaybe(self, topLeft, bottomRight):
|
|
|
|
if topLeft.parent() != QModelIndex():
|
|
|
|
return
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
if topLeft.column() <= Character.name.value <= bottomRight.column():
|
2015-06-29 09:46:51 +12:00
|
|
|
# Update name
|
|
|
|
self.updateNames()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
elif topLeft.column() <= Character.importance.value <= bottomRight.column():
|
2015-06-29 09:46:51 +12:00
|
|
|
# Importance changed
|
|
|
|
self.updateItems()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
def updateMaybe2(self, parent, first, last):
|
2016-02-07 00:34:22 +13:00
|
|
|
# Rows inserted or removed, we update only if they are topLevel rows.
|
2015-06-29 09:46:51 +12:00
|
|
|
if parent == QModelIndex():
|
|
|
|
self.updateItems()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
def updateNames(self):
|
|
|
|
for i in range(self.topLevelItemCount()):
|
|
|
|
item = self.topLevelItem(i)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
for child in range(item.childCount()):
|
|
|
|
sub = item.child(child)
|
2015-06-29 09:46:51 +12:00
|
|
|
ID = sub.data(0, Qt.UserRole)
|
2016-03-04 04:38:38 +13:00
|
|
|
if ID is not None:
|
2015-06-29 20:22:18 +12:00
|
|
|
# Update name
|
2016-03-04 04:38:38 +13:00
|
|
|
c = self._model.getCharacterByID(ID)
|
|
|
|
name = c.name()
|
2015-06-29 09:46:51 +12:00
|
|
|
sub.setText(0, name)
|
2015-06-29 20:22:18 +12:00
|
|
|
# Update icon
|
|
|
|
px = QPixmap(32, 32)
|
2016-03-04 04:38:38 +13:00
|
|
|
color = c.color()
|
2015-06-29 20:22:18 +12:00
|
|
|
px.fill(color)
|
|
|
|
sub.setIcon(0, QIcon(px))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
def updateItems(self):
|
|
|
|
if not self._model:
|
|
|
|
return
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
if self.currentItem():
|
|
|
|
self._lastID = self.currentItem().data(0, Qt.UserRole)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
self._updating = True
|
|
|
|
self.clear()
|
2016-03-04 04:38:38 +13:00
|
|
|
characters = self._model.getCharactersByImportance()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
h = [self.tr("Main"), self.tr("Secondary"), self.tr("Minor")]
|
2016-03-04 04:38:38 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
for i in range(3):
|
2016-03-04 04:38:38 +13:00
|
|
|
# Create category item
|
2015-06-29 09:46:51 +12:00
|
|
|
cat = QTreeWidgetItem(self, [h[i]])
|
|
|
|
cat.setBackground(0, QBrush(QColor(Qt.blue).lighter(190)))
|
|
|
|
cat.setForeground(0, QBrush(Qt.darkBlue))
|
|
|
|
cat.setTextAlignment(0, Qt.AlignCenter)
|
|
|
|
f = cat.font(0)
|
|
|
|
f.setBold(True)
|
|
|
|
cat.setFont(0, f)
|
|
|
|
self.addTopLevelItem(cat)
|
2016-02-07 00:34:22 +13:00
|
|
|
# cat.setChildIndicatorPolicy(cat.DontShowIndicator)
|
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
for c in characters[i]:
|
|
|
|
name = c.name()
|
|
|
|
# Check if name passes filter
|
2016-02-07 00:34:22 +13:00
|
|
|
if not self._filter.lower() in name.lower():
|
2015-06-29 09:46:51 +12:00
|
|
|
continue
|
2016-03-04 04:38:38 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
item = QTreeWidgetItem(cat, [name])
|
2016-03-04 04:38:38 +13:00
|
|
|
item.setData(0, Qt.UserRole, c.ID())
|
2015-06-29 09:46:51 +12:00
|
|
|
px = QPixmap(32, 32)
|
2016-03-04 04:38:38 +13:00
|
|
|
color = QColor(c.color())
|
2015-06-29 09:46:51 +12:00
|
|
|
px.fill(color)
|
|
|
|
item.setIcon(0, QIcon(px))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
if c.ID() == self._lastID:
|
2015-06-29 09:46:51 +12:00
|
|
|
self.setCurrentItem(item)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
self.expandAll()
|
|
|
|
self._updating = False
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
def removeCharacter(self):
|
|
|
|
"""
|
|
|
|
Removes selected character.
|
|
|
|
"""
|
|
|
|
ID = self.currentCharacterID()
|
|
|
|
if ID:
|
|
|
|
self._model.removeCharacter(ID)
|
|
|
|
|
|
|
|
def choseCharacterColor(self):
|
|
|
|
ID = self.currentCharacterID()
|
|
|
|
c = self._model.getCharacterByID(ID)
|
|
|
|
if c:
|
|
|
|
color = iconColor(c.icon)
|
|
|
|
else:
|
|
|
|
color = Qt.white
|
|
|
|
self.colorDialog = QColorDialog(color, mainWindow())
|
|
|
|
color = self.colorDialog.getColor(color)
|
|
|
|
if color.isValid():
|
|
|
|
c.setColor(color)
|
|
|
|
mainWindow().updateCharacterColor(ID)
|
|
|
|
|
|
|
|
def addCharacterInfo(self):
|
|
|
|
self._model.addCharacterInfo(self.currentCharacterID())
|
|
|
|
|
|
|
|
def removeCharacterInfo(self):
|
|
|
|
self._model.removeCharacterInfo(self.currentCharacterID(),
|
|
|
|
)
|
|
|
|
|
|
|
|
def currentCharacterID(self):
|
2015-06-29 09:46:51 +12:00
|
|
|
ID = None
|
|
|
|
if self.currentItem():
|
|
|
|
ID = self.currentItem().data(0, Qt.UserRole)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
return ID
|
|
|
|
|
|
|
|
def currentCharacter(self):
|
|
|
|
"""
|
|
|
|
Returns the selected character
|
|
|
|
@return: Character
|
|
|
|
"""
|
|
|
|
ID = self.currentCharacterID()
|
|
|
|
return self._model.getCharacterByID(ID)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 05:18:30 +13:00
|
|
|
def getItemByID(self, ID):
|
|
|
|
for t in range(self.topLevelItemCount()):
|
|
|
|
for i in range(self.topLevelItem(t).childCount()):
|
|
|
|
item = self.topLevelItem(t).child(i)
|
|
|
|
if item.data(0, Qt.UserRole) == ID:
|
|
|
|
return item
|
|
|
|
|
2015-06-29 09:46:51 +12:00
|
|
|
def mouseDoubleClickEvent(self, event):
|
|
|
|
item = self.currentItem()
|
|
|
|
# Catching double clicks to forbid collapsing of toplevel items
|
|
|
|
if item.parent():
|
|
|
|
QTreeWidget.mouseDoubleClickEvent(self, event)
|