From edb6c41788080c7902cbddf6f8be3e428854054a Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 19 May 2019 21:55:50 +0200 Subject: [PATCH] Manipulating statuses now seem to work, and update correctly. --- nw/enum.py | 9 ---- nw/gui/doctabs.py | 111 ---------------------------------------- nw/gui/doctree.py | 9 +++- nw/gui/itemeditor.py | 4 +- nw/gui/projecteditor.py | 58 +++++++++++---------- nw/gui/winmain.py | 4 +- nw/project/document.py | 3 +- nw/project/project.py | 30 +++++++++-- nw/project/status.py | 58 ++++++++++++++++----- 9 files changed, 114 insertions(+), 172 deletions(-) delete mode 100644 nw/gui/doctabs.py diff --git a/nw/enum.py b/nw/enum.py index 5bbdb311..61540882 100644 --- a/nw/enum.py +++ b/nw/enum.py @@ -76,12 +76,3 @@ class nwAlert(Enum): BUG = 3 # END Enum nwAlert - -class nwChanged(Enum): - - NONE = 0 - DELETE = 1 - NEW = 2 - IGNORE = 3 - -# END Enum nwAlert diff --git a/nw/gui/doctabs.py b/nw/gui/doctabs.py deleted file mode 100644 index b8e5c46c..00000000 --- a/nw/gui/doctabs.py +++ /dev/null @@ -1,111 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter GUI Document Tabs - - novelWriter – GUI Document Tabs -================================= - Class holding the document tab view - - File History: - Created: 2018-09-29 [0.0.1] - -""" - -import logging -import nw - -from PyQt5.QtCore import Qt -from PyQt5.QtWidgets import QTabWidget, QWidget, QVBoxLayout - -from nw.gui.doceditor import GuiDocEditor -from nw.gui.projecteditor import GuiProjectEditor -from nw.gui.aboutview import GuiAboutView - -logger = logging.getLogger(__name__) - -class GuiDocTabs(QTabWidget): - - def __init__(self): - QTabWidget.__init__(self) - - logger.debug("Initialising DocTabs ...") - self.mainConf = nw.CONFIG - self.tabList = [] - - self.resize(900,500) - self.setTabsClosable(True) - - self.tabBar().tabCloseRequested.connect(self._doCloseTab) - - self.createTab(None,nw.DOCTYPE_ABOUT) - - logger.debug("DocTabs initialisation complete") - - return - - def createTab(self, theName=None, tabType=None): - if tabType == nw.DOCTYPE_ABOUT: - self._createTabAbout() - return True - elif tabType == nw.DOCTYPE_PROJECT: - self._createTabProject(theName) - return True - elif tabType == nw.DOCTYPE_DOC: - self._createTabDoc(theName) - return True - return False - - # - # Internal Functions - # - - def _createTabDoc(self, docName=None): - if docName is None: - tabName = "New Document" - else: - tabName = docName - thisTab = GuiDocEditor() - self.addTab(thisTab,tabName) - self.setCurrentWidget(thisTab) - return True - - def _createTabProject(self, projectName=None): - """Display or create the project settings page. - """ - for i in range(self.count()): - thisTab = self.widget(i) - if isinstance(thisTab, GuiProjectEditor): - self.setCurrentWidget(thisTab) - return True - if projectName is None: - tabName = "New Project" - else: - tabName = projectName - thisTab = GuiProjectEditor() - self.addTab(thisTab,tabName) - self.setCurrentWidget(thisTab) - return True - - def _createTabAbout(self): - """Display or create the About tab. - """ - for i in range(self.count()): - thisTab = self.widget(i) - if isinstance(thisTab, GuiAboutView): - self.setCurrentWidget(thisTab) - return True - thisTab = GuiAboutView() - self.addTab(thisTab,"About") - self.setCurrentWidget(thisTab) - return True - - # - # Signals - # - - def _doCloseTab(self, tabIdx): - logger.verbose("User requested tab %d to be closed." % tabIdx) - self.removeTab(tabIdx) - logger.verbose("Tab %d closed." % tabIdx) - return - -# END Class GuiDocTabs diff --git a/nw/gui/doctree.py b/nw/gui/doctree.py index e764709a..e0f066ad 100644 --- a/nw/gui/doctree.py +++ b/nw/gui/doctree.py @@ -259,10 +259,15 @@ class GuiDocTree(QTreeWidget): tStatus = nwLabels.CLASS_FLAG[nwItem.itemClass] if nwItem.itemType == nwItemType.FILE: tStatus += "."+nwLabels.LAYOUT_FLAG[nwItem.itemLayout] + iStatus = nwItem.itemStatus if tClass == nwItemClass.NOVEL: - flagIcon = self.theParent.statusIcons[nwItem.itemStatus] + if iStatus is None: + iStatus = self.theProject.statusItems.checkEntry(iStatus) + flagIcon = self.theParent.statusIcons[iStatus] else: - flagIcon = self.theParent.importIcons[nwItem.itemStatus] + if iStatus is None: + iStatus = self.theProject.importItems.checkEntry(iStatus) + flagIcon = self.theParent.importIcons[iStatus] trItem.setText(self.C_NAME, tName) trItem.setText(self.C_FLAGS,tStatus) diff --git a/nw/gui/itemeditor.py b/nw/gui/itemeditor.py index 7939b8a5..06edf0fd 100644 --- a/nw/gui/itemeditor.py +++ b/nw/gui/itemeditor.py @@ -55,12 +55,12 @@ class GuiItemEditor(QDialog): self.editLayout = QComboBox() if self.theItem.itemClass == nwItemClass.NOVEL: - for sLabel, sCol in self.theProject.statusItems: + for sLabel, _, _ in self.theProject.statusItems: self.editStatus.addItem( self.theParent.statusIcons[sLabel], sLabel, sLabel ) else: - for sLabel, sCol in self.theProject.importItems: + for sLabel, _, _ in self.theProject.importItems: self.editStatus.addItem( self.theParent.importIcons[sLabel], sLabel, sLabel ) diff --git a/nw/gui/projecteditor.py b/nw/gui/projecteditor.py index 640cb1a6..6f5556a2 100644 --- a/nw/gui/projecteditor.py +++ b/nw/gui/projecteditor.py @@ -22,8 +22,7 @@ from PyQt5.QtWidgets import ( QWidget, QTabWidget, QDialogButtonBox, QListWidget, QListWidgetItem, QPushButton, QColorDialog ) - -from nw.enum import nwChanged +from nw.enum import nwAlert logger = logging.getLogger(__name__) @@ -46,6 +45,7 @@ class GuiProjectEditor(QDialog): self.svgGradient = QSvgWidget(self.gradPath) self.svgGradient.setFixedWidth(80) + self.theProject.countStatus() self.tabMain = GuiProjectEditMain(self.theParent, self.theProject) self.tabStatus = GuiProjectEditStatus(self.theParent, self.theProject.statusItems) self.tabImport = GuiProjectEditStatus(self.theParent, self.theProject.importItems) @@ -82,10 +82,10 @@ class GuiProjectEditor(QDialog): self.theProject.setBookTitle(bookTitle) self.theProject.setBookAuthors(bookAuthors) - statusCol, statusChange = self.tabStatus.getNewList() - importCol, importChange = self.tabImport.getNewList() - self.theProject.setStatusColours(statusCol, statusChange) - self.theProject.setImportColours(importCol, importChange) + statusCol = self.tabStatus.getNewList() + importCol = self.tabImport.getNewList() + self.theProject.setStatusColours(statusCol) + self.theProject.setImportColours(importCol) self.close() @@ -135,6 +135,7 @@ class GuiProjectEditStatus(QWidget): self.theParent = theParent self.theStatus = theStatus self.colNames = [] + self.colCounts = [] self.colChanged = False self.mainBox = QHBoxLayout() @@ -143,9 +144,10 @@ class GuiProjectEditStatus(QWidget): self.listBox = QListWidget() self.listBox.itemSelectionChanged.connect(self._selectedItem) - for iName, iCol in self.theStatus: - self._addItem(iName, iCol) + for iName, iCol, nUse in self.theStatus: + self._addItem("%s [%d]" % (iName, nUse), iCol) self.colNames.append(iName) + self.colCounts.append(nUse) self.editName = QLineEdit() self.newButton = QPushButton("New") @@ -182,12 +184,12 @@ class GuiProjectEditStatus(QWidget): newList = [] for n in range(self.listBox.count()): nItem = self.listBox.item(n) - nName = nItem.text() + nName = self._cleanLabel(nItem.text()) nImg = nItem.icon().pixmap(16,16).toImage() nCol = QColor(nImg.pixel(7,7)) - newList.append((nName,nCol.red(),nCol.green(),nCol.blue())) - return newList, self.colNames - return None, None + newList.append((nName,nCol.red(),nCol.green(),nCol.blue(),self.colNames[n])) + return newList + return None ## # User Actions @@ -206,32 +208,29 @@ class GuiProjectEditStatus(QWidget): return def _newItem(self): - newItem = self._addItem("New Item", (0, 0, 0)) + newItem = self._addItem("New Item [0]", (0, 0, 0)) newItem.setBackground(QBrush(QColor(0,255,0,80))) self.colNames.append(None) + self.colCounts.append(0) self.colChanged = True return def _delItem(self): selItem = self._getSelectedItem() - colDel = QBrush(QColor(255,0,0,80)) - colNone = QBrush(QColor(0,0,0,0)) - if selItem is not None: - sText = selItem.text() - iRow = self.listBox.row(selItem) - if sText == "** Delete **": - selItem.setBackground(colNone) - selItem.setText(self.colNames[iRow]) - else: - selItem.setBackground(colDel) - selItem.setText("** Delete **") + iRow = self.listBox.row(selItem) + if self.colCounts[iRow] == 0: + self.listBox.takeItem(iRow) + self.colChanged = True + else: + self.theParent.makeAlert("Cannot delete status item that is in use.",nwAlert.ERROR) return def _saveItem(self): logger.verbose("Item save button clicked") selItem = self._getSelectedItem() + iRow = self.listBox.row(selItem) if selItem is not None: - selItem.setText(self.editName.text().strip()) + selItem.setText("%s [%d]" % (self.editName.text().strip(), self.colCounts[iRow])) selItem.setIcon(self.colButton.icon()) self.colChanged = True return @@ -250,7 +249,7 @@ class GuiProjectEditStatus(QWidget): logger.verbose("Item selected") selItem = self._getSelectedItem() if selItem is not None: - self.editName.setText(selItem.text()) + self.editName.setText(self._cleanLabel(selItem.text())) self.colButton.setIcon(selItem.icon()) return @@ -266,4 +265,11 @@ class GuiProjectEditStatus(QWidget): return selItem[0] return None + def _cleanLabel(self, theText): + iPos = theText.rfind("[") + if iPos > 0: + return theText[:iPos-1] + else: + return theText + # END Class GuiProjectEditStatus diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index 58324e1c..9369569a 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -396,7 +396,7 @@ class GuiMain(QMainWindow): def _makeStatusIcons(self): self.statusIcons = {} - for sLabel, sCol in self.theProject.statusItems: + for sLabel, sCol, _ in self.theProject.statusItems: theIcon = QPixmap(32,32) theIcon.fill(QColor(*sCol)) self.statusIcons[sLabel] = QIcon(theIcon) @@ -404,7 +404,7 @@ class GuiMain(QMainWindow): def _makeImportIcons(self): self.importIcons = {} - for sLabel, sCol in self.theProject.importItems: + for sLabel, sCol, _ in self.theProject.importItems: theIcon = QPixmap(32,32) theIcon.fill(QColor(*sCol)) self.importIcons[sLabel] = QIcon(theIcon) diff --git a/nw/project/document.py b/nw/project/document.py index 1d0a8af3..bb8ad4bd 100644 --- a/nw/project/document.py +++ b/nw/project/document.py @@ -15,8 +15,7 @@ import nw from os import path, mkdir, rename, unlink -from nw.tools.analyse import TextAnalysis -from nw.enum import nwAlert +from nw.enum import nwAlert logger = logging.getLogger(__name__) diff --git a/nw/project/project.py b/nw/project/project.py index 360ca69b..c607763d 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -352,13 +352,25 @@ class NWProject(): self.setProjectChanged(True) return True - def setStatusColours(self, newCols, colChanged): - print(newCols,colChanged) + def setStatusColours(self, newCols): + replaceMap = self.statusItems.setNewEntries(newCols) + if self.projTree is not None: + for nwItem in self.projTree.values(): + if nwItem.itemClass == nwItemClass.NOVEL: + if nwItem.itemStatus in replaceMap.keys(): + nwItem.setStatus(replaceMap[nwItem.itemStatus]) self.setProjectChanged(True) + print(self.statusItems.theLabels) + print(self.statusItems.theColours) return - def setImportColours(self, newCols, colChanged): - print(newCols,colChanged) + def setImportColours(self, newCols,): + replaceMap = self.importItems.setNewEntries(newCols) + if self.projTree is not None: + for nwItem in self.projTree.values(): + if nwItem.itemClass != nwItemClass.NOVEL: + if nwItem.itemStatus in replaceMap.keys(): + nwItem.setStatus(replaceMap[nwItem.itemStatus]) self.setProjectChanged(True) return @@ -435,6 +447,16 @@ class NWProject(): return False return True + def countStatus(self): + self.statusItems.resetCounts() + self.importItems.resetCounts() + for nwItem in self.projTree.values(): + if nwItem.itemClass == nwItemClass.NOVEL: + self.statusItems.countEntry(nwItem.itemStatus) + else: + self.importItems.countEntry(nwItem.itemStatus) + return + ## # Internal Functions ## diff --git a/nw/project/status.py b/nw/project/status.py index 7197e1da..0fef9eb1 100644 --- a/nw/project/status.py +++ b/nw/project/status.py @@ -23,8 +23,9 @@ class NWStatus(): def __init__(self): self.theLabels = [] self.theColours = [] + self.theCounts = [] self.theMap = {} - self.theCount = 0 + self.theLength = 0 self.theIndex = 0 return @@ -33,8 +34,9 @@ class NWStatus(): if self.lookupEntry(theLabel) is None: self.theLabels.append(theLabel) self.theColours.append(theColours) - self.theMap[theLabel] = self.theCount - self.theCount += 1 + self.theCounts.append(0) + self.theMap[theLabel] = self.theLength + self.theLength += 1 return True def lookupEntry(self, theLabel): @@ -44,34 +46,62 @@ class NWStatus(): return None def checkEntry(self, theStatus): - theStatus = theStatus.strip() if isinstance(theStatus, str): + theStatus = theStatus.strip() if self.lookupEntry(theStatus) is not None: return theStatus - theStatus = checkInt(theStatus, None, False) - if theStatus is None: - return None - if theStatus >= 0 and theStatus < self.theCount: + theStatus = checkInt(theStatus, 0, False) + if theStatus >= 0 and theStatus < self.theLength: return self.theLabels[theStatus] + def setNewEntries(self, newList): + + replaceMap = {} + + if newList is not None: + + self.theLabels = [] + self.theColours = [] + self.theCounts = [] + self.theMap = {} + self.theLength = 0 + self.theIndex = 0 + + for nName, nR, nG, nB, oName in newList: + self.addEntry(nName, (nR, nG, nB)) + if nName != oName and oName is not None: + replaceMap[oName] = nName + + return replaceMap + + def resetCounts(self): + self.theCounts = [0]*self.theLength + return + + def countEntry(self, theLabel): + theIndex = self.lookupEntry(theLabel) + if theIndex is not None: + self.theCounts[theIndex] += 1 + return + ## # Iterator Bits ## def __getitem__(self, n): - if n >= 0 and n < self.theCount: - return self.theLabels[n], self.theColours[n] - return None, None + if n >= 0 and n < self.theLength: + return self.theLabels[n], self.theColours[n], self.theCounts[n] + return None, None, None def __iter__(self): self.theIndex = 0 return self def __next__(self): - if self.theIndex < self.theCount: - theLabel, theColour = self.__getitem__(self.theIndex) + if self.theIndex < self.theLength: + theLabel, theColour, theCount = self.__getitem__(self.theIndex) self.theIndex += 1 - return theLabel, theColour + return theLabel, theColour, theCount else: raise StopIteration