Tree context menu can now add files, folders and roots.

This commit is contained in:
Veronica K. B. Olsen
2019-04-23 21:42:11 +02:00
parent 06d1d3c7cc
commit 30c1241488
6 changed files with 98 additions and 116 deletions
+21 -23
View File
@@ -14,7 +14,7 @@ from enum import Enum
class nwItemType(Enum): class nwItemType(Enum):
NONE = 0 NO_TYPE = 0
ROOT = 1 ROOT = 1
FOLDER = 2 FOLDER = 2
FILE = 3 FILE = 3
@@ -25,21 +25,19 @@ class nwItemType(Enum):
class nwItemClass(Enum): class nwItemClass(Enum):
NONE = 0 NO_CLASS = 0
NOVEL = 1 NOVEL = 1
CHAPTER = 2 PLOT = 2
SCENE = 3 CHARACTER = 3
PLOT = 4 WORLD = 4
CHARACTER = 6 TIMELINE = 5
WORLD = 6 OBJECT = 6
TIMELINE = 7
OBJECT = 8
# END Enum nwItemClass # END Enum nwItemClass
class nwItemAction(Enum): class nwItemAction(Enum):
NONE = 0 NO_ACTION = 0
ADD_ROOT = 1 ADD_ROOT = 1
ADD_FOLDER = 2 ADD_FOLDER = 2
ADD_FILE = 3 ADD_FILE = 3
@@ -57,18 +55,18 @@ class nwItemAction(Enum):
class nwDocAction(Enum): class nwDocAction(Enum):
NONE = 0 NO_ACTION = 0
UNDO = 1 UNDO = 1
REDO = 2 REDO = 2
CUT = 3 CUT = 3
COPY = 4 COPY = 4
PASTE = 5 PASTE = 5
BOLD = 6 BOLD = 6
ITALIC = 7 ITALIC = 7
U_LINE = 8 U_LINE = 8
S_QUOTE = 9 S_QUOTE = 9
D_QUOTE = 10 D_QUOTE = 10
SEL_ALL = 11 SEL_ALL = 11
SEL_PARA = 12 SEL_PARA = 12
# END Enum nwDocAction # END Enum nwDocAction
+9 -28
View File
@@ -53,44 +53,25 @@ class GuiDocTree(QTreeWidget):
return return
def newTreeItem(self, itemType): def newTreeItem(self, pHandle, itemType, itemClass):
rHandle = self._getSelectedHandle() logger.verbose("Adding new item of type %s and class %s to handle %s" % (
logger.verbose("Adding item relative to handle %s" % rHandle) itemType.name,itemClass.name,str(pHandle))
)
# 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
# Create the new item # Create the new item
if itemType == nwItemType.FILE: if itemType == nwItemType.FILE:
tHandle = self.theProject.newFile("New File", nwItemClass.NONE, pHandle) tHandle = self.theProject.newFile("New File", itemClass, pHandle)
elif itemType == nwItemType.FOLDER: elif itemType == nwItemType.FOLDER:
if pHandle is None: tHandle = self.theProject.newFolder("New Folder", itemClass, pHandle)
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)
elif itemType == nwItemType.ROOT: elif itemType == nwItemType.ROOT:
tHandle = self.theProject.newRoot("Root Folder", nwItemClass.NONE) tHandle = self.theProject.newRoot(NWItem.CLASS_NAME[itemClass], itemClass)
else: else:
logger.error("Failed to add new item") logger.error("Failed to add new item")
return return
# Add the new item to the tree # Add the new item to the tree
nwItem = self.theProject.projTree[tHandle] nwItem = self.theProject.getItem(tHandle)
self._addTreeItem(nwItem) self._addTreeItem(nwItem)
return return
+14 -49
View File
@@ -13,8 +13,11 @@
import logging import logging
import nw import nw
from copy import copy
from PyQt5.QtWidgets import QMenu, QAction from PyQt5.QtWidgets import QMenu, QAction
from nw.project.item import NWItem
from nw.enum import nwItemType, nwItemClass, nwItemAction from nw.enum import nwItemType, nwItemClass, nwItemAction
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -175,56 +178,18 @@ class GuiDocTreeCtx(QMenu):
return return
def _buildMenuAddRoot(self, vActs): def _buildMenuAddRoot(self, vActs):
for itemClass in vActs[nwItemAction.ADD_ROOT]["Class"]:
self._buildSubMenuAddRoot(itemClass)
return
vClass = vActs[nwItemAction.ADD_ROOT]["Class"] def _buildSubMenuAddRoot(self, itemClass):
mnuItem = QMenu("Add Root Item", self) mnuSub = QAction("Add "+NWItem.CLASS_NAME[itemClass], self)
mnuSub.triggered.connect(lambda: self._ctxSignal(
if nwItemClass.NOVEL in vClass: nwItemAction.ADD_ROOT,
mnuSub = QAction("Novel", self) itemClass,
mnuSub.triggered.connect(lambda: self._ctxSignal( nwItemType.ROOT
nwItemAction.ADD_ROOT, ))
nwItemClass.NOVEL, self.addAction(mnuSub)
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)
return return
## ##
+35 -10
View File
@@ -25,7 +25,7 @@ from nw.gui.projecteditor import GuiProjectEditor
from nw.gui.statusbar import GuiMainStatus from nw.gui.statusbar import GuiMainStatus
from nw.project.project import NWProject from nw.project.project import NWProject
from nw.project.document import NWDoc from nw.project.document import NWDoc
from nw.enum import nwItemType, nwDocAction from nw.enum import nwItemType, nwDocAction, nwItemAction
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -86,9 +86,9 @@ class GuiMain(QMainWindow):
return return
# ##
# Project Actions # Project Actions
# ##
def newProject(self): def newProject(self):
logger.info("Creating new project") logger.info("Creating new project")
@@ -137,9 +137,9 @@ class GuiMain(QMainWindow):
self._setWindowTitle(self.theProject.projName) self._setWindowTitle(self.theProject.projName)
return True return True
# ##
# Document Actions # Document Actions
# ##
def openDocument(self, tHandle): def openDocument(self, tHandle):
self.stackPane.setCurrentIndex(self.stackDoc) self.stackPane.setCurrentIndex(self.stackDoc)
@@ -155,9 +155,9 @@ class GuiMain(QMainWindow):
self.theDocument.saveDocument(docHtml) self.theDocument.saveDocument(docHtml)
return return
# ##
# Internal Functions # Internal Functions
# ##
def _treeDoubleClick(self, tItem, colNo): def _treeDoubleClick(self, tItem, colNo):
tHandle = tItem.text(3) tHandle = tItem.text(3)
@@ -213,6 +213,31 @@ class GuiMain(QMainWindow):
print(selHandle, selAction, selClass, selType, selTarget) 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 return
## ##
@@ -243,9 +268,9 @@ class GuiMain(QMainWindow):
self.docEditor.changeWidth() self.docEditor.changeWidth()
return return
# ##
# GUI Builders # GUI Builders
# ##
def _buildMenu(self): def _buildMenu(self):
menuBar = self.menuBar() menuBar = self.menuBar()
@@ -277,7 +302,7 @@ class GuiMain(QMainWindow):
for recentProject in self.mainConf.recentList: for recentProject in self.mainConf.recentList:
if recentProject == "": continue if recentProject == "": continue
menuItem = QAction(QIcon.fromTheme("folder-open"), "%d: %s" % (itemCount,recentProject), fileMenu) 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) recentMenu.addAction(menuItem)
itemCount += 1 itemCount += 1
+16 -5
View File
@@ -23,7 +23,16 @@ logger = logging.getLogger(__name__)
class NWItem(): 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): def __init__(self):
@@ -31,8 +40,8 @@ class NWItem():
self.itemHandle = None self.itemHandle = None
self.parHandle = None self.parHandle = None
self.itemOrder = None self.itemOrder = None
self.itemType = nwItemType.NONE self.itemType = nwItemType.NO_TYPE
self.itemClass = nwItemClass.NONE self.itemClass = nwItemClass.NO_CLASS
self.itemDepth = None self.itemDepth = None
self.hasChildren = False self.hasChildren = False
self.isExpanded = False self.isExpanded = False
@@ -116,25 +125,27 @@ class NWItem():
def setType(self, theType): def setType(self, theType):
if isinstance(theType, nwItemType): if isinstance(theType, nwItemType):
self.itemType = theType self.itemType = theType
return
else: else:
for itemType in nwItemType: for itemType in nwItemType:
if theType == itemType.name: if theType == itemType.name:
self.itemType = itemType self.itemType = itemType
return return
logger.error("Unrecognised item type '%s'" % theType) logger.error("Unrecognised item type '%s'" % theType)
self.itemType = nwItemType.NONE self.itemType = nwItemType.NO_TYPE
return return
def setClass(self, theClass): def setClass(self, theClass):
if isinstance(theClass, nwItemClass): if isinstance(theClass, nwItemClass):
self.itemClass = theClass self.itemClass = theClass
return
else: else:
for itemClass in nwItemClass: for itemClass in nwItemClass:
if theClass == itemClass.name: if theClass == itemClass.name:
self.itemClass = itemClass self.itemClass = itemClass
return return
logger.error("Unrecognised item class '%s'" % theClass) logger.error("Unrecognised item class '%s'" % theClass)
self.itemClass = nwItemClass.NONE self.itemClass = nwItemClass.NO_CLASS
return return
def setDepth(self, theDepth): def setDepth(self, theDepth):
+3 -1
View File
@@ -245,7 +245,7 @@ class NWProject():
""" """
validActions = {} 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: if tHandle is None:
validActions[nwItemAction.ADD_ROOT] = { validActions[nwItemAction.ADD_ROOT] = {
"Type" : nwItemType.ROOT, "Type" : nwItemType.ROOT,
@@ -318,6 +318,8 @@ class NWProject():
"""Checks if there already is a root entry of class 'theClass' in the """Checks if there already is a root entry of class 'theClass' in the
root of the project tree. root of the project tree.
""" """
if theClass == nwItemClass.NO_CLASS:
return True
for aRoot in self.treeRoots: for aRoot in self.treeRoots:
if theClass == self.projTree[aRoot].itemClass: if theClass == self.projTree[aRoot].itemClass:
return False return False