From af9aa08fe6667a0e6ce7d4b15b239405a3edf6a8 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 28 Apr 2019 13:17:24 +0200 Subject: [PATCH] Finished the edit item dialog and now updating tree and project settings. --- nw/constants.py | 61 ++++++++++++++++++++++++++++++++ nw/gui/docdetails.py | 6 ++-- nw/gui/doctree.py | 47 ++++++++++++++---------- nw/gui/doctreectx.py | 4 +-- nw/gui/itemeditor.py | 58 ++++++++++++++++++++++++------ nw/gui/winmain.py | 5 +-- nw/project/item.py | 42 ---------------------- sample/sampleNovel/nwProject.nwx | 2 +- 8 files changed, 146 insertions(+), 79 deletions(-) create mode 100644 nw/constants.py diff --git a/nw/constants.py b/nw/constants.py new file mode 100644 index 00000000..b9484971 --- /dev/null +++ b/nw/constants.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +"""novelWriter Constants + + novelWriter – Constants +========================= + Constants for translating flags and enums to text + + File History: + Created: 2019-04-28 [0.0.1] + +""" + +from nw.enum import nwItemClass, nwItemLayout + +class nwLabels(): + + CLASS_NAME = { + nwItemClass.NO_CLASS : "None", + nwItemClass.NOVEL : "Novel", + nwItemClass.PLOT : "Plot", + nwItemClass.CHARACTER : "Characters", + nwItemClass.WORLD : "Locations", + nwItemClass.TIMELINE : "Timeline", + nwItemClass.OBJECT : "Objects", + nwItemClass.CUSTOM : "Custom", + } + CLASS_FLAG = { + nwItemClass.NO_CLASS : "0", + nwItemClass.NOVEL : "N", + nwItemClass.PLOT : "P", + nwItemClass.CHARACTER : "C", + nwItemClass.WORLD : "L", + nwItemClass.TIMELINE : "T", + nwItemClass.OBJECT : "O", + nwItemClass.CUSTOM : "X", + } + LAYOUT_NAME = { + nwItemLayout.NO_LAYOUT : "None", + nwItemLayout.TITLE : "Title Page", + nwItemLayout.BOOK : "Book", + nwItemLayout.PAGE : "Plain Page", + nwItemLayout.PARTITION : "Partition", + nwItemLayout.UNNUMBERED : "Un-Numbered", + nwItemLayout.CHAPTER : "Chapter", + nwItemLayout.SCENE : "Scene", + nwItemLayout.NOTE : "Note", + } + LAYOUT_FLAG = { + nwItemLayout.NO_LAYOUT : "Xo", + nwItemLayout.TITLE : "Tt", + nwItemLayout.BOOK : "Bk", + nwItemLayout.PAGE : "Pg", + nwItemLayout.PARTITION : "Pt", + nwItemLayout.UNNUMBERED : "Un", + nwItemLayout.CHAPTER : "Ch", + nwItemLayout.SCENE : "Sc", + nwItemLayout.NOTE : "Nt", + } + + +# END nwLabels diff --git a/nw/gui/docdetails.py b/nw/gui/docdetails.py index 9366a042..138fdfe7 100644 --- a/nw/gui/docdetails.py +++ b/nw/gui/docdetails.py @@ -18,7 +18,7 @@ from PyQt5.QtGui import QFont from PyQt5.QtWidgets import QFrame, QGridLayout, QLabel from nw.enum import nwItemType, nwItemClass, nwItemLayout -from nw.project.item import NWItem +from nw.constants import nwLabels logger = logging.getLogger(__name__) @@ -72,8 +72,8 @@ class GuiDocDetails(QFrame): colTwo = [ nwItem.itemName, self.theProject.statusLabels[itemStatus], - NWItem.CLASS_NAME[nwItem.itemClass], - NWItem.LAYOUT_NAME[nwItem.itemLayout], + nwLabels.CLASS_NAME[nwItem.itemClass], + nwLabels.LAYOUT_NAME[nwItem.itemLayout], ] for nRow in range(4): diff --git a/nw/gui/doctree.py b/nw/gui/doctree.py index 9297285c..3ee5bc95 100644 --- a/nw/gui/doctree.py +++ b/nw/gui/doctree.py @@ -18,8 +18,9 @@ from PyQt5.QtCore import Qt, QSize from PyQt5.QtGui import QIcon, QFont, QColor from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView, QInputDialog, QLineEdit, QApplication -from nw.enum import nwItemType, nwItemClass from nw.project.item import NWItem +from nw.enum import nwItemType, nwItemClass +from nw.constants import nwLabels logger = logging.getLogger(__name__) @@ -76,7 +77,7 @@ class GuiDocTree(QTreeWidget): ) if itemType == nwItemType.ROOT: - tHandle = self.theProject.newRoot(NWItem.CLASS_NAME[itemClass], itemClass) + tHandle = self.theProject.newRoot(nwLabels.CLASS_NAME[itemClass], itemClass) else: # If no parent has been selected, make the new file under the root NOVEL item. @@ -184,31 +185,19 @@ class GuiDocTree(QTreeWidget): def _addTreeItem(self, nwItem): - tName = nwItem.itemName tHandle = nwItem.itemHandle pHandle = nwItem.parHandle - tStatus = NWItem.CLASS_FLAG[nwItem.itemClass] - if nwItem.itemType == nwItemType.FILE: - tStatus += "."+NWItem.LAYOUT_FLAG[nwItem.itemLayout] - nStatus = nwItem.itemStatus - if nStatus < 0 or nStatus >= len(self.theProject.statusIcons): - nStatus = 0 - newItem = QTreeWidgetItem([""]*4) - newItem.setText(self.C_NAME,tName) + newItem.setText(self.C_NAME, "") + newItem.setText(self.C_COUNT, "0") + newItem.setText(self.C_FLAGS, "") + newItem.setText(self.C_HANDLE, tHandle) - newItem.setText(self.C_COUNT,"0") - # newItem.setFont(self.C_COUNT,self.fontCount) newItem.setForeground(self.C_COUNT,QColor(0,105,135)) newItem.setTextAlignment(self.C_COUNT,Qt.AlignRight) - - newItem.setText(self.C_FLAGS,tStatus) - newItem.setIcon(self.C_FLAGS,self.theProject.statusIcons[nStatus]) newItem.setFont(self.C_FLAGS,self.fontFlags) - newItem.setText(self.C_HANDLE, tHandle) - self.theMap[tHandle] = newItem if pHandle is None: self.addTopLevelItem(newItem) @@ -216,6 +205,7 @@ class GuiDocTree(QTreeWidget): self.theMap[pHandle].addChild(newItem) self.propagateCount(tHandle, nwItem.wordCount) + self.setTreeItemValues(tHandle) newItem.setExpanded(nwItem.isExpanded) if nwItem.itemType == nwItemType.ROOT: @@ -228,6 +218,27 @@ class GuiDocTree(QTreeWidget): return newItem + def setTreeItemValues(self, tHandle): + + trItem = self.theMap[tHandle] + nwItem = self.theProject.getItem(tHandle) + tName = nwItem.itemName + tHandle = nwItem.itemHandle + pHandle = nwItem.parHandle + + tStatus = nwLabels.CLASS_FLAG[nwItem.itemClass] + if nwItem.itemType == nwItemType.FILE: + tStatus += "."+nwLabels.LAYOUT_FLAG[nwItem.itemLayout] + nStatus = nwItem.itemStatus + if nStatus < 0 or nStatus >= len(self.theProject.statusIcons): + nStatus = 0 + + trItem.setText(self.C_NAME,tName) + trItem.setText(self.C_FLAGS,tStatus) + trItem.setIcon(self.C_FLAGS,self.theProject.statusIcons[nStatus]) + + return + def propagateCount(self, tHandle, theCount, nDepth=0): tItem = self.theMap[tHandle] tItem.setText(self.C_COUNT,"{:n}".format(theCount)) diff --git a/nw/gui/doctreectx.py b/nw/gui/doctreectx.py index a1a3ce58..9759c5ff 100644 --- a/nw/gui/doctreectx.py +++ b/nw/gui/doctreectx.py @@ -17,8 +17,8 @@ from copy import copy from PyQt5.QtWidgets import QMenu, QAction -from nw.project.item import NWItem from nw.enum import nwItemType, nwItemClass, nwItemAction +from nw.constants import nwLabels logger = logging.getLogger(__name__) @@ -192,7 +192,7 @@ class GuiDocTreeCtx(QMenu): return def _buildSubMenuAddRoot(self, itemClass): - mnuSub = QAction("Add %s Root" % NWItem.CLASS_NAME[itemClass], self) + mnuSub = QAction("Add %s Root" % nwLabels.CLASS_NAME[itemClass], self) mnuSub.triggered.connect(lambda: self._ctxSignal( nwItemAction.ADD_ROOT, itemClass, diff --git a/nw/gui/itemeditor.py b/nw/gui/itemeditor.py index 6a93c627..02358108 100644 --- a/nw/gui/itemeditor.py +++ b/nw/gui/itemeditor.py @@ -15,9 +15,12 @@ import nw from os import path -from PyQt5.QtWidgets import QDialog, QHBoxLayout, QVBoxLayout, QGroupBox, QFormLayout, QLineEdit, QPushButton +from PyQt5.QtWidgets import QDialog, QHBoxLayout, QVBoxLayout, QGroupBox, QFormLayout, QLineEdit, QPushButton, QComboBox from PyQt5.QtSvg import QSvgWidget +from nw.enum import nwItemLayout, nwItemClass, nwItemType +from nw.constants import nwLabels + logger = logging.getLogger(__name__) class GuiItemEditor(QDialog): @@ -47,16 +50,46 @@ class GuiItemEditor(QDialog): self.mainForm = QFormLayout() self.editName = QLineEdit() - self.editStatus = QLineEdit() - self.editLayout = QLineEdit() + self.editStatus = QComboBox() + self.editLayout = QComboBox() + + for n in range(len(self.theProject.statusLabels)): + self.editStatus.addItem( + self.theProject.statusIcons[n], self.theProject.statusLabels[n], n + ) + + self.validLayouts = [] + if self.theItem.itemType == nwItemType.FILE: + if self.theItem.itemClass == nwItemClass.NOVEL: + self.validLayouts.append(nwItemLayout.TITLE) + self.validLayouts.append(nwItemLayout.BOOK) + self.validLayouts.append(nwItemLayout.PAGE) + self.validLayouts.append(nwItemLayout.PARTITION) + self.validLayouts.append(nwItemLayout.UNNUMBERED) + self.validLayouts.append(nwItemLayout.CHAPTER) + self.validLayouts.append(nwItemLayout.SCENE) + else: + self.validLayouts.append(nwItemLayout.NOTE) + else: + self.validLayouts.append(nwItemLayout.NO_LAYOUT) + + for itemLayout in nwItemLayout: + if itemLayout in self.validLayouts: + self.editLayout.addItem(nwLabels.LAYOUT_NAME[itemLayout],itemLayout) self.mainForm.addRow("Name", self.editName) self.mainForm.addRow("Status", self.editStatus) self.mainForm.addRow("Layout", self.editLayout) + self.editName.setMinimumWidth(200) + self.editName.setText(self.theItem.itemName) - self.editStatus.setText(str(self.theItem.itemStatus)) - self.editLayout.setText(str(self.theItem.itemLayout)) + statusIdx = self.editStatus.findData(self.theItem.itemStatus) + if statusIdx != -1: + self.editStatus.setCurrentIndex(statusIdx) + layoutIdx = self.editLayout.findData(self.theItem.itemLayout) + if layoutIdx != -1: + self.editLayout.setCurrentIndex(layoutIdx) self.buttonBox = QHBoxLayout() self.closeButton = QPushButton("Close") @@ -79,16 +112,19 @@ class GuiItemEditor(QDialog): def _doSave(self): logger.verbose("ItemEditor save button clicked") - # projName = self.editName.text() - # bookTitle = self.editTitle.text() - # bookAuthors = self.editAuthors.toPlainText() - # self.theProject.setProjectName(projName) - # self.theProject.setBookTitle(bookTitle) - # self.theProject.setBookAuthors(bookAuthors) + itemName = self.editName.text() + itemStatus = self.editStatus.currentData() + itemLayout = self.editLayout.currentData() + self.theItem.setName(itemName) + self.theItem.setStatus(itemStatus) + self.theItem.setLayout(itemLayout) + self.accept() + self.close() return def _doClose(self): logger.verbose("ItemEditor close button clicked") + self.reject() self.close() return diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index 8850f5ff..adb8d65c 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -171,8 +171,9 @@ class GuiMain(QMainWindow): logger.verbose("Requesting change to item %s" % tHandle) dlgProj = GuiItemEditor(self, self.theProject, tHandle) - dlgProj.exec_() - + if dlgProj.exec_(): + self.treeView.setTreeItemValues(tHandle) + return ## diff --git a/nw/project/item.py b/nw/project/item.py index 3d098e0e..eb2ad27b 100644 --- a/nw/project/item.py +++ b/nw/project/item.py @@ -24,48 +24,6 @@ logger = logging.getLogger(__name__) class NWItem(): MAX_DEPTH = 8 - CLASS_NAME = { - nwItemClass.NO_CLASS : "None", - nwItemClass.NOVEL : "Novel", - nwItemClass.PLOT : "Plot", - nwItemClass.CHARACTER : "Characters", - nwItemClass.WORLD : "Locations", - nwItemClass.TIMELINE : "Timeline", - nwItemClass.OBJECT : "Objects", - nwItemClass.CUSTOM : "Custom", - } - CLASS_FLAG = { - nwItemClass.NO_CLASS : "0", - nwItemClass.NOVEL : "N", - nwItemClass.PLOT : "P", - nwItemClass.CHARACTER : "C", - nwItemClass.WORLD : "L", - nwItemClass.TIMELINE : "T", - nwItemClass.OBJECT : "O", - nwItemClass.CUSTOM : "X", - } - LAYOUT_NAME = { - nwItemLayout.NO_LAYOUT : "None", - nwItemLayout.TITLE : "Title Page", - nwItemLayout.BOOK : "Book", - nwItemLayout.PAGE : "Plain Page", - nwItemLayout.PARTITION : "Partition", - nwItemLayout.UNNUMBERED : "Un-Numbered", - nwItemLayout.CHAPTER : "Chapter", - nwItemLayout.SCENE : "Scene", - nwItemLayout.NOTE : "Note", - } - LAYOUT_FLAG = { - nwItemLayout.NO_LAYOUT : "Xo", - nwItemLayout.TITLE : "Tt", - nwItemLayout.BOOK : "Bk", - nwItemLayout.PAGE : "Pg", - nwItemLayout.PARTITION : "Pt", - nwItemLayout.UNNUMBERED : "Un", - nwItemLayout.CHAPTER : "Ch", - nwItemLayout.SCENE : "Sc", - nwItemLayout.NOTE : "Nt", - } def __init__(self): diff --git a/sample/sampleNovel/nwProject.nwx b/sample/sampleNovel/nwProject.nwx index b800ff14..97b33dbc 100644 --- a/sample/sampleNovel/nwProject.nwx +++ b/sample/sampleNovel/nwProject.nwx @@ -1,5 +1,5 @@ - + Sample Project Sample Project