def initSplit(self): hbox3 = QHBoxLayout(self) self.topleft = TopLeft(self) self.bottomleft = BottomLeft(self) self.right = Right(self) splitter1 = QSplitter(Qt.Vertical) splitter1.addWidget(self.topleft) splitter1.addWidget(self.bottomleft) splitter1.setStretchFactor(1, 2) index = splitter1.indexOf(self.topleft) splitter1.setCollapsible(index, False) index3 = splitter1.indexOf(self.bottomleft) splitter1.setCollapsible(index3, False) splitter2 = QSplitter(Qt.Horizontal) splitter2.addWidget(splitter1) splitter2.addWidget(self.right) splitter2.setStretchFactor(1, 10) index2 = splitter2.indexOf(splitter1) splitter2.setCollapsible(index2, False) hbox3.addWidget(splitter2) self.setLayout(hbox3)
class GuiMain(QMainWindow): def __init__(self): QWidget.__init__(self) logger.info("Starting %s" % nw.__package__) logger.debug("Initialising GUI ...") self.mainConf = nw.CONFIG self.theTheme = Theme(self) self.theProject = NWProject(self) self.theIndex = NWIndex(self.theProject, self) self.hasProject = False logger.info("Qt5 Version: %s (%d)" % (self.mainConf.verQtString, self.mainConf.verQtValue)) logger.info("PyQt5 Version: %s (%d)" % (self.mainConf.verPyQtString, self.mainConf.verPyQtValue)) self.resize(*self.mainConf.winGeometry) self._setWindowTitle() self.setWindowIcon(QIcon(path.join(self.mainConf.appIcon))) # Main GUI Elements self.statusBar = GuiMainStatus(self) self.docEditor = GuiDocEditor(self, self.theProject) self.docViewer = GuiDocViewer(self, self.theProject) self.docDetails = GuiDocDetails(self, self.theProject) self.treeView = GuiDocTree(self, self.theProject) self.mainMenu = GuiMainMenu(self, self.theProject) # Minor Gui Elements self.statusIcons = [] self.importIcons = [] # Assemble Main Window self.treePane = QFrame() self.treeBox = QVBoxLayout() self.treeBox.addWidget(self.treeView) self.treeBox.addWidget(self.docDetails) self.treePane.setLayout(self.treeBox) self.splitView = QSplitter(Qt.Horizontal) self.splitView.addWidget(self.docEditor) self.splitView.addWidget(self.docViewer) self.splitView.splitterMoved.connect(self._splitViewMove) self.splitMain = QSplitter(Qt.Horizontal) self.splitMain.addWidget(self.treePane) self.splitMain.addWidget(self.splitView) self.splitMain.setSizes(self.mainConf.mainPanePos) self.splitMain.splitterMoved.connect(self._splitMainMove) self.setCentralWidget(self.splitMain) self.idxTree = self.splitMain.indexOf(self.treePane) self.idxMain = self.splitMain.indexOf(self.splitView) self.idxEditor = self.splitView.indexOf(self.docEditor) self.idxViewer = self.splitView.indexOf(self.docViewer) self.splitMain.setCollapsible(self.idxTree, False) self.splitMain.setCollapsible(self.idxMain, False) self.splitView.setCollapsible(self.idxEditor, False) self.splitView.setCollapsible(self.idxViewer, True) self.docViewer.setVisible(False) # Build The Tree View self.treeView.itemSelectionChanged.connect(self._treeSingleClick) self.treeView.itemDoubleClicked.connect(self._treeDoubleClick) self.rebuildTree() # Set Main Window Elements self.setMenuBar(self.mainMenu) self.setStatusBar(self.statusBar) self.statusBar.setStatus("Ready") # Set Up Autosaving Project Timer self.asProjTimer = QTimer() self.asProjTimer.timeout.connect(self._autoSaveProject) # Set Up Autosaving Document Timer self.asDocTimer = QTimer() self.asDocTimer.timeout.connect(self._autoSaveDocument) # Keyboard Shortcuts QShortcut(Qt.Key_Return, self.treeView, context=Qt.WidgetShortcut, activated=self._treeKeyPressReturn) # Forward Functions self.setStatus = self.statusBar.setStatus self.setProjectStatus = self.statusBar.setProjectStatus if self.mainConf.showGUI: self.show() self.initMain() self.asProjTimer.start() self.asDocTimer.start() self.statusBar.clearStatus() logger.debug("GUI initialisation complete") return def clearGUI(self): self.treeView.clearTree() self.docEditor.clearEditor() self.closeDocViewer() self.statusBar.clearStatus() return True def initMain(self): self.asProjTimer.setInterval(int(self.mainConf.autoSaveProj * 1000)) self.asDocTimer.setInterval(int(self.mainConf.autoSaveDoc * 1000)) return True ## # Project Actions ## def newProject(self, projPath=None, forceNew=False): if self.hasProject: msgBox = QMessageBox() msgRes = msgBox.warning( self, "New Project", "Please close the current project<br>before making a new one.") return False if projPath is None: projPath = self.newProjectDialog() if projPath is None: return False if path.isfile(path.join(projPath, self.theProject.projFile)) and not forceNew: msgBox = QMessageBox() msgRes = msgBox.critical( self, "New Project", "A project already exists in that location.<br>Please choose another folder." ) return False logger.info("Creating new project") self.theProject.newProject() self.theProject.setProjectPath(projPath) self.rebuildTree() self.saveProject() self.hasProject = True self.statusBar.setRefTime(self.theProject.projOpened) return True def closeProject(self, isYes=False): """Closes the project if one is open. isYes is passed on from the close application event so the user doesn't get prompted twice. """ if not self.hasProject: # There is no project loaded, everything OK return True if self.mainConf.showGUI and not isYes: msgBox = QMessageBox() msgRes = msgBox.question( self, "Close Project", "Save changes and close current project?") if msgRes != QMessageBox.Yes: return False if self.docEditor.docChanged: self.saveDocument() if self.theProject.projChanged: saveOK = self.saveProject() doBackup = False if self.theProject.doBackup and self.mainConf.backupOnClose: doBackup = True if self.mainConf.showGUI and self.mainConf.askBeforeBackup: msgBox = QMessageBox() msgRes = msgBox.question(self, "Backup Project", "Backup current project?") if msgRes != QMessageBox.Yes: doBackup = False if doBackup: self.backupProject() else: saveOK = True if saveOK: self.closeDocument() self.theProject.closeProject() self.theIndex.clearIndex() self.clearGUI() self.hasProject = False return saveOK def openProject(self, projFile=None): """Open a project. projFile is passed from the open recent projects menu, so can be set. If not, we pop the dialog. """ if projFile is None: projFile = self.openProjectDialog() if projFile is None: return False # Make sure any open project is cleared out first before we load another one if not self.closeProject(): return False # Try to open the project if not self.theProject.openProject(projFile): return False # project is loaded self.hasProject = True # Load the tag index self.theIndex.loadIndex() # Update GUI self._setWindowTitle(self.theProject.projName) self.rebuildTree() self.docEditor.setDictionaries() self.docEditor.setSpellCheck(self.theProject.spellCheck) self.statusBar.setRefTime(self.theProject.projOpened) self.mainMenu.updateMenu() # Restore previously open documents, if any if self.theProject.lastEdited is not None: self.openDocument(self.theProject.lastEdited) if self.theProject.lastViewed is not None: self.viewDocument(self.theProject.lastViewed) return True def saveProject(self): """Save the current project. """ if not self.hasProject: return False # If the project is new, it may not have a path, so we need one if self.theProject.projPath is None: projPath = self.saveProjectDialog() self.theProject.setProjectPath(projPath) if self.theProject.projPath is None: return False self.treeView.saveTreeOrder() self.theProject.saveProject() self.theIndex.saveIndex() self.mainMenu.updateRecentProjects() return True def backupProject(self): theBackup = NWBackup(self, self.theProject) theBackup.zipIt() return True ## # Document Actions ## def closeDocument(self): if self.hasProject: if self.docEditor.docChanged: self.saveDocument() self.docEditor.clearEditor() return True def openDocument(self, tHandle): if self.hasProject: self.closeDocument() self.docEditor.loadText(tHandle) self.docEditor.setFocus() self.docEditor.changeWidth() self.theProject.setLastEdited(tHandle) return True def saveDocument(self): if self.hasProject: self.docEditor.saveText() return True def viewDocument(self, tHandle=None): if tHandle is None: tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.debug("No document selected, trying editor document") tHandle = self.docEditor.theHandle if tHandle is None: logger.debug("No document selected, trying last viewed") tHandle = self.theProject.lastViewed if tHandle is None: logger.debug("No document selected, giving up") return False if self.docViewer.loadText(tHandle) and not self.docViewer.isVisible(): bPos = self.splitMain.sizes() self.docViewer.setVisible(True) vPos = [0, 0] vPos[0] = int(bPos[1] / 2) vPos[1] = bPos[1] - vPos[0] self.splitView.setSizes(vPos) self.docEditor.changeWidth() return True ## # Tree Item Actions ## def openSelectedItem(self): tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.warning("No item selected") return False logger.verbose("Opening item %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 not a file" % tHandle) return True def editItem(self): tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.warning("No item selected") return logger.verbose("Requesting change to item %s" % tHandle) if self.mainConf.showGUI: dlgProj = GuiItemEditor(self, self.theProject, tHandle) if dlgProj.exec_(): self.treeView.setTreeItemValues(tHandle) return def rebuildTree(self): self._makeStatusIcons() self._makeImportIcons() self.treeView.clearTree() self.treeView.buildTree() return def rebuildIndex(self): if not self.hasProject: return False logger.debug("Rebuilding indices ...") self.treeView.saveTreeOrder() self.theIndex.clearIndex() nItems = len(self.theProject.treeOrder) dlgProg = QProgressDialog("Scanning files ...", "Cancel", 0, nItems, self) dlgProg.setWindowModality(Qt.WindowModal) dlgProg.setMinimumDuration(0) dlgProg.setFixedWidth(480) dlgProg.setLabelText("Starting file scan ...") dlgProg.setValue(0) dlgProg.show() time.sleep(0.5) nDone = 0 for tHandle in self.theProject.treeOrder: tItem = self.theProject.getItem(tHandle) dlgProg.setValue(nDone) dlgProg.setLabelText("Scanning: %s" % tItem.itemName) logger.verbose("Scanning: %s" % tItem.itemName) if tItem is not None and tItem.itemType == nwItemType.FILE: theDoc = NWDoc(self.theProject, self) theText = theDoc.openDocument(tHandle, False) # Run Word Count cC, wC, pC = countWords(theText) tItem.setCharCount(cC) tItem.setWordCount(wC) tItem.setParaCount(pC) self.treeView.propagateCount(tHandle, wC) self.treeView.projectWordCount() # Build tag index self.theIndex.scanText(tHandle, theText) nDone += 1 if dlgProg.wasCanceled(): break dlgProg.setValue(nItems) return True ## # Main Dialogs ## def openProjectDialog(self): dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.DontUseNativeDialog projFile, _ = QFileDialog.getOpenFileName( self, "Open novelWriter Project", "", "novelWriter Project File (%s);;All Files (*)" % nwFiles.PROJ_FILE, options=dlgOpt) if projFile: return projFile return None def saveProjectDialog(self): dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.ShowDirsOnly dlgOpt |= QFileDialog.DontUseNativeDialog projPath = QFileDialog.getExistingDirectory(self, "Save novelWriter Project", "", options=dlgOpt) if projPath: return projPath return None def newProjectDialog(self): dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.ShowDirsOnly dlgOpt |= QFileDialog.DontUseNativeDialog projPath = QFileDialog.getExistingDirectory( self, "Select Location for New novelWriter Project", "", options=dlgOpt) if projPath: return projPath return None def editConfigDialog(self): dlgConf = GuiConfigEditor(self, self.theProject) if dlgConf.exec_() == QDialog.Accepted: logger.debug("Applying new preferences") self.initMain() self.theTheme.updateTheme() self.docEditor.initEditor() self.docViewer.initViewer() return True def editProjectDialog(self): if self.hasProject: dlgProj = GuiProjectEditor(self, self.theProject) dlgProj.exec_() self._setWindowTitle(self.theProject.projName) return True def showTimeLineDialog(self): if self.hasProject: dlgTLine = GuiTimeLineView(self, self.theProject, self.theIndex) dlgTLine.exec_() return True def makeAlert(self, theMessage, theLevel=nwAlert.INFO): """Alert both the user and the logger at the same time. Message can be either a string or an array of strings. Severity level is 0 = info, 1 = warning, and 2 = error. """ if isinstance(theMessage, list): popMsg = "<br>".join(theMessage) logMsg = theMessage else: popMsg = theMessage logMsg = [theMessage] msgBox = QMessageBox() if theLevel == nwAlert.INFO: for msgLine in logMsg: logger.info(msgLine) msgBox.information(self, "Information", popMsg) elif theLevel == nwAlert.WARN: for msgLine in logMsg: logger.warning(msgLine) msgBox.warning(self, "Warning", popMsg) elif theLevel == nwAlert.ERROR: for msgLine in logMsg: logger.error(msgLine) msgBox.critical(self, "Error", popMsg) elif theLevel == nwAlert.BUG: for msgLine in logMsg: logger.error(msgLine) popMsg += "<br>This is a bug!" msgBox.critical(self, "Internal Error", popMsg) return ## # Main Window Actions ## def closeMain(self): if self.mainConf.showGUI and self.hasProject: msgBox = QMessageBox() msgRes = msgBox.question(self, "Exit", "Do you want to save changes and exit?") if msgRes != QMessageBox.Yes: return False logger.info("Exiting %s" % nw.__package__) self.closeProject(True) self.mainConf.setWinSize(self.width(), self.height()) self.mainConf.setTreeColWidths(self.treeView.getColumnSizes()) self.mainConf.setMainPanePos(self.splitMain.sizes()) self.mainConf.setDocPanePos(self.splitView.sizes()) self.mainConf.saveConfig() qApp.quit() return True def setFocus(self, paneNo): if paneNo == 1: self.treeView.setFocus() elif paneNo == 2: self.docEditor.setFocus() elif paneNo == 3: self.docViewer.setFocus() return def closeDocEditor(self): self.closeDocument() self.theProject.setLastEdited(None) return def closeDocViewer(self): self.docViewer.clearViewer() self.theProject.setLastViewed(None) bPos = self.splitMain.sizes() self.docViewer.setVisible(False) vPos = [bPos[1], 0] self.splitView.setSizes(vPos) self.docEditor.changeWidth() return not self.docViewer.isVisible() ## # Internal Functions ## def _setWindowTitle(self, projName=None): winTitle = "%s" % nw.__package__ if projName is not None: winTitle += " - %s" % projName self.setWindowTitle(winTitle) return True def _autoSaveProject(self): if self.hasProject and self.theProject.projChanged and self.theProject.projPath is not None: logger.debug("Autosaving project") self.saveProject() return def _autoSaveDocument(self): if self.hasProject and self.docEditor.docChanged: logger.debug("Autosaving document") self.saveDocument() return def _makeStatusIcons(self): self.statusIcons = {} for sLabel, sCol, _ in self.theProject.statusItems: theIcon = QPixmap(32, 32) theIcon.fill(QColor(*sCol)) self.statusIcons[sLabel] = QIcon(theIcon) return def _makeImportIcons(self): self.importIcons = {} for sLabel, sCol, _ in self.theProject.importItems: theIcon = QPixmap(32, 32) theIcon.fill(QColor(*sCol)) self.importIcons[sLabel] = QIcon(theIcon) 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) self.docEditor.changeWidth() return def closeEvent(self, theEvent): if self.closeMain(): theEvent.accept() else: theEvent.ignore() return ## # Signal Handlers ## 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 _treeKeyPressReturn(self): tHandle = self.treeView.getSelectedHandle() logger.verbose("User pressed return on 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 _splitMainMove(self, pWidth, pHeight): """Alert dependent GUI elements that the main pane splitter has been moved. """ self.docEditor.changeWidth() return def _splitViewMove(self, pWidth, pHeight): """Alert dependent GUI elements that the main pane splitter has been moved. """ self.docEditor.changeWidth() return
def __init__(self, config: dict = {}, parent=None): """ :param config: application settings :param parent: parent QtObject :return: """ QMainWindow.__init__(self, parent) self.resize(1500, 850) if not config or config['config_ok'] is False: msg = "Loading configuration file failed! " \ "Do you want to continue without it?\n" msg_window = DetailedMsgBox(msg, config['config_error'], 'Warning') reply = msg_window.exec_() if reply == QMessageBox.No: QTimer.singleShot(0, lambda: self.close()) return self.common_settings = config if not config['req_file_path'] or not config['macros_ok']: req_file_macros = config['req_file_macros'] req_file_path = config['req_file_path'] init_path = config['init_path'] configure_dialog = \ SnapshotConfigureDialog(self, init_macros=req_file_macros, init_path=os.path.join(init_path, req_file_path)) configure_dialog.accepted.connect(self.set_request_file) if configure_dialog.exec_() == QDialog.Rejected: QTimer.singleShot(0, lambda: self.close()) return # Before creating GUI, snapshot must be initialized. self.snapshot = Snapshot() # Create main GUI components: # menu bar # ______________________________ # | save_widget | restore_widget | # | | | # | autorefresh | | # -------------------------------- # | compare_widget | # -------------------------------- # | sts_log | # ______________________________ # status_bar # # menu bar menu_bar = self.menuBar() file_menu = QMenu("File", menu_bar) open_new_req_file_action = QAction("Open", file_menu) open_new_req_file_action.setMenuRole(QAction.NoRole) open_new_req_file_action.triggered.connect(self.open_new_req_file) file_menu.addAction(open_new_req_file_action) quit_action = QAction("Quit", file_menu) quit_action.setMenuRole(QAction.NoRole) quit_action.triggered.connect(self.close) file_menu.addAction(quit_action) menu_bar.addMenu(file_menu) # Status components are needed by other GUI elements self.status_log = SnapshotStatusLog(self) self.common_settings["sts_log"] = self.status_log self.status_bar = SnapshotStatus(self.common_settings, self) self.common_settings["sts_info"] = self.status_bar # Create status log show/hide control and add it to status bar self.show_log_control = QCheckBox("Show status log") self.show_log_control.setStyleSheet("background-color: transparent") self.show_log_control.stateChanged.connect(self.status_log.setVisible) self.status_log.setVisible(False) self.status_bar.addPermanentWidget(self.show_log_control) # Creating main layout # Compare widget. Must be updated in case of file selection self.compare_widget = SnapshotCompareWidget(self.snapshot, self.common_settings, self) self.compare_widget.pvs_filtered.connect(self.handle_pvs_filtered) self.compare_widget.restore_requested.connect( self._handle_restore_request) self.save_widget = SnapshotSaveWidget(self.snapshot, self.common_settings, self) self.restore_widget = SnapshotRestoreWidget(self.snapshot, self.common_settings, self) self.restore_widget.files_updated.connect(self.handle_files_updated) self.restore_widget.files_selected.connect(self.handle_selected_files) self.save_widget.saved.connect(self.restore_widget.rebuild_file_list) self.autorefresh = QCheckBox("Periodic PV update") self.autorefresh.setChecked(True) self.autorefresh.toggled.connect(self.toggle_autorefresh) left_layout = QVBoxLayout() left_layout.addWidget(self.save_widget) left_layout.addStretch() left_layout.addWidget(make_separator(self, 'horizontal')) left_layout.addWidget(self.autorefresh) left_widget = QWidget() left_widget.setLayout(left_layout) sr_splitter = QSplitter(self) sr_splitter.addWidget(left_widget) sr_splitter.addWidget(self.restore_widget) sr_splitter.setStretchFactor(0, 1) sr_splitter.setStretchFactor(1, 2) main_splitter = QSplitter(self) main_splitter.addWidget(sr_splitter) main_splitter.addWidget(self.compare_widget) main_splitter.addWidget(self.status_log) main_splitter.setOrientation(Qt.Vertical) main_splitter.setStretchFactor(0, 1) main_splitter.setStretchFactor(1, 3) # Set default widget and add status bar self.setCentralWidget(main_splitter) self.setStatusBar(self.status_bar) # Show GUI and manage window properties self.show() self.setWindowTitle( os.path.basename(self.common_settings["req_file_path"]) + ' - Snapshot') # Status log default height should be 100px Set with splitter methods widgets_sizes = main_splitter.sizes() widgets_sizes[main_splitter.indexOf(main_splitter)] = 100 main_splitter.setSizes(widgets_sizes) # Schedule opening the request file for after the GUI is shown. QTimer.singleShot( 100, lambda: self.change_req_file( self.common_settings['req_file_path'], self.common_settings['req_file_macros'], ))
class SubTabWidget(QWidget): _tabChanged = pyqtSignal(int, name = "tabChanged") def __init__(self, subtitleData, videoWidget, parent = None): super(SubTabWidget, self).__init__(parent) self._subtitleData = subtitleData self.__initTabWidget(videoWidget) def __initTabWidget(self, videoWidget): settings = SubSettings() mainLayout = QVBoxLayout(self) mainLayout.setContentsMargins(0, 0, 0, 0) mainLayout.setSpacing(0) #TabBar self.tabBar = QTabBar(self) # Splitter (bookmarks + pages) self.splitter = QSplitter(self) self.splitter.setObjectName("sidebar_splitter") self._toolbox = ToolBox(self._subtitleData, self) self._toolbox.setObjectName("sidebar") self._toolbox.setMinimumWidth(100) self._toolbox.addTool(Details(self._subtitleData, self)) self._toolbox.addTool(Synchronizer(videoWidget, self._subtitleData, self)) self._toolbox.addTool(History(self)) self.rightWidget = QWidget() rightLayout = QGridLayout() rightLayout.setContentsMargins(0, 0, 0, 0) self.rightWidget.setLayout(rightLayout) self._mainTab = FileList(_("Subtitles"), self._subtitleData, self) self.pages = QStackedWidget(self) rightLayout.addWidget(self.pages, 0, 0) self.tabBar.addTab(self._mainTab.name) self.pages.addWidget(self._mainTab) self.splitter.addWidget(self._toolbox) self.splitter.addWidget(self.rightWidget) self.__drawSplitterHandle(1) # Setting widgets mainLayout.addWidget(self.tabBar) mainLayout.addWidget(self.splitter) # Widgets settings self.tabBar.setMovable(True) self.tabBar.setTabsClosable(True) self.tabBar.setExpanding(False) # Don't resize left panel if it's not needed leftWidgetIndex = self.splitter.indexOf(self._toolbox) rightWidgetIndex = self.splitter.indexOf(self.rightWidget) self.splitter.setStretchFactor(leftWidgetIndex, 0) self.splitter.setStretchFactor(rightWidgetIndex, 1) self.splitter.setCollapsible(leftWidgetIndex, False) self.splitter.setSizes([250]) # Some signals self.tabBar.currentChanged.connect(self.showTab) self.tabBar.tabCloseRequested.connect(self.closeTab) self.tabBar.tabMoved.connect(self.moveTab) self._mainTab.requestOpen.connect(self.openTab) self._mainTab.requestRemove.connect(self.removeFile) self.tabChanged.connect(lambda i: self._toolbox.setContentFor(self.tab(i))) self.setLayout(mainLayout) def __addTab(self, filePath): """Returns existing tab index. Creates a new one if it isn't opened and returns its index otherwise.""" for i in range(self.tabBar.count()): widget = self.pages.widget(i) if not widget.isStatic and filePath == widget.filePath: return i tab = SubtitleEditor(filePath, self._subtitleData, self) newIndex = self.tabBar.addTab(self._createTabName(tab.name, tab.history.isClean())) tab.history.cleanChanged.connect( lambda clean: self._cleanStateForFileChanged(filePath, clean)) self.pages.addWidget(tab) return newIndex def __drawSplitterHandle(self, index): splitterHandle = self.splitter.handle(index) splitterLayout = QVBoxLayout(splitterHandle) splitterLayout.setSpacing(0) splitterLayout.setContentsMargins(0, 0, 0, 0) line = QFrame(splitterHandle) line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) splitterLayout.addWidget(line) splitterHandle.setLayout(splitterLayout) def _createTabName(self, name, cleanState): if cleanState is True: return name else: return "%s +" % name def _cleanStateForFileChanged(self, filePath, cleanState): page = self.tabByPath(filePath) if page is not None: for i in range(self.tabBar.count()): if self.tabBar.tabText(i)[:len(page.name)] == page.name: self.tabBar.setTabText(i, self._createTabName(page.name, cleanState)) return def saveWidgetState(self, settings): settings.setState(self.splitter, self.splitter.saveState()) settings.setHidden(self._toolbox, self._toolbox.isHidden()) def restoreWidgetState(self, settings): self.showPanel(not settings.getHidden(self._toolbox)) splitterState = settings.getState(self.splitter) if not splitterState.isEmpty(): self.splitter.restoreState(settings.getState(self.splitter)) @pyqtSlot(str, bool) def openTab(self, filePath, background=False): if self._subtitleData.fileExists(filePath): tabIndex = self.__addTab(filePath) if background is False: self.showTab(tabIndex) else: log.error(_("SubtitleEditor not created for %s!" % filePath)) @pyqtSlot(str) def removeFile(self, filePath): tab = self.tabByPath(filePath) command = RemoveFile(filePath) if tab is not None: index = self.pages.indexOf(tab) if self.closeTab(index): self._subtitleData.execute(command) else: self._subtitleData.execute(command) @pyqtSlot(int) def closeTab(self, index): tab = self.tab(index) if tab.canClose(): widgetToRemove = self.pages.widget(index) self.tabBar.removeTab(index) self.pages.removeWidget(widgetToRemove) widgetToRemove.deleteLater() return True return False def count(self): return self.tabBar.count() def currentIndex(self): return self.tabBar.currentIndex() def currentPage(self): return self.pages.currentWidget() @pyqtSlot(int, int) def moveTab(self, fromIndex, toIndex): fromWidget = self.pages.widget(fromIndex) toWidget = self.pages.widget(toIndex) if fromWidget.isStatic or toWidget.isStatic: self.tabBar.blockSignals(True) # signals would cause infinite recursion self.tabBar.moveTab(toIndex, fromIndex) self.tabBar.blockSignals(False) return else: self.pages.removeWidget(fromWidget) self.pages.removeWidget(toWidget) if fromIndex < toIndex: self.pages.insertWidget(fromIndex, toWidget) self.pages.insertWidget(toIndex, fromWidget) else: self.pages.insertWidget(toIndex, fromWidget) self.pages.insertWidget(fromIndex, toWidget) # Hack # Qt changes tabs during mouse drag and dropping. The next line is added # to prevent it. self.showTab(self.tabBar.currentIndex()) @pyqtSlot(int) def showTab(self, index): showWidget = self.pages.widget(index) if showWidget: self.pages.setCurrentWidget(showWidget) self.tabBar.blockSignals(True) self.tabBar.setCurrentIndex(index) self.tabBar.blockSignals(False) # Try to update current tab. showWidget.updateTab() self._tabChanged.emit(index) def showPanel(self, val): if val is True: self._toolbox.show() else: self._toolbox.hide() def togglePanel(self): if self._toolbox.isHidden(): self._toolbox.show() else: self._toolbox.hide() def tab(self, index): return self.pages.widget(index) def tabByPath(self, path): for i in range(self.pages.count()): page = self.tab(i) if not page.isStatic and page.filePath == path: return page return None @property def fileList(self): return self._mainTab
class GuiBuildNovel(QDialog): FMT_ODT = 1 FMT_PDF = 2 FMT_HTM = 3 FMT_MD = 4 FMT_NWD = 5 FMT_TXT = 6 FMT_JSON_H = 7 FMT_JSON_M = 8 def __init__(self, theParent, theProject): QDialog.__init__(self, theParent) logger.debug("Initialising GuiBuildNovel ...") self.setObjectName("GuiBuildNovel") self.mainConf = nw.CONFIG self.theProject = theProject self.theParent = theParent self.theTheme = theParent.theTheme self.optState = self.theProject.optState self.htmlText = [] # List of html documents self.htmlStyle = [] # List of html styles self.nwdText = [] # List of markdown documents self.buildTime = 0 # The timestamp of the last build self.setWindowTitle("Build Novel Project") self.setMinimumWidth(self.mainConf.pxInt(700)) self.setMinimumHeight(self.mainConf.pxInt(600)) self.resize( self.mainConf.pxInt( self.optState.getInt("GuiBuildNovel", "winWidth", 900)), self.mainConf.pxInt( self.optState.getInt("GuiBuildNovel", "winHeight", 800))) self.docView = GuiBuildNovelDocView(self, self.theProject) # Title Formats # ============= self.titleGroup = QGroupBox("Title Formats for Novel Files", self) self.titleForm = QGridLayout(self) self.titleGroup.setLayout(self.titleForm) fmtHelp = (r"<b>Formatting Codes:</b><br>" r"%title% for the title as set in the document<br>" r"%ch% for chapter number (1, 2, 3)<br>" r"%chw% for chapter number as a word (one, two)<br>" r"%chI% for chapter number in upper case Roman<br>" r"%chi% for chapter number in lower case Roman<br>" r"%sc% for scene number within chapter<br>" r"%sca% for scene number within novel") fmtScHelp = ( r"<br><br>" r"Leave blank to skip this heading, or set to a static text, like " r"for instance '* * *', to make a separator. The separator will " r"be centred automatically and only appear between sections of " r"the same type.") xFmt = self.mainConf.pxInt(100) self.fmtTitle = QLineEdit() self.fmtTitle.setMaxLength(200) self.fmtTitle.setMinimumWidth(xFmt) self.fmtTitle.setToolTip(fmtHelp) self.fmtTitle.setText( self._reFmtCodes(self.theProject.titleFormat["title"])) self.fmtChapter = QLineEdit() self.fmtChapter.setMaxLength(200) self.fmtChapter.setMinimumWidth(xFmt) self.fmtChapter.setToolTip(fmtHelp) self.fmtChapter.setText( self._reFmtCodes(self.theProject.titleFormat["chapter"])) self.fmtUnnumbered = QLineEdit() self.fmtUnnumbered.setMaxLength(200) self.fmtUnnumbered.setMinimumWidth(xFmt) self.fmtUnnumbered.setToolTip(fmtHelp) self.fmtUnnumbered.setText( self._reFmtCodes(self.theProject.titleFormat["unnumbered"])) self.fmtScene = QLineEdit() self.fmtScene.setMaxLength(200) self.fmtScene.setMinimumWidth(xFmt) self.fmtScene.setToolTip(fmtHelp + fmtScHelp) self.fmtScene.setText( self._reFmtCodes(self.theProject.titleFormat["scene"])) self.fmtSection = QLineEdit() self.fmtSection.setMaxLength(200) self.fmtSection.setMinimumWidth(xFmt) self.fmtSection.setToolTip(fmtHelp + fmtScHelp) self.fmtSection.setText( self._reFmtCodes(self.theProject.titleFormat["section"])) # Dummy boxes due to QGridView and QLineEdit expand bug self.boxTitle = QHBoxLayout() self.boxTitle.addWidget(self.fmtTitle) self.boxChapter = QHBoxLayout() self.boxChapter.addWidget(self.fmtChapter) self.boxUnnumbered = QHBoxLayout() self.boxUnnumbered.addWidget(self.fmtUnnumbered) self.boxScene = QHBoxLayout() self.boxScene.addWidget(self.fmtScene) self.boxSection = QHBoxLayout() self.boxSection.addWidget(self.fmtSection) self.titleForm.addWidget(QLabel("Title"), 0, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxTitle, 0, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(QLabel("Chapter"), 1, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxChapter, 1, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(QLabel("Unnumbered"), 2, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxUnnumbered, 2, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(QLabel("Scene"), 3, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxScene, 3, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(QLabel("Section"), 4, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxSection, 4, 1, 1, 1, Qt.AlignRight) self.titleForm.setColumnStretch(0, 0) self.titleForm.setColumnStretch(1, 1) # Text Options # ============= self.formatGroup = QGroupBox("Formatting Options", self) self.formatForm = QGridLayout(self) self.formatGroup.setLayout(self.formatForm) ## Font Family self.textFont = QLineEdit() self.textFont.setReadOnly(True) self.textFont.setMinimumWidth(xFmt) self.textFont.setText( self.optState.getString("GuiBuildNovel", "textFont", self.mainConf.textFont)) self.fontButton = QPushButton("...") self.fontButton.setMaximumWidth( int(2.5 * self.theTheme.getTextWidth("..."))) self.fontButton.clicked.connect(self._selectFont) self.textSize = QSpinBox(self) self.textSize.setFixedWidth(5 * self.theTheme.textNWidth) self.textSize.setMinimum(6) self.textSize.setMaximum(72) self.textSize.setSingleStep(1) self.textSize.setToolTip( "The size is used for PDF and printing. Other formats have no size set." ) self.textSize.setValue( self.optState.getInt("GuiBuildNovel", "textSize", self.mainConf.textSize)) self.justifyText = QSwitch() self.justifyText.setToolTip( "Applies to PDF, printing, HTML, and Open Document exports.") self.justifyText.setChecked( self.optState.getBool("GuiBuildNovel", "justifyText", False)) self.noStyling = QSwitch() self.noStyling.setToolTip("Disable all styling of the text.") self.noStyling.setChecked( self.optState.getBool("GuiBuildNovel", "noStyling", False)) # Dummy box due to QGridView and QLineEdit expand bug self.boxFont = QHBoxLayout() self.boxFont.addWidget(self.textFont) self.formatForm.addWidget(QLabel("Font family"), 0, 0, 1, 1, Qt.AlignLeft) self.formatForm.addLayout(self.boxFont, 0, 1, 1, 1, Qt.AlignRight) self.formatForm.addWidget(self.fontButton, 0, 2, 1, 1, Qt.AlignRight) self.formatForm.addWidget(QLabel("Font size"), 1, 0, 1, 1, Qt.AlignLeft) self.formatForm.addWidget(self.textSize, 1, 1, 1, 2, Qt.AlignRight) self.formatForm.addWidget(QLabel("Justify text"), 2, 0, 1, 1, Qt.AlignLeft) self.formatForm.addWidget(self.justifyText, 2, 1, 1, 2, Qt.AlignRight) self.formatForm.addWidget(QLabel("Disable styling"), 3, 0, 1, 1, Qt.AlignLeft) self.formatForm.addWidget(self.noStyling, 3, 1, 1, 2, Qt.AlignRight) self.formatForm.setColumnStretch(0, 0) self.formatForm.setColumnStretch(1, 1) self.formatForm.setColumnStretch(2, 0) # Include Switches # ================ self.textGroup = QGroupBox("Text Options", self) self.textForm = QGridLayout(self) self.textGroup.setLayout(self.textForm) self.includeSynopsis = QSwitch() self.includeSynopsis.setToolTip( "Include synopsis comments in the output.") self.includeSynopsis.setChecked( self.optState.getBool("GuiBuildNovel", "incSynopsis", False)) self.includeComments = QSwitch() self.includeComments.setToolTip( "Include plain comments in the output.") self.includeComments.setChecked( self.optState.getBool("GuiBuildNovel", "incComments", False)) self.includeKeywords = QSwitch() self.includeKeywords.setToolTip( "Include meta keywords (tags, references) in the output.") self.includeKeywords.setChecked( self.optState.getBool("GuiBuildNovel", "incKeywords", False)) self.includeBody = QSwitch() self.includeBody.setToolTip("Include body text in the output.") self.includeBody.setChecked( self.optState.getBool("GuiBuildNovel", "incBodyText", True)) self.textForm.addWidget(QLabel("Include synopsis"), 0, 0, 1, 1, Qt.AlignLeft) self.textForm.addWidget(self.includeSynopsis, 0, 1, 1, 1, Qt.AlignRight) self.textForm.addWidget(QLabel("Include comments"), 1, 0, 1, 1, Qt.AlignLeft) self.textForm.addWidget(self.includeComments, 1, 1, 1, 1, Qt.AlignRight) self.textForm.addWidget(QLabel("Include keywords"), 2, 0, 1, 1, Qt.AlignLeft) self.textForm.addWidget(self.includeKeywords, 2, 1, 1, 1, Qt.AlignRight) self.textForm.addWidget(QLabel("Include body text"), 3, 0, 1, 1, Qt.AlignLeft) self.textForm.addWidget(self.includeBody, 3, 1, 1, 1, Qt.AlignRight) self.textForm.setColumnStretch(0, 1) self.textForm.setColumnStretch(1, 0) # File Filter Options # =================== self.fileGroup = QGroupBox("File Filter Options", self) self.fileForm = QGridLayout(self) self.fileGroup.setLayout(self.fileForm) self.novelFiles = QSwitch() self.novelFiles.setToolTip( "Include files with layouts 'Book', 'Page', 'Partition', " "'Chapter', 'Unnumbered', and 'Scene'.") self.novelFiles.setChecked( self.optState.getBool("GuiBuildNovel", "addNovel", True)) self.noteFiles = QSwitch() self.noteFiles.setToolTip("Include files with layout 'Note'.") self.noteFiles.setChecked( self.optState.getBool("GuiBuildNovel", "addNotes", False)) self.ignoreFlag = QSwitch() self.ignoreFlag.setToolTip( "Ignore the 'Include when building project' setting and include " "all files in the output.") self.ignoreFlag.setChecked( self.optState.getBool("GuiBuildNovel", "ignoreFlag", False)) self.fileForm.addWidget(QLabel("Include novel files"), 0, 0, 1, 1, Qt.AlignLeft) self.fileForm.addWidget(self.novelFiles, 0, 1, 1, 1, Qt.AlignRight) self.fileForm.addWidget(QLabel("Include note files"), 1, 0, 1, 1, Qt.AlignLeft) self.fileForm.addWidget(self.noteFiles, 1, 1, 1, 1, Qt.AlignRight) self.fileForm.addWidget(QLabel("Ignore export flag"), 2, 0, 1, 1, Qt.AlignLeft) self.fileForm.addWidget(self.ignoreFlag, 2, 1, 1, 1, Qt.AlignRight) self.fileForm.setColumnStretch(0, 1) self.fileForm.setColumnStretch(1, 0) # Export Options # ============== self.exportGroup = QGroupBox("Export Options", self) self.exportForm = QGridLayout(self) self.exportGroup.setLayout(self.exportForm) self.replaceTabs = QSwitch() self.replaceTabs.setToolTip("Replace all tabs with eight spaces.") self.replaceTabs.setChecked( self.optState.getBool("GuiBuildNovel", "replaceTabs", False)) self.exportForm.addWidget(QLabel("Replace tabs with spaces"), 0, 0, 1, 1, Qt.AlignLeft) self.exportForm.addWidget(self.replaceTabs, 0, 1, 1, 1, Qt.AlignRight) self.exportForm.setColumnStretch(0, 1) self.exportForm.setColumnStretch(1, 0) # Build Button # ============ self.buildProgress = QProgressBar() self.buildProgress = QProgressBar() self.buildNovel = QPushButton("Build Project") self.buildNovel.clicked.connect(self._buildPreview) # Action Buttons # ============== self.buttonBox = QHBoxLayout() self.btnPrint = QPushButton("Print") self.btnPrint.clicked.connect(self._printDocument) self.btnSave = QPushButton("Save As") self.saveMenu = QMenu(self) self.btnSave.setMenu(self.saveMenu) self.saveODT = QAction("Open Document (.odt)", self) self.saveODT.triggered.connect( lambda: self._saveDocument(self.FMT_ODT)) self.saveMenu.addAction(self.saveODT) self.savePDF = QAction("Portable Document Format (.pdf)", self) self.savePDF.triggered.connect( lambda: self._saveDocument(self.FMT_PDF)) self.saveMenu.addAction(self.savePDF) self.saveHTM = QAction("novelWriter HTML (.htm)", self) self.saveHTM.triggered.connect( lambda: self._saveDocument(self.FMT_HTM)) self.saveMenu.addAction(self.saveHTM) self.saveNWD = QAction("novelWriter Markdown (.nwd)", self) self.saveNWD.triggered.connect( lambda: self._saveDocument(self.FMT_NWD)) self.saveMenu.addAction(self.saveNWD) if self.mainConf.verQtValue >= 51400: self.saveMD = QAction("Markdown (.md)", self) self.saveMD.triggered.connect( lambda: self._saveDocument(self.FMT_MD)) self.saveMenu.addAction(self.saveMD) self.saveTXT = QAction("Plain Text (.txt)", self) self.saveTXT.triggered.connect( lambda: self._saveDocument(self.FMT_TXT)) self.saveMenu.addAction(self.saveTXT) self.saveJsonH = QAction("JSON + novelWriter HTML (.json)", self) self.saveJsonH.triggered.connect( lambda: self._saveDocument(self.FMT_JSON_H)) self.saveMenu.addAction(self.saveJsonH) self.saveJsonM = QAction("JSON + novelWriters Markdown (.json)", self) self.saveJsonM.triggered.connect( lambda: self._saveDocument(self.FMT_JSON_M)) self.saveMenu.addAction(self.saveJsonM) self.btnClose = QPushButton("Close") self.btnClose.clicked.connect(self._doClose) self.buttonBox.addWidget(self.btnSave) self.buttonBox.addWidget(self.btnPrint) self.buttonBox.addWidget(self.btnClose) self.buttonBox.setSpacing(self.mainConf.pxInt(4)) # Assemble GUI # ============ # Splitter Position boxWidth = self.mainConf.pxInt(350) boxWidth = self.optState.getInt("GuiBuildNovel", "boxWidth", boxWidth) docWidth = max(self.width() - boxWidth, 100) docWidth = self.optState.getInt("GuiBuildNovel", "docWidth", docWidth) # The Tool Box self.toolsBox = QVBoxLayout() self.toolsBox.addWidget(self.titleGroup) self.toolsBox.addWidget(self.formatGroup) self.toolsBox.addWidget(self.textGroup) self.toolsBox.addWidget(self.fileGroup) self.toolsBox.addWidget(self.exportGroup) self.toolsBox.addStretch(1) # Tool Box Wrapper Widget self.toolsWidget = QWidget() self.toolsWidget.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Minimum) self.toolsWidget.setLayout(self.toolsBox) # Tool Box Scroll Area self.toolsArea = QScrollArea() self.toolsArea.setMinimumWidth(self.mainConf.pxInt(250)) self.toolsArea.setWidgetResizable(True) self.toolsArea.setWidget(self.toolsWidget) if self.mainConf.hideVScroll: self.toolsArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) else: self.toolsArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) if self.mainConf.hideHScroll: self.toolsArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) else: self.toolsArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) # Tools and Buttons Layout tSp = self.mainConf.pxInt(8) self.innerBox = QVBoxLayout() self.innerBox.addWidget(self.toolsArea) self.innerBox.addSpacing(tSp) self.innerBox.addWidget(self.buildProgress) self.innerBox.addWidget(self.buildNovel) self.innerBox.addSpacing(tSp) self.innerBox.addLayout(self.buttonBox) # Tools and Buttons Wrapper Widget self.innerWidget = QWidget() self.innerWidget.setLayout(self.innerBox) # Main Dialog Splitter self.mainSplit = QSplitter(Qt.Horizontal) self.mainSplit.addWidget(self.innerWidget) self.mainSplit.addWidget(self.docView) self.mainSplit.setSizes([boxWidth, docWidth]) self.idxSettings = self.mainSplit.indexOf(self.innerWidget) self.idxDocument = self.mainSplit.indexOf(self.docView) self.mainSplit.setCollapsible(self.idxSettings, False) self.mainSplit.setCollapsible(self.idxDocument, False) # Outer Layout self.outerBox = QHBoxLayout() self.outerBox.addWidget(self.mainSplit) self.setLayout(self.outerBox) self.buildNovel.setFocus() logger.debug("GuiBuildNovel initialisation complete") return def viewCachedDoc(self): """Load the previously generated document from cache. """ if self._loadCache(): textFont = self.textFont.text() textSize = self.textSize.value() justifyText = self.justifyText.isChecked() self.docView.setTextFont(textFont, textSize) self.docView.setJustify(justifyText) if self.noStyling.isChecked(): self.docView.clearStyleSheet() else: self.docView.setStyleSheet(self.htmlStyle) htmlSize = sum([len(x) for x in self.htmlText]) if htmlSize < nwConst.MAX_BUILDSIZE: qApp.processEvents() self.docView.setContent(self.htmlText, self.buildTime) else: self.docView.setText( "Failed to generate preview. The result is too big.") self._enableQtSave(False) else: self.htmlText = [] self.htmlStyle = [] self.nwdText = [] self.buildTime = 0 return False return True ## # Slots ## def _buildPreview(self): """Build a preview of the project in the document viewer. """ # Get Settings fmtTitle = self.fmtTitle.text().strip() fmtChapter = self.fmtChapter.text().strip() fmtUnnumbered = self.fmtUnnumbered.text().strip() fmtScene = self.fmtScene.text().strip() fmtSection = self.fmtSection.text().strip() justifyText = self.justifyText.isChecked() noStyling = self.noStyling.isChecked() textFont = self.textFont.text() textSize = self.textSize.value() incSynopsis = self.includeSynopsis.isChecked() incComments = self.includeComments.isChecked() incKeywords = self.includeKeywords.isChecked() novelFiles = self.novelFiles.isChecked() noteFiles = self.noteFiles.isChecked() ignoreFlag = self.ignoreFlag.isChecked() includeBody = self.includeBody.isChecked() replaceTabs = self.replaceTabs.isChecked() makeHtml = ToHtml(self.theProject, self.theParent) makeHtml.setTitleFormat(fmtTitle) makeHtml.setChapterFormat(fmtChapter) makeHtml.setUnNumberedFormat(fmtUnnumbered) makeHtml.setSceneFormat(fmtScene, fmtScene == "") makeHtml.setSectionFormat(fmtSection, fmtSection == "") makeHtml.setBodyText(includeBody) makeHtml.setSynopsis(incSynopsis) makeHtml.setComments(incComments) makeHtml.setKeywords(incKeywords) makeHtml.setJustify(justifyText) makeHtml.setStyles(not noStyling) # Make sure the project and document is up to date self.theParent.treeView.flushTreeOrder() self.theParent.saveDocument() self.buildProgress.setMaximum(len(self.theProject.projTree)) self.buildProgress.setValue(0) tStart = int(time()) self.htmlText = [] self.htmlStyle = [] self.nwdText = [] htmlSize = 0 for nItt, tItem in enumerate(self.theProject.projTree): noteRoot = noteFiles noteRoot &= tItem.itemType == nwItemType.ROOT noteRoot &= tItem.itemClass != nwItemClass.NOVEL noteRoot &= tItem.itemClass != nwItemClass.ARCHIVE try: if noteRoot: # Add headers for root folders of notes makeHtml.addRootHeading(tItem.itemHandle) makeHtml.doConvert() self.htmlText.append(makeHtml.getResult()) self.nwdText.append(makeHtml.getFilteredMarkdown()) htmlSize += makeHtml.getResultSize() elif self._checkInclude(tItem, noteFiles, novelFiles, ignoreFlag): makeHtml.setText(tItem.itemHandle) makeHtml.doAutoReplace() makeHtml.tokenizeText() makeHtml.doHeaders() makeHtml.doConvert() makeHtml.doPostProcessing() self.htmlText.append(makeHtml.getResult()) self.nwdText.append(makeHtml.getFilteredMarkdown()) htmlSize += makeHtml.getResultSize() except Exception as e: logger.error("Failed to generate html of document '%s'" % tItem.itemHandle) logger.error(str(e)) self.docView.setText( ("Failed to generate preview. " "Document with title '%s' could not be parsed.") % tItem.itemName) return False # Update progress bar, also for skipped items self.buildProgress.setValue(nItt + 1) if makeHtml.errData: self.theParent.makeAlert( ("There were problems when building the project:" "<br>- %s") % "<br>- ".join(makeHtml.errData), nwAlert.ERROR) if replaceTabs: htmlText = [] eightSpace = " " * 8 for aLine in self.htmlText: htmlText.append(aLine.replace("\t", eightSpace)) self.htmlText = htmlText nwdText = [] for aLine in self.nwdText: nwdText.append(aLine.replace("\t", " ")) self.nwdText = nwdText tEnd = int(time()) logger.debug("Built project in %.3f ms" % (1000 * (tEnd - tStart))) self.htmlStyle = makeHtml.getStyleSheet() self.buildTime = tEnd # Load the preview document with the html data self.docView.setTextFont(textFont, textSize) self.docView.setJustify(justifyText) if noStyling: self.docView.clearStyleSheet() else: self.docView.setStyleSheet(self.htmlStyle) if htmlSize < nwConst.MAX_BUILDSIZE: self.docView.setContent(self.htmlText, self.buildTime) self._enableQtSave(True) else: self.docView.setText( "Failed to generate preview. The result is too big.") self._enableQtSave(False) self._saveCache() return def _checkInclude(self, theItem, noteFiles, novelFiles, ignoreFlag): """This function checks whether a file should be included in the export or not. For standard note and novel files, this is controlled by the options selected by the user. For other files classified as non-exportable, a few checks must be made, and the following are not: * Items that are not actual files. * Items that have been orphaned which are tagged as NO_LAYOUT and NO_CLASS. * Items that appear in the TRASH folder or have parent set to None (orphaned files). """ if theItem is None: return False if not theItem.isExported and not ignoreFlag: return False isNone = theItem.itemType != nwItemType.FILE isNone |= theItem.itemLayout == nwItemLayout.NO_LAYOUT isNone |= theItem.itemClass == nwItemClass.NO_CLASS isNone |= theItem.itemClass == nwItemClass.TRASH isNone |= theItem.itemParent == self.theProject.projTree.trashRoot() isNone |= theItem.itemParent is None isNote = theItem.itemLayout == nwItemLayout.NOTE isNovel = not isNone and not isNote if isNone: return False if isNote and not noteFiles: return False if isNovel and not novelFiles: return False rootItem = self.theProject.projTree.getRootItem(theItem.itemHandle) if rootItem.itemClass == nwItemClass.ARCHIVE: return False return True def _saveDocument(self, theFormat): """Save the document to various formats. """ byteFmt = QByteArray() fileExt = "" textFmt = "" outTool = "" # Create the settings if theFormat == self.FMT_ODT: byteFmt.append("odf") fileExt = "odt" textFmt = "Open Document" outTool = "Qt" elif theFormat == self.FMT_PDF: fileExt = "pdf" textFmt = "PDF" outTool = "QtPrint" elif theFormat == self.FMT_HTM: fileExt = "htm" textFmt = "Plain HTML" outTool = "NW" elif theFormat == self.FMT_MD: byteFmt.append("markdown") fileExt = "md" textFmt = "Markdown" outTool = "Qt" elif theFormat == self.FMT_NWD: fileExt = "nwd" textFmt = "%s Markdown" % nw.__package__ outTool = "NW" elif theFormat == self.FMT_TXT: byteFmt.append("plaintext") fileExt = "txt" textFmt = "Plain Text" outTool = "Qt" elif theFormat == self.FMT_JSON_H: fileExt = "json" textFmt = "JSON + %s HTML" % nw.__package__ outTool = "NW" elif theFormat == self.FMT_JSON_M: fileExt = "json" textFmt = "JSON + %s Markdown" % nw.__package__ outTool = "NW" else: return False # Generate the file name if fileExt: cleanName = makeFileNameSafe(self.theProject.projName) fileName = "%s.%s" % (cleanName, fileExt) saveDir = self.mainConf.lastPath savePath = os.path.join(saveDir, fileName) if not os.path.isdir(saveDir): saveDir = self.mainConf.homePath dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.DontUseNativeDialog savePath, _ = QFileDialog.getSaveFileName(self, "Save Document As", savePath, options=dlgOpt) if not savePath: return False self.mainConf.setLastPath(savePath) else: return False # Do the actual writing wSuccess = False errMsg = "" if outTool == "Qt": docWriter = QTextDocumentWriter() docWriter.setFileName(savePath) docWriter.setFormat(byteFmt) wSuccess = docWriter.write(self.docView.qDocument) elif outTool == "NW": try: with open(savePath, mode="w", encoding="utf8") as outFile: if theFormat == self.FMT_HTM: # Write novelWriter HTML data theStyle = self.htmlStyle.copy() theStyle.append( r"article {width: 800px; margin: 40px auto;}") bodyText = "".join(self.htmlText) bodyText = bodyText.replace("\t", "	") theHtml = ("<!DOCTYPE html>\n" "<html>\n" "<head>\n" "<meta charset='utf-8'>\n" "<title>{projTitle:s}</title>\n" "</head>\n" "<style>\n" "{htmlStyle:s}\n" "</style>\n" "<body>\n" "<article>\n" "{bodyText:s}\n" "</article>\n" "</body>\n" "</html>\n").format( projTitle=self.theProject.projName, htmlStyle="\n".join(theStyle), bodyText=bodyText, ) outFile.write(theHtml) elif theFormat == self.FMT_NWD: # Write novelWriter markdown data for aLine in self.nwdText: outFile.write(aLine) elif theFormat == self.FMT_JSON_H or theFormat == self.FMT_JSON_M: jsonData = { "meta": { "workingTitle": self.theProject.projName, "novelTitle": self.theProject.bookTitle, "authors": self.theProject.bookAuthors, "buildTime": self.buildTime, } } if theFormat == self.FMT_JSON_H: theBody = [] for htmlPage in self.htmlText: theBody.append( htmlPage.rstrip("\n").split("\n")) jsonData["text"] = { "css": self.htmlStyle, "html": theBody, } elif theFormat == self.FMT_JSON_M: theBody = [] for nwdPage in self.nwdText: theBody.append(nwdPage.split("\n")) jsonData["text"] = { "nwd": theBody, } outFile.write(json.dumps(jsonData, indent=2)) wSuccess = True except Exception as e: errMsg = str(e) elif outTool == "QtPrint" and theFormat == self.FMT_PDF: try: thePrinter = QPrinter() thePrinter.setOutputFormat(QPrinter.PdfFormat) thePrinter.setOrientation(QPrinter.Portrait) thePrinter.setDuplex(QPrinter.DuplexLongSide) thePrinter.setFontEmbeddingEnabled(True) thePrinter.setColorMode(QPrinter.Color) thePrinter.setOutputFileName(savePath) self.docView.qDocument.print(thePrinter) wSuccess = True except Exception as e: errMsg - str(e) else: errMsg = "Unknown format" # Report to user if wSuccess: self.theParent.makeAlert( "%s file successfully written to:<br> %s" % (textFmt, savePath), nwAlert.INFO) else: self.theParent.makeAlert( "Failed to write %s file. %s" % (textFmt, errMsg), nwAlert.ERROR) return wSuccess def _printDocument(self): """Open the print preview dialog. """ thePreview = QPrintPreviewDialog(self) thePreview.paintRequested.connect(self._doPrintPreview) thePreview.exec_() return def _doPrintPreview(self, thePrinter): """Connect the print preview painter to the document viewer. """ qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) thePrinter.setOrientation(QPrinter.Portrait) self.docView.qDocument.print(thePrinter) qApp.restoreOverrideCursor() return def _selectFont(self): """Open the QFontDialog and set a font for the font style. """ currFont = QFont() currFont.setFamily(self.textFont.text()) currFont.setPointSize(self.textSize.value()) theFont, theStatus = QFontDialog.getFont(currFont, self) if theStatus: self.textFont.setText(theFont.family()) self.textSize.setValue(theFont.pointSize()) self.raise_() # Move the dialog to front (fixes a bug on macOS) return def _loadCache(self): """Save the current data to cache. """ buildCache = os.path.join(self.theProject.projCache, nwFiles.BUILD_CACHE) dataCount = 0 if os.path.isfile(buildCache): logger.debug("Loading build cache") try: with open(buildCache, mode="r", encoding="utf8") as inFile: theJson = inFile.read() theData = json.loads(theJson) except Exception as e: logger.error("Failed to load build cache") logger.error(str(e)) return False if "htmlText" in theData.keys(): self.htmlText = theData["htmlText"] dataCount += 1 if "htmlStyle" in theData.keys(): self.htmlStyle = theData["htmlStyle"] dataCount += 1 if "nwdText" in theData.keys(): self.nwdText = theData["nwdText"] dataCount += 1 if "buildTime" in theData.keys(): self.buildTime = theData["buildTime"] return dataCount == 3 def _saveCache(self): """Save the current data to cache. """ buildCache = os.path.join(self.theProject.projCache, nwFiles.BUILD_CACHE) logger.debug("Saving build cache") try: with open(buildCache, mode="w+", encoding="utf8") as outFile: outFile.write( json.dumps( { "htmlText": self.htmlText, "htmlStyle": self.htmlStyle, "nwdText": self.nwdText, "buildTime": self.buildTime, }, indent=2)) except Exception as e: logger.error("Failed to save build cache") logger.error(str(e)) return False return True def _doClose(self): """Close button was clicked. """ self.close() return ## # Events ## def closeEvent(self, theEvent): """Capture the user closing the window so we can save settings. """ self._saveSettings() self.docView.clear() theEvent.accept() return ## # Internal Functions ## def _enableQtSave(self, theState): """Set the enabled status of Save menu entries that depend on the QTextDocument. """ self.saveODT.setEnabled(theState) self.savePDF.setEnabled(theState) self.saveTXT.setEnabled(theState) if self.mainConf.verQtValue >= 51400: self.saveMD.setEnabled(theState) return def _saveSettings(self): """Save the various user settings. """ logger.debug("Saving GuiBuildNovel settings") # Formatting self.theProject.setTitleFormat({ "title": self.fmtTitle.text().strip(), "chapter": self.fmtChapter.text().strip(), "unnumbered": self.fmtUnnumbered.text().strip(), "scene": self.fmtScene.text().strip(), "section": self.fmtSection.text().strip(), }) winWidth = self.mainConf.rpxInt(self.width()) winHeight = self.mainConf.rpxInt(self.height()) justifyText = self.justifyText.isChecked() noStyling = self.noStyling.isChecked() textFont = self.textFont.text() textSize = self.textSize.value() novelFiles = self.novelFiles.isChecked() noteFiles = self.noteFiles.isChecked() ignoreFlag = self.ignoreFlag.isChecked() incSynopsis = self.includeSynopsis.isChecked() incComments = self.includeComments.isChecked() incKeywords = self.includeKeywords.isChecked() incBodyText = self.includeBody.isChecked() replaceTabs = self.replaceTabs.isChecked() mainSplit = self.mainSplit.sizes() if len(mainSplit) == 2: boxWidth = self.mainConf.rpxInt(mainSplit[0]) docWidth = self.mainConf.rpxInt(mainSplit[1]) else: boxWidth = 100 docWidth = 100 # GUI Settings self.optState.setValue("GuiBuildNovel", "winWidth", winWidth) self.optState.setValue("GuiBuildNovel", "winHeight", winHeight) self.optState.setValue("GuiBuildNovel", "boxWidth", boxWidth) self.optState.setValue("GuiBuildNovel", "docWidth", docWidth) self.optState.setValue("GuiBuildNovel", "justifyText", justifyText) self.optState.setValue("GuiBuildNovel", "noStyling", noStyling) self.optState.setValue("GuiBuildNovel", "textFont", textFont) self.optState.setValue("GuiBuildNovel", "textSize", textSize) self.optState.setValue("GuiBuildNovel", "addNovel", novelFiles) self.optState.setValue("GuiBuildNovel", "addNotes", noteFiles) self.optState.setValue("GuiBuildNovel", "ignoreFlag", ignoreFlag) self.optState.setValue("GuiBuildNovel", "incSynopsis", incSynopsis) self.optState.setValue("GuiBuildNovel", "incComments", incComments) self.optState.setValue("GuiBuildNovel", "incKeywords", incKeywords) self.optState.setValue("GuiBuildNovel", "incBodyText", incBodyText) self.optState.setValue("GuiBuildNovel", "replaceTabs", replaceTabs) self.optState.saveSettings() return def _reFmtCodes(self, theFormat): """Translates old formatting codes to new ones. """ theFormat = theFormat.replace(r"%chnum%", r"%ch%") theFormat = theFormat.replace(r"%scnum%", r"%sc%") theFormat = theFormat.replace(r"%scabsnum%", r"%sca%") theFormat = theFormat.replace(r"%chnumword%", r"%chw%") return theFormat
class GuiMain(QMainWindow): def __init__(self): QMainWindow.__init__(self) logger.info("Starting %s" % nw.__package__) logger.debug("Initialising GUI ...") self.mainConf = nw.CONFIG self.theTheme = GuiTheme(self) self.theProject = NWProject(self) self.theIndex = NWIndex(self.theProject, self) self.hasProject = False self.isZenMode = False logger.info("OS: %s" % ( self.mainConf.osType) ) logger.info("Qt5 Version: %s (%d)" % ( self.mainConf.verQtString, self.mainConf.verQtValue) ) logger.info("PyQt5 Version: %s (%d)" % ( self.mainConf.verPyQtString, self.mainConf.verPyQtValue) ) logger.info("Python Version: %s (0x%x)" % ( self.mainConf.verPyString, self.mainConf.verPyHexVal) ) self.resize(*self.mainConf.winGeometry) self._setWindowTitle() self.setWindowIcon(QIcon(path.join(self.mainConf.appIcon))) # Main GUI Elements self.statusBar = GuiMainStatus(self) self.noticeBar = GuiNoticeBar(self) self.docEditor = GuiDocEditor(self, self.theProject) self.docViewer = GuiDocViewer(self, self.theProject) self.viewMeta = GuiDocViewDetails(self, self.theProject) self.searchBar = GuiSearchBar(self) self.treeMeta = GuiDocDetails(self, self.theProject) self.treeView = GuiDocTree(self, self.theProject) self.mainMenu = GuiMainMenu(self, self.theProject) # Minor Gui Elements self.statusIcons = [] self.importIcons = [] # Assemble Main Window self.treePane = QFrame() self.treeBox = QVBoxLayout() self.treeBox.setContentsMargins(0,0,0,0) self.treeBox.addWidget(self.treeView) self.treeBox.addWidget(self.treeMeta) self.treePane.setLayout(self.treeBox) self.editPane = QFrame() self.docEdit = QVBoxLayout() self.docEdit.setContentsMargins(0,0,0,0) self.docEdit.addWidget(self.searchBar) self.docEdit.addWidget(self.noticeBar) self.docEdit.addWidget(self.docEditor) self.editPane.setLayout(self.docEdit) self.viewPane = QFrame() self.docView = QVBoxLayout() self.docView.setContentsMargins(0,0,0,0) self.docView.addWidget(self.docViewer) self.docView.addWidget(self.viewMeta) self.docView.setStretch(0, 1) self.viewPane.setLayout(self.docView) self.splitView = QSplitter(Qt.Horizontal) self.splitView.setOpaqueResize(False) self.splitView.addWidget(self.editPane) self.splitView.addWidget(self.viewPane) self.splitMain = QSplitter(Qt.Horizontal) self.splitMain.setContentsMargins(4,4,4,4) self.splitMain.setOpaqueResize(False) self.splitMain.addWidget(self.treePane) self.splitMain.addWidget(self.splitView) self.splitMain.setSizes(self.mainConf.mainPanePos) self.setCentralWidget(self.splitMain) self.idxTree = self.splitMain.indexOf(self.treePane) self.idxMain = self.splitMain.indexOf(self.splitView) self.idxEditor = self.splitView.indexOf(self.editPane) self.idxViewer = self.splitView.indexOf(self.viewPane) self.splitMain.setCollapsible(self.idxTree, False) self.splitMain.setCollapsible(self.idxMain, False) self.splitView.setCollapsible(self.idxEditor, False) self.splitView.setCollapsible(self.idxViewer, True) self.viewPane.setVisible(False) self.searchBar.setVisible(False) # Build The Tree View self.treeView.itemSelectionChanged.connect(self._treeSingleClick) self.treeView.itemDoubleClicked.connect(self._treeDoubleClick) self.rebuildTree() # Set Main Window Elements self.setMenuBar(self.mainMenu) self.setStatusBar(self.statusBar) self.statusBar.setStatus("Ready") # Set Up Autosaving Project Timer self.asProjTimer = QTimer() self.asProjTimer.timeout.connect(self._autoSaveProject) # Set Up Autosaving Document Timer self.asDocTimer = QTimer() self.asDocTimer.timeout.connect(self._autoSaveDocument) # Shortcuts and Actions self._connectMenuActions() keyReturn = QShortcut(self.treeView) keyReturn.setKey(QKeySequence(Qt.Key_Return)) keyReturn.activated.connect(self._treeKeyPressReturn) keyEscape = QShortcut(self) keyEscape.setKey(QKeySequence(Qt.Key_Escape)) keyEscape.activated.connect(self._keyPressEscape) # Forward Functions self.setStatus = self.statusBar.setStatus self.setProjectStatus = self.statusBar.setProjectStatus if self.mainConf.showGUI: self.show() self.initMain() self.asProjTimer.start() self.asDocTimer.start() self.statusBar.clearStatus() self.showNormal() if self.mainConf.isFullScreen: self.toggleFullScreenMode() logger.debug("GUI initialisation complete") if self.mainConf.cmdOpen is not None: logger.debug("Opening project from additional command line option") self.openProject(self.mainConf.cmdOpen) return def clearGUI(self): """Wrapper function to clear all sub-elements of the main GUI. """ self.treeView.clearTree() self.docEditor.clearEditor() self.closeDocViewer() self.statusBar.clearStatus() return True def initMain(self): self.asProjTimer.setInterval(int(self.mainConf.autoSaveProj*1000)) self.asDocTimer.setInterval(int(self.mainConf.autoSaveDoc*1000)) return True ## # Project Actions ## def newProject(self, projPath=None, forceNew=False): if self.hasProject: msgBox = QMessageBox() msgRes = msgBox.warning( self, "New Project", "Please close the current project<br>before making a new one." ) return False if projPath is None: projPath = self.newProjectDialog() if projPath is None: return False if path.isfile(path.join(projPath,self.theProject.projFile)) and not forceNew: msgBox = QMessageBox() msgRes = msgBox.critical( self, "New Project", "A project already exists in that location.<br>Please choose another folder." ) return False logger.info("Creating new project") self.theProject.newProject() self.theProject.setProjectPath(projPath) self.rebuildTree() self.saveProject() self.hasProject = True self.statusBar.setRefTime(self.theProject.projOpened) return True def closeProject(self, isYes=False): """Closes the project if one is open. isYes is passed on from the close application event so the user doesn't get prompted twice. """ if not self.hasProject: # There is no project loaded, everything OK return True if self.mainConf.showGUI and not isYes: msgBox = QMessageBox() msgRes = msgBox.question( self, "Close Project", "Save changes and close current project?" ) if msgRes != QMessageBox.Yes: return False if self.docEditor.docChanged: self.saveDocument() if self.theProject.projAltered: saveOK = self.saveProject() doBackup = False if self.theProject.doBackup and self.mainConf.backupOnClose: doBackup = True if self.mainConf.showGUI and self.mainConf.askBeforeBackup: msgBox = QMessageBox() msgRes = msgBox.question( self, "Backup Project", "Backup current project?" ) if msgRes != QMessageBox.Yes: doBackup = False if doBackup: self.backupProject() else: saveOK = True if saveOK: self.closeDocument() self.theProject.closeProject() self.theIndex.clearIndex() self.clearGUI() self.hasProject = False return saveOK def openProject(self, projFile=None): """Open a project. The parameter projFile is passed from the open recent projects menu, so it can be set. If not, we pop the dialog. """ if projFile is None: projFile = self.openProjectDialog() if projFile is None: return False # Make sure any open project is cleared out first before we load # another one if not self.closeProject(): return False # Try to open the project if not self.theProject.openProject(projFile): return False # project is loaded self.hasProject = True # Load the tag index self.theIndex.loadIndex() # Update GUI self._setWindowTitle(self.theProject.projName) self.rebuildTree() self.docEditor.setDictionaries() self.docEditor.setSpellCheck(self.theProject.spellCheck) self.statusBar.setRefTime(self.theProject.projOpened) self.mainMenu.updateMenu() # Restore previously open documents, if any if self.theProject.lastEdited is not None: self.openDocument(self.theProject.lastEdited) if self.theProject.lastViewed is not None: self.viewDocument(self.theProject.lastViewed) # Check if we need to rebuild the index if self.theIndex.indexBroken: self.rebuildIndex() return True def saveProject(self): """Save the current project. """ if not self.hasProject: return False # If the project is new, it may not have a path, so we need one if self.theProject.projPath is None: projPath = self.saveProjectDialog() self.theProject.setProjectPath(projPath) if self.theProject.projPath is None: return False self.treeView.saveTreeOrder() self.theProject.saveProject() self.theIndex.saveIndex() self.mainMenu.updateRecentProjects() return True def backupProject(self): theBackup = NWBackup(self, self.theProject) theBackup.zipIt() return True ## # Document Actions ## def closeDocument(self): if self.hasProject: if self.docEditor.docChanged: self.saveDocument() self.docEditor.clearEditor() return True def openDocument(self, tHandle): if self.hasProject: self.closeDocument() if self.docEditor.loadText(tHandle): self.docEditor.setFocus() self.theProject.setLastEdited(tHandle) else: return False return True def saveDocument(self): if self.hasProject: self.docEditor.saveText() return True def viewDocument(self, tHandle=None): if tHandle is None: tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.debug("No document selected, trying editor document") tHandle = self.docEditor.theHandle if tHandle is None: logger.debug("No document selected, trying last viewed") tHandle = self.theProject.lastViewed if tHandle is None: logger.debug("No document selected, giving up") return False if self.docViewer.loadText(tHandle) and not self.viewPane.isVisible(): bPos = self.splitMain.sizes() self.viewPane.setVisible(True) vPos = [0,0] vPos[0] = int(bPos[1]/2) vPos[1] = bPos[1]-vPos[0] self.splitView.setSizes(vPos) return True def importDocument(self): lastPath = self.mainConf.lastPath extFilter = [ "Text files (*.txt)", "Markdown files (*.md)", "All files (*.*)", ] dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.DontUseNativeDialog inPath = QFileDialog.getOpenFileName( self,"Import File",lastPath,options=dlgOpt,filter=";;".join(extFilter) ) if inPath: loadFile = inPath[0] else: return False if loadFile.strip() == "": return False theText = None try: with open(loadFile,mode="rt",encoding="utf8") as inFile: theText = inFile.read() self.mainConf.setLastPath(loadFile) except Exception as e: self.makeAlert( ["Could not read file. The file must be an existing text file.",str(e)], nwAlert.ERROR ) return False if self.docEditor.theHandle is None: self.makeAlert( ["Please open a document to import the text file into."], nwAlert.ERROR ) return False if not self.docEditor.isEmpty(): if self.mainConf.showGUI: msgBox = QMessageBox() msgRes = msgBox.question(self, "Import Document",( "Importing the file will overwrite the current content of the document. " "Do you want to proceed?" )) if msgRes != QMessageBox.Yes: return False else: return False self.docEditor.replaceText(theText) return True def mergeDocuments(self): """Merge multiple documents to one single new document. """ if self.mainConf.showGUI: dlgMerge = GuiDocMerge(self, self.theProject) dlgMerge.exec_() return True def splitDocument(self): """Split a single document into multiple documents. """ if self.mainConf.showGUI: dlgSplit = GuiDocSplit(self, self.theProject) dlgSplit.exec_() return True def passDocumentAction(self, theAction): """Pass on document action theAction to whatever document has the focus. If no document has focus, the action is discarded. """ if self.docEditor.hasFocus(): self.docEditor.docAction(theAction) elif self.docViewer.hasFocus(): self.docViewer.docAction(theAction) else: logger.debug("Document action requested, but no document has focus") return True ## # Tree Item Actions ## def openSelectedItem(self): tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.warning("No item selected") return False logger.verbose("Opening item %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 not a file" % tHandle) return True def editItem(self): tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.warning("No item selected") return logger.verbose("Requesting change to item %s" % tHandle) if self.mainConf.showGUI: dlgProj = GuiItemEditor(self, self.theProject, tHandle) if dlgProj.exec_(): self.treeView.setTreeItemValues(tHandle) return def rebuildTree(self): self._makeStatusIcons() self._makeImportIcons() self.treeView.clearTree() self.treeView.buildTree() return def rebuildIndex(self): if not self.hasProject: return False logger.debug("Rebuilding indices ...") self.treeView.saveTreeOrder() self.theIndex.clearIndex() nItems = len(self.theProject.treeOrder) dlgProg = QProgressDialog("Scanning files ...", "Cancel", 0, nItems, self) dlgProg.setWindowModality(Qt.WindowModal) dlgProg.setMinimumDuration(0) dlgProg.setFixedWidth(480) dlgProg.setLabelText("Starting file scan ...") dlgProg.setValue(0) dlgProg.show() time.sleep(0.5) nDone = 0 for tHandle in self.theProject.treeOrder: tItem = self.theProject.getItem(tHandle) dlgProg.setValue(nDone) dlgProg.setLabelText("Scanning: %s" % tItem.itemName) logger.verbose("Scanning: %s" % tItem.itemName) if tItem is not None and tItem.itemType == nwItemType.FILE: theDoc = NWDoc(self.theProject, self) theText = theDoc.openDocument(tHandle, False) # Run Word Count cC, wC, pC = countWords(theText) tItem.setCharCount(cC) tItem.setWordCount(wC) tItem.setParaCount(pC) self.treeView.propagateCount(tHandle, wC) self.treeView.projectWordCount() # Build tag index self.theIndex.scanText(tHandle, theText) nDone += 1 if dlgProg.wasCanceled(): break dlgProg.setValue(nItems) return True ## # Main Dialogs ## def openProjectDialog(self): dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.DontUseNativeDialog projFile, _ = QFileDialog.getOpenFileName( self, "Open novelWriter Project", "", "novelWriter Project File (%s);;All Files (*)" % nwFiles.PROJ_FILE, options=dlgOpt ) if projFile: return projFile return None def saveProjectDialog(self): dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.ShowDirsOnly dlgOpt |= QFileDialog.DontUseNativeDialog projPath = QFileDialog.getExistingDirectory( self, "Save novelWriter Project", "", options=dlgOpt ) if projPath: return projPath return None def newProjectDialog(self): dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.ShowDirsOnly dlgOpt |= QFileDialog.DontUseNativeDialog projPath = QFileDialog.getExistingDirectory( self, "Select Location for New novelWriter Project", "", options=dlgOpt ) if projPath: return projPath return None def editConfigDialog(self): dlgConf = GuiConfigEditor(self, self.theProject) if dlgConf.exec_() == QDialog.Accepted: logger.debug("Applying new preferences") self.initMain() self.theTheme.updateTheme() self.saveDocument() self.docEditor.initEditor() self.docViewer.initViewer() return True def editProjectDialog(self): if self.hasProject: dlgProj = GuiProjectEditor(self, self.theProject) dlgProj.exec_() self._setWindowTitle(self.theProject.projName) return True def exportProjectDialog(self): if self.hasProject: dlgExport = GuiExport(self, self.theProject) dlgExport.exec_() return True def showTimeLineDialog(self): if self.hasProject: dlgTLine = GuiTimeLineView(self, self.theProject, self.theIndex) dlgTLine.exec_() return True def showSessionLogDialog(self): if self.hasProject: dlgTLine = GuiSessionLogView(self, self.theProject) dlgTLine.exec_() return True def makeAlert(self, theMessage, theLevel=nwAlert.INFO): """Alert both the user and the logger at the same time. Message can be either a string or an array of strings. Severity level is 0 = info, 1 = warning, and 2 = error. """ if isinstance(theMessage, list): popMsg = " ".join(theMessage) logMsg = theMessage else: popMsg = theMessage logMsg = [theMessage] msgBox = QMessageBox() if theLevel == nwAlert.INFO: for msgLine in logMsg: logger.info(msgLine) msgBox.information(self, "Information", popMsg) elif theLevel == nwAlert.WARN: for msgLine in logMsg: logger.warning(msgLine) msgBox.warning(self, "Warning", popMsg) elif theLevel == nwAlert.ERROR: for msgLine in logMsg: logger.error(msgLine) msgBox.critical(self, "Error", popMsg) elif theLevel == nwAlert.BUG: for msgLine in logMsg: logger.error(msgLine) popMsg += "<br>This is a bug!" msgBox.critical(self, "Internal Error", popMsg) return ## # Main Window Actions ## def closeMain(self): if self.mainConf.showGUI and self.hasProject: msgBox = QMessageBox() msgRes = msgBox.question( self, "Exit", "Do you want to save changes and exit?" ) if msgRes != QMessageBox.Yes: return False logger.info("Exiting %s" % nw.__package__) self.closeProject(True) self.mainConf.setTreeColWidths(self.treeView.getColumnSizes()) if not self.mainConf.isFullScreen: self.mainConf.setWinSize(self.width(), self.height()) if not self.isZenMode: self.mainConf.setMainPanePos(self.splitMain.sizes()) self.mainConf.setDocPanePos(self.splitView.sizes()) self.mainConf.saveConfig() qApp.quit() return True def setFocus(self, paneNo): if paneNo == 1: self.treeView.setFocus() elif paneNo == 2: self.docEditor.setFocus() elif paneNo == 3: self.docViewer.setFocus() return def closeDocEditor(self): self.closeDocument() self.theProject.setLastEdited(None) return def closeDocViewer(self): self.docViewer.clearViewer() self.theProject.setLastViewed(None) bPos = self.splitMain.sizes() self.viewPane.setVisible(False) vPos = [bPos[1],0] self.splitView.setSizes(vPos) return not self.viewPane.isVisible() def toggleZenMode(self): """Main GUI Zen Mode hides tree, view pane and optionally also statusbar and menu. """ if self.docEditor.theHandle is None: logger.error("No document open, so not activating Zen Mode") return False self.isZenMode = not self.isZenMode if self.isZenMode: logger.debug("Activating Zen mode") else: logger.debug("Deactivating Zen mode") isVisible = not self.isZenMode self.treePane.setVisible(isVisible) self.statusBar.setVisible(isVisible) self.mainMenu.setVisible(isVisible) if self.viewPane.isVisible(): self.viewPane.setVisible(False) elif self.docViewer.theHandle is not None: self.viewPane.setVisible(True) return True def toggleFullScreenMode(self): """Main GUI full screen mode. The mode is tracked by the flag in config. This only tracks whether the window has been maximised using the internal commands, and may not be correct if the user uses the system window manager. Currently, Qt doesn't have access to the exact state of the window. """ self.setWindowState(self.windowState() ^ Qt.WindowFullScreen) winState = self.windowState() & Qt.WindowFullScreen == Qt.WindowFullScreen if winState: logger.debug("Activated full screen mode") else: logger.debug("Deactivated full screen mode") self.mainConf.isFullScreen = winState return ## # Internal Functions ## def _connectMenuActions(self): """Connect to the main window all menu actions that need to be available also when the main menu is hidden. """ self.addAction(self.mainMenu.aSaveProject) self.addAction(self.mainMenu.aExitNW) self.addAction(self.mainMenu.aSaveDoc) self.addAction(self.mainMenu.aFileDetails) self.addAction(self.mainMenu.aZenMode) self.addAction(self.mainMenu.aFullScreen) self.addAction(self.mainMenu.aViewTimeLine) self.addAction(self.mainMenu.aEditUndo) self.addAction(self.mainMenu.aEditRedo) self.addAction(self.mainMenu.aEditCut) self.addAction(self.mainMenu.aEditCopy) self.addAction(self.mainMenu.aEditPaste) self.addAction(self.mainMenu.aSelectAll) self.addAction(self.mainMenu.aSelectPar) self.addAction(self.mainMenu.aFmtBold) self.addAction(self.mainMenu.aFmtItalic) self.addAction(self.mainMenu.aFmtULine) self.addAction(self.mainMenu.aFmtDQuote) self.addAction(self.mainMenu.aFmtSQuote) self.addAction(self.mainMenu.aFmtHead1) self.addAction(self.mainMenu.aFmtHead2) self.addAction(self.mainMenu.aFmtHead3) self.addAction(self.mainMenu.aFmtHead4) self.addAction(self.mainMenu.aFmtComment) self.addAction(self.mainMenu.aFmtNoFormat) self.addAction(self.mainMenu.aSpellCheck) self.addAction(self.mainMenu.aReRunSpell) self.addAction(self.mainMenu.aPreferences) self.addAction(self.mainMenu.aHelp) return True def _setWindowTitle(self, projName=None): winTitle = "%s" % nw.__package__ if projName is not None: winTitle += " - %s" % projName self.setWindowTitle(winTitle) return True def _autoSaveProject(self): if (self.hasProject and self.theProject.projChanged and self.theProject.projPath is not None): logger.debug("Autosaving project") self.saveProject() return def _autoSaveDocument(self): if self.hasProject and self.docEditor.docChanged: logger.debug("Autosaving document") self.saveDocument() return def _makeStatusIcons(self): self.statusIcons = {} for sLabel, sCol, _ in self.theProject.statusItems: theIcon = QPixmap(32,32) theIcon.fill(QColor(*sCol)) self.statusIcons[sLabel] = QIcon(theIcon) return def _makeImportIcons(self): self.importIcons = {} for sLabel, sCol, _ in self.theProject.importItems: theIcon = QPixmap(32,32) theIcon.fill(QColor(*sCol)) self.importIcons[sLabel] = QIcon(theIcon) return ## # Events ## def closeEvent(self, theEvent): if self.closeMain(): theEvent.accept() else: theEvent.ignore() return ## # Signal Handlers ## def _treeSingleClick(self): sHandle = self.treeView.getSelectedHandle() if sHandle is not None: self.treeMeta.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 _treeKeyPressReturn(self): tHandle = self.treeView.getSelectedHandle() logger.verbose("User pressed return on 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 _keyPressEscape(self): """When the escape key is pressed somewhere in the main window, do the following, in order. """ if self.searchBar.isVisible(): self.searchBar.setVisible(False) return elif self.isZenMode: self.toggleZenMode() return
def __init__(self): super().__init__() #title and icon self.setWindowIcon(QIcon(app_icon)) self.setWindowTitle('{} {}'.format(app_name, version)) #longzhu obj self.longzhu = None #add login window self.login_window = LoginWindow() self.login_window.set_main_window(self) #horizontal splitter self.setOrientation(Qt.Horizontal) #mpv(left) mpvwidget = MpvWidget(self) #right splitter right_splitter = QSplitter(Qt.Vertical, self) #login splitter login_splitter = QSplitter(Qt.Horizontal, right_splitter) #menubar menubar_widget = QMenuBar(right_splitter) live_menu = menubar_widget.addMenu("live") #dummy chatroom with roomid = 0 chatroom = ChatRoom(right_splitter) #login button login_button = QPushButton("登陆") login_button.clicked.connect(self.login_logout_process) #user info user_info = QLineEdit(right_splitter) user_info.setFrame(False) user_info.setReadOnly(True) #input inputwidget = InputEditor(self.login_window, right_splitter) inputwidget.setFont(textfont) #fill layout self.addWidget(mpvwidget) self.addWidget(right_splitter) self.setStretchFactor(0, 1) right_splitter.addWidget(menubar_widget) right_splitter.addWidget(chatroom) right_splitter.addWidget(login_splitter) right_splitter.addWidget(inputwidget) right_splitter.setStretchFactor(right_splitter.indexOf(chatroom), 1) login_splitter.addWidget(login_button) login_splitter.addWidget(user_info) self.setStretchFactor(0, 1) self.setStretchFactor(1, 0) mp = mpv.MPV( wid=str(int(mpvwidget.winId())), keep_open="yes", idle="yes", osc="yes", cursor_autohide="no", input_cursor="no", input_default_bindings="no", config="yes", ytdl="yes", ) self.right_splitter = right_splitter self.login_splitter = login_splitter self.inputwidget = inputwidget self.menubar_widget = menubar_widget self.live_menu = live_menu self.chatroom = chatroom self.login_button = login_button self.user_info = user_info self.mpvwidget = mpvwidget self.mp = mp
class GuiBuildNovel(QDialog): FMT_PDF = 1 # Print to PDF FMT_ODT = 2 # Open Document file FMT_FODT = 3 # Flat Open Document file FMT_HTM = 4 # HTML5 FMT_NWD = 5 # nW Markdown FMT_MD = 6 # Standard Markdown FMT_GH = 7 # GitHub Markdown FMT_JSON_H = 8 # HTML5 wrapped in JSON FMT_JSON_M = 9 # nW Markdown wrapped in JSON def __init__(self, mainGui): QDialog.__init__(self, mainGui) logger.debug("Initialising GuiBuildNovel ...") self.setObjectName("GuiBuildNovel") self.mainConf = novelwriter.CONFIG self.mainGui = mainGui self.mainTheme = mainGui.mainTheme self.theProject = mainGui.theProject self.htmlText = [] # List of html documents self.htmlStyle = [] # List of html styles self.htmlSize = 0 # Size of the html document self.buildTime = 0 # The timestamp of the last build self.setWindowTitle(self.tr("Build Novel Project")) self.setMinimumWidth(self.mainConf.pxInt(700)) self.setMinimumHeight(self.mainConf.pxInt(600)) pOptions = self.theProject.options self.resize( self.mainConf.pxInt(pOptions.getInt("GuiBuildNovel", "winWidth", 900)), self.mainConf.pxInt(pOptions.getInt("GuiBuildNovel", "winHeight", 800)) ) self.docView = GuiBuildNovelDocView(self, self.theProject) hS = self.mainTheme.fontPixelSize wS = 2*hS # Title Formats # ============= self.titleGroup = QGroupBox(self.tr("Title Formats for Novel Files"), self) self.titleForm = QGridLayout(self) self.titleGroup.setLayout(self.titleForm) fmtHelp = "<br>".join([ "<b>%s</b>" % self.tr("Formatting Codes:"), self.tr("{0} for the title as set in the document").format(r"%title%"), self.tr("{0} for chapter number (1, 2, 3)").format(r"%ch%"), self.tr("{0} for chapter number as a word (one, two)").format(r"%chw%"), self.tr("{0} for chapter number in upper case Roman").format(r"%chI%"), self.tr("{0} for chapter number in lower case Roman").format(r"%chi%"), self.tr("{0} for scene number within chapter").format(r"%sc%"), self.tr("{0} for scene number within novel").format(r"%sca%"), ]) fmtScHelp = "<br><br>%s" % self.tr( "Leave blank to skip this heading, or set to a static text, like " "for instance '{0}', to make a separator. The separator will " "be centred automatically and only appear between sections of " "the same type." ).format("* * *") xFmt = self.mainConf.pxInt(100) self.fmtTitle = QLineEdit() self.fmtTitle.setMaxLength(200) self.fmtTitle.setMinimumWidth(xFmt) self.fmtTitle.setToolTip(fmtHelp) self.fmtTitle.setText( self._reFmtCodes(self.theProject.titleFormat["title"]) ) self.fmtChapter = QLineEdit() self.fmtChapter.setMaxLength(200) self.fmtChapter.setMinimumWidth(xFmt) self.fmtChapter.setToolTip(fmtHelp) self.fmtChapter.setText( self._reFmtCodes(self.theProject.titleFormat["chapter"]) ) self.fmtUnnumbered = QLineEdit() self.fmtUnnumbered.setMaxLength(200) self.fmtUnnumbered.setMinimumWidth(xFmt) self.fmtUnnumbered.setToolTip(fmtHelp) self.fmtUnnumbered.setText( self._reFmtCodes(self.theProject.titleFormat["unnumbered"]) ) self.fmtScene = QLineEdit() self.fmtScene.setMaxLength(200) self.fmtScene.setMinimumWidth(xFmt) self.fmtScene.setToolTip(fmtHelp + fmtScHelp) self.fmtScene.setText( self._reFmtCodes(self.theProject.titleFormat["scene"]) ) self.fmtSection = QLineEdit() self.fmtSection.setMaxLength(200) self.fmtSection.setMinimumWidth(xFmt) self.fmtSection.setToolTip(fmtHelp + fmtScHelp) self.fmtSection.setText( self._reFmtCodes(self.theProject.titleFormat["section"]) ) self.buildLang = QComboBox() self.buildLang.setMinimumWidth(xFmt) theLangs = self.mainConf.listLanguages(self.mainConf.LANG_PROJ) self.buildLang.addItem("[%s]" % self.tr("Not Set"), "None") for langID, langName in theLangs: self.buildLang.addItem(langName, langID) langIdx = self.buildLang.findData(self.theProject.projLang) if langIdx != -1: self.buildLang.setCurrentIndex(langIdx) self.hideScene = QSwitch(width=wS, height=hS) self.hideScene.setChecked( pOptions.getBool("GuiBuildNovel", "hideScene", False) ) self.hideSection = QSwitch(width=wS, height=hS) self.hideSection.setChecked( pOptions.getBool("GuiBuildNovel", "hideSection", True) ) # Wrapper boxes due to QGridView and QLineEdit expand bug self.boxTitle = QHBoxLayout() self.boxTitle.addWidget(self.fmtTitle) self.boxChapter = QHBoxLayout() self.boxChapter.addWidget(self.fmtChapter) self.boxUnnumb = QHBoxLayout() self.boxUnnumb.addWidget(self.fmtUnnumbered) self.boxScene = QHBoxLayout() self.boxScene.addWidget(self.fmtScene) self.boxSection = QHBoxLayout() self.boxSection.addWidget(self.fmtSection) titleLabel = QLabel(self.tr("Title")) chapterLabel = QLabel(self.tr("Chapter")) unnumbLabel = QLabel(self.tr("Unnumbered")) sceneLabel = QLabel(self.tr("Scene")) sectionLabel = QLabel(self.tr("Section")) langLabel = QLabel(self.tr("Language")) hSceneLabel = QLabel(self.tr("Hide scene")) hSectionLabel = QLabel(self.tr("Hide section")) self.titleForm.addWidget(titleLabel, 0, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxTitle, 0, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(chapterLabel, 1, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxChapter, 1, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(unnumbLabel, 2, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxUnnumb, 2, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(sceneLabel, 3, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxScene, 3, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(sectionLabel, 4, 0, 1, 1, Qt.AlignLeft) self.titleForm.addLayout(self.boxSection, 4, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(langLabel, 5, 0, 1, 1, Qt.AlignLeft) self.titleForm.addWidget(self.buildLang, 5, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(hSceneLabel, 6, 0, 1, 1, Qt.AlignLeft) self.titleForm.addWidget(self.hideScene, 6, 1, 1, 1, Qt.AlignRight) self.titleForm.addWidget(hSectionLabel, 7, 0, 1, 1, Qt.AlignLeft) self.titleForm.addWidget(self.hideSection, 7, 1, 1, 1, Qt.AlignRight) self.titleForm.setColumnStretch(0, 0) self.titleForm.setColumnStretch(1, 1) # Font Options # ============ self.fontGroup = QGroupBox(self.tr("Font Options"), self) self.fontForm = QGridLayout(self) self.fontGroup.setLayout(self.fontForm) # Font Family self.textFont = QLineEdit() self.textFont.setReadOnly(True) self.textFont.setMinimumWidth(xFmt) self.textFont.setText( pOptions.getString("GuiBuildNovel", "textFont", self.mainConf.textFont) ) self.fontButton = QPushButton("...") self.fontButton.setMaximumWidth(int(2.5*self.mainTheme.getTextWidth("..."))) self.fontButton.clicked.connect(self._selectFont) self.textSize = QSpinBox(self) self.textSize.setFixedWidth(6*self.mainTheme.textNWidth) self.textSize.setMinimum(6) self.textSize.setMaximum(72) self.textSize.setSingleStep(1) self.textSize.setValue( pOptions.getInt("GuiBuildNovel", "textSize", self.mainConf.textSize) ) self.lineHeight = QDoubleSpinBox(self) self.lineHeight.setFixedWidth(6*self.mainTheme.textNWidth) self.lineHeight.setMinimum(0.8) self.lineHeight.setMaximum(3.0) self.lineHeight.setSingleStep(0.05) self.lineHeight.setDecimals(2) self.lineHeight.setValue( pOptions.getFloat("GuiBuildNovel", "lineHeight", 1.15) ) # Wrapper box due to QGridView and QLineEdit expand bug self.boxFont = QHBoxLayout() self.boxFont.addWidget(self.textFont) fontFamilyLabel = QLabel(self.tr("Font family")) fontSizeLabel = QLabel(self.tr("Font size")) lineHeightLabel = QLabel(self.tr("Line height")) justifyLabel = QLabel(self.tr("Justify text")) stylingLabel = QLabel(self.tr("Disable styling")) self.fontForm.addWidget(fontFamilyLabel, 0, 0, 1, 1, Qt.AlignLeft) self.fontForm.addLayout(self.boxFont, 0, 1, 1, 1, Qt.AlignRight) self.fontForm.addWidget(self.fontButton, 0, 2, 1, 1, Qt.AlignRight) self.fontForm.addWidget(fontSizeLabel, 1, 0, 1, 1, Qt.AlignLeft) self.fontForm.addWidget(self.textSize, 1, 1, 1, 2, Qt.AlignRight) self.fontForm.addWidget(lineHeightLabel, 2, 0, 1, 1, Qt.AlignLeft) self.fontForm.addWidget(self.lineHeight, 2, 1, 1, 2, Qt.AlignRight) self.fontForm.setColumnStretch(0, 0) self.fontForm.setColumnStretch(1, 1) self.fontForm.setColumnStretch(2, 0) # Styling Options # =============== self.styleGroup = QGroupBox(self.tr("Styling Options"), self) self.styleForm = QGridLayout(self) self.styleGroup.setLayout(self.styleForm) self.justifyText = QSwitch(width=wS, height=hS) self.justifyText.setChecked( pOptions.getBool("GuiBuildNovel", "justifyText", False) ) self.noStyling = QSwitch(width=wS, height=hS) self.noStyling.setChecked( pOptions.getBool("GuiBuildNovel", "noStyling", False) ) self.styleForm.addWidget(justifyLabel, 1, 0, 1, 1, Qt.AlignLeft) self.styleForm.addWidget(self.justifyText, 1, 1, 1, 2, Qt.AlignRight) self.styleForm.addWidget(stylingLabel, 2, 0, 1, 1, Qt.AlignLeft) self.styleForm.addWidget(self.noStyling, 2, 1, 1, 2, Qt.AlignRight) self.styleForm.setColumnStretch(0, 0) self.styleForm.setColumnStretch(1, 1) # Include Options # =============== self.textGroup = QGroupBox(self.tr("Include Options"), self) self.textForm = QGridLayout(self) self.textGroup.setLayout(self.textForm) self.includeSynopsis = QSwitch(width=wS, height=hS) self.includeSynopsis.setChecked( pOptions.getBool("GuiBuildNovel", "incSynopsis", False) ) self.includeComments = QSwitch(width=wS, height=hS) self.includeComments.setChecked( pOptions.getBool("GuiBuildNovel", "incComments", False) ) self.includeKeywords = QSwitch(width=wS, height=hS) self.includeKeywords.setChecked( pOptions.getBool("GuiBuildNovel", "incKeywords", False) ) self.includeBody = QSwitch(width=wS, height=hS) self.includeBody.setChecked( pOptions.getBool("GuiBuildNovel", "incBodyText", True) ) synopsisLabel = QLabel(self.tr("Include synopsis")) commentsLabel = QLabel(self.tr("Include comments")) keywordsLabel = QLabel(self.tr("Include keywords")) bodyLabel = QLabel(self.tr("Include body text")) self.textForm.addWidget(synopsisLabel, 0, 0, 1, 1, Qt.AlignLeft) self.textForm.addWidget(self.includeSynopsis, 0, 1, 1, 1, Qt.AlignRight) self.textForm.addWidget(commentsLabel, 1, 0, 1, 1, Qt.AlignLeft) self.textForm.addWidget(self.includeComments, 1, 1, 1, 1, Qt.AlignRight) self.textForm.addWidget(keywordsLabel, 2, 0, 1, 1, Qt.AlignLeft) self.textForm.addWidget(self.includeKeywords, 2, 1, 1, 1, Qt.AlignRight) self.textForm.addWidget(bodyLabel, 3, 0, 1, 1, Qt.AlignLeft) self.textForm.addWidget(self.includeBody, 3, 1, 1, 1, Qt.AlignRight) self.textForm.setColumnStretch(0, 1) self.textForm.setColumnStretch(1, 0) # File Filter Options # =================== self.fileGroup = QGroupBox(self.tr("File Filter Options"), self) self.fileForm = QGridLayout(self) self.fileGroup.setLayout(self.fileForm) self.novelFiles = QSwitch(width=wS, height=hS) self.novelFiles.setChecked( pOptions.getBool("GuiBuildNovel", "addNovel", True) ) self.noteFiles = QSwitch(width=wS, height=hS) self.noteFiles.setChecked( pOptions.getBool("GuiBuildNovel", "addNotes", False) ) self.ignoreFlag = QSwitch(width=wS, height=hS) self.ignoreFlag.setChecked( pOptions.getBool("GuiBuildNovel", "ignoreFlag", False) ) novelLabel = QLabel(self.tr("Include novel files")) notesLabel = QLabel(self.tr("Include note files")) exportLabel = QLabel(self.tr("Ignore export flag")) self.fileForm.addWidget(novelLabel, 0, 0, 1, 1, Qt.AlignLeft) self.fileForm.addWidget(self.novelFiles, 0, 1, 1, 1, Qt.AlignRight) self.fileForm.addWidget(notesLabel, 1, 0, 1, 1, Qt.AlignLeft) self.fileForm.addWidget(self.noteFiles, 1, 1, 1, 1, Qt.AlignRight) self.fileForm.addWidget(exportLabel, 2, 0, 1, 1, Qt.AlignLeft) self.fileForm.addWidget(self.ignoreFlag, 2, 1, 1, 1, Qt.AlignRight) self.fileForm.setColumnStretch(0, 1) self.fileForm.setColumnStretch(1, 0) # Export Options # ============== self.exportGroup = QGroupBox(self.tr("Export Options"), self) self.exportForm = QGridLayout(self) self.exportGroup.setLayout(self.exportForm) self.replaceTabs = QSwitch(width=wS, height=hS) self.replaceTabs.setChecked( pOptions.getBool("GuiBuildNovel", "replaceTabs", False) ) self.replaceUCode = QSwitch(width=wS, height=hS) self.replaceUCode.setChecked( pOptions.getBool("GuiBuildNovel", "replaceUCode", False) ) tabsLabel = QLabel(self.tr("Replace tabs with spaces")) uCodeLabel = QLabel(self.tr("Replace Unicode in HTML")) self.exportForm.addWidget(tabsLabel, 0, 0, 1, 1, Qt.AlignLeft) self.exportForm.addWidget(self.replaceTabs, 0, 1, 1, 1, Qt.AlignRight) self.exportForm.addWidget(uCodeLabel, 1, 0, 1, 1, Qt.AlignLeft) self.exportForm.addWidget(self.replaceUCode, 1, 1, 1, 1, Qt.AlignRight) self.exportForm.setColumnStretch(0, 1) self.exportForm.setColumnStretch(1, 0) # Build Button # ============ self.buildProgress = QProgressBar() self.buildNovel = QPushButton(self.tr("Build Preview")) self.buildNovel.clicked.connect(self._buildPreview) # Action Buttons # ============== self.buttonBox = QHBoxLayout() # Printing self.printMenu = QMenu(self) self.btnPrint = QPushButton(self.tr("Print")) self.btnPrint.setMenu(self.printMenu) self.printSend = QAction(self.tr("Print Preview"), self) self.printSend.triggered.connect(self._printDocument) self.printMenu.addAction(self.printSend) self.printFile = QAction(self.tr("Print to PDF"), self) self.printFile.triggered.connect(lambda: self._saveDocument(self.FMT_PDF)) self.printMenu.addAction(self.printFile) # Saving to File self.saveMenu = QMenu(self) self.btnSave = QPushButton(self.tr("Save As")) self.btnSave.setMenu(self.saveMenu) self.saveODT = QAction(self.tr("Open Document (.odt)"), self) self.saveODT.triggered.connect(lambda: self._saveDocument(self.FMT_ODT)) self.saveMenu.addAction(self.saveODT) self.saveFODT = QAction(self.tr("Flat Open Document (.fodt)"), self) self.saveFODT.triggered.connect(lambda: self._saveDocument(self.FMT_FODT)) self.saveMenu.addAction(self.saveFODT) self.saveHTM = QAction(self.tr("novelWriter HTML (.htm)"), self) self.saveHTM.triggered.connect(lambda: self._saveDocument(self.FMT_HTM)) self.saveMenu.addAction(self.saveHTM) self.saveNWD = QAction(self.tr("novelWriter Markdown (.nwd)"), self) self.saveNWD.triggered.connect(lambda: self._saveDocument(self.FMT_NWD)) self.saveMenu.addAction(self.saveNWD) self.saveMD = QAction(self.tr("Standard Markdown (.md)"), self) self.saveMD.triggered.connect(lambda: self._saveDocument(self.FMT_MD)) self.saveMenu.addAction(self.saveMD) self.saveGH = QAction(self.tr("GitHub Markdown (.md)"), self) self.saveGH.triggered.connect(lambda: self._saveDocument(self.FMT_GH)) self.saveMenu.addAction(self.saveGH) self.saveJsonH = QAction(self.tr("JSON + novelWriter HTML (.json)"), self) self.saveJsonH.triggered.connect(lambda: self._saveDocument(self.FMT_JSON_H)) self.saveMenu.addAction(self.saveJsonH) self.saveJsonM = QAction(self.tr("JSON + novelWriter Markdown (.json)"), self) self.saveJsonM.triggered.connect(lambda: self._saveDocument(self.FMT_JSON_M)) self.saveMenu.addAction(self.saveJsonM) self.btnClose = QPushButton(self.tr("Close")) self.btnClose.clicked.connect(self._doClose) self.buttonBox.addWidget(self.btnSave) self.buttonBox.addWidget(self.btnPrint) self.buttonBox.addWidget(self.btnClose) self.buttonBox.setSpacing(self.mainConf.pxInt(4)) # Assemble GUI # ============ # Splitter Position boxWidth = self.mainConf.pxInt(350) boxWidth = pOptions.getInt("GuiBuildNovel", "boxWidth", boxWidth) docWidth = max(self.width() - boxWidth, 100) docWidth = pOptions.getInt("GuiBuildNovel", "docWidth", docWidth) # The Tool Box self.toolsBox = QVBoxLayout() self.toolsBox.addWidget(self.titleGroup) self.toolsBox.addWidget(self.fontGroup) self.toolsBox.addWidget(self.styleGroup) self.toolsBox.addWidget(self.textGroup) self.toolsBox.addWidget(self.fileGroup) self.toolsBox.addWidget(self.exportGroup) self.toolsBox.addStretch(1) # Tool Box Wrapper Widget self.toolsWidget = QWidget() self.toolsWidget.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Minimum) self.toolsWidget.setLayout(self.toolsBox) # Tool Box Scroll Area self.toolsArea = QScrollArea() self.toolsArea.setMinimumWidth(self.mainConf.pxInt(250)) self.toolsArea.setWidgetResizable(True) self.toolsArea.setWidget(self.toolsWidget) if self.mainConf.hideVScroll: self.toolsArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) else: self.toolsArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded) if self.mainConf.hideHScroll: self.toolsArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) else: self.toolsArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) # Tools and Buttons Layout tSp = self.mainConf.pxInt(8) self.innerBox = QVBoxLayout() self.innerBox.addWidget(self.toolsArea) self.innerBox.addSpacing(tSp) self.innerBox.addWidget(self.buildProgress) self.innerBox.addWidget(self.buildNovel) self.innerBox.addSpacing(tSp) self.innerBox.addLayout(self.buttonBox) # Tools and Buttons Wrapper Widget self.innerWidget = QWidget() self.innerWidget.setLayout(self.innerBox) # Main Dialog Splitter self.mainSplit = QSplitter(Qt.Horizontal) self.mainSplit.addWidget(self.innerWidget) self.mainSplit.addWidget(self.docView) self.mainSplit.setSizes([boxWidth, docWidth]) self.idxSettings = self.mainSplit.indexOf(self.innerWidget) self.idxDocument = self.mainSplit.indexOf(self.docView) self.mainSplit.setCollapsible(self.idxSettings, False) self.mainSplit.setCollapsible(self.idxDocument, False) # Outer Layout self.outerBox = QHBoxLayout() self.outerBox.addWidget(self.mainSplit) self.setLayout(self.outerBox) self.buildNovel.setFocus() logger.debug("GuiBuildNovel initialisation complete") return def viewCachedDoc(self): """Load the previously generated document from cache. """ if self._loadCache(): textFont = self.textFont.text() textSize = self.textSize.value() justifyText = self.justifyText.isChecked() self.docView.setTextFont(textFont, textSize) self.docView.setJustify(justifyText) if self.noStyling.isChecked(): self.docView.clearStyleSheet() else: self.docView.setStyleSheet(self.htmlStyle) htmlSize = sum([len(x) for x in self.htmlText]) if htmlSize < nwConst.MAX_BUILDSIZE: qApp.processEvents() self.docView.setContent(self.htmlText, self.buildTime) else: self.docView.setText( self.tr("Failed to generate preview. The result is too big.") ) else: self.htmlText = [] self.htmlStyle = [] self.buildTime = 0 return False return True ## # Slots and Related ## def _buildPreview(self): """Build a preview of the project in the document viewer. """ # Get Settings justifyText = self.justifyText.isChecked() noStyling = self.noStyling.isChecked() textFont = self.textFont.text() textSize = self.textSize.value() replaceTabs = self.replaceTabs.isChecked() self.htmlText = [] self.htmlStyle = [] self.htmlSize = 0 # Build Preview # ============= makeHtml = ToHtml(self.theProject) self._doBuild(makeHtml, isPreview=True) if replaceTabs: makeHtml.replaceTabs() self.htmlText = makeHtml.fullHTML self.htmlStyle = makeHtml.getStyleSheet() self.htmlSize = makeHtml.getFullResultSize() self.buildTime = int(time()) # Load Preview # ============ self.docView.setTextFont(textFont, textSize) self.docView.setJustify(justifyText) if noStyling: self.docView.clearStyleSheet() else: self.docView.setStyleSheet(self.htmlStyle) if self.htmlSize < nwConst.MAX_BUILDSIZE: self.docView.setContent(self.htmlText, self.buildTime) else: self.docView.setText( "Failed to generate preview. The result is too big." ) self._saveCache() return def _doBuild(self, bldObj, isPreview=False, doConvert=True): """Rund the build with a specific build object. """ tStart = int(time()) # Get Settings fmtTitle = self.fmtTitle.text() fmtChapter = self.fmtChapter.text() fmtUnnumbered = self.fmtUnnumbered.text() fmtScene = self.fmtScene.text() fmtSection = self.fmtSection.text() buildLang = self.buildLang.currentData() hideScene = self.hideScene.isChecked() hideSection = self.hideSection.isChecked() textFont = self.textFont.text() textSize = self.textSize.value() lineHeight = self.lineHeight.value() justifyText = self.justifyText.isChecked() noStyling = self.noStyling.isChecked() incSynopsis = self.includeSynopsis.isChecked() incComments = self.includeComments.isChecked() incKeywords = self.includeKeywords.isChecked() novelFiles = self.novelFiles.isChecked() noteFiles = self.noteFiles.isChecked() ignoreFlag = self.ignoreFlag.isChecked() includeBody = self.includeBody.isChecked() replaceUCode = self.replaceUCode.isChecked() # The language lookup dict is reloaded if needed self.theProject.setProjectLang(buildLang) # Get font information fontInfo = QFontInfo(QFont(textFont, textSize)) textFixed = fontInfo.fixedPitch() isHtml = isinstance(bldObj, ToHtml) isOdt = isinstance(bldObj, ToOdt) bldObj.setTitleFormat(fmtTitle) bldObj.setChapterFormat(fmtChapter) bldObj.setUnNumberedFormat(fmtUnnumbered) bldObj.setSceneFormat(fmtScene, hideScene) bldObj.setSectionFormat(fmtSection, hideSection) bldObj.setFont(textFont, textSize, textFixed) bldObj.setJustify(justifyText) bldObj.setLineHeight(lineHeight) bldObj.setSynopsis(incSynopsis) bldObj.setComments(incComments) bldObj.setKeywords(incKeywords) bldObj.setBodyText(includeBody) if isHtml: bldObj.setStyles(not noStyling) bldObj.setReplaceUnicode(replaceUCode) if isOdt: bldObj.setColourHeaders(not noStyling) bldObj.setLanguage(buildLang) bldObj.initDocument() # Make sure the project and document is up to date self.mainGui.saveDocument() self.buildProgress.setMaximum(len(self.theProject.tree)) self.buildProgress.setValue(0) for nItt, tItem in enumerate(self.theProject.tree): noteRoot = noteFiles noteRoot &= tItem.itemType == nwItemType.ROOT noteRoot &= tItem.itemClass != nwItemClass.NOVEL noteRoot &= tItem.itemClass != nwItemClass.ARCHIVE try: if noteRoot: # Add headers for root folders of notes bldObj.addRootHeading(tItem.itemHandle) if doConvert: bldObj.doConvert() elif self._checkInclude(tItem, noteFiles, novelFiles, ignoreFlag): bldObj.setText(tItem.itemHandle) bldObj.doPreProcessing() bldObj.tokenizeText() bldObj.doHeaders() if doConvert: bldObj.doConvert() bldObj.doPostProcessing() except Exception: logger.error("Failed to build document '%s'", tItem.itemHandle) logException() if isPreview: self.docView.setText(( "Failed to generate preview. " "Document with title '%s' could not be parsed." ) % tItem.itemName) return False # Update progress bar, also for skipped items self.buildProgress.setValue(nItt+1) if isOdt: bldObj.closeDocument() tEnd = int(time()) logger.debug("Built project in %.3f ms", 1000*(tEnd - tStart)) if bldObj.errData: self.mainGui.makeAlert([ self.tr("There were problems when building the project:") ] + bldObj.errData, nwAlert.ERROR) return def _checkInclude(self, theItem, noteFiles, novelFiles, ignoreFlag): """This function checks whether a file should be included in the export or not. For standard note and novel files, this is controlled by the options selected by the user. For other files classified as non-exportable, a few checks must be made, and the following are not: * Items that are not actual files. * Items that have been orphaned which are tagged as NO_LAYOUT and NO_CLASS. * Items that appear in the TRASH folder or have parent set to None (orphaned files). """ if theItem is None: return False if not (theItem.isExported or ignoreFlag): return False isNone = theItem.itemType != nwItemType.FILE isNone |= theItem.itemLayout == nwItemLayout.NO_LAYOUT isNone |= theItem.isInactive() isNone |= theItem.itemParent is None isNote = theItem.itemLayout == nwItemLayout.NOTE isNovel = not isNone and not isNote if isNone: return False if isNote and not noteFiles: return False if isNovel and not novelFiles: return False return True def _saveDocument(self, theFmt): """Save the document to various formats. """ replaceTabs = self.replaceTabs.isChecked() fileExt = "" textFmt = "" # Settings # ======== if theFmt == self.FMT_ODT: fileExt = "odt" textFmt = self.tr("Open Document") elif theFmt == self.FMT_FODT: fileExt = "fodt" textFmt = self.tr("Flat Open Document") elif theFmt == self.FMT_HTM: fileExt = "htm" textFmt = self.tr("Plain HTML") elif theFmt == self.FMT_NWD: fileExt = "nwd" textFmt = self.tr("novelWriter Markdown") elif theFmt == self.FMT_MD: fileExt = "md" textFmt = self.tr("Standard Markdown") elif theFmt == self.FMT_GH: fileExt = "md" textFmt = self.tr("GitHub Markdown") elif theFmt == self.FMT_JSON_H: fileExt = "json" textFmt = self.tr("JSON + novelWriter HTML") elif theFmt == self.FMT_JSON_M: fileExt = "json" textFmt = self.tr("JSON + novelWriter Markdown") elif theFmt == self.FMT_PDF: fileExt = "pdf" textFmt = self.tr("PDF") else: return False # Generate File Name # ================== cleanName = makeFileNameSafe(self.theProject.projName) fileName = "%s.%s" % (cleanName, fileExt) saveDir = self.mainConf.lastPath if not os.path.isdir(saveDir): saveDir = os.path.expanduser("~") savePath = os.path.join(saveDir, fileName) savePath, _ = QFileDialog.getSaveFileName( self, self.tr("Save Document As"), savePath ) if not savePath: return False self.mainConf.setLastPath(savePath) # Build and Write # =============== errMsg = "" wSuccess = False if theFmt == self.FMT_ODT: makeOdt = ToOdt(self.theProject, isFlat=False) self._doBuild(makeOdt) try: makeOdt.saveOpenDocText(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_FODT: makeOdt = ToOdt(self.theProject, isFlat=True) self._doBuild(makeOdt) try: makeOdt.saveFlatXML(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_HTM: makeHtml = ToHtml(self.theProject) self._doBuild(makeHtml) if replaceTabs: makeHtml.replaceTabs() try: makeHtml.saveHTML5(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_NWD: makeNwd = ToMarkdown(self.theProject) makeNwd.setKeepMarkdown(True) self._doBuild(makeNwd, doConvert=False) if replaceTabs: makeNwd.replaceTabs(spaceChar=" ") try: makeNwd.saveRawMarkdown(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt in (self.FMT_MD, self.FMT_GH): makeMd = ToMarkdown(self.theProject) if theFmt == self.FMT_GH: makeMd.setGitHubMarkdown() else: makeMd.setStandardMarkdown() self._doBuild(makeMd) if replaceTabs: makeMd.replaceTabs(nSpaces=4, spaceChar=" ") try: makeMd.saveMarkdown(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_JSON_H or theFmt == self.FMT_JSON_M: jsonData = { "meta": { "workingTitle": self.theProject.projName, "novelTitle": self.theProject.bookTitle, "authors": self.theProject.bookAuthors, "buildTime": self.buildTime, } } if theFmt == self.FMT_JSON_H: makeHtml = ToHtml(self.theProject) self._doBuild(makeHtml) if replaceTabs: makeHtml.replaceTabs() theBody = [] for htmlPage in makeHtml.fullHTML: theBody.append(htmlPage.rstrip("\n").split("\n")) jsonData["text"] = { "css": self.htmlStyle, "html": theBody, } elif theFmt == self.FMT_JSON_M: makeMd = ToHtml(self.theProject) makeMd.setKeepMarkdown(True) self._doBuild(makeMd, doConvert=False) if replaceTabs: makeMd.replaceTabs(spaceChar=" ") theBody = [] for nwdPage in makeMd.theMarkdown: theBody.append(nwdPage.split("\n")) jsonData["text"] = { "nwd": theBody, } try: with open(savePath, mode="w", encoding="utf-8") as outFile: outFile.write(json.dumps(jsonData, indent=2)) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_PDF: try: thePrinter = QPrinter() thePrinter.setOutputFormat(QPrinter.PdfFormat) thePrinter.setOrientation(QPrinter.Portrait) thePrinter.setDuplex(QPrinter.DuplexLongSide) thePrinter.setFontEmbeddingEnabled(True) thePrinter.setColorMode(QPrinter.Color) thePrinter.setOutputFileName(savePath) self.docView.document().print(thePrinter) wSuccess = True except Exception as exc: errMsg = formatException(exc) else: # If the if statements above and here match, it should not # be possible to reach this else statement. return False # pragma: no cover # Report to User # ============== if wSuccess: self.mainGui.makeAlert([ self.tr("{0} file successfully written to:").format(textFmt), savePath ], nwAlert.INFO) else: self.mainGui.makeAlert(self.tr( "Failed to write {0} file. {1}" ).format(textFmt, errMsg), nwAlert.ERROR) return wSuccess def _printDocument(self): """Open the print preview dialog. """ thePreview = QPrintPreviewDialog(self) thePreview.paintRequested.connect(self._doPrintPreview) thePreview.exec_() return def _doPrintPreview(self, thePrinter): """Connect the print preview painter to the document viewer. """ qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) thePrinter.setOrientation(QPrinter.Portrait) self.docView.document().print(thePrinter) qApp.restoreOverrideCursor() return def _selectFont(self): """Open the QFontDialog and set a font for the font style. """ currFont = QFont() currFont.setFamily(self.textFont.text()) currFont.setPointSize(self.textSize.value()) theFont, theStatus = QFontDialog.getFont(currFont, self) if theStatus: self.textFont.setText(theFont.family()) self.textSize.setValue(theFont.pointSize()) self.raise_() # Move the dialog to front (fixes a bug on macOS) return def _loadCache(self): """Save the current data to cache. """ buildCache = os.path.join(self.theProject.projCache, nwFiles.BUILD_CACHE) dataCount = 0 if os.path.isfile(buildCache): logger.debug("Loading build cache") try: with open(buildCache, mode="r", encoding="utf-8") as inFile: theJson = inFile.read() theData = json.loads(theJson) except Exception: logger.error("Failed to load build cache") logException() return False if "buildTime" in theData.keys(): self.buildTime = theData["buildTime"] if "htmlStyle" in theData.keys(): self.htmlStyle = theData["htmlStyle"] dataCount += 1 if "htmlText" in theData.keys(): self.htmlText = theData["htmlText"] dataCount += 1 return dataCount == 2 def _saveCache(self): """Save the current data to cache. """ buildCache = os.path.join(self.theProject.projCache, nwFiles.BUILD_CACHE) logger.debug("Saving build cache") try: with open(buildCache, mode="w+", encoding="utf-8") as outFile: outFile.write(json.dumps({ "buildTime": self.buildTime, "htmlStyle": self.htmlStyle, "htmlText": self.htmlText, }, indent=2)) except Exception: logger.error("Failed to save build cache") logException() return False return True def _doClose(self): """Close button was clicked. """ self.close() return ## # Events ## def closeEvent(self, theEvent): """Capture the user closing the window so we can save settings. """ self._saveSettings() self.docView.clear() theEvent.accept() return ## # Internal Functions ## def _saveSettings(self): """Save the various user settings. """ logger.debug("Saving GuiBuildNovel settings") # Formatting self.theProject.setTitleFormat({ "title": self.fmtTitle.text().strip(), "chapter": self.fmtChapter.text().strip(), "unnumbered": self.fmtUnnumbered.text().strip(), "scene": self.fmtScene.text().strip(), "section": self.fmtSection.text().strip(), }) buildLang = self.buildLang.currentData() hideScene = self.hideScene.isChecked() hideSection = self.hideSection.isChecked() winWidth = self.mainConf.rpxInt(self.width()) winHeight = self.mainConf.rpxInt(self.height()) justifyText = self.justifyText.isChecked() noStyling = self.noStyling.isChecked() textFont = self.textFont.text() textSize = self.textSize.value() lineHeight = self.lineHeight.value() novelFiles = self.novelFiles.isChecked() noteFiles = self.noteFiles.isChecked() ignoreFlag = self.ignoreFlag.isChecked() incSynopsis = self.includeSynopsis.isChecked() incComments = self.includeComments.isChecked() incKeywords = self.includeKeywords.isChecked() incBodyText = self.includeBody.isChecked() replaceTabs = self.replaceTabs.isChecked() replaceUCode = self.replaceUCode.isChecked() mainSplit = self.mainSplit.sizes() boxWidth = self.mainConf.rpxInt(mainSplit[0]) docWidth = self.mainConf.rpxInt(mainSplit[1]) self.theProject.setProjectLang(buildLang) # GUI Settings pOptions = self.theProject.options pOptions.setValue("GuiBuildNovel", "hideScene", hideScene) pOptions.setValue("GuiBuildNovel", "hideSection", hideSection) pOptions.setValue("GuiBuildNovel", "winWidth", winWidth) pOptions.setValue("GuiBuildNovel", "winHeight", winHeight) pOptions.setValue("GuiBuildNovel", "boxWidth", boxWidth) pOptions.setValue("GuiBuildNovel", "docWidth", docWidth) pOptions.setValue("GuiBuildNovel", "justifyText", justifyText) pOptions.setValue("GuiBuildNovel", "noStyling", noStyling) pOptions.setValue("GuiBuildNovel", "textFont", textFont) pOptions.setValue("GuiBuildNovel", "textSize", textSize) pOptions.setValue("GuiBuildNovel", "lineHeight", lineHeight) pOptions.setValue("GuiBuildNovel", "addNovel", novelFiles) pOptions.setValue("GuiBuildNovel", "addNotes", noteFiles) pOptions.setValue("GuiBuildNovel", "ignoreFlag", ignoreFlag) pOptions.setValue("GuiBuildNovel", "incSynopsis", incSynopsis) pOptions.setValue("GuiBuildNovel", "incComments", incComments) pOptions.setValue("GuiBuildNovel", "incKeywords", incKeywords) pOptions.setValue("GuiBuildNovel", "incBodyText", incBodyText) pOptions.setValue("GuiBuildNovel", "replaceTabs", replaceTabs) pOptions.setValue("GuiBuildNovel", "replaceUCode", replaceUCode) pOptions.saveSettings() return def _reFmtCodes(self, theFormat): """Translates old formatting codes to new ones. """ theFormat = theFormat.replace(r"%chnum%", r"%ch%") theFormat = theFormat.replace(r"%scnum%", r"%sc%") theFormat = theFormat.replace(r"%scabsnum%", r"%sca%") theFormat = theFormat.replace(r"%chnumword%", r"%chw%") return theFormat
class GuiMain(QMainWindow): def __init__(self): QMainWindow.__init__(self) logger.debug("Initialising GUI ...") self.setObjectName("GuiMain") self.mainConf = nw.CONFIG self.threadPool = QThreadPool() # System Info # =========== logger.info("OS: %s" % self.mainConf.osType) logger.info("Kernel: %s" % self.mainConf.kernelVer) logger.info("Host: %s" % self.mainConf.hostName) logger.info("Qt5 Version: %s (%d)" % ( self.mainConf.verQtString, self.mainConf.verQtValue) ) logger.info("PyQt5 Version: %s (%d)" % ( self.mainConf.verPyQtString, self.mainConf.verPyQtValue) ) logger.info("Python Version: %s (0x%x)" % ( self.mainConf.verPyString, self.mainConf.verPyHexVal) ) # Core Classes # ============ # Core Classes and Settings self.theTheme = GuiTheme(self) self.theProject = NWProject(self) self.theIndex = NWIndex(self.theProject, self) self.hasProject = False self.isFocusMode = False # Prepare Main Window self.resize(*self.mainConf.getWinSize()) self._updateWindowTitle() self.setWindowIcon(QIcon(self.mainConf.appIcon)) # Build the GUI # ============= # Main GUI Elements self.statusBar = GuiMainStatus(self) self.treeView = GuiProjectTree(self) self.docEditor = GuiDocEditor(self) self.viewMeta = GuiDocViewDetails(self) self.docViewer = GuiDocViewer(self) self.treeMeta = GuiItemDetails(self) self.projView = GuiOutline(self) self.projMeta = GuiOutlineDetails(self) self.mainMenu = GuiMainMenu(self) # Minor Gui Elements self.statusIcons = [] self.importIcons = [] # Project Tree View self.treePane = QWidget() self.treeBox = QVBoxLayout() self.treeBox.setContentsMargins(0, 0, 0, 0) self.treeBox.addWidget(self.treeView) self.treeBox.addWidget(self.treeMeta) self.treePane.setLayout(self.treeBox) # Splitter : Document Viewer / Document Meta self.splitView = QSplitter(Qt.Vertical) self.splitView.addWidget(self.docViewer) self.splitView.addWidget(self.viewMeta) self.splitView.setSizes(self.mainConf.getViewPanePos()) # Splitter : Document Editor / Document Viewer self.splitDocs = QSplitter(Qt.Horizontal) self.splitDocs.addWidget(self.docEditor) self.splitDocs.addWidget(self.splitView) # Splitter : Project Outlie / Outline Details self.splitOutline = QSplitter(Qt.Vertical) self.splitOutline.addWidget(self.projView) self.splitOutline.addWidget(self.projMeta) self.splitOutline.setSizes(self.mainConf.getOutlinePanePos()) # Main Tabs : Edirot / Outline self.tabWidget = QTabWidget() self.tabWidget.setTabPosition(QTabWidget.East) self.tabWidget.setStyleSheet("QTabWidget::pane {border: 0;}") self.tabWidget.addTab(self.splitDocs, "Editor") self.tabWidget.addTab(self.splitOutline, "Outline") self.tabWidget.currentChanged.connect(self._mainTabChanged) # Splitter : Project Tree / Main Tabs xCM = self.mainConf.pxInt(4) self.splitMain = QSplitter(Qt.Horizontal) self.splitMain.setContentsMargins(xCM, xCM, xCM, xCM) self.splitMain.addWidget(self.treePane) self.splitMain.addWidget(self.tabWidget) self.splitMain.setSizes(self.mainConf.getMainPanePos()) # Indices of All Splitter Widgets self.idxTree = self.splitMain.indexOf(self.treePane) self.idxMain = self.splitMain.indexOf(self.tabWidget) self.idxEditor = self.splitDocs.indexOf(self.docEditor) self.idxViewer = self.splitDocs.indexOf(self.splitView) self.idxViewDoc = self.splitView.indexOf(self.docViewer) self.idxViewMeta = self.splitView.indexOf(self.viewMeta) self.idxTabEdit = self.tabWidget.indexOf(self.splitDocs) self.idxTabProj = self.tabWidget.indexOf(self.splitOutline) # Splitter Behaviour self.splitMain.setCollapsible(self.idxTree, False) self.splitMain.setCollapsible(self.idxMain, False) self.splitDocs.setCollapsible(self.idxEditor, False) self.splitDocs.setCollapsible(self.idxViewer, True) self.splitView.setCollapsible(self.idxViewDoc, False) self.splitView.setCollapsible(self.idxViewMeta, False) # Editor / Viewer Default State self.splitView.setVisible(False) self.docEditor.closeSearch() # Initialise the Project Tree self.treeView.itemSelectionChanged.connect(self._treeSingleClick) self.treeView.itemDoubleClicked.connect(self._treeDoubleClick) self.rebuildTree() # Set Main Window Elements self.setMenuBar(self.mainMenu) self.setCentralWidget(self.splitMain) self.setStatusBar(self.statusBar) # Finalise Initialisation # ======================= # Set Up Auto-Save Project Timer self.asProjTimer = QTimer() self.asProjTimer.timeout.connect(self._autoSaveProject) # Set Up Auto-Save Document Timer self.asDocTimer = QTimer() self.asDocTimer.timeout.connect(self._autoSaveDocument) # Shortcuts and Actions self._connectMenuActions() keyReturn = QShortcut(self.treeView) keyReturn.setKey(QKeySequence(Qt.Key_Return)) keyReturn.activated.connect(self._treeKeyPressReturn) keyEscape = QShortcut(self) keyEscape.setKey(QKeySequence(Qt.Key_Escape)) keyEscape.activated.connect(self._keyPressEscape) # Forward Functions self.setStatus = self.statusBar.setStatus self.setProjectStatus = self.statusBar.setProjectStatus # Force a show of the GUI self.show() # Check that config loaded fine self.reportConfErr() # Initialise Main GUI self.initMain() self.asProjTimer.start() self.asDocTimer.start() self.statusBar.clearStatus() # Handle Windows Mode self.showNormal() if self.mainConf.isFullScreen: self.toggleFullScreenMode() logger.debug("GUI initialisation complete") # Check if a project path was provided at command line, and if # not, open the project manager instead. if self.mainConf.cmdOpen is not None: logger.debug("Opening project from additional command line option") self.openProject(self.mainConf.cmdOpen) else: if self.mainConf.showGUI: self.showProjectLoadDialog() # Show the latest release notes, if they haven't been shown before if hexToInt(self.mainConf.lastNotes) < hexToInt(nw.__hexversion__): if self.mainConf.showGUI: self.showAboutNWDialog(showNotes=True) self.mainConf.lastNotes = nw.__hexversion__ logger.debug("novelWriter is ready ...") self.setStatus("novelWriter is ready ...") return def clearGUI(self): """Wrapper function to clear all sub-elements of the main GUI. """ # Project Area self.treeView.clearTree() self.treeMeta.clearDetails() # Work Area self.docEditor.clearEditor() self.docEditor.setDictionaries() self.closeDocViewer() self.projMeta.clearDetails() # General self.statusBar.clearStatus() self._updateWindowTitle() return True def initMain(self): """Initialise elements that depend on user settings. """ self.asProjTimer.setInterval(int(self.mainConf.autoSaveProj*1000)) self.asDocTimer.setInterval(int(self.mainConf.autoSaveDoc*1000)) return True ## # Project Actions ## def newProject(self, projData=None): """Create new project via the new project wizard. """ if self.hasProject: if not self.closeProject(): self.makeAlert( "Cannot create new project when another project is open.", nwAlert.ERROR ) return False if projData is None: projData = self.showNewProjectDialog() if projData is None: return False projPath = projData.get("projPath", None) if projPath is None or projData is None: logger.error("No projData or projPath set") return False if os.path.isfile(os.path.join(projPath, self.theProject.projFile)): self.makeAlert( "A project already exists in that location. Please choose another folder.", nwAlert.ERROR ) return False logger.info("Creating new project") if self.theProject.newProject(projData): self.rebuildTree() self.saveProject() self.hasProject = True self.docEditor.setDictionaries() self.rebuildIndex(beQuiet=True) self.statusBar.setRefTime(self.theProject.projOpened) self.statusBar.setProjectStatus(True) self.statusBar.setDocumentStatus(None) self.statusBar.setStatus("New project created ...") self._updateWindowTitle(self.theProject.projName) else: self.theProject.clearProject() return False return True def closeProject(self, isYes=False): """Closes the project if one is open. isYes is passed on from the close application event so the user doesn't get prompted twice to confirm. """ if not self.hasProject: # There is no project loaded, everything OK return True if not isYes: msgYes = self.askQuestion( "Close Project", "Close the current project?<br>Changes are saved automatically." ) if not msgYes: return False if self.docEditor.docChanged: self.saveDocument() if self.theProject.projAltered: saveOK = self.saveProject() doBackup = False if self.theProject.doBackup and self.mainConf.backupOnClose: doBackup = True if self.mainConf.askBeforeBackup: msgYes = self.askQuestion( "Backup Project", "Backup the current project?" ) if not msgYes: doBackup = False if doBackup: self.theProject.zipIt(False) else: saveOK = True if saveOK: self.closeDocument() self.docViewer.clearNavHistory() self.projView.closeOutline() self.theProject.closeProject() self.theIndex.clearIndex() self.clearGUI() self.hasProject = False self.tabWidget.setCurrentWidget(self.splitDocs) return saveOK def openProject(self, projFile): """Open a project from a projFile path. """ if projFile is None: return False # Make sure any open project is cleared out first before we load # another one if not self.closeProject(): return False # Switch main tab to editor view self.tabWidget.setCurrentWidget(self.splitDocs) # Try to open the project if not self.theProject.openProject(projFile): # The project open failed. if self.theProject.lockedBy is None: # The project is not locked, so failed for some other # reason handled by the project class. return False try: lockDetails = ( "<br><br>The project was locked by the computer " "'%s' (%s %s), last active on %s" ) % ( self.theProject.lockedBy[0], self.theProject.lockedBy[1], self.theProject.lockedBy[2], datetime.fromtimestamp( int(self.theProject.lockedBy[3]) ).strftime("%x %X") ) except Exception: lockDetails = "" msgBox = QMessageBox() msgRes = msgBox.warning( self, "Project Locked", ( "The project is already open by another instance of novelWriter, and " "is therefore locked. Override lock and continue anyway?<br><br>" "Note: If the program or the computer previously crashed, the lock " "can safely be overridden. If, however, another instance of " "novelWriter has the project open, overriding the lock may corrupt " "the project, and is not recommended.%s" ) % lockDetails, QMessageBox.Yes | QMessageBox.No, QMessageBox.No ) if msgRes == QMessageBox.Yes: if not self.theProject.openProject(projFile, overrideLock=True): return False else: return False # Project is loaded self.hasProject = True # Load the tag index self.theIndex.loadIndex() # Update GUI self._updateWindowTitle(self.theProject.projName) self.rebuildTree() self.docEditor.setDictionaries() self.docEditor.setSpellCheck(self.theProject.spellCheck) self.mainMenu.setAutoOutline(self.theProject.autoOutline) self.statusBar.setRefTime(self.theProject.projOpened) self.statusBar.setStats(self.theProject.currWCount, 0) # Restore previously open documents, if any if self.theProject.lastEdited is not None: self.openDocument(self.theProject.lastEdited, doScroll=True) if self.theProject.lastViewed is not None: self.viewDocument(self.theProject.lastViewed) # Check if we need to rebuild the index if self.theIndex.indexBroken: self.rebuildIndex() # Make sure the changed status is set to false on all that was # just opened qApp.processEvents() self.docEditor.setDocumentChanged(False) self.theProject.setProjectChanged(False) logger.debug("Project load complete") return True def saveProject(self, autoSave=False): """Save the current project. """ if not self.hasProject: logger.error("No project open") return False # If the project is new, it may not have a path, so we need one if self.theProject.projPath is None: projPath = self.selectProjectPath() self.theProject.setProjectPath(projPath) if self.theProject.projPath is None: return False self.treeView.saveTreeOrder() self.theProject.saveProject(autoSave=autoSave) self.theIndex.saveIndex() return True ## # Document Actions ## def closeDocument(self): """Close the document and clear the editor and title field. """ if not self.hasProject: logger.error("No project open") return False self.docEditor.saveCursorPosition() if self.docEditor.docChanged: self.saveDocument() self.docEditor.clearEditor() return True def openDocument(self, tHandle, tLine=None, changeFocus=True, doScroll=False): """Open a specific document, optionally at a given line. """ if not self.hasProject: logger.error("No project open") return False self.closeDocument() self.tabWidget.setCurrentWidget(self.splitDocs) if self.docEditor.loadText(tHandle, tLine): if changeFocus: self.docEditor.setFocus() self.theProject.setLastEdited(tHandle) self.treeView.setSelectedHandle(tHandle, doScroll=doScroll) else: return False return True def openNextDocument(self, tHandle, wrapAround=False): """Opens the next document in the project tree, following the document with the given handle. Stops when reaching the end. """ if not self.hasProject: logger.error("No project open") return False self.treeView.flushTreeOrder() nHandle = None # The next handle after tHandle fHandle = None # The first file handle we encounter foundIt = False # We've found tHandle, pick the next we see for tItem in self.theProject.projTree: if tItem is None: continue if tItem.itemType != nwItemType.FILE: continue if fHandle is None: fHandle = tItem.itemHandle if tItem.itemHandle == tHandle: foundIt = True elif foundIt: nHandle = tItem.itemHandle break if nHandle is not None: self.openDocument(nHandle, tLine=0, doScroll=True) return True elif wrapAround: self.openDocument(fHandle, tLine=0, doScroll=True) return False return False def saveDocument(self): """Save the current documents. """ if not self.hasProject: logger.error("No project open") return False self.docEditor.saveText() return True def viewDocument(self, tHandle=None, tAnchor=None): """Load a document for viewing in the view panel. """ if not self.hasProject: logger.error("No project open") return False if tHandle is None: logger.debug("Viewing document, but no handle provided") if self.docEditor.hasFocus(): logger.verbose("Trying editor document") tHandle = self.docEditor.theHandle if tHandle is not None: self.saveDocument() else: logger.verbose("Trying selected document") tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.verbose("Trying last viewed document") tHandle = self.theProject.lastViewed if tHandle is None: logger.verbose("No document to view, giving up") return False # Make sure main tab is in Editor view self.tabWidget.setCurrentWidget(self.splitDocs) logger.debug("Viewing document with handle %s" % tHandle) if self.docViewer.loadText(tHandle): if not self.splitView.isVisible(): bPos = self.splitMain.sizes() self.splitView.setVisible(True) vPos = [0, 0] vPos[0] = int(bPos[1]/2) vPos[1] = bPos[1] - vPos[0] self.splitDocs.setSizes(vPos) self.viewMeta.setVisible(self.mainConf.showRefPanel) self.docViewer.navigateTo(tAnchor) return True def importDocument(self): """Import the text contained in an out-of-project text file, and insert the text into the currently open document. """ if not self.hasProject: logger.error("No project open") return False lastPath = self.mainConf.lastPath extFilter = [ "Text files (*.txt)", "Markdown files (*.md)", "novelWriter files (*.nwd)", "All files (*.*)", ] dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.DontUseNativeDialog loadFile, _ = QFileDialog.getOpenFileName( self, "Import File", lastPath, options=dlgOpt, filter=";;".join(extFilter) ) if not loadFile: return False if loadFile.strip() == "": return False theText = None try: with open(loadFile, mode="rt", encoding="utf8") as inFile: theText = inFile.read() self.mainConf.setLastPath(loadFile) except Exception as e: self.makeAlert( ["Could not read file. The file must be an existing text file.", str(e)], nwAlert.ERROR ) return False if self.docEditor.theHandle is None: self.makeAlert( "Please open a document to import the text file into.", nwAlert.ERROR ) return False if not self.docEditor.isEmpty(): msgYes = self.askQuestion("Import Document", ( "Importing the file will overwrite the current content of the document. " "Do you want to proceed?" )) if not msgYes: return False self.docEditor.replaceText(theText) return True def mergeDocuments(self): """Merge multiple documents to one single new document. """ if not self.hasProject: logger.error("No project open") return False dlgMerge = GuiDocMerge(self, self.theProject) dlgMerge.exec_() return True def splitDocument(self): """Split a single document into multiple documents. """ if not self.hasProject: logger.error("No project open") return False dlgSplit = GuiDocSplit(self, self.theProject) dlgSplit.exec_() return True def passDocumentAction(self, theAction): """Pass on document action theAction to the document viewer if it has focus, otherwise pass it to the document editor. """ if self.docViewer.hasFocus(): self.docViewer.docAction(theAction) else: self.docEditor.docAction(theAction) return True ## # Tree Item Actions ## def openSelectedItem(self): """Open the selected documents. """ if not self.hasProject: logger.error("No project open") return False tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.warning("No item selected") return False logger.verbose("Opening item %s" % tHandle) nwItem = self.theProject.projTree[tHandle] if nwItem.itemType == nwItemType.FILE: logger.verbose("Requested item %s is a file" % tHandle) self.openDocument(tHandle, doScroll=False) else: logger.verbose("Requested item %s is not a file" % tHandle) return True def editItem(self, tHandle=None): """Open the edit item dialog. """ if not self.hasProject: logger.error("No project open") return False if tHandle is None: tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.warning("No item selected") return tItem = self.theProject.projTree[tHandle] if tItem is None: return if tItem.itemType not in nwLists.REG_TYPES: return logger.verbose("Requesting change to item %s" % tHandle) dlgProj = GuiItemEditor(self, self.theProject, tHandle) dlgProj.exec_() if dlgProj.result() == QDialog.Accepted: self.treeView.setTreeItemValues(tHandle) self.treeMeta.updateViewBox(tHandle) self.docEditor.updateDocInfo(tHandle) self.docViewer.updateDocInfo(tHandle) return def rebuildTree(self): """Rebuild the project tree. """ self._makeStatusIcons() self._makeImportIcons() self.treeView.clearTree() self.treeView.buildTree() return def rebuildIndex(self, beQuiet=False): """Rebuild the entire index. """ if not self.hasProject: logger.error("No project open") return False logger.debug("Rebuilding index ...") qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) tStart = time() self.treeView.saveTreeOrder() self.theIndex.clearIndex() theDoc = NWDoc(self.theProject, self) for nDone, tItem in enumerate(self.theProject.projTree): if tItem is not None: self.setStatus("Indexing: '%s'" % tItem.itemName) else: self.setStatus("Indexing: Unknown item") if tItem is not None and tItem.itemType == nwItemType.FILE: logger.verbose("Scanning: %s" % tItem.itemName) theText = theDoc.openDocument(tItem.itemHandle, showStatus=False) # Build tag index self.theIndex.scanText(tItem.itemHandle, theText) # Get Word Counts cC, wC, pC = self.theIndex.getCounts(tItem.itemHandle) tItem.setCharCount(cC) tItem.setWordCount(wC) tItem.setParaCount(pC) self.treeView.propagateCount(tItem.itemHandle, wC) self.treeView.projectWordCount() tEnd = time() self.setStatus("Indexing completed in %.1f ms" % ((tEnd - tStart)*1000.0)) self.docEditor.updateTagHighLighting() qApp.restoreOverrideCursor() if not beQuiet: self.makeAlert("The project index has been successfully rebuilt.", nwAlert.INFO) return True def rebuildOutline(self): """Force a rebuild of the Outline view. """ if not self.hasProject: logger.error("No project open") return False logger.verbose("Forcing a rebuild of the Project Outline") self.tabWidget.setCurrentWidget(self.splitOutline) self.projView.refreshTree(overRide=True) return True ## # Main Dialogs ## def selectProjectPath(self): """Select where to save project. """ dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.ShowDirsOnly dlgOpt |= QFileDialog.DontUseNativeDialog projPath = QFileDialog.getExistingDirectory( self, "Save novelWriter Project", "", options=dlgOpt ) if projPath: return projPath return None def showProjectLoadDialog(self): """Opens the projects dialog for selecting either existing projects from a cache of recently opened projects, or provide a browse button for projects not yet cached. Selecting to create a new project is forwarded to the new project wizard. """ dlgProj = GuiProjectLoad(self) dlgProj.exec_() if dlgProj.result() == QDialog.Accepted: if dlgProj.openState == GuiProjectLoad.OPEN_STATE: self.openProject(dlgProj.openPath) elif dlgProj.openState == GuiProjectLoad.NEW_STATE: self.newProject() return True def showNewProjectDialog(self): """Open the wizard and assemble a project options dict. """ newProj = GuiProjectWizard(self) newProj.exec_() if newProj.result() == QDialog.Accepted: return self._assembleProjectWizardData(newProj) return None def showPreferencesDialog(self): """Open the preferences dialog. """ dlgConf = GuiPreferences(self, self.theProject) dlgConf.exec_() if dlgConf.result() == QDialog.Accepted: logger.debug("Applying new preferences") self.initMain() self.theTheme.updateTheme() self.saveDocument() self.docEditor.initEditor() self.docViewer.initViewer() self.treeView.initTree() self.projView.initOutline() self.projMeta.initDetails() return def showProjectSettingsDialog(self): """Open the project settings dialog. """ if not self.hasProject: logger.error("No project open") return dlgProj = GuiProjectSettings(self, self.theProject) dlgProj.exec_() if dlgProj.result() == QDialog.Accepted: logger.debug("Applying new project settings") self.docEditor.setDictionaries() self._updateWindowTitle(self.theProject.projName) return def showBuildProjectDialog(self): """Open the build project dialog. """ if not self.hasProject: logger.error("No project open") return dlgBuild = getGuiItem("GuiBuildNovel") if dlgBuild is None: dlgBuild = GuiBuildNovel(self, self.theProject) dlgBuild.setModal(False) dlgBuild.show() qApp.processEvents() dlgBuild.viewCachedDoc() return def showWritingStatsDialog(self): """Open the session log dialog. """ if not self.hasProject: logger.error("No project open") return dlgStats = getGuiItem("GuiWritingStats") if dlgStats is None: dlgStats = GuiWritingStats(self, self.theProject) dlgStats.setModal(False) dlgStats.show() qApp.processEvents() dlgStats.populateGUI() return def showAboutNWDialog(self, showNotes=False): """Show the about dialog for novelWriter. """ dlgAbout = GuiAbout(self) dlgAbout.setModal(True) dlgAbout.show() qApp.processEvents() dlgAbout.populateGUI() if showNotes: dlgAbout.showReleaseNotes() return def showAboutQtDialog(self): """Show the about dialog for Qt. """ msgBox = QMessageBox() msgBox.aboutQt(self, "About Qt") return def makeAlert(self, theMessage, theLevel=nwAlert.INFO): """Alert both the user and the logger at the same time. Message can be either a string or an array of strings. """ if isinstance(theMessage, list): popMsg = "<br>".join(theMessage) logMsg = theMessage else: popMsg = theMessage logMsg = [theMessage] # Write to Log if theLevel == nwAlert.INFO: for msgLine in logMsg: logger.info(msgLine) elif theLevel == nwAlert.WARN: for msgLine in logMsg: logger.warning(msgLine) elif theLevel == nwAlert.ERROR: for msgLine in logMsg: logger.error(msgLine) elif theLevel == nwAlert.BUG: for msgLine in logMsg: logger.error(msgLine) # Popup msgBox = QMessageBox() if theLevel == nwAlert.INFO: msgBox.information(self, "Information", popMsg) elif theLevel == nwAlert.WARN: msgBox.warning(self, "Warning", popMsg) elif theLevel == nwAlert.ERROR: msgBox.critical(self, "Error", popMsg) elif theLevel == nwAlert.BUG: popMsg += "<br>This is a bug!" msgBox.critical(self, "Internal Error", popMsg) return def askQuestion(self, theTitle, theQuestion): """Ask the user a Yes/No question. """ msgBox = QMessageBox() msgRes = msgBox.question(self, theTitle, theQuestion) return msgRes == QMessageBox.Yes def reportConfErr(self): """Checks if the Config module has any errors to report, and let the user know if this is the case. The Config module caches errors since it is initialised before the GUI itself. """ if self.mainConf.hasError: self.makeAlert(self.mainConf.getErrData(), nwAlert.ERROR) return True return False ## # Main Window Actions ## def closeMain(self): """Save everything, and close novelWriter. """ if self.hasProject: msgYes = self.askQuestion( "Exit", "Do you want to exit novelWriter?<br>Changes are saved automatically." ) if not msgYes: return False logger.info("Exiting novelWriter") if not self.isFocusMode: self.mainConf.setMainPanePos(self.splitMain.sizes()) self.mainConf.setDocPanePos(self.splitDocs.sizes()) self.mainConf.setOutlinePanePos(self.splitOutline.sizes()) if self.viewMeta.isVisible(): self.mainConf.setViewPanePos(self.splitView.sizes()) self.mainConf.setShowRefPanel(self.viewMeta.isVisible()) self.mainConf.setTreeColWidths(self.treeView.getColumnSizes()) if not self.mainConf.isFullScreen: self.mainConf.setWinSize(self.width(), self.height()) if self.hasProject: self.closeProject(True) self.mainConf.saveConfig() self.reportConfErr() self.mainMenu.closeHelp() qApp.quit() return True def setFocus(self, paneNo): """Switch focus to one of the three main GUI panes. """ if paneNo == 1: self.treeView.setFocus() elif paneNo == 2: self.docEditor.setFocus() elif paneNo == 3: self.docViewer.setFocus() return def closeDocEditor(self): """Close the document edit panel. This does not hide the editor. """ self.closeDocument() self.theProject.setLastEdited(None) return def closeDocViewer(self): """Close the document view panel. """ self.docViewer.clearViewer() self.theProject.setLastViewed(None) bPos = self.splitMain.sizes() self.splitView.setVisible(False) vPos = [bPos[1], 0] self.splitDocs.setSizes(vPos) return not self.splitView.isVisible() def toggleFocusMode(self): """Main GUI Focus Mode hides tree, view pane and optionally also statusbar and menu. """ if self.docEditor.theHandle is None: logger.error("No document open, so not activating Focus Mode") self.mainMenu.aFocusMode.setChecked(self.isFocusMode) return False self.isFocusMode = not self.isFocusMode self.mainMenu.aFocusMode.setChecked(self.isFocusMode) if self.isFocusMode: logger.debug("Activating Focus Mode") self.tabWidget.setCurrentWidget(self.splitDocs) else: logger.debug("Deactivating Focus Mode") isVisible = not self.isFocusMode self.treePane.setVisible(isVisible) self.statusBar.setVisible(isVisible) self.mainMenu.setVisible(isVisible) self.tabWidget.tabBar().setVisible(isVisible) hideDocFooter = self.isFocusMode and self.mainConf.hideFocusFooter self.docEditor.docFooter.setVisible(not hideDocFooter) if self.splitView.isVisible(): self.splitView.setVisible(False) elif self.docViewer.theHandle is not None: self.splitView.setVisible(True) return True def toggleFullScreenMode(self): """Main GUI full screen mode. The mode is tracked by the flag in config. This only tracks whether the window has been maximised using the internal commands, and may not be correct if the user uses the system window manager. Currently, Qt doesn't have access to the exact state of the window. """ self.setWindowState(self.windowState() ^ Qt.WindowFullScreen) winState = self.windowState() & Qt.WindowFullScreen == Qt.WindowFullScreen if winState: logger.debug("Activated full screen mode") else: logger.debug("Deactivated full screen mode") self.mainConf.isFullScreen = winState return ## # Internal Functions ## def _connectMenuActions(self): """Connect to the main window all menu actions that need to be available also when the main menu is hidden. """ # Project self.addAction(self.mainMenu.aSaveProject) self.addAction(self.mainMenu.aExitNW) # Document self.addAction(self.mainMenu.aSaveDoc) self.addAction(self.mainMenu.aFileDetails) # Edit self.addAction(self.mainMenu.aEditUndo) self.addAction(self.mainMenu.aEditRedo) self.addAction(self.mainMenu.aEditCut) self.addAction(self.mainMenu.aEditCopy) self.addAction(self.mainMenu.aEditPaste) self.addAction(self.mainMenu.aSelectAll) self.addAction(self.mainMenu.aSelectPar) # View self.addAction(self.mainMenu.aFocusMode) self.addAction(self.mainMenu.aFullScreen) # Insert self.addAction(self.mainMenu.aInsENDash) self.addAction(self.mainMenu.aInsEMDash) self.addAction(self.mainMenu.aInsEllipsis) self.addAction(self.mainMenu.aInsQuoteLS) self.addAction(self.mainMenu.aInsQuoteRS) self.addAction(self.mainMenu.aInsQuoteLD) self.addAction(self.mainMenu.aInsQuoteRD) self.addAction(self.mainMenu.aInsMSApos) self.addAction(self.mainMenu.aInsHardBreak) self.addAction(self.mainMenu.aInsNBSpace) self.addAction(self.mainMenu.aInsThinSpace) self.addAction(self.mainMenu.aInsThinNBSpace) for mAction, _ in self.mainMenu.mInsKWItems.values(): self.addAction(mAction) # Format self.addAction(self.mainMenu.aFmtEmph) self.addAction(self.mainMenu.aFmtStrong) self.addAction(self.mainMenu.aFmtStrike) self.addAction(self.mainMenu.aFmtDQuote) self.addAction(self.mainMenu.aFmtSQuote) self.addAction(self.mainMenu.aFmtHead1) self.addAction(self.mainMenu.aFmtHead2) self.addAction(self.mainMenu.aFmtHead3) self.addAction(self.mainMenu.aFmtHead4) self.addAction(self.mainMenu.aFmtComment) self.addAction(self.mainMenu.aFmtNoFormat) # Tools self.addAction(self.mainMenu.aSpellCheck) self.addAction(self.mainMenu.aReRunSpell) self.addAction(self.mainMenu.aPreferences) # Help if self.mainConf.hasHelp and self.mainConf.hasAssistant: self.addAction(self.mainMenu.aHelpLoc) self.addAction(self.mainMenu.aHelpWeb) return True def _updateWindowTitle(self, projName=None): """Set the window title and add the project's working title. """ winTitle = self.mainConf.appName if projName is not None: winTitle += " - %s" % projName self.setWindowTitle(winTitle) return True def _autoSaveProject(self): """Triggered by the auto-save project timer to save the project. """ doSave = self.hasProject doSave &= self.theProject.projChanged doSave &= self.theProject.projPath is not None if doSave: logger.debug("Autosaving project") self.saveProject(autoSave=True) return def _autoSaveDocument(self): """Triggered by the auto-save document timer to save the document. """ if self.hasProject and self.docEditor.docChanged: logger.debug("Autosaving document") self.saveDocument() return def _makeStatusIcons(self): """Generate all the item status icons based on project settings. """ self.statusIcons = {} iPx = self.mainConf.pxInt(32) for sLabel, sCol, _ in self.theProject.statusItems: theIcon = QPixmap(iPx, iPx) theIcon.fill(QColor(*sCol)) self.statusIcons[sLabel] = QIcon(theIcon) return def _makeImportIcons(self): """Generate all the item importance icons based on project settings. """ self.importIcons = {} iPx = self.mainConf.pxInt(32) for sLabel, sCol, _ in self.theProject.importItems: theIcon = QPixmap(iPx, iPx) theIcon.fill(QColor(*sCol)) self.importIcons[sLabel] = QIcon(theIcon) return def _assembleProjectWizardData(self, newProj): """Extract the user choices from the New Project Wizard and store them in a dictionary. """ projData = { "projName": newProj.field("projName"), "projTitle": newProj.field("projTitle"), "projAuthors": newProj.field("projAuthors"), "projPath": newProj.field("projPath"), "popSample": newProj.field("popSample"), "popMinimal": newProj.field("popMinimal"), "popCustom": newProj.field("popCustom"), "addRoots": [], "numChapters": 0, "numScenes": 0, "chFolders": False, } if newProj.field("popCustom"): addRoots = [] if newProj.field("addPlot"): addRoots.append(nwItemClass.PLOT) if newProj.field("addChar"): addRoots.append(nwItemClass.CHARACTER) if newProj.field("addWorld"): addRoots.append(nwItemClass.WORLD) if newProj.field("addTime"): addRoots.append(nwItemClass.TIMELINE) if newProj.field("addObject"): addRoots.append(nwItemClass.OBJECT) if newProj.field("addEntity"): addRoots.append(nwItemClass.ENTITY) projData["addRoots"] = addRoots projData["numChapters"] = newProj.field("numChapters") projData["numScenes"] = newProj.field("numScenes") projData["chFolders"] = newProj.field("chFolders") return projData ## # Events ## def closeEvent(self, theEvent): """Capture the closing event of the GUI and call the close function to handle all the close process steps. """ if self.closeMain(): theEvent.accept() else: theEvent.ignore() return ## # Signal Handlers ## def _treeSingleClick(self): """Single click on a project tree item just updates the details panel below the tree. """ sHandle = self.treeView.getSelectedHandle() if sHandle is not None: self.treeMeta.updateViewBox(sHandle) return def _treeDoubleClick(self, tItem, colNo): """The user double-clicked an item in the tree. If it is a file, we open it. Otherwise, we do nothing. """ tHandle = tItem.data(self.treeView.C_NAME, Qt.UserRole) logger.verbose("User double clicked tree item with handle %s" % tHandle) nwItem = self.theProject.projTree[tHandle] if nwItem is not None: if nwItem.itemType == nwItemType.FILE: logger.verbose("Requested item %s is a file" % tHandle) self.openDocument(tHandle, changeFocus=False, doScroll=False) else: logger.verbose("Requested item %s is a folder" % tHandle) return def _treeKeyPressReturn(self): """The user pressed return on an item in the tree. If it is a file, we open it. Otherwise, we do nothing. Pressing return does not change focus to the editor as double click does. """ tHandle = self.treeView.getSelectedHandle() logger.verbose("User pressed return on tree item with handle %s" % tHandle) nwItem = self.theProject.projTree[tHandle] if nwItem is not None: if nwItem.itemType == nwItemType.FILE: logger.verbose("Requested item %s is a file" % tHandle) self.openDocument(tHandle, changeFocus=False, doScroll=False) else: logger.verbose("Requested item %s is a folder" % tHandle) return def _keyPressEscape(self): """When the escape key is pressed somewhere in the main window, do the following, in order: """ if self.docEditor.docSearch.isVisible(): self.docEditor.closeSearch() elif self.isFocusMode: self.toggleFocusMode() return def _mainTabChanged(self, tabIndex): """Activated when the main window tab is changed. """ if tabIndex == self.idxTabEdit: logger.verbose("Editor tab activated") elif tabIndex == self.idxTabProj: logger.verbose("Project outline tab activated") if self.hasProject: self.projView.refreshTree() return