From 9704307b4d22b6f0d4009bde144617310d69b7ba Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Tue, 23 Oct 2018 21:52:50 +0200 Subject: [PATCH] Save project added, and a number of bits here and there. --- nw/__init__.py | 5 +--- nw/config.py | 33 ++++++++++++++++++++---- nw/gui/doceditor.py | 9 +++++++ nw/gui/doctabs.py | 6 ++--- nw/gui/winmain.py | 60 +++++++++++++++++++++++++++++++++++-------- nw/project/project.py | 37 ++++++++++++++------------ 6 files changed, 112 insertions(+), 38 deletions(-) diff --git a/nw/__init__.py b/nw/__init__.py index 1476db51..6fac954d 100644 --- a/nw/__init__.py +++ b/nw/__init__.py @@ -12,9 +12,6 @@ import logging import getopt -import gi - -gi.require_version("Gtk","3.0") from os import path, remove, rename from PyQt5.QtWidgets import QApplication @@ -31,7 +28,7 @@ __date__ = "2018" __maintainer__ = "Veronica Berglyd Olsen" __email__ = "code@vkbo.net" __status__ = "Development" -__website__ = "https://github.com/vkbo/novelWriter" +__url__ = "https://github.com/vkbo/novelWriter" # # Logging diff --git a/nw/config.py b/nw/config.py index b527980b..d7ff625b 100644 --- a/nw/config.py +++ b/nw/config.py @@ -14,8 +14,9 @@ import logging import configparser import nw -from os import path, mkdir, getcwd -from appdirs import user_config_dir +from os import path, mkdir, getcwd +from appdirs import user_config_dir +from datetime import datetime logger = logging.getLogger(__name__) @@ -29,7 +30,6 @@ class Config: # Set Application Variables self.appName = nw.__package__ self.appHandle = nw.__package__.lower() - self.appURL = "https://github.com/vkbo/novelWriter" # Set Paths self.confPath = user_config_dir(self.appHandle) @@ -38,6 +38,7 @@ class Config: self.appPath = path.dirname(__file__) self.guiPath = path.join(self.appPath,"gui") self.themePath = path.join(self.appPath,"themes") + self.recentList = [""]*10 # If config folder does not exist, make it. # This assumes that the os config folder itself exists. @@ -81,11 +82,18 @@ class Config: confParser.get(cnfSec,"geometry"), 2, self.winGeometry ) + ## Path + cnfSec = "Path" + if confParser.has_section(cnfSec): + for i in range(10): + if confParser.has_option(cnfSec,"recent%d" % i): + self.recentList[i] = confParser.get(cnfSec,"recent%d" % i) + return def saveConfig(self): - logger.debug("Config: Saving") + logger.debug("Saving config file") confParser = configparser.ConfigParser() # Set options @@ -93,7 +101,14 @@ class Config: ## Main cnfSec = "Main" confParser.add_section(cnfSec) - confParser.set(cnfSec,"geometry", self.packList(self.winGeometry)) + confParser.set(cnfSec,"timestamp", datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + confParser.set(cnfSec,"geometry", self.packList(self.winGeometry)) + + ## Path + cnfSec = "Path" + confParser.add_section(cnfSec) + for i in range(10): + confParser.set(cnfSec,"recent%d" % i, str(self.recentList[i])) # Write config file confParser.write(open(path.join(self.confPath,self.confFile),"w")) @@ -118,6 +133,14 @@ class Config: # Setters ## + def setRecent(self, recentPath): + if recentPath == "": return + if recentPath in self.recentList[0:10]: + self.recentList.remove(recentPath) + self.recentList.insert(0,recentPath) + return + + def setConfPath(self, newPath): if newPath is None: return if not path.isfile(newPath): diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 89bbe59d..5c2f069e 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -47,11 +47,20 @@ class GuiDocEditor(QWidget): self.outerBox.addWidget(self.splitEdit) self.guiEditor.setPlainText("Hello Kitty!") + self.guiEditor.setFixedWidth(600) logger.debug("DocEditor initialisation complete") return + def getText(self): + + theText = self.guiEditor.toHtml() + print(theText) + print(self.guiEditor.toPlainText()) + + return + def _buildTabToolBar(self): toolBar = self.editToolBar toolBar.setToolButtonStyle(Qt.ToolButtonIconOnly) diff --git a/nw/gui/doctabs.py b/nw/gui/doctabs.py index 9372ecc9..27630244 100644 --- a/nw/gui/doctabs.py +++ b/nw/gui/doctabs.py @@ -46,12 +46,12 @@ class GuiDocTabs(QTabWidget): if tabType == nw.DOCTYPE_ABOUT: self._createTabAbout() return True - elif tabType == nw.DOCTYPE_DOC: - self._createTabDoc(theName) - return True elif tabType == nw.DOCTYPE_PROJECT: self._createTabProject(theName) return True + elif tabType == nw.DOCTYPE_DOC: + self._createTabDoc(theName) + return True return False # diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index 9fbf7b60..a2fc31fc 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -13,13 +13,14 @@ import logging import nw -from os import path -from PyQt5.QtWidgets import qApp, QWidget, QMainWindow, QHBoxLayout, QVBoxLayout, QFrame, QSplitter, QAction, QToolBar -from PyQt5.QtCore import Qt, QSize -from PyQt5.QtGui import QIcon, QStandardItemModel +from os import path +from PyQt5.QtWidgets import qApp, QWidget, QMainWindow, QHBoxLayout, QVBoxLayout, QFrame, QSplitter, QAction, QToolBar, QFileDialog +from PyQt5.QtCore import Qt, QSize +from PyQt5.QtGui import QIcon, QStandardItemModel -from nw.gui.doctree import GuiDocTree -from nw.gui.doctabs import GuiDocTabs +from nw.gui.doctree import GuiDocTree +from nw.gui.doceditor import GuiDocEditor +from nw.project.project import NWProject logger = logging.getLogger(__name__) @@ -29,7 +30,8 @@ class GuiMain(QMainWindow): QWidget.__init__(self) logger.debug("Initialising GUI ...") - self.mainConf = nw.CONFIG + self.mainConf = nw.CONFIG + self.theProject = NWProject() self.resize(1100,650) self.setTitle() @@ -37,13 +39,13 @@ class GuiMain(QMainWindow): self.statBar = self.statusBar() - self.docTabs = GuiDocTabs() + self.docEditor = GuiDocEditor() self.treePane = QFrame() self.treePane.setFrameShape(QFrame.StyledPanel) self.splitMain = QSplitter(Qt.Horizontal) self.splitMain.addWidget(self.treePane) - self.splitMain.addWidget(self.docTabs) + self.splitMain.addWidget(self.docEditor) self.treeBox = QVBoxLayout() self.treeView = GuiDocTree() @@ -89,6 +91,29 @@ class GuiMain(QMainWindow): return + def saveProject(self): + if self.theProject.projPath is None: + dlgOpt = QFileDialog.Options() + dlgOpt |= QFileDialog.DontUseNativeDialog + projPath, _ = QFileDialog.getSaveFileName( + self,"Save novelWriter Project","","novelWriter Project File (nwProject.nwx);;All Files (*)", options=dlgOpt) + if projPath: + self.theProject.setProjectPath(projPath) + else: + return False + self.theProject.saveProject() + return True + + # + # Document Actions + # + + def saveDocument(self): + + self.docEditor.getText() + + return + # # Internal Functions # @@ -113,6 +138,7 @@ class GuiMain(QMainWindow): # File > Save Project menuSaveProject = QAction(QIcon.fromTheme("document-save"), "Save Project", menuBar) menuSaveProject.setStatusTip("Save Project") + menuSaveProject.triggered.connect(self.saveProject) fileMenu.addAction(menuSaveProject) # File > Recent Project @@ -147,6 +173,7 @@ class GuiMain(QMainWindow): menuSave = QAction(QIcon.fromTheme("document-save"), "&Save", menuBar) menuSave.setShortcut("Ctrl+S") menuSave.setStatusTip("Save document") + menuSave.triggered.connect(self.saveDocument) fileMenu.addAction(menuSave) # --------------------- @@ -189,12 +216,17 @@ class GuiMain(QMainWindow): return + def _closeMain(self): + logger.info("Exiting %s" % nw.__package__) + self.mainConf.saveConfig() + return + # # Menu Action # def _menuExit(self): - logger.info("Exiting %s" % nw.__package__) + self._closeMain() qApp.quit() return True @@ -202,4 +234,12 @@ class GuiMain(QMainWindow): self.docTabs.createTab(None,nw.DOCTYPE_ABOUT) return True + # + # Events + # + + def closeEvent(self, guiEvent): + self._closeMain() + guiEvent.accept() + # END Class GuiMain diff --git a/nw/project/project.py b/nw/project/project.py index 900775b5..cc98d67f 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -13,8 +13,9 @@ import logging import nw -from os import path, mkdir -from lxml import etree +from os import path, mkdir +from lxml import etree +from datetime import datetime logger = logging.getLogger(__name__) @@ -22,13 +23,14 @@ class NWProject(): def __init__(self): + self.mainConf = nw.CONFIG + self.projFolder = None self.projTree = [] # Project Settings - self.projPath = "" - self.projData = "" - self.projName = "" + self.projPath = None + self.projFile = "nwProject.nwx" self.bookTitle = "" self.bookAuthors = [] @@ -40,22 +42,19 @@ class NWProject(): def saveProject(self): - projDir = path.dirname(self.projPath) - projFile = path.basename(self.projPath) - logger.vverbose("Project folder is %s" % projDir) - logger.vverbose("Project file is %s" % projFile) + if self.projPath is None: + logger.error("Project path not set, cannot save.") + return False - if bookFile[-4:] == ".nwx": - self.projData = path.join(projDir,projFile[:-4]+".nwd") - if not path.isdir(self.projData): - logger.info("Created folder %s" % self.projData) - mkdir(self.projData) + if not path.isdir(self.projPath): + logger.info("Created folder %s" % self.projPath) + mkdir(self.projPath) # Root element and book details nwXML = etree.Element("novelWriterXML",attrib={ "fileVersion" : "1.0", "appVersion" : str(nw.__version__), - "timeStamp" : getTimeStamp("-"), + "timeStamp" : datetime.now().strftime("%Y-%m-%d %H:%M:%S"), }) xBook = etree.SubElement(nwXML,"book") xBookTitle = etree.SubElement(xBook,"title") @@ -66,7 +65,7 @@ class NWProject(): xBookAuthor.text = bookAuthor # Write the xml tree to file - with open(self.bookPath,"wb") as outFile: + with open(path.join(self.projPath,self.projFile),"wb") as outFile: outFile.write(etree.tostring( nwXML, pretty_print = True, @@ -74,12 +73,18 @@ class NWProject(): xml_declaration = True )) + self.mainConf.setRecent(self.projPath) + return True # # Set Functions # + def setProjectPath(self, projPath): + self.projPath = projPath + return True + def setProjectName(self, projName): self.projName = projName.strip() return True