def initUI(self): hbox = QHBoxLayout(self) topleft = QFrame(self) topleft.setMinimumHeight(100) topleft.setFrameShape(QFrame.StyledPanel) topright = QFrame(self) topright.setMinimumHeight(100) topright.setFrameShape(QFrame.StyledPanel) bottom = QFrame(self) bottom.setFrameShape(QFrame.StyledPanel) splitter1 = QSplitter(Qt.Horizontal) splitter1.addWidget(topleft) splitter1.addWidget(topright) splitter2 = QSplitter(Qt.Vertical) splitter2.addWidget(splitter1) splitter2.setCollapsible(0, False) splitter2.addWidget(bottom) hbox.addWidget(splitter2) self.setLayout(hbox) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('QSplitter') self.show()
class LmrManager(QWidget): def __init__(self): super(LmrManager, self).__init__() self.h_layout = QHBoxLayout() self.left_frame = LeftFrame() self.middle_frame = MiddleFrame() self.right_frame = RightFrame() self.mr_splitter = QSplitter() # 缩小三个框直接的缝隙 self.mr_splitter.setHandleWidth(1) self.mr_splitter.insertWidget(0, self.middle_frame) self.mr_splitter.insertWidget(1, self.right_frame) self.mr_splitter.setStretchFactor( 0, 1) # 全屏后保持1:4的比例,但是之前设置的最小宽度此时可能就比较小了 self.mr_splitter.setStretchFactor(1, 4) # 设置为不可拖动至隐藏 self.mr_splitter.setCollapsible(0, False) self.mr_splitter.setCollapsible(1, False) self.h_layout.addWidget(self.left_frame) self.h_layout.addWidget(self.mr_splitter) self.setLayout(self.h_layout)
def __init__(self, parent=None, project_path=None): super().__init__() self.parent = parent mono = MonoPanel(parent) node_list = NodeTree() self.scene = mono.view.scene() left_panel = QWidget() config_button = QPushButton('Global\nConfiguration') config_button.setMinimumHeight(40) config_button.clicked.connect(self.global_config) vlayout = QVBoxLayout() vlayout.addWidget(config_button) vlayout.addWidget(node_list) vlayout.setContentsMargins(0, 0, 0, 0) left_panel.setLayout(vlayout) splitter = QSplitter() splitter.addWidget(left_panel) splitter.addWidget(mono) splitter.setHandleWidth(10) splitter.setCollapsible(0, False) splitter.setCollapsible(1, False) splitter.setStretchFactor(1, 1) mainLayout = QHBoxLayout() mainLayout.addWidget(splitter) self.setLayout(mainLayout) if project_path is not None: self.scene.load(project_path) self.scene.run_all()
class RootWidget(QWidget): def __init__(self): super().__init__() self.splitter = QSplitter(Qt.Horizontal) self.opened_dialog_frame = OpenedDialogWidget() self.dialogs_list_frame = DialogsListRootWidget() self.init_ui() def init_ui(self): layout = QHBoxLayout(self) self.setStyleSheet( ThemeLoader.loaded_theme.apply_to_stylesheet( styles.main_window_style)) self.dialogs_list_frame.setFrameShape(QFrame.StyledPanel) self.dialogs_list_frame.setMinimumWidth(200) self.opened_dialog_frame.setFrameShape(QFrame.StyledPanel) self.opened_dialog_frame.setMinimumWidth(300) self.splitter.addWidget(self.dialogs_list_frame) self.splitter.addWidget(self.opened_dialog_frame) self.splitter.setCollapsible(0, False) self.splitter.setCollapsible(1, False) layout.addWidget(self.splitter) layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout)
def create_layout(self): figure = Figure((8.0, 6.0), dpi=100) self.canvas = FigureCanvas(figure) main_view = QWidget() self.canvas.setParent(main_view) self.axes = figure.add_subplot(111) mpl_toolbar = NavigationToolbar(self.canvas, main_view) self.qcb_show_points.setChecked(DISPLAY_POINTS) self.qcb_show_points.stateChanged.connect(self.on_show) self.qcb_show_legend.setChecked(DISPLAY_LEGEND) self.qcb_show_legend.stateChanged.connect(self.on_show) self.qpb_show.clicked.connect(self.on_show) qvb_graph = QVBoxLayout() qvb_graph.addWidget(self.canvas) qvb_graph.addWidget(mpl_toolbar) qw_graph = QWidget() qw_graph.setLayout(qvb_graph) qf_separator = QFrame() qf_separator.setFrameShape(QFrame.HLine) qhb_time_unit = QHBoxLayout() for unit in TIME_UNITS.keys(): qrb_unit = QRadioButton(unit) if self.parent.time_unit == unit: qrb_unit.setChecked(True) else: qrb_unit.setChecked(False) self.qbg_time_unit.addButton(qrb_unit) qhb_time_unit.addWidget(qrb_unit) self.qbg_time_unit.buttonClicked.connect(self.time_unit_changed) self.qvb_options.addStretch(1) self.qvb_options.addWidget(qf_separator) self.qvb_options.addStretch(1) self.qvb_options.addLayout(qhb_time_unit) self.qvb_options.addWidget(self.qcb_show_points) self.qvb_options.addWidget(self.qcb_show_legend) self.qvb_options.addWidget(self.qpb_show) qw_options = QWidget() qw_options.setLayout(self.qvb_options) splitter = QSplitter() splitter.addWidget(qw_options) splitter.addWidget(qw_graph) splitter.setHandleWidth(10) splitter.setCollapsible(0, False) splitter.setCollapsible(1, False) splitter.setStretchFactor(1, 1) main_layout = QHBoxLayout() main_layout.addWidget(splitter) self.setLayout(main_layout)
def __init__(self): super().__init__() self.language = settings.LANG self.csv_separator = settings.CSV_SEPARATOR self.fmt_float = settings.FMT_FLOAT self.logging_level = settings.LOGGING_LEVEL self.panel = MainPanel(self) config_button = QPushButton('Global\nConfiguration') config_button.setMinimumHeight(40) config_button.clicked.connect(self.global_config) pageList = QListWidget() for name in ['Start', 'Extract variables', 'Max/Min/Mean/Arrival/Duration', 'Interpolate on points', 'Interpolate along lines', 'Project along lines', 'Project mesh', 'Compute volume', 'Compute flux', 'Compare two results', 'Transform coordinate systems', 'Convert geom file formats', 'Variable Calculator']: pageList.addItem('\n' + name + '\n') pageList.setFlow(QListView.TopToBottom) pageList.currentRowChanged.connect(self.panel.layout().setCurrentIndex) pageList.setCurrentRow(0) splitter = QSplitter() left_widget = QWidget() vlayout = QVBoxLayout() vlayout.addWidget(config_button) vlayout.addWidget(pageList) left_widget.setLayout(vlayout) splitter.addWidget(left_widget) splitter.addWidget(self.panel) splitter.setHandleWidth(5) splitter.setCollapsible(0, False) splitter.setCollapsible(1, False) handle = splitter.handle(1) layout = QVBoxLayout() layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) line = QFrame() line.setFrameShape(QFrame.VLine) line.setFrameShadow(QFrame.Sunken) layout.addWidget(line) handle.setLayout(layout) mainLayout = QHBoxLayout() mainLayout.addWidget(splitter) mainLayout.setContentsMargins(0, 0, 0, 0) self.setLayout(mainLayout) self.setWindowTitle('PyTelTools :: Classic interface') self.setWindowFlags(self.windowFlags() | Qt.CustomizeWindowHint) self.frameGeom = self.frameGeometry() self.move(self.frameGeom.center())
def loadSplitter(self): w1 = QWidget() w2 = QWidget() w1.setLayout(self.firstLayout) w2.setLayout(self.secondLayout) s = QSplitter(Qt.Horizontal) s.addWidget(w1) s.addWidget(w2) s.setCollapsible(0, False) s.setCollapsible(1, False) s.setStretchFactor(1, 1) self.setCentralWidget(s)
def __init__(self, parent=None, project_path=None, ncsize=settings.NCSIZE): super().__init__() self.parent = parent self.table = MultiTable() self.view = MultiView(self, self.table) self.scene = self.view.scene() self.toolbar = QToolBar() self.save_act = QAction('Save\n(Ctrl+S)', self, triggered=self.save, shortcut='Ctrl+S') self.run_act = QAction('Run\n(F5)', self, triggered=self.run, shortcut='F5') self.init_toolbar() if project_path is not None: self.message_box = CmdMessage() else: self.message_box = QPlainTextEdit() self.message_box.setReadOnly(True) # right panel with table and message_box right_panel = QSplitter(Qt.Vertical) right_panel.addWidget(self.table) right_panel.addWidget(self.message_box) right_panel.setHandleWidth(10) right_panel.setCollapsible(0, False) right_panel.setCollapsible(1, False) right_panel.setSizes([200, 200]) # left panel left_panel = QWidget() layout = QVBoxLayout() layout.addWidget(self.toolbar) layout.addWidget(self.view) layout.setContentsMargins(0, 0, 0, 0) left_panel.setLayout(layout) splitter = QSplitter(Qt.Horizontal) splitter.addWidget(left_panel) splitter.addWidget(right_panel) splitter.setHandleWidth(10) splitter.setCollapsible(0, False) splitter.setCollapsible(1, False) splitter.setSizes([500, 300]) mainLayout = QHBoxLayout() mainLayout.addWidget(splitter) self.setLayout(mainLayout) self.ncsize = ncsize self.worker = worker.Workers(self.ncsize) if project_path is not None: self.scene.load(project_path) self.run()
def initUI(self): self.screen_label = QLabel() channels_widget = QWidget() self.channels_layout = QGridLayout() channels_widget.setLayout(self.channels_layout) show_data_screen_widget = QCheckBox( "Show processed images (might slow down processing)") show_data_screen_widget.setChecked(True) show_data_screen_widget.stateChanged.connect(self.data_screen_toggled) screen_layout = QVBoxLayout() screen_layout_widget = QWidget() screen_layout.addWidget(self.screen_label) screen_layout.addWidget(show_data_screen_widget) screen_layout_widget.setLayout(screen_layout) channels_screen_splitter = QSplitter(Qt.Horizontal) channels_screen_splitter.addWidget(screen_layout_widget) channels_screen_splitter.addWidget(channels_widget) channels_screen_splitter.setSizes([400, 200]) channels_screen_splitter.setCollapsible(1, False) self.process_button = QPushButton("Process") self.process_button.toggle() self.process_button.setCheckable(True) self.process_button.clicked.connect(self.toggle_process_button) self.process_button.setEnabled(False) data_folder_selection = self.init_data_folder_selection() model_selection = self.init_model_selection() menu_widget = QWidget() menu_layout = QGridLayout() menu_layout.setColumnStretch(1, 1) menu_layout.setColumnStretch(2, 1) menu_layout.setColumnStretch(3, 3) menu_layout.addWidget(QLabel("Select raw data folder"), 1, 1, Qt.AlignRight) menu_layout.addWidget(data_folder_selection, 1, 2, Qt.AlignRight) menu_layout.addWidget(QLabel("Select target model"), 2, 1, Qt.AlignRight) menu_layout.addWidget(model_selection, 2, 2, Qt.AlignRight) menu_layout.addWidget(self.process_button, 3, 1) menu_widget.setLayout(menu_layout) main_layout = QVBoxLayout() main_layout.addWidget(channels_screen_splitter) main_layout.addWidget(menu_widget) self.setLayout(main_layout)
def __init__(self): QMainWindow.__init__(self, flags=Qt.Window) # Set menu bar menu_bar = self.menuBar() file = menu_bar.addMenu("&File") new_action = QAction('&New Cluster', self, shortcut='Ctrl+N', triggered=self.new_cluster) exit_action = QAction('&Exit', self, shortcut='Alt+F4', triggered=self.close) file.addAction(new_action) file.addSeparator() file.addAction(exit_action) # Set core widgets self.conf_widget = QListView(self) self.conf_widget.setMinimumWidth(150) self.conf_widget.clicked.connect(self.load_cluster) self.context_widget = QTabWidget(self) self.context_widget.setMinimumWidth(100) central_widget = QSplitter(Qt.Horizontal, self) central_widget.addWidget(self.conf_widget) central_widget.addWidget(self.context_widget) central_widget.setCollapsible(0, False) central_widget.setCollapsible(1, False) central_widget.setSizes([150, 999]) self.setCentralWidget(central_widget) # Set UI self.setWindowIcon(QIcon(':/icons/fire_damage.ico')) self.setWindowTitle(WINDOW_TITLE) self.setMinimumSize(600, 400) # Set vars self.config_manager = ConfigurationManager() self.load_configuration() self.loaded_clusters = []
def __init__(self, master, cb_update_subscriptions): super().__init__(master) grid = QGridLayout() grid.setContentsMargins(0, 0, 0, 0) self.cbUpdateSubscriptions = cb_update_subscriptions self.curveCommandList = [] self.scatterCommandList = [] for command in COMMAND_LIST: if command.type == CommandType.SUBSCRIPTION_CURVE_DATA: self.curveCommandList.append(command) elif command.type == CommandType.SUBSCRIPTION_SCATTER_DATA: self.scatterCommandList.append(command) scrollArea = QScrollArea(self) scrollArea.setFrameShape(QFrame.StyledPanel) scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.graphSettings = GraphSettings(scrollArea, self.update_displayed_data, self.curveCommandList, self.scatterCommandList) scrollArea.setWidgetResizable(True) scrollArea.setWidget(self.graphSettings) self.graphCurveArea = GraphCurveArea(self, self.curveCommandList) self.graphCurveArea.setFrameShape(QFrame.StyledPanel) self.graphScatterArea = GraphScatterArea(self, self.scatterCommandList) self.graphScatterArea.setFrameShape(QFrame.StyledPanel) subSplitter = QSplitter(Qt.Horizontal) subSplitter.addWidget(self.graphCurveArea) subSplitter.addWidget(self.graphScatterArea) splitter = QSplitter(Qt.Vertical) splitter.addWidget(scrollArea) splitter.addWidget(subSplitter) splitter.setCollapsible(1, False) splitter.setStretchFactor(1, 1) grid.addWidget(splitter) self.setLayout(grid) self.update_displayed_data()
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 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 NavigationBar(QWidget): """ Class implementing the navigation bar. """ def __init__(self, mainWindow, parent=None): """ Constructor @param mainWindow reference to the browser main window @type WebBrowserWindow @param parent reference to the parent widget @type QWidget """ super(NavigationBar, self).__init__(parent) self.setObjectName("navigationbar") self.__mw = mainWindow self.__layout = QHBoxLayout(self) margin = self.style().pixelMetric(QStyle.PM_ToolBarItemMargin, None, self) self.__layout.setContentsMargins(margin, margin, margin, margin) self.__layout.setSpacing(self.style().pixelMetric( QStyle.PM_ToolBarItemSpacing, None, self)) self.setLayout(self.__layout) self.__backButton = E5ToolButton(self) self.__backButton.setObjectName("navigation_back_button") self.__backButton.setToolTip(self.tr("Move one screen backward")) self.__backButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.__backButton.setFocusPolicy(Qt.NoFocus) self.__backButton.setAutoRaise(True) self.__backButton.setIcon(UI.PixmapCache.getIcon("back.png")) self.__backButton.setEnabled(False) self.__forwardButton = E5ToolButton(self) self.__forwardButton.setObjectName("navigation_forward_button") self.__forwardButton.setToolTip(self.tr("Move one screen forward")) self.__forwardButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.__forwardButton.setFocusPolicy(Qt.NoFocus) self.__forwardButton.setAutoRaise(True) self.__forwardButton.setIcon(UI.PixmapCache.getIcon("forward.png")) self.__forwardButton.setEnabled(False) self.__backNextLayout = QHBoxLayout() self.__backNextLayout.setContentsMargins(0, 0, 0, 0) self.__backNextLayout.setSpacing(0) self.__backNextLayout.addWidget(self.__backButton) self.__backNextLayout.addWidget(self.__forwardButton) self.__reloadStopButton = ReloadStopButton(self) self.__homeButton = E5ToolButton(self) self.__homeButton.setObjectName("navigation_home_button") self.__homeButton.setToolTip(self.tr("Move to the initial screen")) self.__homeButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.__homeButton.setFocusPolicy(Qt.NoFocus) self.__homeButton.setAutoRaise(True) self.__homeButton.setIcon(UI.PixmapCache.getIcon("home.png")) self.__exitFullScreenButton = E5ToolButton(self) self.__exitFullScreenButton.setObjectName( "navigation_exitfullscreen_button") self.__exitFullScreenButton.setIcon( UI.PixmapCache.getIcon("windowRestore.png")) self.__exitFullScreenButton.setToolTip(self.tr("Exit Fullscreen")) self.__exitFullScreenButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.__exitFullScreenButton.setFocusPolicy(Qt.NoFocus) self.__exitFullScreenButton.setAutoRaise(True) self.__exitFullScreenButton.clicked.connect(self.__mw.toggleFullScreen) self.__exitFullScreenButton.setVisible(False) self.__downloadManagerButton = DownloadManagerButton(self) self.__superMenuButton = E5ToolButton(self) self.__superMenuButton.setObjectName("navigation_supermenu_button") self.__superMenuButton.setIcon(UI.PixmapCache.getIcon("superMenu.png")) self.__superMenuButton.setToolTip(self.tr("Main Menu")) self.__superMenuButton.setPopupMode(QToolButton.InstantPopup) self.__superMenuButton.setToolButtonStyle(Qt.ToolButtonIconOnly) self.__superMenuButton.setFocusPolicy(Qt.NoFocus) self.__superMenuButton.setAutoRaise(True) self.__superMenuButton.setShowMenuInside(True) self.__navigationSplitter = QSplitter(self) urlBar = self.__mw.tabWidget().stackedUrlBar() self.__navigationSplitter.addWidget(urlBar) from WebBrowser.WebBrowserWebSearchWidget import ( WebBrowserWebSearchWidget) self.__searchEdit = WebBrowserWebSearchWidget(self.__mw, self) sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(2) sizePolicy.setVerticalStretch(0) self.__searchEdit.setSizePolicy(sizePolicy) self.__searchEdit.search.connect(self.__mw.openUrl) self.__navigationSplitter.addWidget(self.__searchEdit) self.__navigationSplitter.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) self.__navigationSplitter.setCollapsible(0, False) self.__layout.addLayout(self.__backNextLayout) self.__layout.addWidget(self.__reloadStopButton) self.__layout.addWidget(self.__homeButton) self.__layout.addWidget(self.__navigationSplitter) self.__layout.addWidget(self.__downloadManagerButton) self.__layout.addWidget(self.__exitFullScreenButton) self.__layout.addWidget(self.__superMenuButton) self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.__contextMenuRequested) self.__backMenu = QMenu(self) self.__backMenu.triggered.connect(self.__navigationMenuActionTriggered) self.__backButton.setMenu(self.__backMenu) self.__backButton.aboutToShowMenu.connect(self.__showBackMenu) self.__forwardMenu = QMenu(self) self.__forwardMenu.triggered.connect( self.__navigationMenuActionTriggered) self.__forwardButton.setMenu(self.__forwardMenu) self.__forwardButton.aboutToShowMenu.connect(self.__showForwardMenu) self.__backButton.clicked.connect(self.__goBack) self.__backButton.middleClicked.connect(self.__goBackInNewTab) self.__backButton.controlClicked.connect(self.__goBackInNewTab) self.__forwardButton.clicked.connect(self.__goForward) self.__forwardButton.middleClicked.connect(self.__goForwardInNewTab) self.__forwardButton.controlClicked.connect(self.__goForwardInNewTab) self.__reloadStopButton.reloadClicked.connect(self.__reload) self.__reloadStopButton.stopClicked.connect(self.__stopLoad) self.__homeButton.clicked.connect(self.__goHome) self.__homeButton.middleClicked.connect(self.__goHomeInNewTab) self.__homeButton.controlClicked.connect(self.__goHomeInNewTab) def superMenuButton(self): """ Public method to get a reference to the super menu button. @return reference to the super menu button @rtype QToolButton """ return self.__superMenuButton def backButton(self): """ Public method to get a reference to the back button. @return reference to the back button @rtype QToolButton """ return self.__backButton def forwardButton(self): """ Public method to get a reference to the forward button. @return reference to the forward button @rtype QToolButton """ return self.__forwardButton def reloadStopButton(self): """ Public method to get a reference to the reload/stop button. @return reference to the reload/stop button @rtype QToolButton """ return self.__reloadStopButton def exitFullScreenButton(self): """ Public method to get a reference to the exit full screen button. @return reference to the exit full screen button @rtype QToolButton """ return self.__exitFullScreenButton def searchEdit(self): """ Public method to get a reference to the web search edit. @return reference to the web search edit @rtype WebBrowserWebSearchWidget """ return self.__searchEdit def __showBackMenu(self): """ Private slot showing the backwards navigation menu. """ self.__backMenu.clear() history = self.__mw.currentBrowser().history() historyCount = history.count() backItems = history.backItems(historyCount) count = 0 for index in range(len(backItems) - 1, -1, -1): item = backItems[index] act = QAction(self) act.setData(-1 * (index + 1)) icon = WebBrowserWindow.icon(item.url()) act.setIcon(icon) act.setText(item.title()) self.__backMenu.addAction(act) count += 1 if count == 20: break self.__backMenu.addSeparator() self.__backMenu.addAction(self.tr("Clear History"), self.__clearHistory) def __showForwardMenu(self): """ Private slot showing the forwards navigation menu. """ self.__forwardMenu.clear() history = self.__mw.currentBrowser().history() historyCount = history.count() forwardItems = history.forwardItems(historyCount) count = 0 for index in range(len(forwardItems)): item = forwardItems[index] act = QAction(self) act.setData(index + 1) icon = WebBrowserWindow.icon(item.url()) act.setIcon(icon) act.setText(item.title()) self.__forwardMenu.addAction(act) count += 1 if count == 20: break self.__forwardMenu.addSeparator() self.__forwardMenu.addAction(self.tr("Clear History"), self.__clearHistory) def __navigationMenuActionTriggered(self, act): """ Private slot to go to the selected page. @param act reference to the action selected in the navigation menu (QAction) """ offset = act.data() if offset is not None: history = self.__mw.currentBrowser().history() historyCount = history.count() if offset < 0: # go back history.goToItem( history.backItems(historyCount)[-1 * offset - 1]) else: # go forward history.goToItem( history.forwardItems(historyCount)[offset - 1]) def __goBack(self): """ Private slot called to handle the backward button. """ self.__mw.currentBrowser().backward() def __goBackInNewTab(self): """ Private slot handling a middle click or Ctrl left click of the backward button. """ history = self.__mw.currentBrowser().history() if history.canGoBack(): backItem = history.backItem() self.__mw.newTab(link=backItem.url(), addNextTo=self.__mw.currentBrowser(), background=True) def __goForward(self): """ Private slot called to handle the forward button. """ self.__mw.currentBrowser().forward() def __goForwardInNewTab(self): """ Private slot handling a middle click or Ctrl left click of the forward button. """ history = self.__mw.currentBrowser().history() if history.canGoForward(): forwardItem = history.forwardItem() self.__mw.newTab(link=forwardItem.url(), addNextTo=self.__mw.currentBrowser(), background=True) def __goHome(self): """ Private slot called to handle the home button. """ self.__mw.currentBrowser().home() def __goHomeInNewTab(self): """ Private slot handling a middle click or Ctrl left click of the home button. """ homeUrl = QUrl(Preferences.getWebBrowser("HomePage")) self.__mw.newTab(link=homeUrl, addNextTo=self.__mw.currentBrowser(), background=True) def __reload(self): """ Private slot called to handle the reload button. """ self.__mw.currentBrowser().reloadBypassingCache() def __stopLoad(self): """ Private slot called to handle loading of the current page. """ self.__mw.currentBrowser().stop() def __clearHistory(self): """ Private slot to clear the history of the current web browser tab. """ cb = self.__mw.currentBrowser() if cb is not None: cb.history().clear() self.__mw.setForwardAvailable(cb.isForwardAvailable()) self.__mw.setBackwardAvailable(cb.isBackwardAvailable()) def __contextMenuRequested(self, pos): """ Private method to handle a context menu request. @param pos position of the request @type QPoint """ menu = self.__mw.createPopupMenu() menu.exec_(self.mapToGlobal(pos))
def __initUI__(self): # ---- TAB WIDGET # download weather data : splash.showMessage("Initializing download weather data...") self.tab_dwnld_data = DwnldWeatherWidget(self) self.tab_dwnld_data.set_workdir(self.projectdir) # gapfill weather data : splash.showMessage("Initializing gapfill weather data...") self.tab_fill_weather_data = GapFillWeatherGUI(self) self.tab_fill_weather_data.set_workdir(self.projectdir) # hydrograph : splash.showMessage("Initializing plot hydrograph...") self.tab_hydrograph = HydroPrint.HydroprintGUI(self.dmanager) splash.showMessage("Initializing analyse hydrograph...") self.tab_hydrocalc = HydroCalc.WLCalc(self.dmanager) self.tab_hydrocalc.sig_new_mrc.connect( self.tab_hydrograph.mrc_wl_changed) self.tab_hydrocalc.rechg_eval_widget.sig_new_gluedf.connect( self.tab_hydrograph.glue_wl_changed) # ---- TABS ASSEMBLY self.tab_widget = TabWidget() self.tab_widget.addTab(self.tab_dwnld_data, 'Download Weather') self.tab_widget.addTab(self.tab_fill_weather_data, 'Gapfill Weather') self.tab_widget.addTab(self.tab_hydrograph, 'Plot Hydrograph') self.tab_widget.addTab(self.tab_hydrocalc, 'Analyze Hydrograph') self.tab_widget.setCornerWidget(self.pmanager) self.tab_widget.currentChanged.connect(self.sync_datamanagers) # ---- Main Console splash.showMessage("Initializing main window...") self.main_console = QTextEdit() self.main_console.setReadOnly(True) self.main_console.setLineWrapMode(QTextEdit.NoWrap) style = 'Regular' family = StyleDB().fontfamily size = self.whatPref.fontsize_console fontSS = ('font-style: %s;' 'font-size: %s;' 'font-family: %s;') % (style, size, family) self.main_console.setStyleSheet("QWidget{%s}" % fontSS) msg = '<font color=black>Thanks for using %s.</font>' % __appname__ self.write2console(msg) self.write2console('<font color=black>' 'Please report any bug or wishful feature at' ' [email protected].' '</font>') # ---- Signal Piping issuer = self.tab_dwnld_data issuer.ConsoleSignal.connect(self.write2console) issuer = self.tab_fill_weather_data issuer.ConsoleSignal.connect(self.write2console) issuer = self.tab_hydrograph issuer.ConsoleSignal.connect(self.write2console) # ---- Splitter Widget splitter = QSplitter(self) splitter.setOrientation(Qt.Vertical) splitter.addWidget(self.tab_widget) splitter.addWidget(self.main_console) splitter.setCollapsible(0, True) splitter.setStretchFactor(0, 100) # Forces initially the main_console to its minimal height: splitter.setSizes([100, 1]) # ---- Main Grid main_widget = QWidget() self.setCentralWidget(main_widget) mainGrid = QGridLayout(main_widget) mainGrid.addWidget(splitter, 0, 0) mainGrid.addWidget(self.tab_fill_weather_data.pbar, 1, 0) mainGrid.addWidget(self.tab_dwnld_data.pbar, 2, 0) mainGrid.addWidget(self.tab_hydrocalc.rechg_eval_widget.progressbar, 3, 0)
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, parent=None): super(UIMainWindow, self).__init__(parent) currentDir = os.getcwd() dataDir = os.path.join(currentDir, "app_data") workFileDir = os.path.join(dataDir, "workfiles") self._interface_lang_file = os.path.join( workFileDir, 'interface_language_setting.txt') self._interface_lang_dict = os.path.join( workFileDir, 'interface_language_dict.json') self.fc_lg, self.fc_dict = self.set_lang() # region create window aboutAction = QAction(self.fc_dict['menu_about_item'][self.fc_lg], self) aboutAction.triggered.connect(self._info) aboutAction.setStatusTip(self.fc_dict["menu_about_tip"][self.fc_lg]) outTxtAction = QAction(self.fc_dict["menu_output_txt"][self.fc_lg], self) outTxtAction.triggered.connect(self.save_text) outTxtAction.setStatusTip( self.fc_dict["menu_output_txt_tip"][self.fc_lg]) outHtmlAction = QAction(self.fc_dict["menu_output_html"][self.fc_lg], self) outHtmlAction.triggered.connect(self.save_html) outHtmlAction.setStatusTip( self.fc_dict["menu_output_html_tip"][self.fc_lg]) menubar = self.menuBar() menubar.setContextMenuPolicy(Qt.PreventContextMenu) fileMenu = menubar.addMenu(self.fc_dict['menu_file'][self.fc_lg]) fileMenu_saveGroup = fileMenu.addMenu( self.fc_dict["menu_output"][self.fc_lg]) fileMenu_saveGroup.addAction(outTxtAction) fileMenu_saveGroup.addAction(outHtmlAction) infoMenu = menubar.addMenu(self.fc_dict['menu_about'][self.fc_lg]) infoMenu.addAction(aboutAction) self._left_frame_layout = QVBoxLayout() self._left_frame_a = QGroupBox( self.fc_dict["corpora_list"][self.fc_lg]) self._left_frame_a.setMaximumWidth(250) self._left_frame_a_layout = QVBoxLayout() self._json_list_window = QListWidget() # setStatusTip or setToolTip self._json_list_window.setToolTip( self.fc_dict["corpora_list_tip"][self.fc_lg]) self._json_list_window.setMaximumWidth(240) self._json_list_window.setSortingEnabled(True) self._json_list_window.setSelectionMode( QAbstractItemView.ExtendedSelection) self._json_list_window.setContextMenuPolicy(Qt.CustomContextMenu) self._left_frame_a_layout.addWidget(self._json_list_window) self._left_frame_a.setLayout(self._left_frame_a_layout) self._left_frame_b = QGroupBox( self.fc_dict["conc_options"][self.fc_lg]) self._left_frame_b.setMaximumWidth(250) self._left_frame_b_layout = QVBoxLayout() self._input_layout = QHBoxLayout() self._input_box = QLineEdit() self._input_box.setFixedWidth(150) self._input_button = QPushButton( self.fc_dict["src_button"][self.fc_lg]) self._input_button.clicked.connect(self.search) self._input_button.setFixedWidth(80) self._input_layout.addWidget(self._input_box) self._input_layout.addWidget(self._input_button) self._src_mode = QButtonGroup() self._src_mode_list = QLabel(self.fc_dict["conc_mode"][self.fc_lg]) self._src_mode_1 = QRadioButton( self.fc_dict["conc_mode_gm"][self.fc_lg]) self._src_mode_1.setToolTip( self.fc_dict["conc_mode_gm_tip"][self.fc_lg]) self._src_mode_2 = QRadioButton( self.fc_dict["conc_mode_em"][self.fc_lg]) self._src_mode_2.setToolTip( self.fc_dict["conc_mode_em_tip"][self.fc_lg]) self._src_mode_2.setChecked(True) self._src_mode_3 = QRadioButton( self.fc_dict["conc_mode_regex"][self.fc_lg]) self._src_mode_3.setToolTip( self.fc_dict["conc_mode_regex_tip"][self.fc_lg]) self._src_mode.addButton(self._src_mode_1) self._src_mode.addButton(self._src_mode_2) self._src_mode.addButton(self._src_mode_3) self._src_mode_layout = QGridLayout() self._src_mode_layout.addWidget(self._src_mode_list, 0, 0) self._src_mode_layout.addWidget(self._src_mode_1, 1, 0) self._src_mode_layout.addWidget(self._src_mode_2, 1, 1) self._src_mode_layout.addWidget(self._src_mode_3, 1, 2) self._src_category = QButtonGroup() self._src_scope_list = QLabel(self.fc_dict["conc_scope"][self.fc_lg]) self._src_scope_1 = QRadioButton( self.fc_dict["conc_scope_all"][self.fc_lg]) self._src_scope_1.setToolTip( self.fc_dict["conc_scope_all_tip"][self.fc_lg]) self._src_scope_1.setChecked(True) self._src_scope_2 = QRadioButton( self.fc_dict["conc_scope_this"][self.fc_lg]) self._src_scope_2.setToolTip( self.fc_dict["conc_scope_this_tip"][self.fc_lg]) self._src_scope_3 = QComboBox() self._src_scope_3.setEnabled(False) self._src_scope_3.addItem(self.fc_dict["conc_scope_tls"][self.fc_lg]) self._src_scope_3.addItem( self.fc_dict["conc_scope_this_tl"][self.fc_lg]) self._src_scope_3.setCurrentIndex(0) self._src_author_btn = QRadioButton( self.fc_dict["conc_author"][self.fc_lg]) self._src_author_opt = QComboBox() self._src_author_opt.setEnabled(False) self._src_translator_btn = QRadioButton( self.fc_dict["conc_translator"][self.fc_lg]) self._src_translator_opt = QComboBox() self._src_translator_opt.setEnabled(False) self._src_genre_btn = QRadioButton( self.fc_dict["conc_genre"][self.fc_lg]) self._src_genre_opt = QComboBox() self._src_genre_opt.setEnabled(False) self._src_category.addButton(self._src_scope_1) self._src_category.addButton(self._src_scope_2) self._src_category.addButton(self._src_author_btn) self._src_category.addButton(self._src_translator_btn) self._src_category.addButton(self._src_genre_btn) self._display_context_label = QLabel( self.fc_dict["display_opt"][self.fc_lg]) self._display_context_button = QCheckBox( self.fc_dict["display_opt_context"][self.fc_lg]) self._display_context_button.setToolTip( self.fc_dict["display_opt_context_tip"][self.fc_lg]) self._display_context_button.setChecked(False) self._display_context_choice = QComboBox() self._display_context_choice.setEnabled(False) self._display_context_choice.addItem( self.fc_dict["display_opt_context_sl"][self.fc_lg]) self._display_context_choice.addItem( self.fc_dict["display_opt_context_tl"][self.fc_lg]) self._display_context_choice.addItem( self.fc_dict["display_opt_context_bi"][self.fc_lg]) self._display_context_choice.setCurrentIndex(0) self._display_source_button = QCheckBox( self.fc_dict["hide_source"][self.fc_lg]) self._display_source_button.setToolTip( self.fc_dict["hide_source_tip"][self.fc_lg]) self._display_source_button.setChecked(False) self._display_source_choice = QComboBox() self._display_source_choice.setEnabled(False) self._display_source_choice.addItem( self.fc_dict["hide_source_ar"][self.fc_lg]) self._display_source_choice.addItem( self.fc_dict["hide_source_tr"][self.fc_lg]) self._display_source_choice.addItem( self.fc_dict["hide_source_ar_tr"][self.fc_lg]) self._display_source_choice.addItem( self.fc_dict["hide_source_title"][self.fc_lg]) self._display_source_choice.setCurrentIndex(0) self._src_scope_layout = QGridLayout() self._src_scope_layout.addWidget(self._src_scope_list, 0, 0) self._src_scope_layout.addWidget(self._src_scope_1, 1, 0) self._src_scope_layout.addWidget(self._src_scope_2, 1, 1) self._src_scope_layout.addWidget(self._src_scope_3, 1, 2) self._src_scope_layout.addWidget(self._src_author_btn, 2, 0) self._src_scope_layout.addWidget(self._src_author_opt, 2, 1, 1, 2) self._src_scope_layout.addWidget(self._src_translator_btn, 3, 0) self._src_scope_layout.addWidget(self._src_translator_opt, 3, 1, 1, 2) self._src_scope_layout.addWidget(self._src_genre_btn, 4, 0) self._src_scope_layout.addWidget(self._src_genre_opt, 4, 1, 1, 2) self._src_scope_layout.addWidget(self._display_context_label, 5, 0) self._src_scope_layout.addWidget(self._display_context_button, 5, 1) self._src_scope_layout.addWidget(self._display_context_choice, 5, 2) self._src_scope_layout.addWidget(self._display_source_button, 6, 1) self._src_scope_layout.addWidget(self._display_source_choice, 6, 2) self._left_frame_b_layout.addLayout(self._src_mode_layout) self._left_frame_b_layout.addLayout(self._src_scope_layout) self._left_frame_b.setLayout(self._left_frame_b_layout) self._left_frame_layout.addWidget(self._left_frame_a) self._left_frame_layout.addLayout(self._input_layout) self._left_frame_layout.addWidget(self._left_frame_b) self._json_info_form = QGroupBox( self.fc_dict["corpus_pro"][self.fc_lg]) self._json_info_form.setAlignment(Qt.AlignRight) self._book_header_layout = QGridLayout() self._ss_book_title = QLabel( self.fc_dict["corpus_pro_title"][self.fc_lg]) self._ss_book_titleBox = QLineEdit() self._ss_book_author = QLabel( self.fc_dict["corpus_pro_author"][self.fc_lg]) self._ss_book_authorBox = QLineEdit() self._ss_book_date = QLabel( self.fc_dict["corpus_pro_date"][self.fc_lg]) self._ss_book_dateBox = QLineEdit() #缩小宽度,以防止contentsBox出现左右拉横条 self._ss_book_dateBox.setFixedWidth(115) self._ss_book_genre = QLabel( self.fc_dict["corpus_pro_genre"][self.fc_lg]) self._ss_book_genreBox = QLineEdit() #缩小宽度,以防止contentsBox出现左右拉横条 self._ss_book_genreBox.setFixedWidth(115) self._ss_book_contentsBox = QListWidget() self._ss_book_contentsBox.setToolTip( self.fc_dict["corpus_pro_content_tip"][self.fc_lg]) #self.ss_book_contentsBox.setSortingEnabled(True) # 排序会导致章节排序错误 #限定高度 self._ss_book_contentsBox.setFixedHeight(100) self._ss_book_contentsBox.setSelectionMode( QAbstractItemView.ExtendedSelection) self._ss_book_contentsBox.setContextMenuPolicy(Qt.CustomContextMenu) self._tt_book_list = QLabel( self.fc_dict["corpus_pro_tlvn"][self.fc_lg]) self._tt_book_listBox = QComboBox() self._tt_book_listBox.setToolTip( self.fc_dict["corpus_pro_tlvn_tip"][self.fc_lg]) self._book_header_layout.addWidget(self._ss_book_title, 0, 0) self._book_header_layout.addWidget(self._ss_book_titleBox, 0, 1, 1, 3) self._book_header_layout.addWidget(self._ss_book_author, 1, 0) self._book_header_layout.addWidget(self._ss_book_authorBox, 1, 1, 1, 3) self._book_header_layout.addWidget(self._ss_book_date, 2, 0) self._book_header_layout.addWidget(self._ss_book_dateBox, 2, 1) self._book_header_layout.addWidget(self._ss_book_genre, 2, 2) self._book_header_layout.addWidget(self._ss_book_genreBox, 2, 3) self._book_header_layout.addWidget(self._tt_book_list, 3, 0) self._book_header_layout.addWidget(self._tt_book_listBox, 3, 1, 1, 3) self._book_header_layout.addWidget(self._ss_book_contentsBox, 0, 4, 4, 1) self._json_info_form.setLayout(self._book_header_layout) self._src_result_form = QGroupBox( self.fc_dict["conc_result"][self.fc_lg]) self._src_result_form.setAlignment(Qt.AlignCenter) # 用QTextBrowser取代QWebEngineView以减小软件体积 # self._result_window = QWebEngineView(parent) self._result_window = QTextBrowser(parent) self._result_window.setFrameStyle(0) # FrameStyle: NoFrame = 0 Box = 1 Panel = 2 WinPanel = 3 HLine = 4 # VLine = 5 StyledPanel = 6 Plain = 16 Raised = 32 Sunken = 48 self._src_result_formLayout = QHBoxLayout() self._src_result_formLayout.addWidget(self._result_window) self._src_result_form.setLayout(self._src_result_formLayout) self._next_page_button = QPushButton() self._next_page_button.setText(self.fc_dict["next_button"][self.fc_lg]) self._next_page_button.clicked[bool].connect(self.print_result) self._next_page_button.setDisabled(True) self._next_page_button.setToolTip( self.fc_dict["next_button_tip"][self.fc_lg]) info_splitter = QSplitter(Qt.Vertical) info_splitter.addWidget(self._json_info_form) info_splitter.addWidget(self._src_result_form) info_splitter.addWidget(self._next_page_button) info_splitter.setStretchFactor(1, 1) info_splitter.setCollapsible(0, False) info_splitter.setCollapsible(1, False) info_splitter.setCollapsible(0, False) mainWidget = QWidget() mainLayout = QHBoxLayout(mainWidget) mainLayout.setSpacing(2) mainLayout.addLayout(self._left_frame_layout) mainLayout.addWidget(info_splitter) self.setCentralWidget(mainWidget) # ----------创建主窗口状态栏---------- self._statusBar = QStatusBar() self._statusBar.showMessage(self.fc_dict["info_welcome"][self.fc_lg]) self._copyRightLabel = QLabel( self.fc_dict["info_copyright"][self.fc_lg]) self._statusBar.addPermanentWidget(self._copyRightLabel) self.setStatusBar(self._statusBar) # ----------设置页面尺寸及标题等---------- self.setGeometry(200, 50, 900, 610) self.setObjectName("MainWindow") self.setWindowTitle(self.fc_dict["info_title"][self.fc_lg]) currentDir = os.getcwd() # self.setWindowIcon(QIcon("./app_data/workfiles/myIcon.png")) self.setWindowIcon(QIcon(currentDir + "/app_data/workfiles/myIcon.png")) self.setIconSize(QSize(100, 40)) # endregion 创建窗口 # region 关联事件 self._src_scope_2.toggled.connect(self._src_scope_3.setEnabled) self._src_author_btn.toggled.connect(self._src_author_opt.setEnabled) self._src_translator_btn.toggled.connect( self._src_translator_opt.setEnabled) self._src_genre_btn.toggled.connect(self._src_genre_opt.setEnabled) self._display_context_button.toggled.connect( self._display_context_choice.setEnabled) self._display_source_button.toggled.connect( self._display_source_choice.setEnabled) self._tt_book_listBox.currentIndexChanged.connect( self._version_change_info) self._json_list_window.itemDoubleClicked.connect( self._json_list_window_item_double_clicked) self._ss_book_contentsBox.itemDoubleClicked.connect( self._ss_book_contentsBox_double_clicked) # endregion 关联事件 self._corpus = None self._left_frame_a.setMaximumWidth(350) self._json_list_window.setMaximumWidth(340) self._left_frame_b.setMaximumWidth(350)
def __init__(self, panel): super(Widget, self).__init__(panel) layout = QVBoxLayout() self.setLayout(layout) layout.setSpacing(0) self.searchEntry = SearchLineEdit() self.treeView = QTreeView(contextMenuPolicy=Qt.CustomContextMenu) self.textView = QTextBrowser() applyButton = QToolButton(autoRaise=True) editButton = QToolButton(autoRaise=True) addButton = QToolButton(autoRaise=True) self.menuButton = QPushButton(flat=True) menu = QMenu(self.menuButton) self.menuButton.setMenu(menu) splitter = QSplitter(Qt.Vertical) top = QHBoxLayout() layout.addLayout(top) splitter.addWidget(self.treeView) splitter.addWidget(self.textView) layout.addWidget(splitter) splitter.setSizes([200, 100]) splitter.setCollapsible(0, False) top.addWidget(self.searchEntry) top.addWidget(applyButton) top.addSpacing(10) top.addWidget(addButton) top.addWidget(editButton) top.addWidget(self.menuButton) # action generator for actions added to search entry def act(slot, icon=None): a = QAction(self, triggered=slot) self.addAction(a) a.setShortcutContext(Qt.WidgetWithChildrenShortcut) icon and a.setIcon(icons.get(icon)) return a # hide if ESC pressed in lineedit a = act(self.slotEscapePressed) a.setShortcut(QKeySequence(Qt.Key_Escape)) # import action a = self.importAction = act(self.slotImport, 'document-open') menu.addAction(a) # export action a = self.exportAction = act(self.slotExport, 'document-save-as') menu.addAction(a) # apply button a = self.applyAction = act(self.slotApply, 'edit-paste') applyButton.setDefaultAction(a) menu.addSeparator() menu.addAction(a) # add button a = self.addAction_ = act(self.slotAdd, 'list-add') a.setShortcut(QKeySequence(Qt.Key_Insert)) addButton.setDefaultAction(a) menu.addSeparator() menu.addAction(a) # edit button a = self.editAction = act(self.slotEdit, 'document-edit') a.setShortcut(QKeySequence(Qt.Key_F2)) editButton.setDefaultAction(a) menu.addAction(a) # set shortcut action a = self.shortcutAction = act( self.slotShortcut, 'preferences-desktop-keyboard-shortcuts') menu.addAction(a) # delete action a = self.deleteAction = act(self.slotDelete, 'list-remove') a.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_Delete)) menu.addAction(a) # restore action a = self.restoreAction = act(self.slotRestore) menu.addSeparator() menu.addAction(a) # help button a = self.helpAction = act(self.slotHelp, 'help-contents') menu.addSeparator() menu.addAction(a) self.treeView.setSelectionBehavior(QTreeView.SelectRows) self.treeView.setSelectionMode(QTreeView.ExtendedSelection) self.treeView.setRootIsDecorated(False) self.treeView.setAllColumnsShowFocus(True) self.treeView.setModel(model.model()) self.treeView.setCurrentIndex(QModelIndex()) # signals self.searchEntry.returnPressed.connect(self.slotReturnPressed) self.searchEntry.textChanged.connect(self.updateFilter) self.treeView.doubleClicked.connect(self.slotDoubleClicked) self.treeView.customContextMenuRequested.connect(self.showContextMenu) self.treeView.selectionModel().currentChanged.connect(self.updateText) self.treeView.model().dataChanged.connect(self.updateFilter) # highlight text self.highlighter = highlight.Highlighter(self.textView.document()) # complete on snippet variables self.searchEntry.setCompleter( QCompleter([ ':icon', ':indent', ':menu', ':name', ':python', ':selection', ':set', ':symbol', ':template', ':template-run' ], self.searchEntry)) self.readSettings() app.settingsChanged.connect(self.readSettings) app.translateUI(self) self.updateColumnSizes() self.setAcceptDrops(True)
def __initUI__(self): """ Setup the GUI of the main window. """ # Setup the main console. splash.showMessage("Initializing main window...") self.main_console = QTextBrowser() self.main_console.setReadOnly(True) self.main_console.setLineWrapMode(QTextEdit.NoWrap) self.main_console.setOpenExternalLinks(True) style = 'Regular' family = StyleDB().fontfamily size = CONF.get('main', 'fontsize_console') fontSS = ('font-style: %s;' 'font-size: %s;' 'font-family: %s;') % (style, size, family) self.main_console.setStyleSheet("QWidget{%s}" % fontSS) msg = '<font color=black>Thanks for using %s.</font>' % __appname__ self.write2console(msg) msg = ('Please help GWHAT by reporting bugs on our ' '<a href="https://github.com/jnsebgosselin/gwhat/issues">' 'Issues Tracker</a>.') self.write2console('<font color=black>%s</font>' % msg) # Setup the tab plot hydrograph. splash.showMessage("Initializing plot hydrograph...") self.tab_hydrograph = HydroPrint.HydroprintGUI(self.dmanager) self.tab_hydrograph.ConsoleSignal.connect(self.write2console) # Setup the tab analyse hydrograph. splash.showMessage("Initializing analyse hydrograph...") self.tab_hydrocalc = HydroCalc.WLCalc(self.dmanager) self.tab_hydrocalc.sig_new_mrc.connect( self.tab_hydrograph.mrc_wl_changed) self.tab_hydrocalc.rechg_eval_widget.sig_new_gluedf.connect( self.tab_hydrograph.glue_wl_changed) # Add each tab to the tab widget. self.tab_widget = TabWidget() self.tab_widget.addTab(self.tab_hydrograph, 'Plot Hydrograph') self.tab_widget.addTab(self.tab_hydrocalc, 'Analyze Hydrograph') self.tab_widget.setCornerWidget(self.pmanager) self.tab_widget.currentChanged.connect(self.sync_datamanagers) self.sync_datamanagers() # Setup the splitter widget. splitter = QSplitter(Qt.Vertical, parent=self) splitter.addWidget(self.tab_widget) splitter.addWidget(self.main_console) splitter.setCollapsible(0, True) splitter.setStretchFactor(0, 100) # Forces initially the main_console to its minimal height: splitter.setSizes([100, 1]) # Setup the layout of the main widget. main_widget = QWidget() self.setCentralWidget(main_widget) mainGrid = QGridLayout(main_widget) mainGrid.addWidget(splitter, 0, 0) mainGrid.addWidget(self.tab_hydrocalc.rechg_eval_widget.progressbar, 3, 0)
class MyCodeEditor(QWidget): def __init__(self,parent=None): QWidget.__init__(self,parent) self.__editorSettings = EditorSettings() self.__textDocument = PythonTextDocument() self.__splitter = QSplitter(self) # 横纵滚动条 self.__verticalScrollBar = QScrollBar(self) self.__verticalScrollBar.adjustSize() self.__verticalScrollBar.setMinimumWidth(self.__verticalScrollBar.width()) self.__verticalScrollBar.setMaximumWidth(self.__verticalScrollBar.width()) self.__verticalScrollBar.valueChanged.connect(self.__onVScrollValueChanged) self.__editorSettings.startDisLineNumberChangedSignal.connect(self.__verticalScrollBar.setValue) self.__horizontalScrollBar = QScrollBar(QtCore.Qt.Horizontal,self) self.__horizontalScrollBar.adjustSize() self.__horizontalScrollBar.setMinimumHeight(self.__horizontalScrollBar.height()) self.__horizontalScrollBar.setMaximumHeight(self.__horizontalScrollBar.height()) self.__horizontalScrollBar.valueChanged.connect(self.__onHScrollValueChanged) self.__editorSettings.startDisLetterXOffChangedSignal.connect(self.__horizontalScrollBar.setValue) self.__lineNumberWidget = LineNumberWidget(self.__editorSettings,self.__splitter) setattr(self.__lineNumberWidget,'resizeEvent',self.__onLineNumberWidgetSizeChanged) self.__codeTextWidget = CodeTextWidget(self.__textDocument,self.__editorSettings,self.__splitter) self.__codeTextWidget.document().totalLevelTextChangedSignal.connect(self.__onCodeTextChanged) self.__codeTextWidget.settings().lineTextMaxPixelChangedSignal.connect(self.__onLineStrLengthChanged) self.__codeTextWidget.visibleLineYOffInfoChangedSignal.connect( self.__lineNumberWidget.setVisibleLineYOffInfoArray ) self.__splitter.addWidget( self.__lineNumberWidget ) self.__splitter.addWidget( self.__codeTextWidget ) self.__splitter.setCollapsible( 0,False ) self.__splitter.setCollapsible( 1,False ) self.setText = self.__codeTextWidget.setText self.getText = self.__codeTextWidget.getText def __onLineStrLengthChanged(self,newMaxLength): hMax = newMaxLength-1 self.__horizontalScrollBar.setRange(0,hMax) if self.__horizontalScrollBar.value() > hMax: self.__horizontalScrollBar.setValue(hMax) def __onCodeTextChanged(self,*arg1,**arg2): vMax = self.__codeTextWidget.document().getLineCount()-1 self.__verticalScrollBar.setRange(0,vMax) if self.__verticalScrollBar.value() > vMax: self.__verticalScrollBar.setValue(vMax) def __onVScrollValueChanged(self): self.__codeTextWidget.showLineNumberAsTop(self.__verticalScrollBar.value()) def __onHScrollValueChanged(self): self.__codeTextWidget.showLeftXOffAsLeft(self.__horizontalScrollBar.value()) def __onLineNumberWidgetSizeChanged(self,*arg1,**arg2): lineNumberWidgetWidth = self.__lineNumberWidget.width() vScrollBarWidth = self.__verticalScrollBar.width() hScrollBarHeight = self.__horizontalScrollBar.height() self.__horizontalScrollBar.setGeometry(lineNumberWidgetWidth,self.height()-hScrollBarHeight, \ self.width()-vScrollBarWidth-lineNumberWidgetWidth,hScrollBarHeight) def resizeEvent(self, event): vScrollBarWidth = self.__verticalScrollBar.width() hScrollBarHeight = self.__horizontalScrollBar.height() self.__splitter.setGeometry( 0,0,self.width()-vScrollBarWidth,self.height()-hScrollBarHeight ) self.__verticalScrollBar.setGeometry(self.width()-vScrollBarWidth,0,vScrollBarWidth,self.height()-hScrollBarHeight) self.__onLineNumberWidgetSizeChanged() def wheelEvent(self, event): changedV = 3 if event.angleDelta().y() < 0 else -3 self.__verticalScrollBar.setValue( self.__verticalScrollBar.value() + changedV )
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
def init_widgets(self): mainWindow = RememberedMainWindow() self.janus.widgets["mainwindow"] = mainWindow mainWindow.setWindowTitle(self.applicationName()) central_widget = QWidget(mainWindow) mainLayout = QVBoxLayout(central_widget) mainWindow.setCentralWidget(central_widget) self.janus.widgets["mainWindowlayout"] = mainLayout # # setup main splitter # splitter = QSplitter(central_widget) # # left side camera stuff # cameraSceneWidget = QWidget(splitter) cameraSceneLayout = QVBoxLayout(cameraSceneWidget) cameraSceneLayout.setContentsMargins(6, 0, 6, 0) onaxis = self.janus.devices["onaxis_camera"] axis_controller = self.janus.controllers["grid_axis_controller"] grid = self.janus.controllers["grid"] chip_registry = self.janus.controllers["chip_registry"] grid_widget = GridWidget(parent=cameraSceneWidget, grid_controller=grid, camera=onaxis, axis_controller=axis_controller, chip_registry=chip_registry) self.janus.widgets["gridWidget"] = grid_widget gridToolbar = GridToolBar(cameraSceneWidget, grid_widget) self.janus.widgets["toolBar"] = gridToolbar cameraSceneLayout.addWidget(gridToolbar.widget) cameraSceneLayout.addWidget(grid_widget) cameraSceneWidget.setLayout(cameraSceneLayout) # # Right Widget and layout for the controls # controlsScrollArea = QScrollArea(splitter) controlsScrollArea.setWidgetResizable(True) scrollAreaWidget = QWidget() scrollAreaLAyout = QVBoxLayout(scrollAreaWidget) cameraControls = CameraControls( parent=scrollAreaWidget, device=self.janus.devices["onaxis_camera"]) self.janus.widgets["cameraControls"] = cameraControls scrollAreaLAyout.addWidget(cameraControls.widget) # grid controls gridControls = GridControls( parent=scrollAreaWidget, grid_controller=self.janus.controllers["grid"], chip_registry=self.janus.controllers["chip_registry"]) self.janus.widgets["gridControls"] = gridControls scrollAreaLAyout.addWidget(gridControls.widget) # auto/continous focus controls focusControls = AutoFocusControls( focus_controller=self.janus.controllers["continuous_focus"], axis_controller=axis_controller, parent=scrollAreaWidget) self.janus.widgets["focusControls"] = focusControls scrollAreaLAyout.addWidget(focusControls.widget) # scan controls raw_tango_devices = { "linear_scan_device": self.janus.devices["linear_scan_device"], #"pilc_shutter_device": self.janus.devices["rr_pilc_device"] } scanControls = ScanControls( parent=scrollAreaWidget, grid_controller=self.janus.controllers["grid"], grid_axis_controller=self.janus. controllers["grid_axis_controller"], raw_tango_devices=raw_tango_devices) self.janus.widgets["scanControls"] = scanControls scrollAreaLAyout.addWidget(scanControls.widget) # motorcontrols controls #motorControls = MotorControls(parent=scrollAreaWidget) #self.janus.widgets["motorControls"] = motorControls #scrollAreaLAyout.addWidget(motorControls.widget) # beamprofile controls beamProfile = BeamProfile(parent=scrollAreaWidget) self.janus.widgets["beamProfile"] = beamProfile scrollAreaLAyout.addWidget(beamProfile.widget) # log console logView = Log(parent=scrollAreaWidget, handler=self.janus.utils["logHandler"]) self.janus.widgets["logView"] = logView scrollAreaLAyout.addWidget(logView.widget) # all the bars! #self.janus.widgets["menuBar"] = MainMenuBar(mainWindow) self.janus.widgets["statusBar"] = StatusBar(mainWindow) controlsScrollArea.setWidget(scrollAreaWidget) splitter.addWidget(cameraSceneWidget) splitter.addWidget(controlsScrollArea) splitter.setCollapsible(0, False) splitter.setCollapsible(1, False) splitter.setStretchFactor(0, 250) splitter.setStretchFactor(1, 150) mainLayout.addWidget(splitter)
def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(850, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout.setContentsMargins(0, 12, 0, 0) self.gridLayout.setObjectName("gridLayout") self.tabWidget = QtWidgets.QTabWidget(self.centralwidget) font = QtGui.QFont() font.setBold(False) font.setItalic(False) font.setUnderline(False) font.setWeight(50) font.setStrikeOut(False) # tabWidget and wordListTab self.tabWidget.setFont(font) self.tabWidget.setObjectName("tabWidget") self.wordListTab = QtWidgets.QWidget() self.wordListTab.setObjectName("wordListTab") self.gridLayout_2 = QtWidgets.QGridLayout(self.wordListTab) self.gridLayout_2.setContentsMargins(8, 5, 8, 8) self.gridLayout_2.setObjectName("gridLayout_2") # wordListTable self.wordListTable = QtWidgets.QTableView(self.wordListTab) self.wordListTable.setObjectName("wordListTable") self.wordListTable.horizontalHeader().setSectionResizeMode( QtWidgets.QHeaderView.ResizeToContents) self.wordListTable.horizontalHeader().setStretchLastSection(True) self.wordListTable.setSelectionBehavior(QAbstractItemView.SelectRows) self.gridLayout_2.addWidget(self.wordListTable, 0, 0, 1, 1) self.tabWidget.addTab(self.wordListTab, "") # headListSpanTab self.headListSpanTab = QtWidgets.QWidget() self.headListSpanTab.setObjectName("headListSpanTab") self.gridLayout_3 = QtWidgets.QGridLayout(self.headListSpanTab) self.gridLayout_3.setContentsMargins(0, 0, 0, 0) # self.gridLayout_3.setContentsMargins(8, 5, 8, 8) self.gridLayout_3.setObjectName("gridLayout_3") self.container = QtWidgets.QFrame(self.headListSpanTab) self.container.setFrameShape(QtWidgets.QFrame.StyledPanel) self.container.setFrameShadow(QtWidgets.QFrame.Raised) self.container.setObjectName("container") # headSpanTable self.headSpanTable = QtWidgets.QTableView(self.container) self.headSpanTable.setMinimumWidth(35 * MainWindow.width() / 50) self.headSpanTable.setGeometry(QtCore.QRect(170, 0, 541, 501)) self.headSpanTable.setObjectName("headSpanTable") self.headSpanTable.setColumnWidth(3, MainWindow.width() / 3) self.headSpanTable.horizontalHeader().setStretchLastSection(True) self.headSpanTable.horizontalHeader().setSectionResizeMode( QtWidgets.QHeaderView.ResizeToContents) self.headSpanTable.setSelectionBehavior(QAbstractItemView.SelectRows) # headSpanTree self.headSpanTree = QtWidgets.QTreeView(self.container) self.headSpanTree.setObjectName("headSpanTree") self.headSpanTree.setGeometry(QtCore.QRect(0, 0, 90, 501)) self.headSpanTree.setMinimumWidth(50) self.treeModel = QStandardItemModel() self.rootNode = self.treeModel.invisibleRootItem() self.headSpanTree.setHeaderHidden(True) self.headSpanTree.setModel(self.treeModel) self.headSpanTree.expandAll() #add the splitter hbox = QHBoxLayout() splitter = QSplitter(Qt.Horizontal) splitter.addWidget(self.headSpanTree) splitter.addWidget(self.headSpanTable) splitter.setSizes([75, 250]) splitter.setCollapsible(0, False) splitter.setCollapsible(1, False) hbox.addWidget(splitter) hbox.setContentsMargins(7, 0, 7, 5) self.container.setLayout(hbox) self.gridLayout_3.addWidget(self.container, 0, 0, 1, 1) self.tabWidget.addTab(self.headListSpanTab, "") self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1) self.tabWidget.setCurrentIndex(0) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 731, 21)) self.menubar.setObjectName("menubar") self.menuFile = QtWidgets.QMenu(self.menubar) self.menuFile.setObjectName("menuFile") self.menuEdit = QtWidgets.QMenu(self.menubar) self.menuEdit.setObjectName("menuEdit") self.menuOperation = QtWidgets.QMenu(self.menubar) self.menuOperation.setObjectName("menuOperation") self.menuAbout = QtWidgets.QMenu(self.menubar) self.menuAbout.setObjectName("menuAbout") self.actionOpen = QtWidgets.QAction(MainWindow) self.actionOpen.setObjectName("actionOpen") self.actionSave = QtWidgets.QAction(MainWindow) self.actionSave.setObjectName("actionSave") self.actionUpdate = QtWidgets.QAction(MainWindow) self.actionUpdate.setObjectName("actionUpdate") self.actionHelp = QtWidgets.QAction(MainWindow) self.actionHelp.setObjectName("actionHelp") self.actionExit = QtWidgets.QAction(MainWindow) self.actionExit.setObjectName("actionExit") self.actionAbout_Programmer = QtWidgets.QAction(MainWindow) self.actionAbout_Programmer.setObjectName("actionAbout_Programmer") self.actionAbout_Application = QtWidgets.QAction(MainWindow) self.actionAbout_Application.setObjectName("actionAbout_Application") self.actionAdd = QtWidgets.QAction(MainWindow) self.actionAdd.setObjectName("actionAdd") self.actionDelete = QtWidgets.QAction(MainWindow) self.actionDelete.setObjectName("actionDelete") self.actionModify = QtWidgets.QAction(MainWindow) self.actionModify.setObjectName("actionModify") self.actionUndo = QtWidgets.QAction(MainWindow) self.actionUndo.setObjectName("actionUndo") self.actionReset = QtWidgets.QAction(MainWindow) self.actionReset.setObjectName("actionReset") self.actionSelect_Dictionary = QtWidgets.QAction(MainWindow) self.actionSelect_Dictionary.setObjectName("actionSelect_Dictionary") self.actionSelect_Languary = QtWidgets.QAction(MainWindow) self.actionSelect_Languary.setObjectName("actionSelect_Languary") self.menuFile.addAction(self.actionOpen) self.menuFile.addAction(self.actionSave) self.menuFile.addAction(self.actionUpdate) self.menuFile.addSeparator() self.menuFile.addAction(self.actionHelp) self.menuFile.addSeparator() self.menuFile.addAction(self.actionExit) self.menuEdit.addAction(self.actionAdd) self.menuEdit.addAction(self.actionDelete) self.menuEdit.addAction(self.actionModify) self.menuEdit.addSeparator() self.menuEdit.addAction(self.actionUndo) self.menuEdit.addAction(self.actionReset) self.menuOperation.addAction(self.actionSelect_Dictionary) self.menuOperation.addAction(self.actionSelect_Languary) self.menuAbout.addAction(self.actionAbout_Application) self.menuAbout.addAction(self.actionAbout_Programmer) self.menuAbout.addSeparator() self.menubar.addAction(self.menuFile.menuAction()) self.menubar.addAction(self.menuEdit.menuAction()) self.menubar.addAction(self.menuOperation.menuAction()) self.menubar.addAction(self.menuAbout.menuAction()) # add button listener # self.actionOpen.triggered.connect(lambda pass) MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") self.status_Internet = QLabel('Wifi Connection: ' + 'on') self.status_Lang = QLabel('Language: ' + ('中文' if self._lIndex == 1 else 'English')) self.status_Dictionary = QLabel('Dictionary Source: ' + ( 'A' if self._onlineSource == "YouDao" else 'B')) self.status_Lang.setAlignment(QtCore.Qt.AlignCenter) self.status_Dictionary.setAlignment(QtCore.Qt.AlignRight) self.statusbar.addPermanentWidget(self.status_Internet, stretch=1) self.statusbar.addPermanentWidget(self.status_Lang, stretch=1) self.statusbar.addPermanentWidget(self.status_Dictionary, stretch=1) MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow)
class BrowserWindow(QMainWindow): class SavedWindow(object): def __init__(self, window=None): ''' @param: window BrowserWindow ''' self.windowState = QByteArray() self.windowGeometry = QByteArray() self.windowUiState = {} # QString -> QVariant self.virtualDesktop = -1 self.currentTab = -1 self.tabs = [] # WebTab.SavedTab if window: self.init(window) def init(self, window): if window.isFullScreen(): self.windowState = QByteArray() else: self.windowState = window.saveState() self.windowGeometry = window.saveGeometry() self.windowUiState = window._saveUiState() tabsCount = window.tabCount() for idx in range(tabsCount): # TabbedWebView webView = window.weView(idx) if not webView: continue webTab = webView.webTab() if not webTab: continue tab = WebTab.SavedTab(webTab) if not tab.isValid(): continue if webTab.isCurrentTab(): self.currentTab = len(self.tabs) self.tabs.append(tab) def isValid(self): for tab in self.tabs: if not tab.isValid(): return False return self.currentTab > -1 def clear(self): self.windowState.clear() self.windowGeometry.clear() self.virtualDesktop = -1 self.currentTab = -1 self.tabs.clear() def __init__(self, type_, startUrl=QUrl()): super().__init__(None) self._startUrl = startUrl self._homepage = QUrl() self._windowType = type_ self._startTab = None # WebTab self._startPage = None # WebPage self._mainLayout = None # QVBoxLayout self._mainSplitter = None # QSplitter self._tabWidget = None # TabWidget self._sideBar = None # QPointer<SideBar> self._sideBarManager = SideBarManager(self) self._statusBar = None self._navigationContainer = None # NavigationContainer self._navigationToolbar = None # NavigationBar self._bookmarksToolbar = None # BookmarksToolbar self._progressBar = None # ProgressBar self._ipLabel = None # QLabel self._superMenu = None # QMenu self._mainMenu = None # MainMenu self._tabModel = None # TabModel self._tabMruModel = None # TabMruModel self._sideBarWidth = 0 self._webViewWidth = 0 # Shortcuts self._useTabNumberShortcuts = False self._useSpeedDialNumberShortcuts = False self._useSingleKeyShortcuts = False # Remember visibility of menubar and statusbar after entering Fullscreen self._menuBarVisible = False self._statusBarVisible = False self._htmlFullScreenView = None # TabbedWebView self._hideNavigationTimer = None # QTimer self._deleteOnCloseWidgets = [] # QList<QPointer<QWidget>> self.setAttribute(Qt.WA_DeleteOnClose) self.setAttribute(Qt.WA_DontCreateNativeAncestors) self.setObjectName('mainwindow') self.setWindowTitle(const.APPNAME) self.setProperty('private', gVar.app.isPrivate()) self._setupUi() self._setupMenu() self._hideNavigationTimer = QTimer(self) self._hideNavigationTimer.setInterval(1000) self._hideNavigationTimer.setSingleShot(True) self._hideNavigationTimer.timeout.connect(self._hideNavigationSlot) gVar.app.settingsReloaded.connect(self._loadSettings) QTimer.singleShot(0, self._postLaunch) if gVar.app.isPrivate(): gVar.appTools.setWmClass( '%s Browser (Private Window)' % const.APPNAME, self) else: gVar.appTools.setWmClass('%s Browser' % const.APPNAME, self) def __del__(self): gVar.app.plugins().emitMainWindowDeleted(self) for widget in self._deleteOnCloseWidgets: widget.deleteLater() def setStartTab(self, tab): ''' @param: tab WebTab ''' self._startTab = tab def setStartPage(self, page): ''' @param: page WebPage ''' self._startPage = page def restoreWindow(self, window): ''' @param: window SavedWindow ''' self.restoreState(window.windowState) self.restoreGeometry(window.windowGeometry) self._restoreUiState(window.windowUiState) if not gVar.app.isTestModeEnabled(): self.show( ) # Window has to be visible before adding QWebEngineView's self._tabWidget.restoreState(window.tabs, window.currentTab) self._updateStartupFocus() def fullScreenNavigationVisible(self): return self._navigationContainer.isVisible() def showNavigationWithFullScreen(self): if self._htmlFullScreenView: return if self._hideNavigationTimer.isActive(): self._hideNavigationTimer.stop() self._navigationContainer.show() def hideNavigationWithFullScreen(self): if self._tabWidget.isCurrentTabFresh(): return if not self._hideNavigationTimer.isActive(): self._hideNavigationTimer.start() def currentTabChanged(self): view = self.weView() self._navigationToolbar.setCurrentView(view) if not view: return title = view.webTab().title(True) # allowEmpty if not title: self.setWindowTitle(const.APPNAME) else: self.setWindowTitle('%s - %s' % (title, const.APPNAME)) self._ipLabel.setText(view.getIp()) view.setFocus() self.updateLoadingActions() # Settings correct tab order (LocationBar -> WebSearchBar -> WebView) self.setTabOrder(self.locationBar(), self._navigationToolbar.webSearchBar()) self.setTabOrder(self._navigationToolbar.webSearchBar(), view) def updateLoadingActions(self): view = self.weView() if not view: return isLoading = view.isLoading() self._ipLabel.setVisible(not isLoading) self._progressBar.setVisible(isLoading) self.action('View/Stop').setEnabled(isLoading) self.action('View/Reload').setEnabled(not isLoading) if isLoading: self._progressBar.setValue(view.loadingProgress()) self._navigationToolbar.showStopButton() else: self._navigationToolbar.showReloadButton() def addBookmark(self, url, title): BookmarksTools.addBookmarkDilaog(self, url, title) def addDeleteOnCloseWidget(self, widget): ''' @param: widget QWidget ''' if widget not in self._deleteOnCloseWidgets: self._deleteOnCloseWidgets.append(widget) def createToolbarsMenu(self, menu): self.removeActions(menu.actions()) menu.clear() action = menu.addAction('&Menu Bar', self.toggleShowMenubar) action.setCheckable(True) action.setChecked(self.menuBar().isVisible()) action = menu.addAction('&Navigation Toolbar', self.toggleShowNavigationToolbar) action.setCheckable(True) action.setChecked(self._navigationToolbar.isVisible()) action = menu.addAction('&Bookmarks Toolbar', self.toggleShowBookmarksToolbar) action.setCheckable(True) action.setChecked(Settings().value( 'Browser-View-Settings/showBookmarksToolbar', True)) menu.addSeparator() action = menu.addAction('&Tabs on Top', self.toggleTabsOnTop) action.setCheckable(True) action.setChecked(gVar.appSettings.tabsOnTop) self.addActions(menu.actions()) def createSidebarsMenu(self, menu): self._sideBarManager.createMenu(menu) def createEncodingMenu(self, menu): isoCodecs = [] utfCodecs = [] windowsCodecs = [] isciiCodecs = [] ibmCodecs = [] otherCodecs = [] allCodecs = [] for mib in QTextCodec.availableMibs(): codecName = QTextCodec.codecForMib(mib).name() codecName = codecName.data().decode('utf8') if codecName in allCodecs: continue allCodecs.append(codecName) if codecName.startswith('ISO'): isoCodecs.append(codecName) elif codecName.startswith('UTF'): utfCodecs.append(codecName) elif codecName.startswith('windows'): windowsCodecs.append(codecName) elif codecName.startswith('Iscii'): isciiCodecs.append(codecName) elif codecName.startswith('IBM'): ibmCodecs.append(codecName) else: otherCodecs.append(codecName) if not menu.isEmpty(): menu.addSeperator() self._createEncodingSubMenu('ISO', isoCodecs, menu) self._createEncodingSubMenu('UTF', utfCodecs, menu) self._createEncodingSubMenu('Windows', windowsCodecs, menu) self._createEncodingSubMenu('Iscii', isciiCodecs, menu) self._createEncodingSubMenu('IBM', ibmCodecs, menu) self._createEncodingSubMenu('Other', otherCodecs, menu) def removeActions(self, actions): ''' @param: actions QList<QAction*> ''' for action in actions: self.removeAction(action) def addSideBar(self): ''' @return SideBar* ''' if self._sideBar: return self._sideBar self._sideBar = SideBar(self._sideBarManager, self) self._mainSplitter.insertWidget(0, self._sideBar) self._mainSplitter.setCollapsible(0, False) self._mainSplitter.setSizes([self._sideBarWidth, self._webViewWidth]) return self._sideBar def setSideBarNone(self): ''' @note: when sideBar is notified destroyed, we should clean python ref in window ''' self._sideBar = None def saveSideBarSettings(self): if self._sideBar: # That +1 is important here, without it, the sidebar width would # decrease by 1 pixel every close self._sideBarWidth = self._mainSplitter.sizes()[0] + 1 self._webViewWidth = self.width() - self._sideBarWidth Settings().setValue('Browser-View-Settings/SideBar', self._sideBarManager.activeSideBar()) def tabCount(self): return self._tabWidget.count() def weView(self, index=None): ''' @return TabbedWebView ''' if index is None: index = self._tabWidget.currentIndex() webTab = self._tabWidget.widget(index) if not webTab: return None return webTab.webView() def windowType(self): ''' @return const.BrowserWindowType ''' return self._windowType def locationBar(self): ''' @return LocationBar ''' return self._tabWidget.locationBars().currentWidget() def tabWidget(self): return self._tabWidget def bookmarksToolbar(self): ''' @return BookmarksToolbar ''' return self._bookmarksToolbar def statusBar(self): ''' @return StatusBar ''' return self._statusBar def navigationBar(self): ''' @return NavigationBar ''' return self._navigationToolbar def sideBarManager(self): ''' @return SideBarManager ''' return self._sideBarManager def ipLabel(self): ''' @return QLabel ''' return self._ipLabel def superMenu(self): ''' @return QMenu ''' return self._superMenu def homepageUrl(self): return self._homepage def action(self, name): ''' @return QAction ''' return self._mainMenu.action(name) def tabModel(self): ''' @return TabModel ''' return self._tabModel def tabMruModel(self): ''' @return TabMruModel ''' return self._tabMruModel # public Q_SIGNALS: startingCompleted = pyqtSignal() aboutToClose = pyqtSignal() # public Q_SLOTS: def addTab(self): self._tabWidget.addViewByUrl(QUrl(), const.NT_SelectedNewEmptyTab, True) self._tabWidget.setCurrentTabFresh(True) if self.isFullScreen(): self.showNavigationWithFullScreen() def goHome(self): self.loadAddress(self._homepage) def goHomeInNewTab(self): self._tabWidget.addViewByUrl(self._homepage, const.NT_SelectedTab) def goBack(self): self.weView().back() def goForward(self): self.weView().forward() def reload(self): self.weView().reload() def reloadBypassCache(self): self.weView().reloadBypassCache() def setWindowTitle(self, title): if gVar.app.isPrivate(): title = '%s (Private Browsing)' % title super().setWindowTitle(title) def showWebInspector(self): webView = self.weView() if webView and webView.webTab(): webView.webTab().showWebInspector() def toggleWebInspector(self): webView = self.weView() if webView and webView.webTab(): webView.webTab().toggleWebInspector() def showHistoryManager(self): gVar.app.browsingLibrary().showHistory(self) def toggleShowMenubar(self): self.setUpdatesEnabled(False) menuBar = self.menuBar() menuBar.setVisible(not menuBar.isVisible()) self._navigationToolbar.setSuperMenuVisible(not menuBar.isVisible()) self.setUpdatesEnabled(True) Settings().setValue('Browser-View-Settings/showMenuBar', menuBar.isVisible()) # Make sure we show Navigation Toolbar when Menu Bar is hidden if not self._navigationToolbar.isVisible() and not menuBar.isVisible(): self.toggleShowNavigationToolbar() def toggleShowStatusBar(self): self.setUpdatesEnabled(False) self._statusBar.setVisible(not self._statusBar.isVisible()) self.setUpdatesEnabled(True) Settings().setValue('Browser-View-Settings/showStatusBar', self._statusBar.isVisible()) def toggleShowBookmarksToolbar(self): self.setUpdatesEnabled(False) self._bookmarksToolbar.setVisible( not self._bookmarksToolbar.isVisible()) self.setUpdatesEnabled(True) Settings().setValue('Browser-View-Settings/showBookmarksToolbar', self._bookmarksToolbar.isVisible()) Settings().setValue('Browser-View-Settings/instantBookmarksToolbar', False) def toggleShowNavigationToolbar(self): self.setUpdatesEnabled(False) self._navigationToolbar.setVisible( not self._navigationToolbar.isVisible()) self.setUpdatesEnabled(True) Settings().setValue('Browser-View-Settings/showNavigationToolbar', self._navigationToolbar.isVisible()) # Make sure we show Navigation Toolbar when Menu Bar is hidden if not self._navigationToolbar.isVisible() and not self.menuBar( ).isVisible(): self.toggleShowMenubar() def toggleTabsOnTop(self, enable): gVar.appSettings.tabsOnTop = enable self._navigationContainer.toggleTabsOnTop(enable) def toggleFullScreen(self): if self._htmlFullScreenView: self.weView().triggerPageAction(QWebEnginePage.ExitFullScreen) return if self.isFullScreen(): self.setWindowState(self.windowState() & ~Qt.WindowFullScreen) else: self.setWindowState(self.windowState() | Qt.WindowFullScreen) def requestHtmlFullScreen(self, view, enable): ''' @param: view TabbedWebView @param: enable bool ''' if enable: self.setWindowState(self.windowState() | Qt.WindowFullScreen) else: self.setWindowState(self.windowState() & ~Qt.WindowFullScreen) if self._sideBar: self._sideBar.setHidden(enable) self._htmlFullScreenView = enable and view or None def loadActionUrl(self, obj=None): if not obj: obj = self.sender() action = obj if isinstance(action, QAction): self.loadAddress(action.data().toUrl()) def loadActionUrlInNewTab(self, obj=None): if not obj: obj = self.sender() action = obj if isinstance(action, QAction): self.loadAddress(action.data().toUrl(), const.NT_SelectedTabAtTheEnd) def bookmarkPage(self): view = self.weView() BookmarksTools.addBookmarkDialog(self, view.url(), view.title()) def bookmarkAllTabs(self): BookmarksTools.bookmarkAllTabsDialog(self, self._tabWidget) def loadAddress(self, url): webView = self.weView() if webView.webTab().isPinned(): index = self._tabWidget.addViewByUrl( url, gVar.appSettings.newTabPosition) self.weView(index).setFocus() else: webView.setFocus() webView.loadByReq(LoadRequest(url)) def showSource(self, view=None): if not view: view = self.weView() if not view: return view.showSource() # private Q_SLOTS: def _openLocation(self): if self.isFullScreen(): self.showNavigationWithFullScreen() self.locationBar().setFocus() self.locationBar().selectAll() def _openFile(self): fileTypes = ("%s(*.html *.htm *.shtml *.shtm *.xhtml);;" "%s(*.png *.jpg *.jpeg *.bmp *.gif *.svg *.tiff);;" "%s(*.txt);;" "%s(*.*)") fileTypes %= ("HTML files", "Image files", "Text files", "All files") filePath = gVar.appTools.getOpenFileName("MainWindow-openFile", self, "Open file...", QDir.homePath(), fileTypes) if filePath: self.loadAddress(QUrl.fromLocalFile(filePath)) def _closeWindow(self): if const.OS_MAC: self.close() return if gVar.app.windowCount() > 1: self.close() def _closeTab(self): # Don't close pinned tabs with keyboard shortcuts (Ctrl+w, Ctrl+F4) webView = self.weView() if webView and not webView.webTab().isPinned(): self._tabWidget.requestCloseTab() def _loadSettings(self): settings = Settings() # Url settings settings.beginGroup('Web-URL-Settings') self._homepage = settings.value('homepage', 'app:start', type=QUrl) settings.endGroup() # Browser Window settings settings.beginGroup('Browser-View-Settings') showStatusBar = settings.value('showStatusBar', False) showBookmarksToolbar = settings.value('showBookmarksToolbar', False) showNavigationToolbar = settings.value('showNavigationToolbar', True) showMenuBar = settings.value('showMenuBar', False) # Make sure both menubar and navigationbar are not hidden if not showNavigationToolbar: showMenuBar = True settings.setValue('showMenubar', True) settings.endGroup() settings.beginGroup('Shortcuts') self._useTabNumberShortcuts = settings.value('useTabNumberShortcuts', True) self._useSpeedDialNumberShortcuts = settings.value( 'useSpeedDialNumberShortcuts', True) self._useSingleKeyShortcuts = settings.value('useSingleKeyShortcuts', False) settings.endGroup() settings.beginGroup('Web-Browser-Settings') quitAction = self._mainMenu.action('Standard/Quit') if settings.value('closeAppWithCtrlQ', True): quitAction.setShortcut( gVar.appTools.actionShortcut(QKeySequence.Quit, QKeySequence('Ctrl+Q'))) else: quitAction.setShortcut(QKeySequence()) settings.endGroup() self._statusBarVisible = showStatusBar self.statusBar().setVisible(not self.isFullScreen() and showStatusBar) self._bookmarksToolbar.setVisible(showBookmarksToolbar) self._navigationToolbar.setVisible(showNavigationToolbar) if not const.OS_MACOS: self._menuBarVisible = showMenuBar self.menuBar().setVisible(not self.isFullScreen() and showMenuBar) showSuperMenu = self.isFullScreen() or not showMenuBar # TODO: debug code showSuperMenu = True self._navigationToolbar.setSuperMenuVisible(showSuperMenu) def _postLaunch(self): # noqa C901 self._loadSettings() addTab = True startUrl = QUrl() from .MainApplication import MainApplication afterLaunch = gVar.app.afterLaunch() if afterLaunch == MainApplication.OpenBlankPage: startUrl = QUrl() elif afterLaunch == MainApplication.OpenSpeedDial: startUrl = QUrl('app:speeddial') elif afterLaunch in (MainApplication.OpenHomePage, MainApplication.RestoreSession, MainApplication.SelectSession): startUrl = QUrl(self._homepage) if not gVar.app.isTestModeEnabled(): self.show() if self._windowType == const.BW_FirstAppWindow: if gVar.app.isStartingAfterCrash(): addTab = False startUrl.clear() self._tabWidget.addViewByUrl(QUrl('app:restore'), const.NT_CleanSelectedTabAtTheEnd) elif afterLaunch in (MainApplication.SelectSession, MainApplication.RestoreSession): addTab = self._tabWidget.count() <= 0 elif self._windowType in (const.BW_NewWindow, const.BW_MacFirstWindow): addTab = True elif self._windowType == const.BW_OtherRestoredWindow: addTab = False if not self._startUrl.isEmpty(): startUrl = QUrl(self._startUrl) addTab = True if self._startTab: addTab = False self._tabWidget.addViewByTab(self._startTab, const.NT_SelectedTab) if self._startPage: addTab = False self._tabWidget.addViewByUrl(QUrl()) self.weView().setPage(self._startPage) if addTab: self._tabWidget.addViewByUrl(startUrl, const.NT_CleanSelectedTabAtTheEnd) if not startUrl or startUrl == 'app:speeddial': self.locationBar().setFocus() # Someting went really wrong .. add one tab if self._tabWidget.count() <= 0: self._tabWidget.addViewByUrl(self._homepage, const.NT_SelectedTabAtTheEnd) gVar.app.plugins().emitMainWindowCreated(self) self.startingCompleted.emit() self.raise_() self.activateWindow() self._updateStartupFocus() def _webSearch(self): self._navigationToolbar.webSearchBar().setFocus() self._navigationToolbar.webSearchBar().selectAll() def _searchOnPage(self): webView = self.weView() if webView and webView.webTab(): searchText = webView.page().selectedText() if '\n' not in searchText: webView.webTab().showSearchToolBar(searchText) else: webView.webTab().showSearchToolBar() def _changeEncoding(self): action = self.sender() if action: encoding = action.data() gVar.app.webSettings().setDefaultTextEncoding(encoding) Settings().setValue('Web-Browser-Settings/DefaultEncoding', encoding) self.weView().reload() def _printPage(self): self.weView().printPage() def _saveSettings(self): if gVar.app.isPrivate(): return settings = Settings() settings.beginGroup('Browser-View-Settings') settings.setValue('WindowGeometry', self.saveGeometry()) state = self._saveUiState() for key, val in state.items(): settings.setValue(key, val) settings.endGroup() def _hideNavigationSlot(self): view = self.weView() mouseInView = view and view.underMouse() if self.isFullScreen() and mouseInView: self._navigationContainer.hide() # private # override def event(self, event): ''' @param: event QEvent ''' if event.type() == QEvent.WindowStateChange: e = event assert (isinstance(e, QWindowStateChangeEvent)) if not (e.oldState() & Qt.WindowFullScreen) and ( self.windowState() & Qt.WindowFullScreen): # Enter fullscreen self._statusBarVisible = self._statusBar.isVisible() if not const.OS_MACOS: self._menuBarVisible = self.menuBar().isVisible() self.menuBar().hide() self._statusBar.hide() self._navigationContainer.hide() self._navigationToolbar.enterFullScreen() # Show main menu button since menubar is hidden self._navigationToolbar.setSuperMenuVisible(True) elif (e.oldState() & Qt.WindowFullScreen ) and not (self.windowState() & Qt.WindowFullScreen): # Leave fullscreen self._statusBar.setVisible(self._statusBarVisible) if not const.OS_MACOS: self.menuBar().setVisible(self._menuBarVisible) self._navigationContainer.show() self._navigationToolbar.setSuperMenuVisible( not self._menuBarVisible) self._navigationToolbar.leaveFullScreen() self._htmlFullScreenView = None if self._hideNavigationTimer: self._hideNavigationTimer.stop() return super().event(event) # override def resizeEvent(self, event): ''' @param: event QResizeEvent ''' self._bookmarksToolbar.setMaximumWidth(self.width()) super().resizeEvent(event) # override def keyPressEvent(self, event): # noqa C901 ''' @param: event QKeyEvent ''' if gVar.app.plugins().processKeyPress(const.ON_BrowserWindow, self, event): return number = -1 # TabbedWebView view = self.weView() evtKey = event.key() if evtKey == Qt.Key_Back: if view: view.back() event.accept() elif evtKey == Qt.Key_Forward: if view: view.forward() event.accept() elif evtKey == Qt.Key_Stop: if view: view.stop() event.accept() elif evtKey in (Qt.Key_Reload, Qt.Key_Refresh): if view: view.reload() event.accept() elif evtKey == Qt.Key_HomePage: self.goHome() event.accept() elif evtKey == Qt.Key_Favorites: gVar.app.browsingLibrary().showBookmarks(self) event.accept() elif evtKey == Qt.Key_Search: self.searchOnPage() event.accept() elif evtKey in (Qt.Key_F6, Qt.Key_OpenUrl): self.openLocation() event.accept() elif evtKey == Qt.Key_History: self.showHistoryManager() event.accept() elif evtKey == Qt.Key_AddFavorite: self.bookmarkPage() event.accept() elif evtKey == Qt.Key_News: self.action("Tools/RssReader").trigger() event.accept() elif evtKey == Qt.Key_Tools: self.action("Standard/Preferences").trigger() event.accept() elif evtKey == Qt.Key_Tab: if event.modifiers() == Qt.ControlModifier: self._tabWidget.event(event) elif evtKey == Qt.Key_Backtab: if event.modifiers() == Qt.ControlModifier + Qt.ShiftModifier: self._tabWidget.event(event) elif evtKey == Qt.Key_PageDown: if event.modifiers() == Qt.ControlModifier: self._tabWidget.nextTab() event.accept() elif evtKey == Qt.Key_PageUp: if event.modifiers() == Qt.ControlModifier: self._tabWidget.previousTab() event.accept() elif evtKey == Qt.Key_Equal: if view and event.modifiers() == Qt.ControlModifier: view.zoomIn() event.accept() elif evtKey == Qt.Key_I: if event.modifiers() == Qt.ControlModifier: self.action("Tools/SiteInfo").trigger() event.accept() elif evtKey == Qt.Key_U: if event.modifiers() == Qt.ControlModifier: self.action("View/PageSource").trigger() event.accept() elif evtKey == Qt.Key_F: if event.modifiers() == Qt.ControlModifier: self.action("Edit/Find").trigger() event.accept() elif evtKey == Qt.Key_Slash: if self._useSingleKeyShortcuts: self.action("Edit/Find").trigger() event.accept() elif evtKey == Qt.Key_1: number = 1 elif evtKey == Qt.Key_2: number = 2 elif evtKey == Qt.Key_3: number = 3 elif evtKey == Qt.Key_4: number = 4 elif evtKey == Qt.Key_5: number = 5 elif evtKey == Qt.Key_6: number = 6 elif evtKey == Qt.Key_7: number = 7 elif evtKey == Qt.Key_8: number = 8 elif evtKey == Qt.Key_9: number = 9 if number != -1: modifiers = event.modifiers() if modifiers & Qt.AltModifier and self._useTabNumberShortcuts: if number == 9: number = self._tabWidget.count() self._tabWidget.setCurrentIndex(number - 1) event.accept() return if modifiers & Qt.ControlModifier and self._useSpeedDialNumberShortcuts: # QUrl url = gVar.app.plugins().speedDial().urlForShortcut(number - 1) if url.isValid(): self.loadAddress(url) event.accept() return if modifiers == Qt.NoModifier and self._useSingleKeyShortcuts: if number == 1: self._tabWidget.previousTab() if number == 2: self._tabWidget.nextTab() super().keyPressEvent(event) # override def keyReleaseEvent(self, event): ''' @param: event QKeyEvent ''' if gVar.app.plugins().processKeyRelease(const.ON_BrowserWindow, self, event): return evtKey = event.key() if evtKey == Qt.Key_F: if event.modifiers() == Qt.ControlModifier: self.action('Edit/Find').trigger() event.accept() super().keyReleaseEvent(event) # override def closeEvent(self, event): ''' @param: event QCloseEvent ''' if gVar.app.isClosing(): self._saveSettings() return settings = Settings() askOnClose = settings.value('Browser-Tabs-Settings/AskOnClosing', True) from .MainApplication import MainApplication if gVar.app.afterLaunch() in (MainApplication.SelectSession, MainApplication.RestoreSession ) and gVar.app.windowCount() == 1: askOnClose = False if askOnClose and self._tabWidget.normalTabsCount() > 1: dialog = CheckBoxDialog(QMessageBox.Yes | QMessageBox.No, self) dialog.setDefaultButton(QMessageBox.No) dialog.setText( "There are still %s open tabs and your session won't be stored.\n" % self._tabWidget.count() + "Are you sure you want to close this window?") dialog.setCheckBoxText("Don't ask again") dialog.setWindowTitle('There are still open tabs') dialog.setIcon(QMessageBox.Warning) if dialog.exec_() != QMessageBox.Yes: event.ignore() return if dialog.isChecked(): settings.setValue('Browser-Tabs-Settings/AskOnClosing', False) self.aboutToClose.emit() self._saveSettings() gVar.app.closedWindowsManager().saveWindow(self) if gVar.app.windowCount() == 1: gVar.app.quitApplication() event.accept() # == private == def _setupUi(self): settings = Settings() settings.beginGroup('Browser-View-Settings') windowGeometry = settings.value('WindowGeometry', b'') keys = [ ('LocationBarWidth', int), ('WebSearchBarWidth', int), ('SideBarWidth', int), ('WebViewWidth', int), ('SideBar', str), ] uiState = {} for key, typ in keys: if settings.contains(key): uiState[key] = typ(settings.value(key)) settings.endGroup() widget = QWidget(self) widget.setCursor(Qt.ArrowCursor) self.setCentralWidget(widget) self._mainLayout = QVBoxLayout(widget) self._mainLayout.setContentsMargins(0, 0, 0, 0) self._mainLayout.setSpacing(0) self._mainSplitter = QSplitter(self) self._mainSplitter.setObjectName('sidebar-splitter') self._tabWidget = TabWidget(self) self._superMenu = QMenu(self) self._navigationToolbar = NavigationBar(self) self._bookmarksToolbar = BookmarksToolbar(self) self._tabModel = TabModel(self, self) self._tabMruModel = TabMruModel(self, self) self._tabMruModel.setSourceModel(self._tabModel) self._navigationContainer = NavigationContainer(self) self._navigationContainer.addWidget(self._navigationToolbar) self._navigationContainer.addWidget(self._bookmarksToolbar) self._navigationContainer.setTabBar(self._tabWidget.tabBar()) self._mainSplitter.addWidget(self._tabWidget) self._mainSplitter.setCollapsible(0, False) self._mainLayout.addWidget(self._navigationContainer) self._mainLayout.addWidget(self._mainSplitter) self._statusBar = StatusBar(self) self._statusBar.setObjectName('mainwindow-statusbar') self._statusBar.setCursor(Qt.ArrowCursor) self.setStatusBar(self._statusBar) self._progressBar = ProgressBar(self._statusBar) self._ipLabel = QLabel(self) self._ipLabel.setObjectName('statusbar-ip-label') self._ipLabel.setToolTip('IP Address of current page') self._statusBar.addPermanentWidget(self._progressBar) self._statusBar.addPermanentWidget(self.ipLabel()) downloadsButton = DownloadsButton(self) self._statusBar.addButton(downloadsButton) self._navigationToolbar.addToolButton(downloadsButton) desktop = gVar.app.desktop() windowWidth = desktop.availableGeometry().width() / 1.3 windowHeight = desktop.availableGeometry().height() / 1.3 # Let the WM decides where to put new browser window if self._windowType not in (const.BW_FirstAppWindow, const.BW_MacFirstWindow) and \ gVar.app.getWindow(): if const.OS_WIN: # Windows WM places every new window in the middle of screen .. for some reason p = gVar.app.getWindow().geometry().topLeft() p.setX(p.x() + 30) p.setY(p.y() + 30) if not desktop.availableGeometry( gVar.app.getWindow()).contains(p): p.setX( desktop.availableGeometry(gVar.app.getWindow()).x() + 30) p.setY( desktop.availableGeometry(gVar.app.getWindow()).y() + 30) self.setGeometry(QRect(p, gVar.app.getWindow().size())) else: self.resize(gVar.app.getWindow().size()) elif not self.restoreGeometry(windowGeometry): if const.OS_WIN: self.setGeometry( QRect( desktop.availableGeometry(gVar.app.getWindow()).x() + 30, desktop.availableGeometry(gVar.app.getWindow()).y() + 30, windowWidth, windowHeight)) else: self.resize(windowWidth, windowHeight) self._restoreUiState(uiState) # Set some sane minimum width self.setMinimumWidth(300) def _setupMenu(self): if const.OS_MACOS: macMainMenu = None if not macMainMenu: macMainMenu = MainMenu(self, 0) macMainMenu.initMenuBar(QMenuBar(0)) gVar.app.activeWindowChanged.connect(macMainMenu.setWindow) else: macMainMenu.setWindow(self) else: self.setMenuBar(MenuBar(self)) self._mainMenu = MainMenu(self, self) self._mainMenu.initMenuBar(self.menuBar()) self._mainMenu.initSuperMenu(self._superMenu) # Setup other shortcuts reloadBypassCacheAction = QShortcut(QKeySequence('Ctrl+F5'), self) reloadBypassCacheAction2 = QShortcut(QKeySequence('Ctrl+Shift+R'), self) reloadBypassCacheAction.activated.connect(self.reloadBypassCache) reloadBypassCacheAction2.activated.connect(self.reloadBypassCache) closeTabAction = QShortcut(QKeySequence('Ctrl+W'), self) closeTabAction2 = QShortcut(QKeySequence('Ctrl+F4'), self) closeTabAction.activated.connect(self._closeTab) closeTabAction2.activated.connect(self._closeTab) reloadAction = QShortcut(QKeySequence('Ctrl+R'), self) reloadAction.activated.connect(self.reload) openLocationAction = QShortcut(QKeySequence('Alt+D'), self) openLocationAction.activated.connect(self._openLocation) inspectorAction = QShortcut(QKeySequence('F12'), self) inspectorAction.activated.connect(self.toggleWebInspector) restoreClosedWindow = QShortcut(QKeySequence('Ctrl+Shift+N'), self) restoreClosedWindow.activated.connect( gVar.app.closedWindowsManager().restoreClosedWindow) def _updateStartupFocus(self): def _updateStartupFocusCb(): # Scroll to current tab self.tabWidget().tabBar().ensureVisible() # Update focus page = self.weView().page() url = page.requestedUrl() if not self._startPage and not LocationBar.convertUrlToText(url): self.locationBar().setFocus() else: self.weView().setFocus() QTimer.singleShot(500, _updateStartupFocusCb) def _createEncodingAction(self, codecName, activeCodecName, menu): ''' @param: codecName QString @param: activeCodecName QString @param: menu QMenu ''' action = QAction(codecName, menu) action.setData(codecName) action.setCheckable(True) action.triggered.connect(self._changeEncoding) if activeCodecName.lower() == codecName.lower(): action.setChecked(True) return action def _createEncodingSubMenu(self, name, codecNames, menu): ''' @param: name QString @param: codecName QStringList @param: menu QMenu ''' if not codecNames: return codecNames.sort() subMenu = QMenu(name, menu) activeCodecName = gVar.app.webSettings().defaultTextEncoding() group = QActionGroup(subMenu) for codecName in codecNames: act = self._createEncodingAction(codecName, activeCodecName, subMenu) group.addAction(act) subMenu.addAction(act) menu.addMenu(subMenu) def _saveUiState(self): ''' @return: QHash<QStirng, QVariant> ''' self.saveSideBarSettings() state = {} state['LocationBarWidth'] = self._navigationToolbar.splitter().sizes( )[0] state['WebSearchBarWidth'] = self._navigationToolbar.splitter().sizes( )[1] state['SideBarWidth'] = self._sideBarWidth state['WebViewWidth'] = self._webViewWidth state['SideBar'] = self._sideBarManager.activeSideBar() return state def _restoreUiState(self, state): ''' @param: state QHash<QString, QVariant> ''' locationBarWidth = state.get('LocationBarWidth', 480) webSearchBarWidth = state.get('WebSearchBarWidth', 140) self._navigationToolbar.setSplitterSizes(locationBarWidth, webSearchBarWidth) self._sideBarWidth = state.get('SideBarWidth', 250) self._webViewWidth = state.get('WebViewWidth', 2000) if self._sideBar: self._mainSplitter.setSizes( [self._sideBarWidth, self._webViewWidth]) activeSideBar = state.get('SideBar') if not activeSideBar and self._sideBar: self._sideBar.close() else: self._sideBarManager.showSideBar(activeSideBar, False)
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
class AcquisitionWindow(QMainWindow): """ This is the window that acts as the central hub for all the widgets. Contains a left Toolbox for streaming configuration and a right Toolbox for recording configuration. The center contain the Time Domain and Frequency Domain Plots, along with a status bar below them. The channel levels plot is placed in the right Toolbox. The widgets communicate with each other using the pyqt Signal and Slot method, so most callback functions are self-contained in the respective widget. However, some callback function are still present in the main body, mostly functions to handle the streaming and the recording processes. Attributes ---------- sig_time_series_data_saved: pyqtSignal(ChannelSet) Emitted when time series data is recorded sig_transfer_function_data_saved: pyqtSignal(ChannelSet) Emitted when transfer function data is recorded playing: bool Indicate whether the stream is playing rec: Recorder object Object which handles the streaming and recording See documentation for Recorder classes """ sig_time_series_data_saved = pyqtSignal(object) sig_transfer_function_data_saved = pyqtSignal(object) sig_closed = pyqtSignal() def __init__(self, parent=None, recType=mR, configs=['', 44100, 2, 1024, 6]): """ Initialise and construct the window application. Parameters ---------- parent: object The object which the window interacts with e.g. The analysis window """ super().__init__() self.parent = parent # Set window parameter self.setGeometry(400, 300, WIDTH, HEIGHT) self.setWindowTitle('AcquisitionWindow') self.meta_window = None # Set recorder object self.playing = False self.rec = recType.Recorder(rate=configs[1], channels=configs[2], chunk_size=configs[3], num_chunk=configs[4], device_name=configs[0]) # Set up the TimeSeries and FreqSeries self.timedata = None self.freqdata = None # Set up tallies for the average transfer function calculation self.autospec_in_tally = [] self.autospec_out_tally = [] self.crossspec_tally = [] try: # Construct UI self.initUI() except Exception: # If it fails, show the window and stop t, v, tb = sys.exc_info() print(t) print(v) print(traceback.format_tb(tb)) self.show() return # Connect the recorder Signals self.connect_rec_signals() # Attempt to start streaming self.init_and_check_stream() # Center and show window self.adjust_position() self.setFocus() self.show() def initUI(self): """ Construct the window by calling the widgets classes, and any additional widgets (such as Qsplitters, Toolbox) Then, perform the signal connection among the widgets. Also sets up the update timer for the streaming. Lastly, finalise the plots and misc. items. There is also an experimental styling section just after the UI construction. """ # Set up the main widget self.main_widget = QWidget(self) main_layout = QHBoxLayout(self.main_widget) #------------------------- STREAM TOOLBOX ------------------------------ self.stream_toolbox = MasterToolbox() self.stream_tools = Toolbox('left', self.main_widget) self.stream_toolbox.add_toolbox(self.stream_tools) self.chantoggle_UI = ChanToggleUI(self.main_widget) self.chanconfig_UI = ChanConfigUI(self.main_widget) self.devconfig_UI = DevConfigUI(self.main_widget) self.devconfig_UI.set_recorder(self.rec) self.devconfig_UI.config_setup() NI_btn = self.devconfig_UI.typegroup.findChildren(QRadioButton)[1] if not NI_drivers: NI_btn.setDisabled(True) self.stream_tools.addTab(self.chantoggle_UI, 'Channel Toggle') self.stream_tools.addTab(self.chanconfig_UI, 'Channel Config') self.stream_tools.addTab(self.devconfig_UI, 'Device Config') main_layout.addWidget(self.stream_toolbox) main_layout.setStretchFactor(self.stream_toolbox, 0) #---------------------------PLOT + STATUS WIDGETS----------------------------- self.mid_splitter = QSplitter(self.main_widget, orientation=Qt.Vertical) self.timeplot = TimeLiveGraph(self.mid_splitter) self.freqplot = FreqLiveGraph(self.mid_splitter) self.stats_UI = StatusUI(self.mid_splitter) self.mid_splitter.addWidget(self.timeplot) self.mid_splitter.addWidget(self.freqplot) self.mid_splitter.addWidget(self.stats_UI) self.mid_splitter.setCollapsible(2, False) main_layout.addWidget(self.mid_splitter) main_layout.setStretchFactor(self.mid_splitter, 1) #---------------------------RECORDING TOOLBOX------------------------------- self.recording_toolbox = MasterToolbox() self.recording_tools = Toolbox('right', self.main_widget) self.recording_toolbox.add_toolbox(self.recording_tools) self.right_splitter = QSplitter(self.main_widget, orientation=Qt.Vertical) self.RecUI = RecUI(self.main_widget) self.RecUI.set_recorder(self.rec) self.right_splitter.addWidget(self.RecUI) self.levelsplot = LevelsLiveGraph(self.rec, self.right_splitter) self.right_splitter.addWidget(self.levelsplot) self.recording_tools.addTab(self.right_splitter, 'Record Time Series') main_layout.addWidget(self.recording_toolbox) main_layout.setStretchFactor(self.recording_toolbox, 0) #-----------------------EXPERIMENTAL STYLING---------------------------- #self.main_splitter.setFrameShape(QFrame.Panel) #self.main_splitter.setFrameShadow(QFrame.Sunken) """ self.main_widget.setStyleSheet(''' .QWidget{ background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #eee, stop:1 #ccc); border: 1px solid #777; width: 13px; margin-top: 2px; margin-bottom: 2px; border-radius: 4px; } .QSplitter::handle{ background: #737373; } .QGroupBox{ border: 1px solid black; margin-top: 0.5em; font: italic; } .QGroupBox::title { top: -6px; left: 10px; } .QWidget #subWidget{ background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #eee, stop:1 #ccc); border: 1px solid #777; width: 13px; margin-top: 2px; margin-bottom: 2px; border-radius: 4px; } ''') """ #----------------------SIGNAL CONNECTIONS--------------------------- self.chantoggle_UI.sigToggleChanged.connect( self.timeplot.toggle_plotline) self.chantoggle_UI.sigToggleChanged.connect( self.freqplot.toggle_plotline) self.chanconfig_UI.chans_num_box.currentIndexChanged.connect( self.display_chan_config) self.chanconfig_UI.meta_btn.clicked.connect(self.open_meta_window) self.chanconfig_UI.sigTimeOffsetChanged.connect( self.timeplot.set_offset) self.chanconfig_UI.sigFreqOffsetChanged.connect( self.freqplot.set_offset) self.chanconfig_UI.sigHoldChanged.connect(self.timeplot.set_sig_hold) self.chanconfig_UI.sigColourChanged.connect( self.timeplot.set_plot_colour) self.chanconfig_UI.sigColourChanged.connect( self.freqplot.set_plot_colour) self.chanconfig_UI.sigColourChanged.connect( self.levelsplot.set_plot_colour) self.chanconfig_UI.sigColourReset.connect( self.timeplot.reset_default_colour) self.chanconfig_UI.sigColourReset.connect( self.freqplot.reset_default_colour) self.chanconfig_UI.sigColourReset.connect( self.levelsplot.reset_default_colour) self.devconfig_UI.configRecorder.connect(self.ResetRecording) self.timeplot.plotColourChanged.connect( self.chanconfig_UI.set_colour_btn) self.freqplot.plotColourChanged.connect( self.chanconfig_UI.set_colour_btn) self.levelsplot.plotColourChanged.connect( self.chanconfig_UI.set_colour_btn) self.levelsplot.thresholdChanged.connect( self.RecUI.rec_boxes[4].setText) self.stats_UI.statusbar.messageChanged.connect(self.default_status) self.stats_UI.resetView.pressed.connect(self.ResetSplitterSizes) self.stats_UI.togglebtn.pressed.connect(lambda: self.toggle_rec()) self.stats_UI.sshotbtn.pressed.connect(self.get_snapshot) self.RecUI.rec_boxes[4].textEdited.connect( self.levelsplot.change_threshold) self.RecUI.startRecording.connect(self.start_recording) self.RecUI.cancelRecording.connect(self.cancel_recording) self.RecUI.undoLastTfAvg.connect(self.undo_tf_tally) self.RecUI.clearTfAvg.connect(self.remove_tf_tally) #---------------------------RESETTING METHODS--------------------------- self.ResetMetaData() self.ResetChanBtns() self.ResetPlots() self.ResetChanConfigs() self.levelsplot.reset_channel_levels() self.ResetSplitterSizes() #-----------------------FINALISE THE MAIN WIDGET------------------------- #Set the main widget as central widget self.main_widget.setFocus() self.setCentralWidget(self.main_widget) # Set up a timer to update the plot self.plottimer = QTimer(self) self.plottimer.timeout.connect(self.update_line) #self.plottimer.timeout.connect(self.update_chanlvls) self.plottimer.start(self.rec.chunk_size * 1000 // self.rec.rate) self.show() def adjust_position(self): """ If it has a parent, adjust its position to be slightly out of the parent window towards the left """ if self.parent: pr = self.parent.frameGeometry() qr = self.frameGeometry() self.move(pr.topLeft()) self.move(qr.left() / 2, qr.top()) #----------------CHANNEL CONFIGURATION WIDGET--------------------------- def display_chan_config(self, arg): """ Displays the channel plot offsets, colours, and signal holding. """ # This function is here because it is easier for channel config to # get data from the plot widgets if type(arg) == pg.PlotDataItem: num = self.timeplot.check_line(arg) if num == None: num = self.freqplot.check_line(arg) self.chanconfig_UI.chans_num_box.setCurrentIndex(num) else: num = arg self.chanconfig_UI.colbox.setColor(self.timeplot.plot_colours[num]) self.chanconfig_UI.time_offset_config[0].setValue( self.timeplot.plot_xoffset[num]) self.chanconfig_UI.time_offset_config[1].setValue( self.timeplot.plot_yoffset[num]) self.chanconfig_UI.hold_tickbox.setCheckState( self.timeplot.sig_hold[num]) self.chanconfig_UI.fft_offset_config[0].setValue( self.freqplot.plot_xoffset[num]) self.chanconfig_UI.fft_offset_config[1].setValue( self.freqplot.plot_yoffset[num]) def open_meta_window(self): """ Open the metadata window """ if not self.meta_window: try: self.meta_window = ChanMetaWin(self) self.meta_window.show() self.meta_window.finished.connect(self.meta_win_closed) except: t, v, tb = sys.exc_info() print(t) print(v) print(traceback.format_tb(tb)) def meta_win_closed(self): """ Callback when the metadata window is closed """ self.meta_window = None self.update_chan_names() #----------------------PLOT WIDGETS----------------------------------- # Updates the plots def update_line(self): """ Callback to update the time domain and frequency domain plot """ # Get the buffer data = self.rec.get_buffer() # Take the last chunk for the levels plot currentdata = data[len(data) - self.rec.chunk_size:, :] currentdata -= np.mean(currentdata) rms = np.sqrt(np.mean(currentdata**2, axis=0)) maxs = np.amax(abs(currentdata), axis=0) self.levelsplot.set_channel_levels(rms, maxs) # Prepare the window and weightage for FFT plot window = np.hanning(data.shape[0]) weightage = np.exp(2 * self.timedata / self.timedata[-1]) # Update each plot item's data + level peaks for i in range(data.shape[1]): plotdata = data[:, i].reshape((len(data[:, i]), )) # Check for zero crossing if needed zc = 0 if self.timeplot.sig_hold[i] == Qt.Checked: avg = np.mean(plotdata) zero_crossings = np.where( np.diff(np.sign(plotdata - avg)) > 0)[0] if zero_crossings.shape[0]: zc = zero_crossings[0] + 1 self.timeplot.update_line(i, x=self.timedata[:len(plotdata) - zc], y=plotdata[zc:]) fft_data = rfft(plotdata * window * weightage) psd_data = abs(fft_data)**0.5 self.freqplot.update_line(i, x=self.freqdata, y=psd_data) self.levelsplot.set_peaks(i, maxs[i]) #-------------------------STATUS BAR WIDGET-------------------------------- def toggle_rec(self, stop=None): """ Callback to pause/resume the stream, unless explicitly specified to stop or not Parameters ---------- stop: bool Specify whether to stop the stream. None to toggle the current state """ if not stop == None: self.playing = stop if self.playing: self.rec.stream_stop() self.stats_UI.togglebtn.setText('Resume') self.RecUI.recordbtn.setDisabled(True) else: self.rec.stream_start() self.stats_UI.togglebtn.setText('Pause') self.RecUI.recordbtn.setEnabled(True) self.playing = not self.playing # Clear the status, allow it to auto update itself self.stats_UI.statusbar.clearMessage() # Get the current instantaneous plot and transfer to main window def get_snapshot(self): """ Callback to take the current buffer data and send it out to parent window """ snapshot = self.rec.get_buffer() for i in range(snapshot.shape[1]): self.live_chanset.set_channel_data(i, 'time_series', snapshot[:, i]) self.live_chanset.set_channel_metadata(tuple(range(snapshot.shape[1])), {'sample_rate': self.rec.rate}) self.save_time_series() self.stats_UI.statusbar.showMessage('Snapshot Captured!', 1500) def default_status(self, msg): """ Callback to set the status message to the default messages if it is empty (ie when cleared) """ # Placed here because it accesses self.playing (might change it soon?) if not msg: if self.playing: self.stats_UI.statusbar.showMessage('Streaming') else: self.stats_UI.statusbar.showMessage('Stream Paused') #---------------------------RECORDING WIDGET------------------------------- def start_recording(self): """ Callback to start the data recording """ success = False rec_configs = self.RecUI.get_record_config() if rec_configs[2] >= 0: # Set up the trigger if specified if self.rec.trigger_start(posttrig=rec_configs[0], duration=rec_configs[1], pretrig=rec_configs[2], channel=rec_configs[3], threshold=rec_configs[4]): success = True self.stats_UI.statusbar.showMessage('Trigger Set!') else: # Record normally self.rec.record_init(samples=rec_configs[0], duration=rec_configs[1]) if self.rec.record_start(): success = True self.stats_UI.statusbar.showMessage('Recording...') if success: # Disable buttons for btn in [ self.stats_UI.togglebtn, self.devconfig_UI.config_button, self.RecUI.recordbtn ]: btn.setDisabled(True) self.RecUI.switch_rec_box.setDisabled(True) self.RecUI.spec_settings_widget.setDisabled(True) # Enable the cancel buttons self.RecUI.cancelbtn.setEnabled(True) # def stop_recording(self): """ Callback when the recording finishes. Process the data first, if specify, then transfer the data to the parent window """ # Enable the buttons for btn in self.main_widget.findChildren(QPushButton): btn.setEnabled(True) # Disable the cancel button self.RecUI.cancelbtn.setDisabled(True) # Get the recorded data and compute DFT data = self.rec.flush_record_data() ft_datas = np.zeros((int(data.shape[0] / 2) + 1, data.shape[1]), dtype=np.complex) for i in range(data.shape[1]): self.live_chanset.set_channel_data(i, 'time_series', data[:, i]) ft = rfft(data[:, i]) self.live_chanset.add_channel_dataset(i, 'spectrum', ft) ft_datas[:, i] = ft self.live_chanset.set_channel_metadata(tuple(range(data.shape[1])), {'sample_rate': self.rec.rate}) # Check recording mode rec_mode = self.RecUI.get_recording_mode() if rec_mode == 'Normal': # Send data normally self.live_chanset.add_channel_dataset(tuple(range(data.shape[1])), 'frequency', []) self.live_chanset.add_channel_dataset(tuple(range(data.shape[1])), 'transfer_function', []) self.live_chanset.add_channel_dataset(tuple(range(data.shape[1])), 'coherence', []) self.save_transfer_function() elif rec_mode == 'TF Avg.': # Compute the auto- and crossspectrum for average transfer function # while store them in the tally chans = list(range(self.rec.channels)) in_chan = self.RecUI.get_input_channel() chans.remove(in_chan) input_chan_data = ft_datas[:, in_chan] # Check for incorrect data length with previous recorded data if not len(self.autospec_in_tally) == 0: if not input_chan_data.shape[0] == self.autospec_in_tally[ -1].shape[0]: print( 'Data shape does not match, you may have fiddle the settings' ) print( 'Please either clear the past data, or revert the settings' ) self.stats_UI.statusbar.clearMessage() self.RecUI.spec_settings_widget.setEnabled(True) self.RecUI.switch_rec_box.setEnabled(True) return self.autospec_in_tally.append( calculate_auto_spectrum(input_chan_data)) autospec_out = np.zeros((ft_datas.shape[0], ft_datas.shape[1] - 1), dtype=np.complex) crossspec = np.zeros(autospec_out.shape, dtype=np.complex) for i, chan in enumerate(chans): autospec_out[:, i] = calculate_auto_spectrum(ft_datas[:, chan]) crossspec[:, i] = calculate_cross_spectrum( input_chan_data, ft_datas[:, chan]) self.autospec_out_tally.append(autospec_out) self.crossspec_tally.append(crossspec) auto_in_sum = np.array(self.autospec_in_tally).sum(axis=0) auto_out_sum = np.array(self.autospec_out_tally).sum(axis=0) cross_sum = np.array(self.crossspec_tally).sum(axis=0) for i, chan in enumerate(chans): tf_avg, cor = compute_transfer_function( auto_in_sum, auto_out_sum[:, i], cross_sum[:, i]) self.live_chanset.add_channel_dataset(chan, 'transfer_function', tf_avg) self.live_chanset.add_channel_dataset(chan, 'coherence', cor) # Update the average count and send the data self.RecUI.update_TFavg_count(len(self.autospec_in_tally)) self.save_transfer_function() elif rec_mode == 'TF Grid': # TODO: Implement recording for grid transfer function pass else: # TODO?: Failsafe for unknown mode pass # Enable more widgets self.stats_UI.statusbar.clearMessage() self.RecUI.spec_settings_widget.setEnabled(True) self.RecUI.switch_rec_box.setEnabled(True) def undo_tf_tally(self): """ Callback to remove the last autospectrum and crossspectrum in the tally """ if self.autospec_in_tally: self.autospec_in_tally.pop() self.autospec_out_tally.pop() self.crossspec_tally.pop() self.RecUI.update_TFavg_count(len(self.autospec_in_tally)) def remove_tf_tally(self): """ Callback to clear the autospectrum and crossspectrum tallies """ if self.autospec_in_tally: self.autospec_in_tally = [] self.autospec_out_tally = [] self.crossspec_tally = [] self.RecUI.update_TFavg_count(len(self.autospec_in_tally)) # Cancel the data recording def cancel_recording(self): """ Callback to cancel the recording and re-enable the UIs """ self.rec.record_cancel() for btn in self.main_widget.findChildren(QPushButton): btn.setEnabled(True) self.RecUI.switch_rec_box.setEnabled(True) self.RecUI.spec_settings_widget.setEnabled(True) self.RecUI.cancelbtn.setDisabled(True) self.stats_UI.statusbar.clearMessage() #--------------------------- RESET METHODS------------------------------------- def ResetRecording(self): """ Reset the stream to the specified configuration, then calls upon other reset methods """ # Spew errors to console at each major step of the resetting self.stats_UI.statusbar.showMessage('Resetting...') # Stop the update and Delete the stream self.playing = False self.plottimer.stop() self.rec.close() del self.rec try: # Get Input from the Device Configuration UI Rtype, settings = self.devconfig_UI.read_device_config() # Reinitialise the recording object self.rec = Rtype.Recorder() # Set the recorder parameters dev_name = self.rec.available_devices()[0] sel_ind = min(settings[0], len(dev_name) - 1) self.rec.set_device_by_name(dev_name[sel_ind]) self.rec.rate = settings[1] self.rec.channels = settings[2] self.rec.chunk_size = settings[3] self.rec.num_chunk = settings[4] self.devconfig_UI.configboxes[0].setCurrentIndex( dev_name.index(self.rec.device_name)) except: t, v, tb = sys.exc_info() print(t) print(v) print(traceback.format_tb(tb)) print('Cannot set up new recorder') try: # Open the stream self.init_and_check_stream() # Reset channel configs self.ResetPlots() self.levelsplot.reset_channel_peaks(self.rec) self.ResetChanConfigs() self.levelsplot.reset_channel_levels() except: t, v, tb = sys.exc_info() print(t) print(v) print(traceback.format_tb(tb)) print('Cannot stream,restart the app') try: # Set the recorder reference to RecUI and devconfig_UI, and remove # average transfer function tally self.RecUI.set_recorder(self.rec) self.devconfig_UI.set_recorder(self.rec) self.remove_tf_tally() except: t, v, tb = sys.exc_info() print(t) print(v) print(traceback.format_tb(tb)) print('Cannot recording configs') # Reset metadata and change channel toggles and re-connect the signals self.ResetMetaData() self.ResetChanBtns() self.connect_rec_signals() # Restart the plot update timer self.plottimer.start(self.rec.chunk_size * 1000 // self.rec.rate) def ResetPlots(self): """ Reset the plots """ self.ResetXdata() self.timeplot.reset_plotlines() self.freqplot.reset_plotlines() for i in range(self.rec.channels): tplot = self.timeplot.plot() tplot.sigClicked.connect(self.display_chan_config) fplot = self.freqplot.plot() fplot.sigClicked.connect(self.display_chan_config) self.freqplot.plotItem.setRange(xRange=(0, self.freqdata[-1]), yRange=(0, 100 * self.rec.channels)) self.freqplot.plotItem.setLimits(xMin=0, xMax=self.freqdata[-1], yMin=-20) def ResetXdata(self): """ Reset the time and frequencies plot data """ data = self.rec.get_buffer() self.timedata = np.arange(data.shape[0]) / self.rec.rate self.freqdata = np.arange(int(data.shape[0] / 2) + 1) / data.shape[0] * self.rec.rate def ResetChanBtns(self): """ Reset the amount of channel toggling buttons """ self.chantoggle_UI.adjust_channel_checkboxes(self.rec.channels) self.update_chan_names() def ResetChanConfigs(self): """ Reset the channel plot configuration settings """ self.timeplot.reset_offsets() self.timeplot.reset_plot_visible() self.timeplot.reset_colour() self.timeplot.reset_sig_hold() self.freqplot.reset_offsets() self.freqplot.reset_plot_visible() self.freqplot.reset_colour() self.levelsplot.reset_colour() self.chanconfig_UI.chans_num_box.clear() self.chanconfig_UI.chans_num_box.addItems( [str(i) for i in range(self.rec.channels)]) self.chanconfig_UI.chans_num_box.setCurrentIndex(0) self.display_chan_config(0) def ResetMetaData(self): """ Reset the metadata """ self.live_chanset = ChannelSet(self.rec.channels) self.live_chanset.add_channel_dataset(tuple(range(self.rec.channels)), 'time_series') def ResetSplitterSizes(self): """ Reset the metadata """ self.mid_splitter.setSizes( [HEIGHT * 0.48, HEIGHT * 0.48, HEIGHT * 0.04]) self.right_splitter.setSizes([HEIGHT * 0.05, HEIGHT * 0.85]) def update_chan_names(self): """ Update the channel names, obtained from the ChannelSet """ names = self.live_chanset.channel_metadata( tuple(range(self.rec.channels)), 'name') for n, name in enumerate(names): chan_btn = self.chantoggle_UI.chan_btn_group.button(n) chan_btn.setText(name) #----------------------- DATA TRANSFER METHODS ------------------------------- def save_time_series(self): """ Transfer time series data to parent window """ print('Saving time series...') self.sig_time_series_data_saved.emit(self.live_chanset) print('Time series saved!') def save_transfer_function(self): """ Transfer transfer function data to parent window """ print('Saving transfer function...') self.sig_transfer_function_data_saved.emit(self.live_chanset) print('Transfer function saved!') #-------------------------- STREAM METHODS ------------------------------------ def init_and_check_stream(self): """ Attempts to initialise the stream """ if self.rec.stream_init(playback=PLAYBACK): self.stats_UI.togglebtn.setEnabled(True) self.toggle_rec(stop=False) self.stats_UI.statusbar.showMessage('Streaming') else: self.stats_UI.togglebtn.setDisabled(True) self.toggle_rec(stop=True) self.stats_UI.statusbar.showMessage('Stream not initialised!') def connect_rec_signals(self): """ Connects the signals from the recorder object """ self.rec.rEmitter.recorddone.connect(self.stop_recording) self.rec.rEmitter.triggered.connect(self.stats_UI.trigger_message) #self.rec.rEmitter.newdata.connect(self.update_line) #self.rec.rEmitter.newdata.connect(self.update_chanlvls) #----------------------OVERRIDDEN METHODS------------------------------------ def closeEvent(self, event): """ Reimplemented from QMainWindow Stops the update timer, disconnect any signals and delete self """ if self.plottimer.isActive(): self.plottimer.stop() self.sig_closed.emit() self.rec.close() self.sig_transfer_function_data_saved.disconnect() self.sig_time_series_data_saved.disconnect() event.accept() self.deleteLater()
def __initUI__(self): # ---- TAB WIDGET # download weather data : splash.showMessage("Initializing download weather data...") self.tab_dwnld_data = DwnldWeatherWidget(self) self.tab_dwnld_data.set_workdir(self.projectdir) # gapfill weather data : splash.showMessage("Initializing gapfill weather data...") self.tab_fill_weather_data = GapFillWeatherGUI(self) self.tab_fill_weather_data.set_workdir(self.projectdir) # hydrograph : splash.showMessage("Initializing plot hydrograph...") self.tab_hydrograph = HydroPrint.HydroprintGUI(self.dmanager) splash.showMessage("Initializing analyse hydrograph...") self.tab_hydrocalc = HydroCalc.WLCalc(self.dmanager) self.tab_hydrocalc.sig_new_mrc.connect( self.tab_hydrograph.mrc_wl_changed) self.tab_hydrocalc.rechg_eval_widget.sig_new_gluedf.connect( self.tab_hydrograph.glue_wl_changed) # ---- TABS ASSEMBLY self.tab_widget = TabWidget() self.tab_widget.addTab(self.tab_dwnld_data, 'Download Weather') self.tab_widget.addTab(self.tab_fill_weather_data, 'Gapfill Weather') self.tab_widget.addTab(self.tab_hydrograph, 'Plot Hydrograph') self.tab_widget.addTab(self.tab_hydrocalc, 'Analyze Hydrograph') self.tab_widget.setCornerWidget(self.pmanager) self.tab_widget.currentChanged.connect(self.sync_datamanagers) # ---- Main Console splash.showMessage("Initializing main window...") self.main_console = QTextEdit() self.main_console.setReadOnly(True) self.main_console.setLineWrapMode(QTextEdit.NoWrap) style = 'Regular' family = StyleDB().fontfamily size = self.whatPref.fontsize_console fontSS = ('font-style: %s;' 'font-size: %s;' 'font-family: %s;' ) % (style, size, family) self.main_console.setStyleSheet("QWidget{%s}" % fontSS) msg = '<font color=black>Thanks for using %s.</font>' % __appname__ self.write2console(msg) self.write2console('<font color=black>' 'Please report any bug or wishful feature at' ' [email protected].' '</font>') # ---- Signal Piping issuer = self.tab_dwnld_data issuer.ConsoleSignal.connect(self.write2console) issuer = self.tab_fill_weather_data issuer.ConsoleSignal.connect(self.write2console) issuer = self.tab_hydrograph issuer.ConsoleSignal.connect(self.write2console) # ---- Splitter Widget splitter = QSplitter(self) splitter.setOrientation(Qt.Vertical) splitter.addWidget(self.tab_widget) splitter.addWidget(self.main_console) splitter.setCollapsible(0, True) splitter.setStretchFactor(0, 100) # Forces initially the main_console to its minimal height: splitter.setSizes([100, 1]) # ---- Main Grid main_widget = QWidget() self.setCentralWidget(main_widget) mainGrid = QGridLayout(main_widget) mainGrid.addWidget(splitter, 0, 0) mainGrid.addWidget(self.tab_fill_weather_data.pbar, 1, 0) mainGrid.addWidget(self.tab_dwnld_data.pbar, 2, 0) mainGrid.addWidget( self.tab_hydrocalc.rechg_eval_widget.progressbar, 3, 0)
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 DestinationPanel(ScrollAreaNoFrame): def __init__(self, parent) -> None: super().__init__(parent) assert parent is not None self.rapidApp = parent self.prefs = self.rapidApp.prefs self.setObjectName("destinationPanelScrollArea") self.splitter = QSplitter(parent=self) self.splitter.setObjectName("destinationPanelSplitter") self.splitter.setOrientation(Qt.Vertical) self.createDestinationViews() self.splitter.addWidget(self.photoDestinationContainer) self.splitter.addWidget(self.videoDestination) self.splitter.setCollapsible(0, False) self.splitter.setCollapsible(1, False) self.setWidget(self.splitter) self.setWidgetResizable(True) def createDestinationViews(self) -> None: """ Create the widgets that let the user choose where to download photos and videos to, and that show them how much storage space there is available for their files. """ self.photoDestination = QPanelView(label=_("Photos"), ) self.photoDestination.setObjectName("photoDestinationPanelView") self.videoDestination = QPanelView(label=_("Videos"), ) self.videoDestination.setObjectName("videoDestinationPanelView") # Display storage space when photos and videos are being downloaded to the same # partition. That is, "combined" means not combined widgets, but combined # display of destination download stats the user sees self.combinedDestinationDisplay = DestinationDisplay( parent=self, rapidApp=self.rapidApp) self.combinedDestinationDisplayContainer = QPanelView( _("Projected Storage Use"), ) self.combinedDestinationDisplay.setObjectName( "combinedDestinationDisplay") self.combinedDestinationDisplayContainer.addWidget( self.combinedDestinationDisplay) self.combinedDestinationDisplayContainer.setObjectName( "combinedDestinationDisplayContainer") # Display storage space when photos and videos are being downloaded to different # partitions. # Also display the file system folder chooser for both destinations. self.photoDestinationDisplay = DestinationDisplay( menu=True, file_type=FileType.photo, parent=self, rapidApp=self.rapidApp) self.photoDestinationDisplay.setDestination( self.prefs.photo_download_folder) self.photoDestinationDisplay.setObjectName("photoDestinationDisplay") self.photoDestinationWidget = ComputerWidget( objectName="photoDestination", view=self.photoDestinationDisplay, fileSystemView=self.rapidApp.photoDestinationFSView, select_text=_("Select a destination folder"), ) self.videoDestinationDisplay = DestinationDisplay( menu=True, file_type=FileType.video, parent=self, rapidApp=self.rapidApp) self.videoDestinationDisplay.setObjectName("videoDestinationDisplay") self.videoDestinationDisplay.setDestination( self.prefs.video_download_folder) self.videoDestinationWidget = ComputerWidget( objectName="videoDestination", view=self.videoDestinationDisplay, fileSystemView=self.rapidApp.videoDestinationFSView, select_text=_("Select a destination folder"), ) self.photoDestination.addWidget(self.photoDestinationWidget) self.videoDestination.addWidget(self.videoDestinationWidget) for widget in (self.photoDestinationWidget, self.videoDestinationWidget, self.combinedDestinationDisplay): self.verticalScrollBarVisible.connect( widget.containerVerticalScrollBar) self.horizontalScrollBarVisible.connect( self.videoDestinationWidget.containerHorizontalScrollBar) layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(self.splitter.handleWidth()) layout.addWidget(self.combinedDestinationDisplayContainer) layout.addWidget(self.photoDestination) self.photoDestinationContainer = QWidget() self.photoDestinationContainer.setObjectName( "photoDestinationContainer") self.photoDestinationContainer.setLayout(layout) def updateDestinationPanelViews( self, same_dev: bool, merge: bool, marked_summary: MarkedSummary, downloading_to: Optional[DefaultDict[int, Set[FileType]]] = None, ) -> bool: """ Updates the header bar and storage space view for the photo and video download destinations. :return True if destinations required for the download exist, and there is sufficient space on them, else False. """ size_photos_marked = marked_summary.size_photos_marked size_videos_marked = marked_summary.size_videos_marked marked = marked_summary.marked destinations_good = True if same_dev: files_to_display = DisplayingFilesOfType.photos_and_videos self.combinedDestinationDisplay.downloading_to = downloading_to self.combinedDestinationDisplay.setDestination( self.prefs.photo_download_folder) self.combinedDestinationDisplay.setDownloadAttributes( marked=marked, photos_size=size_photos_marked, videos_size=size_videos_marked, files_to_display=files_to_display, display_type=DestinationDisplayType.usage_only, merge=merge, ) display_type = DestinationDisplayType.folder_only self.combinedDestinationDisplayContainer.setVisible(True) destinations_good = ( self.combinedDestinationDisplay.sufficientSpaceAvailable()) else: files_to_display = DisplayingFilesOfType.photos display_type = DestinationDisplayType.folders_and_usage self.combinedDestinationDisplayContainer.setVisible(False) if self.prefs.photo_download_folder: self.photoDestinationDisplay.downloading_to = downloading_to self.photoDestinationDisplay.setDownloadAttributes( marked=marked, photos_size=size_photos_marked, videos_size=0, files_to_display=files_to_display, display_type=display_type, merge=merge, ) self.photoDestinationWidget.setViewVisible(True) if display_type == DestinationDisplayType.folders_and_usage: destinations_good = ( self.photoDestinationDisplay.sufficientSpaceAvailable()) else: # Photo download folder was invalid or simply not yet set self.photoDestinationWidget.setViewVisible(False) if size_photos_marked: destinations_good = False if not same_dev: files_to_display = DisplayingFilesOfType.videos if self.prefs.video_download_folder: self.videoDestinationDisplay.downloading_to = downloading_to self.videoDestinationDisplay.setDownloadAttributes( marked=marked, photos_size=0, videos_size=size_videos_marked, files_to_display=files_to_display, display_type=display_type, merge=merge, ) self.videoDestinationWidget.setViewVisible(True) if display_type == DestinationDisplayType.folders_and_usage: destinations_good = ( self.videoDestinationDisplay.sufficientSpaceAvailable() and destinations_good) else: # Video download folder was invalid or simply not yet set self.videoDestinationWidget.setViewVisible(False) if size_videos_marked: destinations_good = False return destinations_good
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, panel): super(Widget, self).__init__(panel) layout = QVBoxLayout() self.setLayout(layout) layout.setSpacing(0) self.searchEntry = SearchLineEdit() self.treeView = QTreeView(contextMenuPolicy=Qt.CustomContextMenu) self.textView = QTextBrowser() applyButton = QToolButton(autoRaise=True) editButton = QToolButton(autoRaise=True) addButton = QToolButton(autoRaise=True) self.menuButton = QPushButton(flat=True) menu = QMenu(self.menuButton) self.menuButton.setMenu(menu) splitter = QSplitter(Qt.Vertical) top = QHBoxLayout() layout.addLayout(top) splitter.addWidget(self.treeView) splitter.addWidget(self.textView) layout.addWidget(splitter) splitter.setSizes([200, 100]) splitter.setCollapsible(0, False) top.addWidget(self.searchEntry) top.addWidget(applyButton) top.addSpacing(10) top.addWidget(addButton) top.addWidget(editButton) top.addWidget(self.menuButton) # action generator for actions added to search entry def act(slot, icon=None): a = QAction(self, triggered=slot) self.addAction(a) a.setShortcutContext(Qt.WidgetWithChildrenShortcut) icon and a.setIcon(icons.get(icon)) return a # hide if ESC pressed in lineedit a = act(self.slotEscapePressed) a.setShortcut(QKeySequence(Qt.Key_Escape)) # import action a = self.importAction = act(self.slotImport, 'document-open') menu.addAction(a) # export action a = self.exportAction = act(self.slotExport, 'document-save-as') menu.addAction(a) # apply button a = self.applyAction = act(self.slotApply, 'edit-paste') applyButton.setDefaultAction(a) menu.addSeparator() menu.addAction(a) # add button a = self.addAction_ = act(self.slotAdd, 'list-add') a.setShortcut(QKeySequence(Qt.Key_Insert)) addButton.setDefaultAction(a) menu.addSeparator() menu.addAction(a) # edit button a = self.editAction = act(self.slotEdit, 'document-edit') a.setShortcut(QKeySequence(Qt.Key_F2)) editButton.setDefaultAction(a) menu.addAction(a) # set shortcut action a = self.shortcutAction = act(self.slotShortcut, 'preferences-desktop-keyboard-shortcuts') menu.addAction(a) # delete action a = self.deleteAction = act(self.slotDelete, 'list-remove') a.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_Delete)) menu.addAction(a) # restore action a = self.restoreAction = act(self.slotRestore) menu.addSeparator() menu.addAction(a) # help button a = self.helpAction = act(self.slotHelp, 'help-contents') menu.addSeparator() menu.addAction(a) self.treeView.setSelectionBehavior(QTreeView.SelectRows) self.treeView.setSelectionMode(QTreeView.ExtendedSelection) self.treeView.setRootIsDecorated(False) self.treeView.setAllColumnsShowFocus(True) self.treeView.setModel(model.model()) self.treeView.setCurrentIndex(QModelIndex()) # signals self.searchEntry.returnPressed.connect(self.slotReturnPressed) self.searchEntry.textChanged.connect(self.updateFilter) self.treeView.doubleClicked.connect(self.slotDoubleClicked) self.treeView.customContextMenuRequested.connect(self.showContextMenu) self.treeView.selectionModel().currentChanged.connect(self.updateText) self.treeView.model().dataChanged.connect(self.updateFilter) # highlight text self.highlighter = highlight.Highlighter(self.textView.document()) # complete on snippet variables self.searchEntry.setCompleter(QCompleter([ ':icon', ':indent', ':menu', ':name', ':python', ':selection', ':set', ':symbol', ':template', ':template-run'], self.searchEntry)) self.readSettings() app.settingsChanged.connect(self.readSettings) app.translateUI(self) self.updateColumnSizes() self.setAcceptDrops(True)
def init_record_UI(self): self.save_dir = None self.input_source = None self.record_button = QPushButton("Record") self.record_button.setEnabled(False) self.predict_button = QPushButton("Predict") keyboard.add_hotkey('ctrl+r', self.record_button.click) output_keys_widget = QWidget() self.output_keys_layout = QGridLayout() output_keys_widget.setLayout(self.output_keys_layout) menu_widget = QWidget() record_w_widget = QLineEdit(str(self.record_w)) record_w_widget.setValidator(QIntValidator()) record_w_widget.textChanged.connect( lambda new_val: self.line_edit_value_changed(new_val, "record_w")) record_h_widget = QLineEdit(str(self.record_h)) record_h_widget.setValidator(QIntValidator()) record_h_widget.textChanged.connect( lambda new_val: self.line_edit_value_changed(new_val, "record_h")) capture_w_widget = QLineEdit(str(self.capture_w)) capture_w_widget.setValidator(QIntValidator()) capture_w_widget.textChanged.connect( lambda new_val: self.line_edit_value_changed(new_val, "capture_w")) capture_h_widget = QLineEdit(str(self.capture_h)) capture_h_widget.setValidator(QIntValidator()) capture_h_widget.textChanged.connect( lambda new_val: self.line_edit_value_changed(new_val, "capture_h")) self.record_screen_label = QLabel(self) self.weights_selection = QComboBox() self.weights_selection.setEnabled(False) self.weights_selection.activated.connect(self.select_weights) input_selection = self.init_input_selection() model_selection = self.init_model_selection() self.file_path_widget = QLineEdit() self.file_path_widget.textChanged.connect(self.path_changed) self.record_button.toggle() self.record_button.setCheckable(True) self.record_button.clicked.connect(self.toggle_record_button) self.predict_button.toggle() self.predict_button.setCheckable(True) self.predict_button.clicked.connect(self.toggle_predict_button) resolution_widget = QWidget() resolution_layout = QGridLayout() resolution_layout.addWidget(record_w_widget, 1, 1, Qt.AlignRight) resolution_layout.addWidget(record_h_widget, 1, 2, Qt.AlignLeft) resolution_widget.setLayout(resolution_layout) capture_size_widget = QWidget() capture_size_layout = QGridLayout() capture_size_layout.addWidget(capture_w_widget, 1, 1, Qt.AlignRight) capture_size_layout.addWidget(capture_h_widget, 1, 2, Qt.AlignLeft) capture_size_widget.setLayout(capture_size_layout) menu_layout = QGridLayout() menu_layout.setColumnStretch(1, 1) menu_layout.setColumnStretch(2, 3) menu_layout.setColumnStretch(3, 1) menu_layout.addWidget(QWidget()) menu_layout.addWidget(QLabel("Capture screen size"), 1, 1, Qt.AlignRight) menu_layout.addWidget(capture_size_widget, 1, 2) menu_layout.addWidget(QLabel("Recording resolution"), 2, 1, Qt.AlignRight) menu_layout.addWidget(resolution_widget, 2, 2) menu_layout.addWidget(QLabel("Input device:"), 3, 1, Qt.AlignRight) menu_layout.addWidget(input_selection, 3, 2) menu_layout.addWidget(QLabel("Data name:"), 4, 1, Qt.AlignRight) menu_layout.addWidget(self.file_path_widget) menu_layout.addWidget(self.record_button, 5, 2) menu_layout.addWidget(QLabel("Model:"), 6, 1, Qt.AlignRight) menu_layout.addWidget(model_selection, 6, 2) menu_layout.addWidget(QLabel("Weights:"), 7, 1, Qt.AlignRight) menu_layout.addWidget(self.weights_selection, 7, 2) menu_layout.addWidget(self.predict_button, 8, 2) menu_layout.addWidget(QWidget()) menu_widget.setLayout(menu_layout) keys_screen_splitter = QSplitter(Qt.Horizontal) keys_screen_splitter.addWidget(self.record_screen_label) keys_screen_splitter.addWidget(output_keys_widget) keys_screen_splitter.setSizes([400, 200]) keys_screen_splitter.setCollapsible(1, False) main_layout = QVBoxLayout() main_layout.addWidget(keys_screen_splitter) main_layout.addWidget(menu_widget) self.setLayout(main_layout)
def __init__(self): """Initialize Tab with layout and behavior.""" super(Tab, self).__init__() # regex pattern for SQL query block comments self.block_comment_re = re.compile( r'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', re.DOTALL | re.MULTILINE) main_layout = QVBoxLayout(self) # define gdb props self.gdb = None self.gdb_items = None self.gdb_columns_names = None self.gdb_schemas = None # connected geodatabase path toolbar self.connected_gdb_path_label = QLabel('') self.connected_gdb_path_label.setTextInteractionFlags( Qt.TextSelectableByMouse) self.connected_gdb_path_label.setToolTip( 'Connected geodatabase that queries will be run against') self.connected_gdb_path_label.setText(not_connected_to_gdb_message) self.browse_to_gdb = QPushButton('Browse') self.browse_to_gdb.setShortcut(QKeySequence('Ctrl+B')) self.browse_to_gdb.clicked.connect( lambda evt, arg=True: self.connect_to_geodatabase( evt, triggered_with_browse=True)) self.gdb_sql_dialect_combobox = QComboBox() for dialect in sql_dialects_names: self.gdb_sql_dialect_combobox.addItem(dialect) self.gdb_browse_toolbar = QToolBar() self.gdb_browse_toolbar.setMaximumHeight(50) self.gdb_browse_toolbar.addWidget(self.browse_to_gdb) self.gdb_browse_toolbar.addWidget(self.connected_gdb_path_label) self.gdb_browse_toolbar.addSeparator() self.gdb_browse_toolbar.addWidget(self.gdb_sql_dialect_combobox) # table with results self.table = ResultTable() # execute SQL query self.execute = QAction('Execute', self) self.execute.setShortcuts( [QKeySequence('F5'), QKeySequence('Ctrl+Return')]) self.execute.triggered.connect(self.run_query) self.addAction(self.execute) # enter a SQL query self.query = TextEditor() self.query.setPlainText('') font = self.query.font() font.setFamily('Consolas') font.setStyleHint(QFont.Monospace) # TODO: add line numbers to the text editor font.setPointSize(14) self.query.setFont(font) self.query.setTabStopWidth(20) self.highlighter = Highlighter(self.query.document()) # TODO select block of text - Ctrl+/ and they become comments self.completer = Completer() self.query.set_completer(self.completer.completer) # errors panel to show if query fails to execute properly self.errors_panel = QPlainTextEdit() font = self.query.font() font.setPointSize(12) self.errors_panel.setStyleSheet('color:red') self.errors_panel.setFont(font) self.errors_panel.hide() # splitter between the toolbar, query window, and the result set table splitter = QSplitter(Qt.Vertical) splitter.addWidget(self.gdb_browse_toolbar) splitter.addWidget(self.query) splitter.addWidget(self.table) splitter.addWidget(self.errors_panel) # add the settings after the widget have been added splitter.setCollapsible(0, True) splitter.setCollapsible(1, False) splitter.setCollapsible(2, False) splitter.setCollapsible(3, False) splitter.setStretchFactor(0, 3) splitter.setStretchFactor(1, 7) splitter.setSizes((100, 200, 300)) self.table.hide() # TOC self.toc = QTreeWidget() self.toc.setHeaderHidden(True) # second splitter between the TOC to the left and the query/table to the # right toc_splitter = QSplitter(Qt.Horizontal) toc_splitter.addWidget(self.toc) toc_splitter.addWidget(splitter) toc_splitter.setCollapsible(0, True) toc_splitter.setSizes((200, 800)) # set the TOC vs data panel main_layout.addWidget(toc_splitter) margins = QMargins() margins.setBottom(10) margins.setLeft(10) margins.setRight(10) margins.setTop(10) main_layout.setContentsMargins(margins) self.setLayout(main_layout) QApplication.setStyle(QStyleFactory.create('Cleanlooks')) self.show() return