2015-06-25 01:43:35 +12:00
|
|
|
#!/usr/bin/env python
|
2016-02-07 00:34:22 +13:00
|
|
|
# --!-- coding: utf8 --!--
|
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
from PyQt5.QtGui import QIcon, QBrush
|
|
|
|
from PyQt5.QtWidgets import QComboBox
|
|
|
|
|
|
|
|
from manuskript.enums import Outline
|
2015-06-25 01:43:35 +12:00
|
|
|
|
|
|
|
|
|
|
|
class cmbOutlineTypeChoser(QComboBox):
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
QComboBox.__init__(self, parent)
|
|
|
|
self.activated[int].connect(self.submit)
|
|
|
|
self._column = Outline.type.value
|
|
|
|
self._index = None
|
|
|
|
self._indexes = None
|
|
|
|
self._updating = False
|
|
|
|
self._various = False
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def setModel(self, mdlOutline):
|
|
|
|
self.mdlOutline = mdlOutline
|
|
|
|
self.mdlOutline.dataChanged.connect(self.update)
|
|
|
|
self.updateItems()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def updateItems(self):
|
|
|
|
self.clear()
|
|
|
|
types = [
|
|
|
|
("t2t", self.tr("Txt2Tags"), "text-x-generic"),
|
|
|
|
("html", self.tr("Rich Text (html)"), "text-html"),
|
|
|
|
("txt", self.tr("Plain Text"), "text-x-generic"),
|
2016-02-07 00:34:22 +13:00
|
|
|
]
|
2015-06-25 01:43:35 +12:00
|
|
|
for t in types:
|
|
|
|
self.addItem(QIcon.fromTheme(t[2]), t[1], t[0])
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
self._various = False
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
if self._index or self._indexes:
|
|
|
|
self.updateSelectedItem()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def setCurrentModelIndex(self, index):
|
|
|
|
self._indexes = None
|
|
|
|
if index.column() != self._column:
|
|
|
|
index = index.sibling(index.row(), self._column)
|
|
|
|
self._index = index
|
|
|
|
# Disabled if item type is not text
|
|
|
|
self.setEnabled(index.internalPointer().type() in ["t2t", "html", "txt"])
|
|
|
|
self.updateItems()
|
|
|
|
self.updateSelectedItem()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def setCurrentModelIndexes(self, indexes):
|
|
|
|
self._indexes = []
|
|
|
|
self._index = None
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
hasText = False
|
|
|
|
for i in indexes:
|
|
|
|
if i.isValid():
|
|
|
|
if i.column() != self._column:
|
|
|
|
i = i.sibling(i.row(), self._column)
|
|
|
|
self._indexes.append(i)
|
|
|
|
if i.internalPointer().type() in ["t2t", "html", "txt"]:
|
|
|
|
hasText = True
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
self.setEnabled(hasText)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
self.updateItems()
|
|
|
|
self.updateSelectedItem()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def update(self, topLeft, bottomRight):
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
if self._updating:
|
|
|
|
# We are currently putting data in the model, so no updates
|
|
|
|
return
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
if self._index:
|
|
|
|
if topLeft.row() <= self._index.row() <= bottomRight.row():
|
|
|
|
self.updateSelectedItem()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
elif self._indexes:
|
|
|
|
update = False
|
|
|
|
for i in self._indexes:
|
|
|
|
if topLeft.row() <= i.row() <= bottomRight.row():
|
|
|
|
update = True
|
|
|
|
if update:
|
|
|
|
self.updateSelectedItem()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def getType(self, index):
|
|
|
|
item = index.internalPointer()
|
|
|
|
return item.type()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def updateSelectedItem(self):
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
if self._updating:
|
|
|
|
return
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
if self._index:
|
|
|
|
_type = self.getType(self._index)
|
|
|
|
i = self.findData(_type)
|
|
|
|
if i != -1:
|
|
|
|
self.setCurrentIndex(i)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
elif self._indexes:
|
|
|
|
types = []
|
|
|
|
same = True
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
for i in self._indexes:
|
|
|
|
types.append(self.getType(i))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
for t in types[1:]:
|
|
|
|
if t != types[0]:
|
|
|
|
same = False
|
|
|
|
break
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
if same:
|
|
|
|
self._various = False
|
|
|
|
i = self.findData(types[0])
|
|
|
|
if i != -1:
|
|
|
|
self.setCurrentIndex(i)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
else:
|
|
|
|
if not self._various:
|
|
|
|
self.insertItem(0, self.tr("Various"))
|
|
|
|
f = self.font()
|
|
|
|
f.setItalic(True)
|
|
|
|
self.setItemData(0, f, Qt.FontRole)
|
|
|
|
self.setItemData(0, QBrush(Qt.darkGray), Qt.ForegroundRole)
|
|
|
|
self._various = True
|
|
|
|
self.setCurrentIndex(0)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
else:
|
|
|
|
self.setCurrentIndex(0)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
def submit(self, idx):
|
|
|
|
if self._index:
|
|
|
|
self.mdlOutline.setData(self._index, self.currentData())
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
elif self._indexes:
|
|
|
|
value = self.currentData()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
if self._various and self.currentIndex() == 0:
|
|
|
|
return
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-25 01:43:35 +12:00
|
|
|
self._updating = True
|
|
|
|
for i in self._indexes:
|
|
|
|
self.mdlOutline.setData(i, value)
|
2016-02-07 00:34:22 +13:00
|
|
|
self._updating = False
|