Exemplo n.º 1
0
 def __init__(self):
     QMainWindow.__init__(self)
     self.ui = Ui_MainWindow()
     self.ui.setupUi(self)
     self.ui.actionUndo.setEnabled(False)
     self.ui.actionRedo.setEnabled(False)
     self.ui.actionUndo.triggered.connect(self.tabUndo)
     self.ui.actionUndo.triggered.connect(self.tabRedo)
     self.docs = []
     self.toolsDock = ToolsDock(self)
     self.projDock = ProjectDock(self)
     self.inspDock = InspectorDock(self)
     self.addDockWidget(Qt.LeftDockWidgetArea, self.toolsDock)
     self.addDockWidget(Qt.LeftDockWidgetArea, self.projDock)
     self.addDockWidget(Qt.RightDockWidgetArea, self.inspDock)
     self.docsChanged.connect(self.projDock.onDocsChanged)
     self.projDock.openPage.connect(self.on_pageOpen)
     self.activeTab = None
     self.loadSettings()
Exemplo n.º 2
0
 def __init__(self):
     QMainWindow.__init__(self)
     self.ui = Ui_MainWindow()
     self.ui.setupUi(self)
     self.ui.actionUndo.setEnabled(False)
     self.ui.actionRedo.setEnabled(False)
     self.ui.actionUndo.triggered.connect(self.tabUndo)
     self.ui.actionUndo.triggered.connect(self.tabRedo)
     self.docs = []
     self.toolsDock = ToolsDock(self)
     self.projDock = ProjectDock(self)
     self.inspDock = InspectorDock(self)
     self.addDockWidget(Qt.LeftDockWidgetArea, self.toolsDock)
     self.addDockWidget(Qt.LeftDockWidgetArea, self.projDock)
     self.addDockWidget(Qt.RightDockWidgetArea, self.inspDock)
     self.docsChanged.connect(self.projDock.onDocsChanged)
     self.projDock.openPage.connect(self.on_pageOpen)
     self.activeTab = None
     self.loadSettings()
Exemplo n.º 3
0
class MainWindow(QMainWindow):
    tabUndo = pyqtSignal()
    tabRedo = pyqtSignal()
    docsChanged = pyqtSignal()

    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.actionUndo.setEnabled(False)
        self.ui.actionRedo.setEnabled(False)
        self.ui.actionUndo.triggered.connect(self.tabUndo)
        self.ui.actionUndo.triggered.connect(self.tabRedo)
        self.docs = []
        self.toolsDock = ToolsDock(self)
        self.projDock = ProjectDock(self)
        self.inspDock = InspectorDock(self)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.toolsDock)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.projDock)
        self.addDockWidget(Qt.RightDockWidgetArea, self.inspDock)
        self.docsChanged.connect(self.projDock.onDocsChanged)
        self.projDock.openPage.connect(self.on_pageOpen)
        self.activeTab = None
        self.loadSettings()

    def saveSettings(self):
        s = QSettings()
        s.setValue("MainWindow/state", self.saveState())
        s.setValue("MainWindow/geometry", self.saveGeometry())

    def loadSettings(self):
        s = QSettings()
        self.restoreGeometry(s.value("MainWindow/geometry", QByteArray()))
        self.restoreState(s.value("MainWindow/state", QByteArray()))

    def closeEvent(self, e):
        if self.saveAll():
            e.accept()
            self.saveSettings()
            super().closeEvent(e)
        else:
            e.ignore()

    def currentTab(self):
        return self.activeTab

    def currentDoc(self):
        if self.currentTab() is not None:
            return self.activeTab.doc.parentDoc
        return None

    def saveDoc(self, doc: MasterDocument):
        if doc is None:
            return False
        if doc.fileName is None:
            return self.saveAsDoc(doc)
        else:
            doc.saveToFile()
            self.ui.statusbar.showMessage("Document saved", 5000)
            return True

    def saveAsDoc(self, doc: MasterDocument):
        fn = QFileDialog.getSaveFileName(self)[0]
        if fn == "":
            return False
        doc.saveToFile(fn)
        self.ui.statusbar.showMessage("Document saved as {}".format(fn), 5000)
        return True

    def saveAll(self):
        for doc in self.docs:
            if doc.isModified():
                r = QMessageBox.warning(self,
                                        "pyschem",
                                        "The document {} has been modified\n"
                                        "Do you want to save your changes?".format(doc.name()),
                                        QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)
                if r == QMessageBox.Save:
                    return self.saveDoc(doc)
                elif r == QMessageBox.Cancel:
                    return False
        return True

    @pyqtSlot()
    def onInspectorChanged(self):
        if self.activeTab is not None:
            self.inspDock.setChild(self.activeTab.ctrl.inspector)
        else:
            self.inspDock.setChild(None)

    @pyqtSlot('bool', 'bool')
    def onTabUndoChanged(self, enUndo, enRedo):
        self.ui.actionUndo.setEnabled(enUndo)
        self.ui.actionRedo.setEnabled(enRedo)

    @pyqtSlot()
    def on_actionNew_triggered(self):
        self.docs.append(MasterDocument())
        self.docsChanged.emit()

    @pyqtSlot()
    def on_actionOpen_triggered(self):
        fn = QFileDialog.getOpenFileName(self)[0]
        if fn != "":
            for d in self.docs:
                if d.fileName == fn:
                    self.ui.statusbar.showMessage("Document already open", 5000)
                    return
            try:
                d = MasterDocument()
                d.loadFromFile(fn)
                self.docs.append(d)
                self.docsChanged.emit()
                self.ui.statusbar.showMessage("Document loaded", 5000)
            except Exception as e:
                self.ui.statusbar.showMessage("Error loading document: {}: {}".format(str(type(e)), str(e)), 5000)

    @pyqtSlot()
    def on_actionUndo_triggered(self):
        self.currentTab().undo()

    @pyqtSlot()
    def on_actionRedo_triggered(self):
        self.currentTab().redo()

    @pyqtSlot()
    def on_actionSave_triggered(self):
        return self.saveDoc(self.currentDoc())

    @pyqtSlot()
    def on_actionSave_As_triggered(self):
        return self.saveAsDoc(self.currentDoc())

    def installTab(self, newTab):
        # disconnect signals from old tab
        self.disconnectActiveTab()
        if newTab is not None:
            # restore active tool
            self.toolsDock.on_toolChanged(newTab.ctrl.toolType)
            # connect signals
            newTab.undoChanged.connect(self.onTabUndoChanged)
            self.toolsDock.toolChanged.connect(newTab.ctrl.changeTool)
            newTab.ctrl.sigToolChanged.connect(self.toolsDock.on_toolChanged)
            newTab.ctrl.sigInspectorChanged.connect(self.onInspectorChanged)
            self.onTabUndoChanged(newTab.canUndo(), newTab.canRedo())
            self.onInspectorChanged()
        else:
            self.onTabUndoChanged(False, False)
        self.activeTab = newTab

    def disconnectActiveTab(self):
        if self.activeTab is not None:
            self.activeTab.undoChanged.disconnect(self.onTabUndoChanged)
            self.toolsDock.toolChanged.disconnect(self.activeTab.ctrl.changeTool)
            self.activeTab.ctrl.sigToolChanged.disconnect(self.toolsDock.on_toolChanged)
            self.activeTab.ctrl.sigInspectorChanged.disconnect(self.onInspectorChanged)
        self.activeTab = None
        self.onInspectorChanged()

    @pyqtSlot(AbstractPage)
    def on_pageOpen(self, page):
        for i in range(self.ui.tabWidget.count()):
            t = self.ui.tabWidget.widget(i)
            if t.doc == page:
                # the tab is already open, switch to it
                self.ui.tabWidget.setCurrentIndex(i)
                return
        # tab was not found, create it
        tab = PageTab(page)
        self.installTab(tab)
        self.ui.tabWidget.addTab(tab, page.name)

    @pyqtSlot(int)
    def on_tabWidget_currentChanged(self, idx):
        tab = None
        if idx >= 0:
            tab = self.ui.tabWidget.widget(idx)
        self.installTab(tab)

    @pyqtSlot(int)
    def on_tabWidget_tabCloseRequested(self, idx):
        if self.ui.tabWidget.currentIndex() == idx:
            self.disconnectActiveTab()
        self.ui.tabWidget.removeTab(idx)
Exemplo n.º 4
0
class MainWindow(QMainWindow):
    tabUndo = pyqtSignal()
    tabRedo = pyqtSignal()
    docsChanged = pyqtSignal()

    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.actionUndo.setEnabled(False)
        self.ui.actionRedo.setEnabled(False)
        self.ui.actionUndo.triggered.connect(self.tabUndo)
        self.ui.actionUndo.triggered.connect(self.tabRedo)
        self.docs = []
        self.toolsDock = ToolsDock(self)
        self.projDock = ProjectDock(self)
        self.inspDock = InspectorDock(self)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.toolsDock)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.projDock)
        self.addDockWidget(Qt.RightDockWidgetArea, self.inspDock)
        self.docsChanged.connect(self.projDock.onDocsChanged)
        self.projDock.openPage.connect(self.on_pageOpen)
        self.activeTab = None
        self.loadSettings()

    def saveSettings(self):
        s = QSettings()
        s.setValue("MainWindow/state", self.saveState())
        s.setValue("MainWindow/geometry", self.saveGeometry())

    def loadSettings(self):
        s = QSettings()
        self.restoreGeometry(s.value("MainWindow/geometry", QByteArray()))
        self.restoreState(s.value("MainWindow/state", QByteArray()))

    def closeEvent(self, e):
        if self.saveAll():
            e.accept()
            self.saveSettings()
            super().closeEvent(e)
        else:
            e.ignore()

    def currentTab(self):
        return self.activeTab

    def currentDoc(self):
        if self.currentTab() is not None:
            return self.activeTab.doc.parentDoc
        return None

    def saveDoc(self, doc: MasterDocument):
        if doc is None:
            return False
        if doc.fileName is None:
            return self.saveAsDoc(doc)
        else:
            doc.saveToFile()
            self.ui.statusbar.showMessage("Document saved", 5000)
            return True

    def saveAsDoc(self, doc: MasterDocument):
        fn = QFileDialog.getSaveFileName(self)[0]
        if fn == "":
            return False
        doc.saveToFile(fn)
        self.ui.statusbar.showMessage("Document saved as {}".format(fn), 5000)
        return True

    def saveAll(self):
        for doc in self.docs:
            if doc.isModified():
                r = QMessageBox.warning(
                    self, "pyschem", "The document {} has been modified\n"
                    "Do you want to save your changes?".format(doc.name()),
                    QMessageBox.Save | QMessageBox.Discard
                    | QMessageBox.Cancel)
                if r == QMessageBox.Save:
                    return self.saveDoc(doc)
                elif r == QMessageBox.Cancel:
                    return False
        return True

    @pyqtSlot()
    def onInspectorChanged(self):
        if self.activeTab is not None:
            self.inspDock.setChild(self.activeTab.ctrl.inspector)
        else:
            self.inspDock.setChild(None)

    @pyqtSlot('bool', 'bool')
    def onTabUndoChanged(self, enUndo, enRedo):
        self.ui.actionUndo.setEnabled(enUndo)
        self.ui.actionRedo.setEnabled(enRedo)

    @pyqtSlot()
    def on_actionNew_triggered(self):
        self.docs.append(MasterDocument())
        self.docsChanged.emit()

    @pyqtSlot()
    def on_actionOpen_triggered(self):
        fn = QFileDialog.getOpenFileName(self)[0]
        if fn != "":
            for d in self.docs:
                if d.fileName == fn:
                    self.ui.statusbar.showMessage("Document already open",
                                                  5000)
                    return
            try:
                d = MasterDocument()
                d.loadFromFile(fn)
                self.docs.append(d)
                self.docsChanged.emit()
                self.ui.statusbar.showMessage("Document loaded", 5000)
            except Exception as e:
                self.ui.statusbar.showMessage(
                    "Error loading document: {}: {}".format(
                        str(type(e)), str(e)), 5000)

    @pyqtSlot()
    def on_actionUndo_triggered(self):
        self.currentTab().undo()

    @pyqtSlot()
    def on_actionRedo_triggered(self):
        self.currentTab().redo()

    @pyqtSlot()
    def on_actionSave_triggered(self):
        return self.saveDoc(self.currentDoc())

    @pyqtSlot()
    def on_actionSave_As_triggered(self):
        return self.saveAsDoc(self.currentDoc())

    def installTab(self, newTab):
        # disconnect signals from old tab
        self.disconnectActiveTab()
        if newTab is not None:
            # restore active tool
            self.toolsDock.on_toolChanged(newTab.ctrl.toolType)
            # connect signals
            newTab.undoChanged.connect(self.onTabUndoChanged)
            self.toolsDock.toolChanged.connect(newTab.ctrl.changeTool)
            newTab.ctrl.sigToolChanged.connect(self.toolsDock.on_toolChanged)
            newTab.ctrl.sigInspectorChanged.connect(self.onInspectorChanged)
            self.onTabUndoChanged(newTab.canUndo(), newTab.canRedo())
            self.onInspectorChanged()
        else:
            self.onTabUndoChanged(False, False)
        self.activeTab = newTab

    def disconnectActiveTab(self):
        if self.activeTab is not None:
            self.activeTab.undoChanged.disconnect(self.onTabUndoChanged)
            self.toolsDock.toolChanged.disconnect(
                self.activeTab.ctrl.changeTool)
            self.activeTab.ctrl.sigToolChanged.disconnect(
                self.toolsDock.on_toolChanged)
            self.activeTab.ctrl.sigInspectorChanged.disconnect(
                self.onInspectorChanged)
        self.activeTab = None
        self.onInspectorChanged()

    @pyqtSlot(AbstractPage)
    def on_pageOpen(self, page):
        for i in range(self.ui.tabWidget.count()):
            t = self.ui.tabWidget.widget(i)
            if t.doc == page:
                # the tab is already open, switch to it
                self.ui.tabWidget.setCurrentIndex(i)
                return
        # tab was not found, create it
        tab = PageTab(page)
        self.installTab(tab)
        self.ui.tabWidget.addTab(tab, page.name)

    @pyqtSlot(int)
    def on_tabWidget_currentChanged(self, idx):
        tab = None
        if idx >= 0:
            tab = self.ui.tabWidget.widget(idx)
        self.installTab(tab)

    @pyqtSlot(int)
    def on_tabWidget_tabCloseRequested(self, idx):
        if self.ui.tabWidget.currentIndex() == idx:
            self.disconnectActiveTab()
        self.ui.tabWidget.removeTab(idx)