예제 #1
0
    def __init__(self, document):
        """
        :type document: PyDesignDocument
        :param document:
        :return:
        """
        QMainWindow.__init__(self, None)
        main_windows.append(self)
        self.setStyleSheet(get_stylesheet("main"))
        self._active_analysis = None
        self._active_geometry = None
        self._document = document
        self.resize(1280, 800)
        self.setDockNestingEnabled(True)
        self.setWindowIcon(get_icon("pydesign"))

        # tree dock
        self._tree_dock_widget = QDockWidget(self)
        self._tree_dock_widget.setObjectName("treeDockWidget");
        self._tree_widget = PyDesignTreeWidget(self)
        self._tree_dock_widget.setWidget(self._tree_widget)
        self._tree_dock_widget.setWindowTitle("Document model")
        self.addDockWidget(Qt.LeftDockWidgetArea, self._tree_dock_widget)

        # detail doc
        self._detail_dock_widget = QDockWidget(self)
        self._detail_dock_widget.setObjectName("detailDockWidget");
        self._detail_widget = PyDesignDetailWidget(self, document)
        self._detail_dock_widget.setWidget(self._detail_widget)
        self._detail_dock_widget.setWindowTitle("Details")
        self.addDockWidget(Qt.RightDockWidgetArea, self._detail_dock_widget)

        # View doc
        self._view_dock_widget = QDockWidget(self)
        # self._view_dock_widget.setStyleSheet(get_stylesheet("tab"))
        self._view_dock_widget.setObjectName("ViewDockWidget");
        self._view_widget = PyDesignViewWidget(self, document)
        self._view_dock_widget.setWidget(self._view_widget)
        self._view_dock_widget.setWindowTitle("View")
        self.addDockWidget(Qt.RightDockWidgetArea, self._view_dock_widget)

        # actions

        self._new_action = self.add_action("New", "new", "New", True, self.on_new, QKSec.New)
        self._open_action = self.add_action("Open", "open", "Open file", True, self.on_open_file, QKSec.Open)
        self._save_action = self.add_action("Save", "save", "Save file", True, self.on_save_file, QKSec.Save)
        self._save_as_action = self.add_action("Save \nFile as", "save", "Save file with other file name", True, self.on_save_file_as, QKSec.SaveAs)
        self._copy_action = self.add_action("Copy", "copy", "Copy selection", True, self.on_copy, QKSec.Copy)
        self._undo_action = self.add_action("Undo", "undo", "Undo last action", True, self.on_undo, QKSec.Undo)
        self._redo_action = self.add_action("Redo", "redo", "Redo last undone action", True, self.on_redo, QKSec.Redo)
        self._paste_action = self.add_action("Paste", "paste", "Paste from clipboard", True, self.on_paste, QKSec.Paste)
        self._about_action = self.add_action("About", "about", "About QupyRibbon", True, self.on_about)
        self._license_action = self.add_action("License", "license", "Licence for this software", True, self.on_license)
        self._exit_action = self.add_action("Exit", "exit", "exit now", True, self.on_exit)
        self._add_analysis_action = self.add_action("Add\nAnalysis", "addAnalysis", "Add layout to document", True, self.on_add_analysis)
        self._add_calc_sheet_action = self.add_action("Add\nCalc sheet", "addCalcsheet", "Add Calculation sheet to analysis", True, self.on_add_calc_sheet)
        self._add_geometry_action = self.add_action("Add\nGeometry", "addGeometry", "Add geometry to document", True, self.on_add_geometry)
        self._add_draft_action = self.add_action("Add\nDraft", "addDraft", "Add draft to geometry", True, self.on_add_draft)
        self._add_extrude_action = self.add_action("Extrude\nprofile", "extrude", "Extrude solid from profile", True, None)
        self._add_revolve_action = self.add_action("Revolve\nprofile", "revolve", "Add revolved solid from profile", True, None)
        self._add_polyline_action = self.add_action("Add\nlines", "addLines", "Add polyline to selected draft", True, None)
        self._add_circle_action = self.add_action("Add\ncircle", "addCircle", "Add circle to selected draft", True, None)
        self._add_fillet_action = self.add_action("Add\nfillet", "addFillet", "Add fillet to selected draft", True, None)

        # Status bar

        self.statusBar().showMessage("Ready")
        progress_bar = QProgressBar()
        progress_bar.setMaximumHeight(20)
        progress_bar.setMaximumWidth(300)
        progress_bar.setTextVisible(False)
        self.statusBar().addPermanentWidget(progress_bar, 0);
        progress_bar.setValue(100)

        # toolbar

        self._ribbon = RibbonWidget(self)
        self.addToolBar(self._ribbon)
        self.init_ribbon()

        self.read_settings()
        self.update_actions()
예제 #2
0
class PyDesignMainWindow(QMainWindow):
    def __init__(self, document):
        """
        :type document: PyDesignDocument
        :param document:
        :return:
        """
        QMainWindow.__init__(self, None)
        main_windows.append(self)
        self.setStyleSheet(get_stylesheet("main"))
        self._active_analysis = None
        self._active_geometry = None
        self._document = document
        self.resize(1280, 800)
        self.setDockNestingEnabled(True)
        self.setWindowIcon(get_icon("pydesign"))

        # tree dock
        self._tree_dock_widget = QDockWidget(self)
        self._tree_dock_widget.setObjectName("treeDockWidget");
        self._tree_widget = PyDesignTreeWidget(self)
        self._tree_dock_widget.setWidget(self._tree_widget)
        self._tree_dock_widget.setWindowTitle("Document model")
        self.addDockWidget(Qt.LeftDockWidgetArea, self._tree_dock_widget)

        # detail doc
        self._detail_dock_widget = QDockWidget(self)
        self._detail_dock_widget.setObjectName("detailDockWidget");
        self._detail_widget = PyDesignDetailWidget(self, document)
        self._detail_dock_widget.setWidget(self._detail_widget)
        self._detail_dock_widget.setWindowTitle("Details")
        self.addDockWidget(Qt.RightDockWidgetArea, self._detail_dock_widget)

        # View doc
        self._view_dock_widget = QDockWidget(self)
        # self._view_dock_widget.setStyleSheet(get_stylesheet("tab"))
        self._view_dock_widget.setObjectName("ViewDockWidget");
        self._view_widget = PyDesignViewWidget(self, document)
        self._view_dock_widget.setWidget(self._view_widget)
        self._view_dock_widget.setWindowTitle("View")
        self.addDockWidget(Qt.RightDockWidgetArea, self._view_dock_widget)

        # actions

        self._new_action = self.add_action("New", "new", "New", True, self.on_new, QKSec.New)
        self._open_action = self.add_action("Open", "open", "Open file", True, self.on_open_file, QKSec.Open)
        self._save_action = self.add_action("Save", "save", "Save file", True, self.on_save_file, QKSec.Save)
        self._save_as_action = self.add_action("Save \nFile as", "save", "Save file with other file name", True, self.on_save_file_as, QKSec.SaveAs)
        self._copy_action = self.add_action("Copy", "copy", "Copy selection", True, self.on_copy, QKSec.Copy)
        self._undo_action = self.add_action("Undo", "undo", "Undo last action", True, self.on_undo, QKSec.Undo)
        self._redo_action = self.add_action("Redo", "redo", "Redo last undone action", True, self.on_redo, QKSec.Redo)
        self._paste_action = self.add_action("Paste", "paste", "Paste from clipboard", True, self.on_paste, QKSec.Paste)
        self._about_action = self.add_action("About", "about", "About QupyRibbon", True, self.on_about)
        self._license_action = self.add_action("License", "license", "Licence for this software", True, self.on_license)
        self._exit_action = self.add_action("Exit", "exit", "exit now", True, self.on_exit)
        self._add_analysis_action = self.add_action("Add\nAnalysis", "addAnalysis", "Add layout to document", True, self.on_add_analysis)
        self._add_calc_sheet_action = self.add_action("Add\nCalc sheet", "addCalcsheet", "Add Calculation sheet to analysis", True, self.on_add_calc_sheet)
        self._add_geometry_action = self.add_action("Add\nGeometry", "addGeometry", "Add geometry to document", True, self.on_add_geometry)
        self._add_draft_action = self.add_action("Add\nDraft", "addDraft", "Add draft to geometry", True, self.on_add_draft)
        self._add_extrude_action = self.add_action("Extrude\nprofile", "extrude", "Extrude solid from profile", True, None)
        self._add_revolve_action = self.add_action("Revolve\nprofile", "revolve", "Add revolved solid from profile", True, None)
        self._add_polyline_action = self.add_action("Add\nlines", "addLines", "Add polyline to selected draft", True, None)
        self._add_circle_action = self.add_action("Add\ncircle", "addCircle", "Add circle to selected draft", True, None)
        self._add_fillet_action = self.add_action("Add\nfillet", "addFillet", "Add fillet to selected draft", True, None)

        # Status bar

        self.statusBar().showMessage("Ready")
        progress_bar = QProgressBar()
        progress_bar.setMaximumHeight(20)
        progress_bar.setMaximumWidth(300)
        progress_bar.setTextVisible(False)
        self.statusBar().addPermanentWidget(progress_bar, 0);
        progress_bar.setValue(100)

        # toolbar

        self._ribbon = RibbonWidget(self)
        self.addToolBar(self._ribbon)
        self.init_ribbon()

        self.read_settings()
        self.update_actions()

    def init_ribbon(self):
        home_tab = self._ribbon.add_ribbon_tab("Home")
        file_pane = home_tab.add_ribbon_pane("File")
        file_pane.add_ribbon_widget(RibbonButton(self, self._new_action, True))
        file_pane.add_ribbon_widget(RibbonButton(self, self._open_action, True))
        file_pane.add_ribbon_widget(RibbonButton(self, self._save_action, True))
        file_pane.add_ribbon_widget(RibbonButton(self, self._save_as_action, True))

        edit_panel = home_tab.add_ribbon_pane("Edit")
        edit_panel.add_ribbon_widget(RibbonButton(self, self._copy_action, True))
        edit_panel.add_ribbon_widget(RibbonButton(self, self._paste_action, True))
        grid = edit_panel.add_grid_widget(200)
        grid.addWidget(RibbonButton(self, self._undo_action, False))
        grid.addWidget(RibbonButton(self, self._redo_action, False))
        edit_panel.add_ribbon_widget(RibbonButton(self, self._add_analysis_action, True))
        edit_panel.add_ribbon_widget(RibbonButton(self, self._add_calc_sheet_action, True))
        edit_panel.add_ribbon_widget(RibbonButton(self, self._add_geometry_action, True))

        view_panel = home_tab.add_ribbon_pane("View")
        home_tab.add_spacer()
        calc_tab = self._ribbon.add_ribbon_tab("Calculate")
        geom_tab = self._ribbon.add_ribbon_tab("Geometry")
        add_feature_pane = geom_tab.add_ribbon_pane("Add feature")
        add_feature_pane.add_ribbon_widget(RibbonButton(self, self._add_draft_action, True))
        add_feature_pane.add_ribbon_widget(RibbonButton(self, self._add_extrude_action, True))
        add_feature_pane.add_ribbon_widget(RibbonButton(self, self._add_revolve_action, True))

        draft_tab = self._ribbon.add_ribbon_tab("Draft")
        simples_pane = draft_tab.add_ribbon_pane("Simples")
        simples_pane.add_ribbon_widget(RibbonButton(self, self._add_polyline_action, True))
        simples_pane.add_ribbon_widget(RibbonButton(self, self._add_circle_action, True))
        simples_pane.add_ribbon_widget(RibbonButton(self, self._add_fillet_action, True))

        about_tab = self._ribbon.add_ribbon_tab("About")
        info_panel = about_tab.add_ribbon_pane("Info")
        info_panel.add_ribbon_widget(RibbonButton(self, self._about_action, True))
        info_panel.add_ribbon_widget(RibbonButton(self, self._license_action, True))

    def add_action(self, caption, icon_name, status_tip, icon_visible, connection, shortcut=None):
        action = QAction(get_icon(icon_name), caption, self)
        action.setStatusTip(status_tip)
        if connection is not None:
            action.triggered.connect(connection)
        action.setIconVisibleInMenu(icon_visible)
        if shortcut is not None:
            action.setShortcuts(shortcut)
        self.addAction(action)
        return action

    @property
    def document(self):
        return self._document

    @property
    def models(self):
        return self._models

    def closeEvent(self, event):
        if self.maybe_save():
            self.write_settings()
            event.accept()
            main_windows.remove(self)
            if len(main_windows) == 0:
                sys.exit(1)
        else:
            event.ignore()

    def update_actions(self):
        analysisActive = not self._active_analysis is None
        self._add_geometry_action.setEnabled(analysisActive)
        self._add_calc_sheet_action.setEnabled(analysisActive)
        self._undo_action.setEnabled(False)
        self._redo_action.setEnabled(False)
        return

    def on_rename_pipe(self):
        self._document.pipe(0).name = "Magnus"
        return

    def on_add_calc_sheet(self):
        PyDesignEventHandlers.on_add_sheet(self._active_analysis)

    def on_add_analysis(self):
        PyDesignEventHandlers.on_add_analysis(self._document, self)

    def on_add_geometry(self):
        PyDesignEventHandlers.on_add_geometry(self._active_analysis, self)

    def read_settings(self):
        settings = QSettings("pracedru", "PyDesign")
        pos = settings.value("pos", QPoint(200, 200))
        size = settings.value("size", QSize(400, 400))
        self.resize(size)
        self.move(pos)
        docks_settings_data = settings.value("docks")
        if docks_settings_data is not None:
            self.restoreState(docks_settings_data, 1)

    def write_settings(self):
        settings = QSettings("pracedru", "PyDesign")
        settings.setValue("pos", self.pos())
        settings.setValue("size", self.size())
        settings.setValue("docks", self.saveState(1))

    def maybe_save(self):
        if self.is_modified():
            ret = QMessageBox.warning(self, "Application",
                                      "The document has been modified.\nDo you want to save "
                                      "your changes?",
                                      QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel)

            if ret == QMessageBox.Save:
                return self.on_save_file()

            if ret == QMessageBox.Cancel:
                return False

        return True

    def is_modified(self):
        return self._document.is_modified()

    def on_exit(self):
        self.close()
        return

    def on_open_file(self):
        docs_location = QStandardPaths.standardLocations(QStandardPaths.DocumentsLocation)
        default_path = ""
        if self._document.path == "":
            default_path = docs_location[0]
        file_name = QFileDialog.getOpenFileName(self, 'Open file', default_path,
                                                "PyDesign File (*.pds);;Text files (*.json)")
        doc = deserialize_document_from_disk(file_name[0])
        if doc is not None:
            main_window = PyDesignMainWindow(doc)
            main_window.show()
            if (self._document.is_modified() is False) and self._document.path == "":
                self.close()

        return

    def on_save_file(self):
        if self._document.path != "":
            serialize_document_to_disk(self._document)
            self.statusBar().showMessage("File Saved", 5000)
        else:
            self.on_save_file_as()
            return True
        return False

    def on_save_file_as(self):
        docs_location = QStandardPaths.standardLocations(QStandardPaths.DocumentsLocation)
        default_path = ""
        if self._document.path == "":
            default_path = docs_location[0]
        file_types = "PyDesign File(*.pds);;PyDesign text File(*.json)"
        file_name = QFileDialog.getSaveFileName(self, "Save file", default_path, file_types)
        if file_name[0] != "":
            if ".json" in file_name[0] or ".pds" in file_name[0]:
                file_info = QFileInfo(file_name[0])
            else:
                file_info = QFileInfo(file_name[0] + file_name[1].replace("*", ""))
            self._document.path = file_info.absolutePath()
            self._document.name = file_info.fileName()
            self.on_save_file()
            self.setWindowTitle("%s[*] - PyDesign" % self._document.name)
            return True
        return False

    def on_selection_changed(self, model_item):
        if type(model_item) is PyDesignParameterItem:
            self._detail_widget.on_selection_changed(model_item.parent.data_object)
        else:
            self._detail_widget.on_selection_changed(model_item.data_object)

        if is_analysis_sub_item(model_item):
            self._active_analysis = get_analysis_from_item(model_item)
            self._view_widget.set_view_object(self._active_analysis)
            activate_home_tab = True

            if is_geometry_sub_item(model_item):
                self._view_widget.set_geometry_view_active()
                self._ribbon.set_active("Geometry")
                activate_home_tab = False

            if is_sheets_sub_item(model_item):
                self._view_widget.set_sheets_view_active()
                self._ribbon.set_active("Calculate")
                activate_home_tab = False

            if activate_home_tab:
                self._ribbon.set_active("Home")

        self.update_actions()

    def on_undo(self):
        self._document.undo()

    def on_redo(self):
        self._document.redo()

    def on_context_menu(self, model_item):
        model_item.on_context_menu()

    def on_add_draft(self):
        pass

    def on_copy(self):
        pass

    def on_paste(self):
        pass

    def on_about(self):
        text = "QupyRibbon\n"
        text += "This program was made by Magnus Jørgensen.\n"
        text += "Copyright © 2016 Magnus Jørgensen"
        QMessageBox().about(self, "About QupyRibbon", text)

    def on_license(self):
        file = open('LICENSE', 'r')
        lic = file.read()
        QMessageBox().information(self, "License", lic)

    def on_new(self):
        document = PyDesignDocument("New Document")
        main_window = PyDesignMainWindow(document)
        main_window.show()