2015-06-22 04:13:14 +12:00
|
|
|
#!/usr/bin/env python
|
2016-02-07 00:34:22 +13:00
|
|
|
# --!-- coding: utf8 --!--
|
|
|
|
from PyQt5.QtCore import QModelIndex
|
|
|
|
from PyQt5.QtCore import QSignalMapper
|
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
from PyQt5.QtGui import QBrush
|
|
|
|
from PyQt5.QtGui import QStandardItem
|
|
|
|
from PyQt5.QtGui import QStandardItemModel
|
|
|
|
from PyQt5.QtWidgets import QAction, QMenu
|
|
|
|
|
|
|
|
from manuskript.enums import Plot
|
2016-03-07 04:10:25 +13:00
|
|
|
from manuskript.enums import PlotStep
|
2016-02-07 00:34:22 +13:00
|
|
|
from manuskript.functions import toInt, mainWindow
|
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
|
|
|
|
class plotModel(QStandardItemModel):
|
2015-06-25 06:32:50 +12:00
|
|
|
def __init__(self, parent):
|
|
|
|
QStandardItemModel.__init__(self, 0, 3, parent)
|
2015-06-22 04:13:14 +12:00
|
|
|
self.setHorizontalHeaderLabels([i.name for i in Plot])
|
|
|
|
self.mw = mainWindow()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
self.updatePlotPersoButton()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# QUERIES
|
|
|
|
###############################################################################
|
2015-06-22 23:11:45 +12:00
|
|
|
|
|
|
|
def getPlotsByImportance(self):
|
|
|
|
plots = [[], [], []]
|
|
|
|
for i in range(self.rowCount()):
|
|
|
|
importance = self.item(i, Plot.importance.value).text()
|
|
|
|
ID = self.item(i, Plot.ID.value).text()
|
2016-02-07 00:34:22 +13:00
|
|
|
plots[2 - toInt(importance)].append(ID)
|
2015-06-22 23:11:45 +12:00
|
|
|
return plots
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-23 06:30:43 +12:00
|
|
|
def getSubPlotsByID(self, ID):
|
|
|
|
index = self.getIndexFromID(ID)
|
|
|
|
if not index.isValid():
|
|
|
|
return
|
2016-03-07 04:10:25 +13:00
|
|
|
index = index.sibling(index.row(), Plot.steps.value)
|
2015-06-23 06:30:43 +12:00
|
|
|
item = self.itemFromIndex(index)
|
|
|
|
lst = []
|
|
|
|
for i in range(item.rowCount()):
|
|
|
|
_ID = item.child(i, Plot.ID.value).text()
|
|
|
|
name = item.child(i, Plot.name.value).text()
|
2015-07-03 03:45:27 +12:00
|
|
|
summary = item.child(i, 3).text()
|
|
|
|
lst.append((_ID, name, summary))
|
2015-06-23 06:30:43 +12:00
|
|
|
return lst
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-23 06:30:43 +12:00
|
|
|
def getPlotNameByID(self, ID):
|
2015-06-22 23:11:45 +12:00
|
|
|
for i in range(self.rowCount()):
|
|
|
|
_ID = self.item(i, Plot.ID.value).text()
|
|
|
|
if _ID == ID or toInt(_ID) == ID:
|
|
|
|
name = self.item(i, Plot.name.value).text()
|
|
|
|
return name
|
|
|
|
return None
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2017-07-02 04:09:08 +12:00
|
|
|
def getPlotImportanceByID(self, ID):
|
|
|
|
for i in range(self.rowCount()):
|
|
|
|
_ID = self.item(i, Plot.ID.value).text()
|
|
|
|
if _ID == ID or toInt(_ID) == ID:
|
|
|
|
importance = self.item(i, Plot.importance.value).text()
|
|
|
|
return importance
|
|
|
|
return "0" # Default to "Minor"
|
|
|
|
|
2015-07-03 03:45:27 +12:00
|
|
|
def getSubPlotTextsByID(self, plotID, subplotRaw):
|
|
|
|
"""Returns a tuple (name, summary) for the suplot whose raw in the model
|
|
|
|
is ``subplotRaw``, of plot whose ID is ``plotID``.
|
|
|
|
"""
|
|
|
|
plotIndex = self.getIndexFromID(plotID)
|
|
|
|
name = plotIndex.child(subplotRaw, Plot.name.value).data()
|
|
|
|
summary = plotIndex.child(subplotRaw, 3).data() # 3 is for summary
|
2016-02-07 00:34:22 +13:00
|
|
|
return name, summary
|
|
|
|
|
2015-06-22 23:11:45 +12:00
|
|
|
def getIndexFromID(self, ID):
|
|
|
|
for i in range(self.rowCount()):
|
|
|
|
_ID = self.item(i, Plot.ID.value).text()
|
|
|
|
if _ID == ID or toInt(_ID) == ID:
|
|
|
|
return self.index(i, 0)
|
|
|
|
return QModelIndex()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 23:11:45 +12:00
|
|
|
def currentIndex(self):
|
2015-06-23 06:30:43 +12:00
|
|
|
i = self.mw.lstPlots.currentIndex()
|
2016-02-07 00:34:22 +13:00
|
|
|
if i.isValid():
|
2015-06-22 23:11:45 +12:00
|
|
|
return i
|
|
|
|
else:
|
|
|
|
return None
|
2016-02-07 00:34:22 +13:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# ADDING / REMOVING
|
|
|
|
###############################################################################
|
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
def addPlot(self):
|
|
|
|
p = QStandardItem(self.tr("New plot"))
|
|
|
|
_id = QStandardItem(self.getUniqueID())
|
|
|
|
importance = QStandardItem(str(0))
|
2016-03-07 04:10:25 +13:00
|
|
|
self.appendRow([p, _id, importance, QStandardItem("Characters"),
|
|
|
|
QStandardItem(), QStandardItem(), QStandardItem("Resolution steps")])
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
def getUniqueID(self, parent=QModelIndex()):
|
2016-02-07 00:34:22 +13:00
|
|
|
"""Returns an unused ID"""
|
2015-06-22 04:13:14 +12:00
|
|
|
parentItem = self.itemFromIndex(parent)
|
|
|
|
vals = []
|
|
|
|
for i in range(self.rowCount(parent)):
|
|
|
|
index = self.index(i, Plot.ID.value, parent)
|
2016-02-07 00:34:22 +13:00
|
|
|
# item = self.item(i, Plot.ID.value)
|
2015-06-22 04:13:14 +12:00
|
|
|
if index.isValid() and index.data():
|
|
|
|
vals.append(int(index.data()))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
k = 0
|
2016-02-07 00:34:22 +13:00
|
|
|
while k in vals:
|
|
|
|
k += 1
|
2015-06-22 04:13:14 +12:00
|
|
|
return str(k)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
def removePlot(self, index):
|
|
|
|
self.takeRow(index.row())
|
2016-02-07 00:34:22 +13:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# SUBPLOTS
|
|
|
|
###############################################################################
|
2015-07-07 01:00:22 +12:00
|
|
|
|
|
|
|
def headerData(self, section, orientation, role=Qt.DisplayRole):
|
|
|
|
if role == Qt.DisplayRole:
|
|
|
|
if orientation == Qt.Horizontal:
|
2016-03-07 04:10:25 +13:00
|
|
|
if section == PlotStep.name.value:
|
2015-07-07 01:00:22 +12:00
|
|
|
return self.tr("Name")
|
2016-03-07 04:10:25 +13:00
|
|
|
elif section == PlotStep.meta.value:
|
2015-07-07 01:00:22 +12:00
|
|
|
return self.tr("Meta")
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return QStandardItemModel.headerData(self, section, orientation, role)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-07 01:00:22 +12:00
|
|
|
def data(self, index, role=Qt.DisplayRole):
|
|
|
|
if index.parent().isValid() and \
|
2016-03-07 04:10:25 +13:00
|
|
|
index.parent().column() == Plot.steps.value and \
|
|
|
|
index.column() == PlotStep.meta.value:
|
2015-07-07 01:00:22 +12:00
|
|
|
if role == Qt.TextAlignmentRole:
|
|
|
|
return Qt.AlignRight | Qt.AlignVCenter
|
|
|
|
elif role == Qt.ForegroundRole:
|
|
|
|
return QBrush(Qt.gray)
|
|
|
|
else:
|
|
|
|
return QStandardItemModel.data(self, index, role)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-07-07 01:00:22 +12:00
|
|
|
else:
|
|
|
|
return QStandardItemModel.data(self, index, role)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
def addSubPlot(self):
|
2015-06-23 06:30:43 +12:00
|
|
|
index = self.mw.lstPlots.currentPlotIndex()
|
2015-06-22 23:11:45 +12:00
|
|
|
if not index.isValid():
|
2015-06-22 04:13:14 +12:00
|
|
|
return
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-07 04:10:25 +13:00
|
|
|
parent = index.sibling(index.row(), Plot.steps.value)
|
|
|
|
parentItem = self.item(index.row(), Plot.steps.value)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 23:11:45 +12:00
|
|
|
if not parentItem:
|
|
|
|
return
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-07 04:10:25 +13:00
|
|
|
p = QStandardItem(self.tr("New step"))
|
2015-06-22 04:13:14 +12:00
|
|
|
_id = QStandardItem(self.getUniqueID(parent))
|
|
|
|
summary = QStandardItem()
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 23:11:45 +12:00
|
|
|
# Don't know why, if summary is in third position, then drag/drop deletes it...
|
|
|
|
parentItem.appendRow([p, _id, QStandardItem(), summary])
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-23 07:07:38 +12:00
|
|
|
# Select last index
|
2016-02-07 00:34:22 +13:00
|
|
|
self.mw.lstSubPlots.setCurrentIndex(parent.child(self.rowCount(parent) - 1, 0))
|
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
def removeSubPlot(self):
|
|
|
|
index = self.mw.lstSubPlots.currentIndex()
|
|
|
|
if not index.isValid():
|
|
|
|
return
|
|
|
|
parent = index.parent()
|
|
|
|
parentItem = self.itemFromIndex(parent)
|
|
|
|
parentItem.takeRow(index.row())
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 23:11:45 +12:00
|
|
|
def flags(self, index):
|
|
|
|
parent = index.parent()
|
2016-02-07 00:34:22 +13:00
|
|
|
if parent.isValid(): # this is a subitem
|
|
|
|
return Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled
|
2015-06-22 23:11:45 +12:00
|
|
|
else:
|
|
|
|
return QStandardItemModel.flags(self, index)
|
2015-07-03 03:45:27 +12:00
|
|
|
|
2016-02-07 00:34:22 +13:00
|
|
|
###############################################################################
|
|
|
|
# PLOT PERSOS
|
|
|
|
###############################################################################
|
2015-07-03 03:45:27 +12:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
def addPlotPerso(self, v):
|
2015-06-23 06:30:43 +12:00
|
|
|
index = self.mw.lstPlots.currentPlotIndex()
|
2015-06-22 23:11:45 +12:00
|
|
|
if index.isValid():
|
2016-03-06 21:26:59 +13:00
|
|
|
if not self.item(index.row(), Plot.characters.value):
|
|
|
|
self.setItem(index.row(), Plot.characters.value, QStandardItem())
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-06 21:26:59 +13:00
|
|
|
item = self.item(index.row(), Plot.characters.value)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
# We check that the PersoID is not in the list yet
|
|
|
|
for i in range(item.rowCount()):
|
|
|
|
if item.child(i).text() == str(v):
|
|
|
|
return
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
item.appendRow(QStandardItem(str(v)))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
def removePlotPerso(self):
|
|
|
|
index = self.mw.lstPlotPerso.currentIndex()
|
|
|
|
if not index.isValid():
|
|
|
|
return
|
|
|
|
parent = index.parent()
|
|
|
|
parentItem = self.itemFromIndex(parent)
|
|
|
|
parentItem.takeRow(index.row())
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
def updatePlotPersoButton(self):
|
2015-06-25 06:41:23 +12:00
|
|
|
menu = QMenu(self.mw)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
menus = []
|
|
|
|
for i in [self.tr("Main"), self.tr("Secondary"), self.tr("Minor")]:
|
|
|
|
m = QMenu(i, menu)
|
|
|
|
menus.append(m)
|
|
|
|
menu.addMenu(m)
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
mpr = QSignalMapper(menu)
|
2016-03-04 04:38:38 +13:00
|
|
|
for i in range(self.mw.mdlCharacter.rowCount()):
|
|
|
|
a = QAction(self.mw.mdlCharacter.name(i), menu)
|
|
|
|
a.setIcon(self.mw.mdlCharacter.icon(i))
|
2015-06-29 20:22:18 +12:00
|
|
|
a.triggered.connect(mpr.map)
|
2016-03-04 04:38:38 +13:00
|
|
|
mpr.setMapping(a, int(self.mw.mdlCharacter.ID(i)))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
2016-03-04 04:38:38 +13:00
|
|
|
imp = toInt(self.mw.mdlCharacter.importance(i))
|
2016-02-07 00:34:22 +13:00
|
|
|
|
|
|
|
menus[2 - imp].addAction(a)
|
|
|
|
|
2015-06-22 04:13:14 +12:00
|
|
|
mpr.mapped.connect(self.addPlotPerso)
|
2016-02-07 00:34:22 +13:00
|
|
|
self.mw.btnAddPlotPerso.setMenu(menu)
|