Some changes to item classes, and added a sample context menu to the tree view

This commit is contained in:
Veronica K. B. Olsen
2019-04-13 21:45:55 +02:00
parent eca8a807ce
commit 4d448cad9d
4 changed files with 51 additions and 24 deletions
+3 -2
View File
@@ -26,7 +26,8 @@ class nwItemClass(Enum):
NONE = 0 NONE = 0
NOVEL = 1 NOVEL = 1
CHAPTER = 2 CHAPTER = 2
CHARACTER = 3 SCENE = 3
WORLD = 4 CHARACTER = 4
WORLD = 5
# END Enum nwItemClass # END Enum nwItemClass
+36 -9
View File
@@ -16,7 +16,7 @@ import nw
from os import path from os import path
from PyQt5.QtGui import QIcon from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, QSize from PyQt5.QtCore import Qt, QSize
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QTreeWidgetItemIterator, QAbstractItemView from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QTreeWidgetItemIterator, QAbstractItemView, QMenu, QAction
from nw.enum import nwItemType, nwItemClass from nw.enum import nwItemType, nwItemClass
@@ -42,6 +42,10 @@ class GuiDocTree(QTreeWidget):
if not self.debugGUI: if not self.debugGUI:
self.hideColumn(3) self.hideColumn(3)
# Context Menu
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self._openContextMenu)
# Allow Move by Drag & Drop # Allow Move by Drag & Drop
self.setDragEnabled(True) self.setDragEnabled(True)
self.setDragDropMode(QAbstractItemView.InternalMove) self.setDragDropMode(QAbstractItemView.InternalMove)
@@ -102,6 +106,37 @@ class GuiDocTree(QTreeWidget):
self.theProject.setTreeOrder(theList) self.theProject.setTreeOrder(theList)
return True return True
def getColumnSizes(self):
retVals = [
self.columnWidth(0),
self.columnWidth(1),
self.columnWidth(2),
]
return retVals
##
# Context Menu
##
def _openContextMenu(self, thePos):
sIDs = self.selectedIndexes()
print(sIDs)
ctxMenu = QMenu(self)
menuNewObject = QAction(QIcon.fromTheme("folder-new"), "New Object", ctxMenu)
# menuNewObject.triggered.connect(self.newProject)
ctxMenu.addAction(menuNewObject)
ctxMenu.exec_(self.viewport().mapToGlobal(thePos))
return
##
# Internal Functions
##
def _scanChildren(self, theList, theItem, theIndex): def _scanChildren(self, theList, theItem, theIndex):
tHandle = theItem.text(3) tHandle = theItem.text(3)
nwItem = self.theProject.projTree[tHandle] nwItem = self.theProject.projTree[tHandle]
@@ -155,12 +190,4 @@ class GuiDocTree(QTreeWidget):
return selItem[0].text(3) return selItem[0].text(3)
return None return None
def getColumnSizes(self):
retVals = [
self.columnWidth(0),
self.columnWidth(1),
self.columnWidth(2),
]
return retVals
# END Class GuiDocTree # END Class GuiDocTree
+9 -10
View File
@@ -40,12 +40,13 @@ class NWDoc():
with open(docPath,mode="r") as inFile: with open(docPath,mode="r") as inFile:
return inFile.read() return inFile.read()
else: else:
logger.debug("Document does not exist.") logger.debug("The requested document does not exist.")
return "" return ""
return None return None
def saveDocument(self, docHtml): def saveDocument(self, docHtml):
if self.docHandle is None: return False if self.docHandle is None:
return False
docDir, docFile = self._assemblePath(self.FILE_MN) docDir, docFile = self._assemblePath(self.FILE_MN)
logger.debug("Saving document %s" % path.join(docDir,docFile)) logger.debug("Saving document %s" % path.join(docDir,docFile))
dataDir = path.join(self.theProject.projPath, docDir) dataDir = path.join(self.theProject.projPath, docDir)
@@ -55,17 +56,15 @@ class NWDoc():
logger.debug("Created folder %s" % dataDir) logger.debug("Created folder %s" % dataDir)
with open(docPath,mode="w") as outFile: with open(docPath,mode="w") as outFile:
outFile.write(docHtml) outFile.write(docHtml)
# docData = html.fromstring(docHtml)
# outFile.write(html.tostring(
# docData,
# pretty_print = True,
# encoding = "utf-8",
# method = "html",
# ))
return True return True
##
# Internal Functions
##
def _assemblePath(self, docExt): def _assemblePath(self, docExt):
if self.docHandle is None: return None if self.docHandle is None:
return None
docDir = "data_"+self.docHandle[0] docDir = "data_"+self.docHandle[0]
docFile = self.docHandle[1:13]+"_"+docExt docFile = self.docHandle[1:13]+"_"+docExt
return docDir, docFile return docDir, docFile
+3 -3
View File
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="0.0.1" fileVersion="1.0" timeStamp="2018-11-02 23:17:55"> <novelWriterXML appVersion="0.0.1" fileVersion="1.0" timeStamp="2019-04-13 21:07:45">
<project> <project>
<name>Sample Project</name> <name>Sample Project</name>
<title>Sample Project</title> <title>Sample Project</title>
@@ -22,13 +22,13 @@
<item handle="96b68994dfa3d" order="0" parent="e7ded148d6e4a"> <item handle="96b68994dfa3d" order="0" parent="e7ded148d6e4a">
<name>New Scene</name> <name>New Scene</name>
<type>FILE</type> <type>FILE</type>
<class>NONE</class> <class>SCENE</class>
<expanded>False</expanded> <expanded>False</expanded>
</item> </item>
<item handle="636b6aa9b697b" order="1" parent="e7ded148d6e4a"> <item handle="636b6aa9b697b" order="1" parent="e7ded148d6e4a">
<name>New File</name> <name>New File</name>
<type>FILE</type> <type>FILE</type>
<class>NONE</class> <class>SCENE</class>
<expanded>False</expanded> <expanded>False</expanded>
</item> </item>
<item handle="f6622b4617424" order="1" parent="None"> <item handle="f6622b4617424" order="1" parent="None">