From 30c12414882124665f78527a3b178bb77f8e50a2 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Tue, 23 Apr 2019 21:42:11 +0200 Subject: [PATCH] Tree context menu can now add files, folders and roots. --- nw/enum.py | 44 +++++++++++++++--------------- nw/gui/doctree.py | 37 +++++++------------------ nw/gui/doctreectx.py | 63 ++++++++++--------------------------------- nw/gui/winmain.py | 45 ++++++++++++++++++++++++------- nw/project/item.py | 21 +++++++++++---- nw/project/project.py | 4 ++- 6 files changed, 98 insertions(+), 116 deletions(-) diff --git a/nw/enum.py b/nw/enum.py index e78863d7..deacb379 100644 --- a/nw/enum.py +++ b/nw/enum.py @@ -14,7 +14,7 @@ from enum import Enum class nwItemType(Enum): - NONE = 0 + NO_TYPE = 0 ROOT = 1 FOLDER = 2 FILE = 3 @@ -25,21 +25,19 @@ class nwItemType(Enum): class nwItemClass(Enum): - NONE = 0 + NO_CLASS = 0 NOVEL = 1 - CHAPTER = 2 - SCENE = 3 - PLOT = 4 - CHARACTER = 6 - WORLD = 6 - TIMELINE = 7 - OBJECT = 8 + PLOT = 2 + CHARACTER = 3 + WORLD = 4 + TIMELINE = 5 + OBJECT = 6 # END Enum nwItemClass class nwItemAction(Enum): - NONE = 0 + NO_ACTION = 0 ADD_ROOT = 1 ADD_FOLDER = 2 ADD_FILE = 3 @@ -57,18 +55,18 @@ class nwItemAction(Enum): class nwDocAction(Enum): - NONE = 0 - UNDO = 1 - REDO = 2 - CUT = 3 - COPY = 4 - PASTE = 5 - BOLD = 6 - ITALIC = 7 - U_LINE = 8 - S_QUOTE = 9 - D_QUOTE = 10 - SEL_ALL = 11 - SEL_PARA = 12 + NO_ACTION = 0 + UNDO = 1 + REDO = 2 + CUT = 3 + COPY = 4 + PASTE = 5 + BOLD = 6 + ITALIC = 7 + U_LINE = 8 + S_QUOTE = 9 + D_QUOTE = 10 + SEL_ALL = 11 + SEL_PARA = 12 # END Enum nwDocAction diff --git a/nw/gui/doctree.py b/nw/gui/doctree.py index df827bdb..2dcac2a0 100644 --- a/nw/gui/doctree.py +++ b/nw/gui/doctree.py @@ -53,44 +53,25 @@ class GuiDocTree(QTreeWidget): return - def newTreeItem(self, itemType): + def newTreeItem(self, pHandle, itemType, itemClass): - rHandle = self._getSelectedHandle() - logger.verbose("Adding item relative to handle %s" % rHandle) - - # Figure out where to put the new item - if rHandle is None: - nwType = nwItemType.ROOT - pHandle = None - else: - rItem = self.theProject.projTree[rHandle] - if rItem.itemType == nwItemType.FILE: - pHandle = rItem.parHandle - else: - pHandle = rHandle + logger.verbose("Adding new item of type %s and class %s to handle %s" % ( + itemType.name,itemClass.name,str(pHandle)) + ) # Create the new item - if itemType == nwItemType.FILE: - tHandle = self.theProject.newFile("New File", nwItemClass.NONE, pHandle) + if itemType == nwItemType.FILE: + tHandle = self.theProject.newFile("New File", itemClass, pHandle) elif itemType == nwItemType.FOLDER: - if pHandle is None: - logger.error("Failed to add new item") - return - pItem = self.theProject.projTree[rHandle] - if pItem.itemClass == nwItemClass.NOVEL: - tHandle = self.theProject.newFolder("New Chapter", nwItemClass.CHAPTER, pHandle) - elif pItem.itemClass == nwItemClass.CHAPTER: - tHandle = self.theProject.newFolder("New Chapter", nwItemClass.CHAPTER, pItem.parHandle) - else: - tHandle = self.theProject.newFolder("New Folder", nwItemClass.NONE, pHandle) + tHandle = self.theProject.newFolder("New Folder", itemClass, pHandle) elif itemType == nwItemType.ROOT: - tHandle = self.theProject.newRoot("Root Folder", nwItemClass.NONE) + tHandle = self.theProject.newRoot(NWItem.CLASS_NAME[itemClass], itemClass) else: logger.error("Failed to add new item") return # Add the new item to the tree - nwItem = self.theProject.projTree[tHandle] + nwItem = self.theProject.getItem(tHandle) self._addTreeItem(nwItem) return diff --git a/nw/gui/doctreectx.py b/nw/gui/doctreectx.py index 8bd14e76..779696e1 100644 --- a/nw/gui/doctreectx.py +++ b/nw/gui/doctreectx.py @@ -13,8 +13,11 @@ import logging import nw +from copy import copy + from PyQt5.QtWidgets import QMenu, QAction +from nw.project.item import NWItem from nw.enum import nwItemType, nwItemClass, nwItemAction logger = logging.getLogger(__name__) @@ -175,56 +178,18 @@ class GuiDocTreeCtx(QMenu): return def _buildMenuAddRoot(self, vActs): + for itemClass in vActs[nwItemAction.ADD_ROOT]["Class"]: + self._buildSubMenuAddRoot(itemClass) + return - vClass = vActs[nwItemAction.ADD_ROOT]["Class"] - mnuItem = QMenu("Add Root Item", self) - - if nwItemClass.NOVEL in vClass: - mnuSub = QAction("Novel", self) - mnuSub.triggered.connect(lambda: self._ctxSignal( - nwItemAction.ADD_ROOT, - nwItemClass.NOVEL, - nwItemType.ROOT - )) - mnuItem.addAction(mnuSub) - - if nwItemClass.CHARACTER in vClass: - mnuSub = QAction("Characters", self) - mnuSub.triggered.connect(lambda: self._ctxSignal( - nwItemAction.ADD_ROOT, - nwItemClass.CHARACTER, - nwItemType.ROOT - )) - mnuItem.addAction(mnuSub) - - if nwItemClass.WORLD in vClass: - mnuSub = QAction("Locations", self) - mnuSub.triggered.connect(lambda: self._ctxSignal( - nwItemAction.ADD_ROOT, - nwItemClass.WORLD, - nwItemType.ROOT - )) - mnuItem.addAction(mnuSub) - - if nwItemClass.TIMELINE in vClass: - mnuSub = QAction("Timeline", self) - mnuSub.triggered.connect(lambda: self._ctxSignal( - nwItemAction.ADD_ROOT, - nwItemClass.TIMELINE, - nwItemType.ROOT - )) - mnuItem.addAction(mnuSub) - - if nwItemClass.OBJECT in vClass: - mnuSub = QAction("Objects", self) - mnuSub.triggered.connect(lambda: self._ctxSignal( - nwItemAction.ADD_ROOT, - nwItemClass.OBJECT, - nwItemType.ROOT - )) - mnuItem.addAction(mnuSub) - - self.addMenu(mnuItem) + def _buildSubMenuAddRoot(self, itemClass): + mnuSub = QAction("Add "+NWItem.CLASS_NAME[itemClass], self) + mnuSub.triggered.connect(lambda: self._ctxSignal( + nwItemAction.ADD_ROOT, + itemClass, + nwItemType.ROOT + )) + self.addAction(mnuSub) return ## diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index 4128c94c..b17fcbc2 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -25,7 +25,7 @@ from nw.gui.projecteditor import GuiProjectEditor from nw.gui.statusbar import GuiMainStatus from nw.project.project import NWProject from nw.project.document import NWDoc -from nw.enum import nwItemType, nwDocAction +from nw.enum import nwItemType, nwDocAction, nwItemAction logger = logging.getLogger(__name__) @@ -86,9 +86,9 @@ class GuiMain(QMainWindow): return - # + ## # Project Actions - # + ## def newProject(self): logger.info("Creating new project") @@ -137,9 +137,9 @@ class GuiMain(QMainWindow): self._setWindowTitle(self.theProject.projName) return True - # + ## # Document Actions - # + ## def openDocument(self, tHandle): self.stackPane.setCurrentIndex(self.stackDoc) @@ -155,9 +155,9 @@ class GuiMain(QMainWindow): self.theDocument.saveDocument(docHtml) return - # + ## # Internal Functions - # + ## def _treeDoubleClick(self, tItem, colNo): tHandle = tItem.text(3) @@ -213,6 +213,31 @@ class GuiMain(QMainWindow): print(selHandle, selAction, selClass, selType, selTarget) + if selAction == nwItemAction.ADD_ROOT: + self.treeView.newTreeItem(selHandle, selType, selClass) + elif selAction == nwItemAction.ADD_FOLDER: + self.treeView.newTreeItem(selHandle, selType, selClass) + elif selAction == nwItemAction.ADD_FILE: + self.treeView.newTreeItem(selHandle, selType, selClass) + elif selAction == nwItemAction.MOVE_UP: + pass + elif selAction == nwItemAction.MOVE_DOWN: + pass + elif selAction == nwItemAction.MOVE_TO: + pass + elif selAction == nwItemAction.MOVE_TRASH: + pass + elif selAction == nwItemAction.SPLIT: + pass + elif selAction == nwItemAction.MERGE: + pass + elif selAction == nwItemAction.DELETE: + pass + elif selAction == nwItemAction.DELETE_ROOT: + pass + elif selAction == nwItemAction.EMPTY_TRASH: + pass + return ## @@ -243,9 +268,9 @@ class GuiMain(QMainWindow): self.docEditor.changeWidth() return - # + ## # GUI Builders - # + ## def _buildMenu(self): menuBar = self.menuBar() @@ -277,7 +302,7 @@ class GuiMain(QMainWindow): for recentProject in self.mainConf.recentList: if recentProject == "": continue menuItem = QAction(QIcon.fromTheme("folder-open"), "%d: %s" % (itemCount,recentProject), fileMenu) - menuItem.triggered.connect(self.openRecentProject, itemCount) + menuItem.triggered.connect(lambda: self.openRecentProject(itemCount)) recentMenu.addAction(menuItem) itemCount += 1 diff --git a/nw/project/item.py b/nw/project/item.py index e15f708d..3cd07ef0 100644 --- a/nw/project/item.py +++ b/nw/project/item.py @@ -23,7 +23,16 @@ logger = logging.getLogger(__name__) class NWItem(): - MAX_DEPTH = 8 + MAX_DEPTH = 8 + CLASS_NAME = { + nwItemClass.NO_CLASS : "Folder", + nwItemClass.NOVEL : "Novel", + nwItemClass.PLOT : "Plot", + nwItemClass.CHARACTER : "Characters", + nwItemClass.WORLD : "Locations", + nwItemClass.TIMELINE : "Timeline", + nwItemClass.OBJECT : "Objects", + } def __init__(self): @@ -31,8 +40,8 @@ class NWItem(): self.itemHandle = None self.parHandle = None self.itemOrder = None - self.itemType = nwItemType.NONE - self.itemClass = nwItemClass.NONE + self.itemType = nwItemType.NO_TYPE + self.itemClass = nwItemClass.NO_CLASS self.itemDepth = None self.hasChildren = False self.isExpanded = False @@ -116,25 +125,27 @@ class NWItem(): def setType(self, theType): if isinstance(theType, nwItemType): self.itemType = theType + return else: for itemType in nwItemType: if theType == itemType.name: self.itemType = itemType return logger.error("Unrecognised item type '%s'" % theType) - self.itemType = nwItemType.NONE + self.itemType = nwItemType.NO_TYPE return def setClass(self, theClass): if isinstance(theClass, nwItemClass): self.itemClass = theClass + return else: for itemClass in nwItemClass: if theClass == itemClass.name: self.itemClass = itemClass return logger.error("Unrecognised item class '%s'" % theClass) - self.itemClass = nwItemClass.NONE + self.itemClass = nwItemClass.NO_CLASS return def setDepth(self, theDepth): diff --git a/nw/project/project.py b/nw/project/project.py index 1a2d2c24..91f69ff7 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -245,7 +245,7 @@ class NWProject(): """ validActions = {} - # If we're at root, or didn't select an utem, all we can add is root stuff + # If we're at root, or didn't select an item, all we can add is root stuff if tHandle is None: validActions[nwItemAction.ADD_ROOT] = { "Type" : nwItemType.ROOT, @@ -318,6 +318,8 @@ class NWProject(): """Checks if there already is a root entry of class 'theClass' in the root of the project tree. """ + if theClass == nwItemClass.NO_CLASS: + return True for aRoot in self.treeRoots: if theClass == self.projTree[aRoot].itemClass: return False