From f816005f2be29d9470ddc9e8a4b612a70de8f988 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sat, 27 Apr 2019 10:32:46 +0200 Subject: [PATCH] Moved main menu to separate file --- nw/gui/{winmain.py => mainmenu.py} | 344 +++++------------------------ 1 file changed, 56 insertions(+), 288 deletions(-) rename nw/gui/{winmain.py => mainmenu.py} (51%) diff --git a/nw/gui/winmain.py b/nw/gui/mainmenu.py similarity index 51% rename from nw/gui/winmain.py rename to nw/gui/mainmenu.py index 7d4aa545..250b6e43 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/mainmenu.py @@ -1,145 +1,53 @@ # -*- coding: utf-8 -*- -"""novelWriter GUI Main Window +"""novelWriter GUI Main Menu - novelWriter – GUI Main Window -=============================== + novelWriter – GUI Main Menu +============================= Class holding the main window File History: - Created: 2018-09-22 [0.0.1] + Created: 2019-04-27 [0.0.1] (Split from winmain) """ import logging import nw -from os import path -from PyQt5.QtWidgets import qApp, QWidget, QMainWindow, QVBoxLayout, QFrame, QSplitter, QAction, QToolBar, QFileDialog, QStackedWidget -from PyQt5.QtCore import Qt, QSize, pyqtSlot -from PyQt5.QtGui import QIcon +from PyQt5.QtWidgets import QMenu +# from os import path +# from PyQt5.QtWidgets import qApp, QWidget, QMainWindow, QVBoxLayout, QFrame, QSplitter, QAction, QToolBar, QFileDialog, QStackedWidget +# from PyQt5.QtCore import Qt, QSize, pyqtSlot +# from PyQt5.QtGui import QIcon -from nw.gui.doctree import GuiDocTree -from nw.gui.doctreectx import GuiDocTreeCtx -from nw.gui.doceditor import GuiDocEditor -from nw.gui.docdetails import GuiDocDetails -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.project.item import NWItem -from nw.enum import nwItemType, nwItemClass, nwDocAction, nwItemAction +# from nw.gui.doctree import GuiDocTree +# from nw.gui.doctreectx import GuiDocTreeCtx +# from nw.gui.doceditor import GuiDocEditor +# from nw.gui.docdetails import GuiDocDetails +# 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.project.item import NWItem +# from nw.enum import nwItemType, nwItemClass, nwDocAction, nwItemAction logger = logging.getLogger(__name__) -class GuiMain(QMainWindow): +class GuiMainMenu(QMenu): - def __init__(self): - QWidget.__init__(self) + def __init__(self, theParent, theProject): + QMenu.__init__(self, theParent) - logger.debug("Initialising GUI ...") + logger.debug("Initialising Main Menu ...") self.mainConf = nw.CONFIG self.theProject = NWProject() self.theDocument = NWDoc(self.theProject, self) - self.resize(*self.mainConf.winGeometry) - self._setWindowTitle() - self.setWindowIcon(QIcon(path.join(self.mainConf.appPath,"..","novelWriter.svg"))) - - # Main GUI Elements - self.docEditor = GuiDocEditor(self) - self.docDetails = GuiDocDetails(self.theProject) - self.treeView = GuiDocTree(self.theProject) - - # Assemble Main Window - self.stackPane = QStackedWidget() - self.stackNone = self.stackPane.addWidget(QWidget()) - self.stackDoc = self.stackPane.addWidget(self.docEditor) - self.stackPane.setCurrentIndex(self.stackNone) - - self.treePane = QFrame() - self.treeBox = QVBoxLayout() - self.treeBox.addWidget(self.treeView) - self.treeBox.addWidget(self.docDetails) - self.treePane.setLayout(self.treeBox) - - self.splitMain = QSplitter(Qt.Horizontal) - self.splitMain.addWidget(self.treePane) - self.splitMain.addWidget(self.stackPane) - self.splitMain.setSizes(self.mainConf.mainPanePos) - self.splitMain.splitterMoved.connect(self._splitMainMove) - - self.setCentralWidget(self.splitMain) - - # Build Menus self._buildMenu() - self.treeView.setContextMenuPolicy(Qt.CustomContextMenu) - self.treeView.customContextMenuRequested.connect(self._openDocTreeContextMenu) - self.treeView.itemSelectionChanged.connect(self._treeSingleClick) - self.treeView.itemDoubleClicked.connect(self._treeDoubleClick) - self.treeView.buildTree() - # Build Status Bar - self.statusBar = GuiMainStatus() - self.setStatusBar(self.statusBar) - self.statusBar.showMessage("Ready") - - # Load Theme StyleSheet - cssFile = path.join(self.mainConf.themePath,self.mainConf.guiTheme+".css") - if path.isfile(cssFile): - with open(cssFile,mode="r") as inFile: - theCss = inFile.read() - self.setStyleSheet(theCss) - - self.show() - - logger.debug("GUI initialisation complete") + logger.debug("Main Menu initialisation complete") return - ## - # Project Actions - ## - - def newProject(self): - logger.info("Creating new project") - self.theProject.newProject() - self.treeView.buildTree() - return - - def openProject(self): - dlgOpt = QFileDialog.Options() - dlgOpt |= QFileDialog.DontUseNativeDialog - projPath, _ = QFileDialog.getOpenFileName( - self,"Open novelWriter Project","","novelWriter Project File (nwProject.nwx);;All Files (*)", options=dlgOpt - ) - if projPath: - self.theProject.openProject(projPath) - self.treeView.buildTree() - self._setWindowTitle(self.theProject.projName) - else: - return False - return True - - 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.treeView.saveTreeOrder() - self.theProject.saveProject() - return True - - def editProject(self): - dlgProj = GuiProjectEditor(self, self.theProject) - dlgProj.exec_() - return True - def openRecentProject(self, menuItem, recentItem): logger.verbose("User requested opening recent project #%d" % recentItem) self.theProject.openProject(self.mainConf.recentList[recentItem]) @@ -147,170 +55,30 @@ class GuiMain(QMainWindow): self._setWindowTitle(self.theProject.projName) return True - ## - # Document Actions - ## - - def openDocument(self, tHandle): - self.stackPane.setCurrentIndex(self.stackDoc) - self.docEditor.setText(self.theDocument.openDocument(tHandle)) - self.docEditor.changeWidth() - return - - def saveDocument(self): - docHtml = self.docEditor.getText() - self.theDocument.theItem.setCharCount(self.docEditor.charCount) - self.theDocument.theItem.setWordCount(self.docEditor.wordCount) - self.theDocument.theItem.setParaCount(self.docEditor.paraCount) - self.theDocument.saveDocument(docHtml) - return - - ## - # Internal Functions - ## - - def _treeSingleClick(self): - sHandle = self.treeView.getSelectedHandle() - if sHandle is not None: - self.docDetails.buildViewBox(sHandle) - return - - def _treeDoubleClick(self, tItem, colNo): - tHandle = tItem.text(3) - logger.verbose("User double clicked tree item with handle %s" % tHandle) - nwItem = self.theProject.getItem(tHandle) - if nwItem.itemType == nwItemType.FILE: - logger.verbose("Requested item %s is a file" % tHandle) - self.openDocument(tHandle) - else: - logger.verbose("Requested item %s is a folder" % tHandle) - return - - def _closeMain(self): - logger.info("Exiting %s" % nw.__package__) - self.mainConf.setWinSize(self.width(), self.height()) - self.mainConf.setTreeColWidths(self.treeView.getColumnSizes()) - self.mainConf.setMainPanePos(self.splitMain.sizes()) - self.mainConf.saveConfig() - return - - def _setWindowTitle(self, projName=None): - winTitle = "%s [%s]" % (nw.__package__, nw.__version__) - if projName is not None: - winTitle += " - %s" % projName - self.setWindowTitle(winTitle) - return True - - ## - # Menu Action - ## - - def _menuExit(self): - self._closeMain() - qApp.quit() - return True - - def _showAbout(self): - self.docTabs.createTab(None,nw.DOCTYPE_ABOUT) - return True - - ## - # DocTree Context Menu - ## - - def _openDocTreeContextMenu(self, thePosition): - - ctxMenu = GuiDocTreeCtx(self.treeView, self.theProject, thePosition) - selHandle = ctxMenu.selHandle - selAction = ctxMenu.selAction - selClass = ctxMenu.selClass - selType = ctxMenu.selType - selTarget = ctxMenu.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: - self.treeView.moveTreeItem(selHandle, -1) - elif selAction == nwItemAction.MOVE_DOWN: - self.treeView.moveTreeItem(selHandle, 1) - 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 - elif selAction == nwItemAction.RENAME: - self.treeView.renameTreeItem(selHandle) - - return - - ## - # Events - ## - - def resizeEvent(self, theEvent): - """Extend QMainWindow.resizeEvent to signal dependent GUI elements that its pane may have changed size. - """ - QMainWindow.resizeEvent(self,theEvent) - if self.stackPane.currentIndex() == self.stackDoc: - self.docEditor.changeWidth() - return - - def closeEvent(self, theEvent): - self._closeMain() - QMainWindow.closeEvent(self,theEvent) - return - - ## - # Signal Handlers - ## - - def _splitMainMove(self, pWidth, pHeight): - """Alert dependent GUI elements that the main pane splitter has been moved. - """ - if self.stackPane.currentIndex() == self.stackDoc: - self.docEditor.changeWidth() - return - ## # GUI Builders ## def _buildMenu(self): - menuBar = self.menuBar() # Project - projMenu = menuBar.addMenu("&Project") + projMenu = self.addMenu("&Project") # Project > New Project - menuItem = QAction(QIcon.fromTheme("folder-new"), "New Project", menuBar) + menuItem = QAction(QIcon.fromTheme("folder-new"), "New Project", self) menuItem.setStatusTip("Create New Project") menuItem.triggered.connect(self.newProject) projMenu.addAction(menuItem) # Project > Open Project - menuItem = QAction(QIcon.fromTheme("folder-open"), "Open Project", menuBar) + menuItem = QAction(QIcon.fromTheme("folder-open"), "Open Project", self) menuItem.setStatusTip("Open Project") menuItem.setShortcut("Ctrl+Shift+O") menuItem.triggered.connect(self.openProject) projMenu.addAction(menuItem) # Project > Save Project - menuItem = QAction(QIcon.fromTheme("document-save"), "Save Project", menuBar) + menuItem = QAction(QIcon.fromTheme("document-save"), "Save Project", self) menuItem.setStatusTip("Save Project") menuItem.setShortcut("Ctrl+Shift+S") menuItem.triggered.connect(self.saveProject) @@ -329,7 +97,7 @@ class GuiMain(QMainWindow): projMenu.addSeparator() # Project > Project Settings - menuItem = QAction(QIcon.fromTheme("document-properties"), "Project Settings", menuBar) + menuItem = QAction(QIcon.fromTheme("document-properties"), "Project Settings", self) menuItem.setStatusTip("Project Settings") menuItem.triggered.connect(self.editProject) projMenu.addAction(menuItem) @@ -338,7 +106,7 @@ class GuiMain(QMainWindow): projMenu.addSeparator() # Project > Exit - menuItem = QAction(QIcon.fromTheme("application-exit"), "Exit", menuBar) + menuItem = QAction(QIcon.fromTheme("application-exit"), "Exit", self) menuItem.setStatusTip("Exit %s" % nw.__package__) menuItem.setShortcut("Ctrl+Q") menuItem.triggered.connect(self._menuExit) @@ -347,22 +115,22 @@ class GuiMain(QMainWindow): ############################################################################################ # Item - itemMenu = menuBar.addMenu("&Item") + itemMenu = self.addMenu("&Item") # Item > New - menuItem = QAction(QIcon.fromTheme("document-new"), "&New Document", menuBar) + menuItem = QAction(QIcon.fromTheme("document-new"), "&New Document", self) menuItem.setStatusTip("Create New Document") menuItem.setShortcut("Ctrl+N") itemMenu.addAction(menuItem) # Item > Open - menuItem = QAction(QIcon.fromTheme("document-open"), "&Open Document", menuBar) + menuItem = QAction(QIcon.fromTheme("document-open"), "&Open Document", self) menuItem.setStatusTip("Open Selected Document") menuItem.setShortcut("Ctrl+O") itemMenu.addAction(menuItem) # Item > Save - menuItem = QAction(QIcon.fromTheme("document-save"), "&Save Document", menuBar) + menuItem = QAction(QIcon.fromTheme("document-save"), "&Save Document", self) menuItem.setStatusTip("Save Current Document") menuItem.setShortcut("Ctrl+S") menuItem.triggered.connect(self.saveDocument) @@ -372,7 +140,7 @@ class GuiMain(QMainWindow): itemMenu.addSeparator() # Item > New Folder - menuItem = QAction(QIcon.fromTheme("folder-new"), "New Folder", menuBar) + menuItem = QAction(QIcon.fromTheme("folder-new"), "New Folder", self) menuItem.setStatusTip("New Folder") menuItem.setShortcut("Ctrl+Shift+N") itemMenu.addAction(menuItem) @@ -393,7 +161,7 @@ class GuiMain(QMainWindow): itemMenu.addSeparator() # Item > Delete Item - menuItem = QAction(QIcon.fromTheme("folder-delete"), "Delete Item", menuBar) + menuItem = QAction(QIcon.fromTheme("folder-delete"), "Delete Item", self) menuItem.setStatusTip("Delete Selected Item") menuItem.setShortcut("Del") itemMenu.addAction(menuItem) @@ -401,17 +169,17 @@ class GuiMain(QMainWindow): ############################################################################################ # Edit - editMenu = menuBar.addMenu("&Edit") + editMenu = self.addMenu("&Edit") # Edit > Undo - menuItem = QAction(QIcon.fromTheme("edit-undo"), "Undo", menuBar) + menuItem = QAction(QIcon.fromTheme("edit-undo"), "Undo", self) menuItem.setStatusTip("Undo Last Change") menuItem.setShortcut("Ctrl+Z") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.UNDO)) editMenu.addAction(menuItem) # Edit > Redo - menuItem = QAction(QIcon.fromTheme("edit-redo"), "Redo", menuBar) + menuItem = QAction(QIcon.fromTheme("edit-redo"), "Redo", self) menuItem.setStatusTip("Redo Last Change") menuItem.setShortcut("Ctrl+Y") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.REDO)) @@ -421,21 +189,21 @@ class GuiMain(QMainWindow): editMenu.addSeparator() # Edit > Cut - menuItem = QAction(QIcon.fromTheme("edit-cut"), "Cut", menuBar) + menuItem = QAction(QIcon.fromTheme("edit-cut"), "Cut", self) menuItem.setStatusTip("Cut Selected Text") menuItem.setShortcut("Ctrl+X") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.CUT)) editMenu.addAction(menuItem) # Edit > Copy - menuItem = QAction(QIcon.fromTheme("edit-copy"), "Copy", menuBar) + menuItem = QAction(QIcon.fromTheme("edit-copy"), "Copy", self) menuItem.setStatusTip("Copy Selected Text") menuItem.setShortcut("Ctrl+C") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.COPY)) editMenu.addAction(menuItem) # Edit > Paste - menuItem = QAction(QIcon.fromTheme("edit-paste"), "Paste", menuBar) + menuItem = QAction(QIcon.fromTheme("edit-paste"), "Paste", self) menuItem.setStatusTip("Paste Text from Clipboard") menuItem.setShortcut("Ctrl+V") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.PASTE)) @@ -445,14 +213,14 @@ class GuiMain(QMainWindow): editMenu.addSeparator() # Edit > Select All - menuItem = QAction(QIcon.fromTheme("edit-select-all"), "Select All", menuBar) + menuItem = QAction(QIcon.fromTheme("edit-select-all"), "Select All", self) menuItem.setStatusTip("Select All Text in Document") menuItem.setShortcut("Ctrl+A") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.SEL_ALL)) editMenu.addAction(menuItem) # Edit > Select Paragraph - menuItem = QAction(QIcon.fromTheme("edit-select-all"), "Select Paragraph", menuBar) + menuItem = QAction(QIcon.fromTheme("edit-select-all"), "Select Paragraph", self) menuItem.setStatusTip("Select All Text in Paragraph") menuItem.setShortcut("Ctrl+Shift+A") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.SEL_PARA)) @@ -462,31 +230,31 @@ class GuiMain(QMainWindow): editMenu.addSeparator() # Edit > Settings - menuItem = QAction(QIcon.fromTheme("applications-system"), "Program Setting", menuBar) + menuItem = QAction(QIcon.fromTheme("applications-system"), "Program Setting", self) menuItem.setStatusTip("Change %s Settings" % nw.__package__) editMenu.addAction(menuItem) ############################################################################################ # Format - fmtMenu = menuBar.addMenu("&Format") + fmtMenu = self.addMenu("&Format") # Format > Bold Text - menuItem = QAction(QIcon.fromTheme("format-text-bold"), "Bold Text", menuBar) + menuItem = QAction(QIcon.fromTheme("format-text-bold"), "Bold Text", self) menuItem.setStatusTip("Make Selected Text Bold") menuItem.setShortcut("Ctrl+B") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.BOLD)) fmtMenu.addAction(menuItem) # Format > Italic Text - menuItem = QAction(QIcon.fromTheme("format-text-italic"), "Italic Text", menuBar) + menuItem = QAction(QIcon.fromTheme("format-text-italic"), "Italic Text", self) menuItem.setStatusTip("Make Selected Text Italic") menuItem.setShortcut("Ctrl+I") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.ITALIC)) fmtMenu.addAction(menuItem) # Format > Underline Text - menuItem = QAction(QIcon.fromTheme("format-text-underline"), "Underline Text", menuBar) + menuItem = QAction(QIcon.fromTheme("format-text-underline"), "Underline Text", self) menuItem.setStatusTip("Underline Selected Text") menuItem.setShortcut("Ctrl+U") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.U_LINE)) @@ -496,14 +264,14 @@ class GuiMain(QMainWindow): fmtMenu.addSeparator() # Format > Double Quotes - menuItem = QAction(QIcon.fromTheme("insert-text"), "Wrap Double Quotes", menuBar) + menuItem = QAction(QIcon.fromTheme("insert-text"), "Wrap Double Quotes", self) menuItem.setStatusTip("Wrap Selected Text in Double Quotes") menuItem.setShortcut("Ctrl+D") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.D_QUOTE)) fmtMenu.addAction(menuItem) # Format > Single Quotes - menuItem = QAction(QIcon.fromTheme("insert-text"), "Wrap Single Quotes", menuBar) + menuItem = QAction(QIcon.fromTheme("insert-text"), "Wrap Single Quotes", self) menuItem.setStatusTip("Wrap Selected Text in Single Quotes") menuItem.setShortcut("Ctrl+Shift+D") menuItem.triggered.connect(lambda: self.docEditor.docAction(nwDocAction.S_QUOTE)) @@ -512,16 +280,16 @@ class GuiMain(QMainWindow): ############################################################################################ # Help - helpMenu = menuBar.addMenu("&Help") + helpMenu = self.addMenu("&Help") # Help > About - menuItem = QAction(QIcon.fromTheme("help-about"), "About %s" % nw.__package__, menuBar) + menuItem = QAction(QIcon.fromTheme("help-about"), "About %s" % nw.__package__, self) menuItem.setStatusTip("About %s" % nw.__package__) menuItem.triggered.connect(self._showAbout) helpMenu.addAction(menuItem) # Help > About Qt5 - menuItem = QAction(QIcon.fromTheme("help-about"), "About Qt5", menuBar) + menuItem = QAction(QIcon.fromTheme("help-about"), "About Qt5", self) menuItem.setStatusTip("About Qt5") helpMenu.addAction(menuItem) @@ -531,8 +299,8 @@ class GuiMain(QMainWindow): ############################################################################################ # Debug GUI - debugMenu = menuBar.addMenu("&Debug") + debugMenu = self.addMenu("&Debug") return -# END Class GuiMain +# END Class GuiMainMenu