Esempio n. 1
0
    def __init__(self, parent=None):
        super().__init__(parent)

        # initialize class attributes
        self._INPUTS_FIXED_WIDTH = 180
        self._BUTTON_MIN_WIDTH = 80
        self._OXYGEN_PATH_32 = os.path.join("resources", "icons", "oxygen",
                                            "32")

        # locale and language settings
        self.language = self.locale().name()[:2]
        self.qtTl = QTranslator()
        self.appTl = QTranslator()
        self.switchTranslator(self.qtTl, "qtbase", self.language)
        self.switchTranslator(self.appTl, "wuchshuellenrechner", self.language)

        # TODO(th)
        self.model = TreeModel()

        # setup gui
        self.setMinimumSize(1200, 760)

        self.createActions()
        self.createMainMenu()

        # plot widget
        self.plotWidget = VariantPlotView()
        self.plotWidget.setModel(self.model)

        # create vertical splitter
        splitter = QSplitter(Qt.Vertical)
        splitter.setContentsMargins(11, 11, 11, 11)
        splitter.addWidget(self.createEditBox())
        splitter.addWidget(self.plotWidget)

        self.setCentralWidget(splitter)

        self.dataWidget.setModel(self.model)

        selectionModel = QItemSelectionModel(self.model)
        self.dataWidget.setSelectionModel(selectionModel)
        self.plotWidget.setSelectionModel(selectionModel)

        self.retranslateUi()

        self.model.dataChanged.connect(self.updateInputs)
        self.model.itemsInserted.connect(self.taxInput.setDisabled)
        self.model.allItemsRemoved.connect(self.taxInput.setEnabled)
        self.operationInput.textChanged.connect(self.updateOperation)
        self.districtInput.textChanged.connect(self.updateDistrict)
        self.managerInput.textChanged.connect(self.updateManager)
        self.locationInput.textChanged.connect(self.updateLocation)
        self.taxInput.stateChanged.connect(self.updateTax)
    def __init__(self, parent=None):
        super().__init__(parent)

        # initialize class attributes
        self._INPUTS_FIXED_WIDTH = 180
        self._BUTTON_MIN_WIDTH = 80
        self._OXYGEN_PATH_32 = os.path.join("resources", "icons", "oxygen", "32")

        # locale and language settings
        self.language = self.locale().name()[:2]
        self.qtTl = QTranslator()
        self.appTl = QTranslator()
        self.switchTranslator(self.qtTl, "qtbase", self.language)
        self.switchTranslator(self.appTl, "wuchshuellenrechner", self.language)

        # TODO(th)
        self.model = TreeModel()

        # setup gui
        self.setMinimumSize(1200, 760)

        self.createActions()
        self.createMainMenu()

        # plot widget
        self.plotWidget = VariantPlotView()
        self.plotWidget.setModel(self.model)

        # create vertical splitter
        splitter = QSplitter(Qt.Vertical)
        splitter.setContentsMargins(11, 11, 11, 11)
        splitter.addWidget(self.createEditBox())
        splitter.addWidget(self.plotWidget)

        self.setCentralWidget(splitter)

        self.dataWidget.setModel(self.model)

        selectionModel = QItemSelectionModel(self.model)
        self.dataWidget.setSelectionModel(selectionModel)
        self.plotWidget.setSelectionModel(selectionModel)

        self.retranslateUi()


        self.model.dataChanged.connect(self.updateInputs)
        self.model.itemsInserted.connect(self.taxInput.setDisabled)
        self.model.allItemsRemoved.connect(self.taxInput.setEnabled)
        self.operationInput.textChanged.connect(self.updateOperation)
        self.districtInput.textChanged.connect(self.updateDistrict)
        self.managerInput.textChanged.connect(self.updateManager)
        self.locationInput.textChanged.connect(self.updateLocation)
        self.taxInput.stateChanged.connect(self.updateTax)
Esempio n. 3
0
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        # initialize class attributes
        self._INPUTS_FIXED_WIDTH = 180
        self._BUTTON_MIN_WIDTH = 80
        self._OXYGEN_PATH_32 = os.path.join("resources", "icons", "oxygen",
                                            "32")

        # locale and language settings
        self.language = self.locale().name()[:2]
        self.qtTl = QTranslator()
        self.appTl = QTranslator()
        self.switchTranslator(self.qtTl, "qtbase", self.language)
        self.switchTranslator(self.appTl, "wuchshuellenrechner", self.language)

        # TODO(th)
        self.model = TreeModel()

        # setup gui
        self.setMinimumSize(1200, 760)

        self.createActions()
        self.createMainMenu()

        # plot widget
        self.plotWidget = VariantPlotView()
        self.plotWidget.setModel(self.model)

        # create vertical splitter
        splitter = QSplitter(Qt.Vertical)
        splitter.setContentsMargins(11, 11, 11, 11)
        splitter.addWidget(self.createEditBox())
        splitter.addWidget(self.plotWidget)

        self.setCentralWidget(splitter)

        self.dataWidget.setModel(self.model)

        selectionModel = QItemSelectionModel(self.model)
        self.dataWidget.setSelectionModel(selectionModel)
        self.plotWidget.setSelectionModel(selectionModel)

        self.retranslateUi()

        self.model.dataChanged.connect(self.updateInputs)
        self.model.itemsInserted.connect(self.taxInput.setDisabled)
        self.model.allItemsRemoved.connect(self.taxInput.setEnabled)
        self.operationInput.textChanged.connect(self.updateOperation)
        self.districtInput.textChanged.connect(self.updateDistrict)
        self.managerInput.textChanged.connect(self.updateManager)
        self.locationInput.textChanged.connect(self.updateLocation)
        self.taxInput.stateChanged.connect(self.updateTax)

    def updateOperation(self, value):
        self.model.setProjectData("operation", value)

    def updateDistrict(self, value):
        self.model.setProjectData("district", value)

    def updateManager(self, value):
        self.model.setProjectData("manager", value)

    def updateLocation(self, value):
        self.model.setProjectData("location", value)

    def updateTax(self):
        self.model.setProjectData("tax", self.taxInput.isChecked())
        self.dataWidget.tax = self.taxInput.isChecked()

    def updateInputs(self):
        self.operationInput.setText(self.model.projectData("operation"))
        self.districtInput.setText(self.model.projectData("district"))
        self.managerInput.setText(self.model.projectData("manager"))
        self.locationInput.setText(self.model.projectData("location"))

        self.taxInput.setChecked(self.model.projectData("tax"))

    def loadLanguage(self, language):
        if not self.language == language:
            self.language = language

            locale = QLocale(language)
            QLocale.setDefault(locale)

            self.switchTranslator(self.qtTl, "qtbase", language)
            self.switchTranslator(self.appTl, "wuchshuellenrechner", language)

            # update gui
            self.retranslateUi()
            self.dataWidget.retranslateUi()

    def switchTranslator(self, translator, filename, language):
        app = QApplication.instance()

        # remove the old translator and
        # try to load a new language file
        app.removeTranslator(translator)
        if translator.load(QLocale(), filename, "_", "language"):
            app.installTranslator(translator)

    def setTaxEnabled(self, enable):
        # enable buttons and the view element
        self.removeButton.setEnabled(enable)
        self.modifyButton.setEnabled(enable)
        self.view.setEnabled(enable)

    def setDisabled(self, disable):
        # internaly call the setEnabled function
        # therefore negate the boolean value
        self.setEnabled(not disable)

    def aboutAction(self):
        aboutDialog = AboutDialog()
        aboutDialog.exec()

    def clear(self):
        # first clear the model
        self.model.clear()

        # update fields with model's default values
        self.operationInput.setText(self.model.projectData("operation"))
        self.districtInput.setText(self.model.projectData("district"))
        self.managerInput.setText(self.model.projectData("manager"))
        self.locationInput.setText(self.model.projectData("location"))

        self.taxInput.setEnabled(True)
        self.taxInput.setChecked(self.model.projectData("tax"))

        # reset the tree view
        self.dataWidget.clear()

        # reset the chart and result view
        # actually this is necassary
        self.plotWidget.clear()

    def closeAction(self):
        if self.model.changed:
            question = QMessageBox()
            question.setIcon(QMessageBox.Question)
            question.setWindowTitle(
                "Berechnung speichern? - Wuchshüllenrechner")
            question.setText(
                "Möchten Sie die Änderungen an dieser Berechnung speichern?")
            question.setInformativeText(
                "Die Änderungen gehen verloren, wenn Sie nicht speichern.")
            question.setStandardButtons(QMessageBox.Save | QMessageBox.Discard
                                        | QMessageBox.Cancel)
            question.setDefaultButton(QMessageBox.Save)
            reply = question.exec()

            if not reply == QMessageBox.Cancel:
                if reply == QMessageBox.Save:
                    if self.saveAction():
                        self.close()
                else:
                    self.close()
        else:
            self.close()

    def newAction(self):
        if self.model.changed:
            question = QMessageBox()
            question.setIcon(QMessageBox.Question)
            question.setWindowTitle(
                "Berechnung speichern? - Wuchshüllenrechner")
            question.setText(
                "Möchten Sie die Änderungen an dieser Berechnung speichern?")
            question.setInformativeText(
                "Die Änderungen gehen verloren, wenn Sie nicht speichern.")
            question.setStandardButtons(QMessageBox.Save | QMessageBox.Discard
                                        | QMessageBox.Cancel)
            question.setDefaultButton(QMessageBox.Save)
            reply = question.exec()

            if not reply == QMessageBox.Cancel:
                if reply == QMessageBox.Save:
                    if self.saveAction():
                        # clear all
                        self.clear()
                else:
                    # clear all
                    self.clear()

    def checkSaveState(self):
        if self.model.changed:
            question = QMessageBox(self)
            question.setWindowModality(Qt.WindowModal)  # check for mac only
            question.setIcon(QMessageBox.Warning)
            question.setStandardButtons(QMessageBox.Save | QMessageBox.Discard
                                        | QMessageBox.Cancel)
            question.setDefaultButton(QMessageBox.Save)
            question.setWindowTitle(
                QApplication.translate("MainWindow", "Wuchshüllenrechner"))
            question.setText("<b>" + QApplication.translate(
                "MainWindow", "Do you want to save the changes you made<br>"
                "to the current calculation?") + "</b>")
            question.setInformativeText(
                QApplication.translate(
                    "MainWindow",
                    "Your changes will be lost if you don't save them."))

            reply = question.exec()
            if reply == QMessageBox.Save:
                if self.saveAction():
                    #self.model.clear()
                    self.clear()
                    return True
                else:
                    return False
            elif reply == QMessageBox.Discard:
                #self.model.clear()
                self.clear()
                return True
            else:
                return False

        # always clear
        self.clear()

        return True

    def loadExample(self, example=""):
        # create the path
        exampleFile = os.path.join("examples", example.strip() + ".xml")

        # if all is save, open the example file
        if self.checkSaveState():
            self.model.readFile(exampleFile)

    def mapleAction(self):
        self.loadExample("great_maple")

    def douglasAction(self):
        self.loadExample("douglas_fir")

    def openAction(self):
        # first check save state of the current calculation
        if self.checkSaveState():
            dialog = QFileDialog(self)
            dialog.setWindowModality(Qt.WindowModal)  # check for mac only
            dialog.setWindowTitle(
                QApplication.translate("MainWindow", "Open Calculation"))
            dialog.setDirectory(os.path.expanduser("~"))
            dialog.setNameFilter(
                QApplication.translate("MainWindow",
                                       "XML files (*.xml);;All Files (*)"))

            dialog.exec()
            filename = dialog.selectedFiles()
            if filename:
                self.model.readFile(filename.pop())

    def saveAction(self):
        if self.model.file:
            return self.model.saveFile()
        else:
            return self.saveAsAction()

    def saveAsAction(self):
        dialog = QFileDialog(self)
        dialog.setWindowModality(Qt.WindowModal)  # check for mac only
        dialog.setAcceptMode(QFileDialog.AcceptSave)
        dialog.setWindowTitle(
            QApplication.translate("MainWindow", "Save Calculation"))
        dialog.setDirectory(os.path.expanduser("~"))
        dialog.setNameFilter(
            QApplication.translate("MainWindow",
                                   "XML files (*.xml);;All Files (*)"))

        dialog.exec()
        filename = dialog.selectedFiles()
        if filename:
            self.model.saveFile(filename.pop())
            return True
        else:
            return False

    def exportAction(self):
        dialog = QFileDialog(self)
        dialog.setWindowModality(Qt.WindowModal)  # check for mac only
        dialog.setAcceptMode(QFileDialog.AcceptSave)
        dialog.setWindowTitle(
            QApplication.translate("MainWindow", "Export Calculation"))
        dialog.setDirectory(os.path.expanduser("~"))
        dialog.setNameFilter(
            QApplication.translate(
                "MainWindow",
                "PNG files (*.png);;JPG files (*.jpg);;TIFF files (*.tif)"))

        if dialog.exec():
            filename = dialog.selectedFiles()
            self.plotWidget.plotWidget.plotWidget.export(filename.pop())

    def helpAction(self):
        # create the documentation path with
        # the current locale settings
        docFile = os.path.join("doc",
                               "documentation_" + self.language + ".pdf")

        # on every platfrom a different
        # start operation is needed
        if sys.platform == "win32":
            os.startfile(docFile)
        elif sys.platform == "darwin":
            subprocess.call(("open", docFile))
        elif sys.platform.startswith("linux"):
            subprocess.call(("xdg-open", docFile))

    def scientificAction(self):
        scientificDialog = ScientificBasisDialog()
        scientificDialog.exec()

    def germanAction(self):
        # update language menu
        self.languageEnglish.setChecked(False)
        self.languageGerman.setChecked(True)

        # update gui
        self.loadLanguage("de")

    def englishAction(self):
        # update language menu
        self.languageGerman.setChecked(False)
        self.languageEnglish.setChecked(True)

        # update gui
        self.loadLanguage("en")

    def createActions(self):
        # all actions for the calculation menu
        # create the new calculation action
        self.calculationNew = QAction(self,
                                      icon=QIcon(
                                          os.path.join(self._OXYGEN_PATH_32,
                                                       "document-new.png")),
                                      shortcut=QKeySequence.New,
                                      triggered=self.newAction)

        # create the open calculation action
        self.calculationOpen = QAction(
            self,
            icon=QIcon(os.path.join(self._OXYGEN_PATH_32,
                                    "document-open.png")),
            shortcut=QKeySequence.Open,
            triggered=self.openAction)

        # create the open calculation example actions
        # for great maple and douglas fir
        self.examplesMaple = QAction(self, triggered=self.mapleAction)
        self.examplesDouglas = QAction(self, triggered=self.douglasAction)

        # create the save calculation action
        self.calculationSave = QAction(
            self,
            icon=QIcon(os.path.join(self._OXYGEN_PATH_32,
                                    "document-save.png")),
            shortcut=QKeySequence.Save,
            triggered=self.saveAction)

        # create the save as action
        self.calculationSaveAs = QAction(self,
                                         icon=QIcon(
                                             os.path.join(
                                                 self._OXYGEN_PATH_32,
                                                 "document-save-as.png")),
                                         shortcut=QKeySequence.SaveAs,
                                         triggered=self.saveAsAction)

        # create the print calculation action
        #self.calculationPrint = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "document-print.png")),
        #        shortcut=QKeySequence.Print, triggered=self.printAction)

        # create the export calculation action
        self.calculationExport = QAction(
            self,
            icon=QIcon(
                os.path.join(self._OXYGEN_PATH_32, "document-export.png")),
            shortcut=QKeySequence(Qt.CTRL + Qt.Key_E),
            triggered=self.exportAction)

        # create the quit calculation action
        self.calculationQuit = QAction(self,
                                       icon=QIcon(
                                           os.path.join(
                                               self._OXYGEN_PATH_32,
                                               "application-exit.png")),
                                       shortcut=QKeySequence.Quit,
                                       triggered=self.closeAction)

        # all actions for the language menu
        # create the german and the english language actions
        self.languageGerman = QAction(self,
                                      checkable=True,
                                      triggered=self.germanAction)
        self.languageEnglish = QAction(self,
                                       checkable=True,
                                       triggered=self.englishAction)

        # update the language menu
        if self.language == "de":
            self.languageGerman.setChecked(True)
        elif self.language == "en":
            self.languageEnglish.setChecked(True)

        # create the help contents action
        self.helpContents = QAction(self,
                                    icon=QIcon(
                                        os.path.join(self._OXYGEN_PATH_32,
                                                     "help-contents.png")),
                                    triggered=self.helpAction)

        # create the scientific basis action
        self.scientificBasis = QAction(self,
                                       icon=QIcon(
                                           os.path.join(
                                               self._OXYGEN_PATH_32,
                                               "applications-science.png")),
                                       triggered=self.scientificAction)

        # create the help about action
        self.helpAbout = QAction(self,
                                 icon=QIcon(
                                     os.path.join(self._OXYGEN_PATH_32,
                                                  "dialog-information.png")),
                                 triggered=self.aboutAction)

    def createMainMenu(self):
        # create main menu bar
        self.menu = self.menuBar()

        # create file menu
        self.calculationMenu = QMenu(self)
        self.calculationMenu.addAction(self.calculationNew)
        self.calculationMenu.addAction(self.calculationOpen)

        self.examplesMenu = QMenu(self,
                                  icon=QIcon(
                                      os.path.join(self._OXYGEN_PATH_32,
                                                   "folder-documents.png")))
        self.examplesMenu.addAction(self.examplesMaple)
        self.examplesMenu.addAction(self.examplesDouglas)
        self.calculationMenu.addMenu(self.examplesMenu)
        self.calculationMenu.addSeparator()

        self.calculationMenu.addAction(self.calculationSave)
        self.calculationMenu.addAction(self.calculationSaveAs)
        self.calculationMenu.addAction(self.calculationExport)
        self.calculationMenu.addSeparator()

        #self.calculationMenu.addAction(self.calculationPrint)
        #self.calculationMenu.addSeparator()

        self.calculationMenu.addAction(self.calculationQuit)

        self.menu.addMenu(self.calculationMenu)

        # create language menu
        self.languageMenu = QMenu(self)
        self.languageMenu.addAction(self.languageGerman)
        self.languageMenu.addAction(self.languageEnglish)
        self.menu.addMenu(self.languageMenu)

        # create help menu
        self.helpMenu = QMenu(self)
        self.helpMenu.addAction(self.helpContents)
        self.helpMenu.addAction(self.scientificBasis)
        self.helpMenu.addSeparator()
        self.helpMenu.addAction(self.helpAbout)
        self.menu.addMenu(self.helpMenu)

    def createEditBox(self):
        # create group box
        self.editBox = QGroupBox()
        self.editBox.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum)
        gridLayout = QGridLayout(self.editBox)

        # create input fields
        self.operationInput = QLineEdit(self.model.projectData("operation"))
        self.operationInput.setFixedWidth(self._INPUTS_FIXED_WIDTH)
        self.operationInput.setClearButtonEnabled(True)
        self.operationLabel = QLabel()
        self.operationLabel.setBuddy(self.operationInput)
        gridLayout.addWidget(self.operationLabel, 0, 0)
        gridLayout.addWidget(self.operationInput, 0, 1)

        self.districtInput = QLineEdit(self.model.projectData("district"))
        self.districtInput.setFixedWidth(self._INPUTS_FIXED_WIDTH)
        self.districtInput.setClearButtonEnabled(True)
        self.districtLabel = QLabel()
        self.districtLabel.setBuddy(self.districtInput)
        gridLayout.addWidget(self.districtLabel, 0, 2)
        gridLayout.addWidget(self.districtInput, 0, 3)

        self.managerInput = QLineEdit(self.model.projectData("manager"))
        self.managerInput.setFixedWidth(self._INPUTS_FIXED_WIDTH)
        self.managerInput.setClearButtonEnabled(True)
        self.managerLabel = QLabel()
        self.managerLabel.setBuddy(self.managerInput)
        gridLayout.addWidget(self.managerLabel, 1, 0)
        gridLayout.addWidget(self.managerInput, 1, 1)

        self.locationInput = QLineEdit(self.model.projectData("location"))
        self.locationInput.setFixedWidth(self._INPUTS_FIXED_WIDTH)
        self.locationInput.setClearButtonEnabled(True)
        self.locationLabel = QLabel()
        self.locationLabel.setBuddy(self.locationInput)
        gridLayout.addWidget(self.locationLabel, 1, 2)
        gridLayout.addWidget(self.locationInput, 1, 3)

        lineFrame = QFrame(frameShadow=QFrame.Sunken, frameShape=QFrame.VLine)
        gridLayout.addWidget(lineFrame, 0, 4, 2, 1)

        self.taxInput = QCheckBox()
        self.taxInput.setChecked(self.model.projectData("tax"))
        self.taxLabel = QLabel()
        self.taxLabel.setBuddy(self.taxInput)
        self.taxHint = ToolTipLabel()
        gridLayout.addWidget(self.taxLabel, 0, 5)
        gridLayout.addWidget(self.taxInput, 0, 6)
        gridLayout.addWidget(self.taxHint, 0, 7, Qt.AlignRight)
        gridLayout.setColumnMinimumWidth(7, 40)

        rightSpacer = QSpacerItem(20, 0, QSizePolicy.Expanding,
                                  QSizePolicy.Fixed)
        gridLayout.addItem(rightSpacer, 0, 8, 2, 1)

        self.dataWidget = EnhancedTreeWidget()
        gridLayout.addWidget(self.dataWidget, 2, 0, 1, 9)

        return self.editBox

    def retranslateUi(self):
        # main window title
        self.setWindowTitle(
            QApplication.translate("MainWindow",
                                   "Wuchshüllenrechner"))  # Wuchshüllenrechner

        # all menu actions
        # first the calculation menu
        self.calculationMenu.setTitle(
            QApplication.translate("MainWindow",
                                   "&Calculation"))  # Kalkulation
        self.calculationNew.setText(
            QApplication.translate("MainWindow", "&New"))  # &Neu
        self.calculationOpen.setText(
            QApplication.translate("MainWindow", "&Open"))  # Ö&ffnen
        self.calculationSave.setText(
            QApplication.translate("MainWindow", "&Save"))  # &Speichern
        self.calculationSaveAs.setText(
            QApplication.translate("MainWindow",
                                   "&Save as"))  # &Speichern unter
        #self.calculationPrint.setText(QApplication.translate("MainWindow", "&Print"))           # &Drucken
        self.calculationExport.setText(
            QApplication.translate("MainWindow", "&Export"))  # &Exportieren
        self.calculationQuit.setText(
            QApplication.translate("MainWindow", "E&xit"))  # &Beenden

        # now the examples menu
        self.examplesMenu.setTitle(
            QApplication.translate("MainWindow", "E&xamples"))  # &Beispiele
        self.examplesMaple.setText(
            QApplication.translate("MainWindow",
                                   "&Calculation example for great &maple")
        )  # Berechnungsbeispiel für Berg-&Ahorn
        self.examplesDouglas.setText(
            QApplication.translate("MainWindow",
                                   "&Calculation example for &douglas fir")
        )  # Berechnungsbeispiel für Küsten-Douglasie

        # the language menu
        self.languageMenu.setTitle(
            QApplication.translate("MainWindow", "&Language"))  # &Sprache
        self.languageGerman.setText(
            QApplication.translate("MainWindow", "&German"))  # &Deutsch
        self.languageEnglish.setText(
            QApplication.translate("MainWindow", "&English"))  # &Englisch

        # the help menu
        self.helpMenu.setTitle(QApplication.translate("MainWindow",
                                                      "&Help"))  # &Hilfe
        self.helpContents.setText(
            QApplication.translate("MainWindow",
                                   "Help &Contents"))  # &Hilfe anzeigen
        self.scientificBasis.setText(
            QApplication.translate("MainWindow",
                                   "Specialist &articles"))  # &Fachbeiträge
        self.helpAbout.setText(
            QApplication.translate(
                "MainWindow",
                "&About Wuchshüllenrechner"))  # Über &Wuchshüllenrechner

        # the edit box for data collection
        self.editBox.setTitle(
            QApplication.translate("MainWindow",
                                   "Data collection"))  # Datenerfassung
        self.operationLabel.setText(
            QApplication.translate("MainWindow", "Forestry &operation") +
            ":")  # Forst&betrieb
        self.districtLabel.setText(
            QApplication.translate("MainWindow", "Forest &district") +
            ":")  # Forst&revier
        self.managerLabel.setText(
            QApplication.translate("MainWindow", "&District manager") +
            ":")  # &Revierleiter
        self.locationLabel.setText(
            QApplication.translate("MainWindow", "&Location") +
            ":")  # &Waldort
        self.taxLabel.setText(
            QApplication.translate("MainWindow", "&Sales tax") +
            ":")  # Mehrwert&steuer
        self.taxHint.setToolTip(
            QApplication.translate(
                "MainWindow",
                "Wirkt sich auf Vorgabewerte aus und kann nach\nAnlegen einer "
                "Variante nicht geändert werden."))

        # update the data widget
        self.dataWidget.retranslateUi()

        # update the chart and result view
        self.plotWidget.retranslateUi()
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        # initialize class attributes
        self._INPUTS_FIXED_WIDTH = 180
        self._BUTTON_MIN_WIDTH = 80
        self._OXYGEN_PATH_32 = os.path.join("resources", "icons", "oxygen", "32")

        # locale and language settings
        self.language = self.locale().name()[:2]
        self.qtTl = QTranslator()
        self.appTl = QTranslator()
        self.switchTranslator(self.qtTl, "qtbase", self.language)
        self.switchTranslator(self.appTl, "wuchshuellenrechner", self.language)

        # TODO(th)
        self.model = TreeModel()

        # setup gui
        self.setMinimumSize(1200, 760)

        self.createActions()
        self.createMainMenu()

        # plot widget
        self.plotWidget = VariantPlotView()
        self.plotWidget.setModel(self.model)

        # create vertical splitter
        splitter = QSplitter(Qt.Vertical)
        splitter.setContentsMargins(11, 11, 11, 11)
        splitter.addWidget(self.createEditBox())
        splitter.addWidget(self.plotWidget)

        self.setCentralWidget(splitter)

        self.dataWidget.setModel(self.model)

        selectionModel = QItemSelectionModel(self.model)
        self.dataWidget.setSelectionModel(selectionModel)
        self.plotWidget.setSelectionModel(selectionModel)

        self.retranslateUi()


        self.model.dataChanged.connect(self.updateInputs)
        self.model.itemsInserted.connect(self.taxInput.setDisabled)
        self.model.allItemsRemoved.connect(self.taxInput.setEnabled)
        self.operationInput.textChanged.connect(self.updateOperation)
        self.districtInput.textChanged.connect(self.updateDistrict)
        self.managerInput.textChanged.connect(self.updateManager)
        self.locationInput.textChanged.connect(self.updateLocation)
        self.taxInput.stateChanged.connect(self.updateTax)

    def updateOperation(self, value):
        self.model.setProjectData("operation", value)

    def updateDistrict(self, value):
        self.model.setProjectData("district", value)

    def updateManager(self, value):
        self.model.setProjectData("manager", value)

    def updateLocation(self, value):
        self.model.setProjectData("location", value)

    def updateTax(self):
        self.model.setProjectData("tax", self.taxInput.isChecked())
        self.dataWidget.tax = self.taxInput.isChecked()

    def updateInputs(self):
        self.operationInput.setText(self.model.projectData("operation"))
        self.districtInput.setText(self.model.projectData("district"))
        self.managerInput.setText(self.model.projectData("manager"))
        self.locationInput.setText(self.model.projectData("location"))

        self.taxInput.setChecked(self.model.projectData("tax"))

    def loadLanguage(self, language):
        if not self.language == language:
            self.language = language

            locale = QLocale(language)
            QLocale.setDefault(locale)

            self.switchTranslator(self.qtTl, "qtbase", language)
            self.switchTranslator(self.appTl, "wuchshuellenrechner", language)

            # update gui
            self.retranslateUi()
            self.dataWidget.retranslateUi()

    def switchTranslator(self, translator, filename, language):
        app = QApplication.instance()

        # remove the old translator and
        # try to load a new language file
        app.removeTranslator(translator)
        if translator.load(QLocale(), filename, "_", "language"):
            app.installTranslator(translator)

    def setTaxEnabled(self, enable):
        # enable buttons and the view element
        self.removeButton.setEnabled(enable)
        self.modifyButton.setEnabled(enable)
        self.view.setEnabled(enable)

    def setDisabled(self, disable):
        # internaly call the setEnabled function
        # therefore negate the boolean value
        self.setEnabled(not disable)

    def aboutAction(self):
        aboutDialog = AboutDialog()
        aboutDialog.exec()

    def clear(self):
        # first clear the model
        self.model.clear()

        # update fields with model's default values
        self.operationInput.setText(self.model.projectData("operation"))
        self.districtInput.setText(self.model.projectData("district"))
        self.managerInput.setText(self.model.projectData("manager"))
        self.locationInput.setText(self.model.projectData("location"))

        self.taxInput.setEnabled(True)
        self.taxInput.setChecked(self.model.projectData("tax"))

        # reset the tree view
        self.dataWidget.clear()

        # reset the chart and result view
        # actually this is necassary
        self.plotWidget.clear()

    def closeAction(self):
        if self.model.changed:
            question = QMessageBox()
            question.setIcon(QMessageBox.Question)
            question.setWindowTitle("Berechnung speichern? - Wuchshüllenrechner")
            question.setText("Möchten Sie die Änderungen an dieser Berechnung speichern?")
            question.setInformativeText("Die Änderungen gehen verloren, wenn Sie nicht speichern.")
            question.setStandardButtons(QMessageBox.Save|QMessageBox.Discard|QMessageBox.Cancel)
            question.setDefaultButton(QMessageBox.Save)
            reply = question.exec()

            if not reply == QMessageBox.Cancel:
                if reply == QMessageBox.Save:
                    if self.saveAction():
                        self.close()
                else:
                    self.close()
        else:
            self.close()

    def newAction(self):
        if self.model.changed:
            question = QMessageBox()
            question.setIcon(QMessageBox.Question)
            question.setWindowTitle("Berechnung speichern? - Wuchshüllenrechner")
            question.setText("Möchten Sie die Änderungen an dieser Berechnung speichern?")
            question.setInformativeText("Die Änderungen gehen verloren, wenn Sie nicht speichern.")
            question.setStandardButtons(QMessageBox.Save|QMessageBox.Discard|QMessageBox.Cancel)
            question.setDefaultButton(QMessageBox.Save)
            reply = question.exec()

            if not reply == QMessageBox.Cancel:
                if reply == QMessageBox.Save:
                    if self.saveAction():
                        # clear all
                        self.clear()
                else:
                    # clear all
                    self.clear()

    def checkSaveState(self):
        if self.model.changed:
            question = QMessageBox(self)
            question.setWindowModality(Qt.WindowModal)  # check for mac only
            question.setIcon(QMessageBox.Warning)
            question.setStandardButtons(QMessageBox.Save|QMessageBox.Discard|QMessageBox.Cancel)
            question.setDefaultButton(QMessageBox.Save)
            question.setWindowTitle(QApplication.translate("MainWindow", "Wuchshüllenrechner"))
            question.setText("<b>" + QApplication.translate("MainWindow",
                    "Do you want to save the changes you made<br>"
                    "to the current calculation?") + "</b>")
            question.setInformativeText(QApplication.translate("MainWindow",
                    "Your changes will be lost if you don't save them."))

            reply = question.exec()
            if reply == QMessageBox.Save:
                if self.saveAction():
                    #self.model.clear()
                    self.clear()
                    return True
                else:
                    return False
            elif reply == QMessageBox.Discard:
                #self.model.clear()
                self.clear()
                return True
            else:
                return False

        # always clear
        self.clear()

        return True

    def loadExample(self, example=""):
        # create the path
        exampleFile = os.path.join("examples", example.strip() + ".xml")

         # if all is save, open the example file
        if self.checkSaveState():
            self.model.readFile(exampleFile)

    def mapleAction(self):
        self.loadExample("great_maple")

    def douglasAction(self):
        self.loadExample("douglas_fir")

    def openAction(self):
        # first check save state of the current calculation
        if self.checkSaveState():
            dialog = QFileDialog(self)
            dialog.setWindowModality(Qt.WindowModal)    # check for mac only
            dialog.setWindowTitle(QApplication.translate("MainWindow", "Open Calculation"))
            dialog.setDirectory(os.path.expanduser("~"))
            dialog.setNameFilter(QApplication.translate("MainWindow",
                    "XML files (*.xml);;All Files (*)"))

            dialog.exec()
            filename = dialog.selectedFiles()
            if filename:
                self.model.readFile(filename.pop())

    def saveAction(self):
        if self.model.file:
            return self.model.saveFile()
        else:
            return self.saveAsAction()

    def saveAsAction(self):
        dialog = QFileDialog(self)
        dialog.setWindowModality(Qt.WindowModal)    # check for mac only
        dialog.setAcceptMode(QFileDialog.AcceptSave)
        dialog.setWindowTitle(QApplication.translate("MainWindow", "Save Calculation"))
        dialog.setDirectory(os.path.expanduser("~"))
        dialog.setNameFilter(QApplication.translate("MainWindow",
                "XML files (*.xml);;All Files (*)"))

        dialog.exec()
        filename = dialog.selectedFiles()
        if filename:
            self.model.saveFile(filename.pop())
            return True
        else:
            return False

    def exportAction(self):
        dialog = QFileDialog(self)
        dialog.setWindowModality(Qt.WindowModal)    # check for mac only
        dialog.setAcceptMode(QFileDialog.AcceptSave)
        dialog.setWindowTitle(QApplication.translate("MainWindow", "Export Calculation"))
        dialog.setDirectory(os.path.expanduser("~"))
        dialog.setNameFilter(QApplication.translate("MainWindow",
                "PNG files (*.png);;JPG files (*.jpg);;TIFF files (*.tif)"))

        if dialog.exec():
            filename = dialog.selectedFiles()
            self.plotWidget.plotWidget.plotWidget.export(filename.pop())

    def helpAction(self):
        # create the documentation path with
        # the current locale settings
        docFile = os.path.join("doc", "documentation_" + self.language + ".pdf")

        # on every platfrom a different
        # start operation is needed
        if sys.platform == "win32":
            os.startfile(docFile)
        elif sys.platform == "darwin":
            subprocess.call(("open", docFile))
        elif sys.platform.startswith("linux"):
            subprocess.call(("xdg-open", docFile))

    def scientificAction(self):
        scientificDialog = ScientificBasisDialog()
        scientificDialog.exec()

    def germanAction(self):
        # update language menu
        self.languageEnglish.setChecked(False)
        self.languageGerman.setChecked(True)

        # update gui
        self.loadLanguage("de")

    def englishAction(self):
        # update language menu
        self.languageGerman.setChecked(False)
        self.languageEnglish.setChecked(True)

        # update gui
        self.loadLanguage("en")

    def createActions(self):
        # all actions for the calculation menu
        # create the new calculation action
        self.calculationNew = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "document-new.png")),
                shortcut=QKeySequence.New, triggered=self.newAction)

        # create the open calculation action
        self.calculationOpen = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "document-open.png")),
                shortcut=QKeySequence.Open, triggered=self.openAction)

        # create the open calculation example actions
        # for great maple and douglas fir
        self.examplesMaple = QAction(self, triggered=self.mapleAction)
        self.examplesDouglas = QAction(self, triggered=self.douglasAction)

        # create the save calculation action
        self.calculationSave = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "document-save.png")),
                shortcut=QKeySequence.Save, triggered=self.saveAction)

        # create the save as action
        self.calculationSaveAs = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "document-save-as.png")),
                shortcut=QKeySequence.SaveAs, triggered=self.saveAsAction)

        # create the print calculation action
        #self.calculationPrint = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "document-print.png")),
        #        shortcut=QKeySequence.Print, triggered=self.printAction)

        # create the export calculation action
        self.calculationExport = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "document-export.png")),
                shortcut=QKeySequence(Qt.CTRL + Qt.Key_E), triggered=self.exportAction)

        # create the quit calculation action
        self.calculationQuit = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "application-exit.png")),
                shortcut=QKeySequence.Quit, triggered=self.closeAction)

        # all actions for the language menu
        # create the german and the english language actions
        self.languageGerman = QAction(self, checkable=True, triggered=self.germanAction)
        self.languageEnglish = QAction(self, checkable=True, triggered=self.englishAction)

        # update the language menu
        if self.language == "de":
            self.languageGerman.setChecked(True)
        elif self.language == "en":
            self.languageEnglish.setChecked(True)

        # create the help contents action
        self.helpContents = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "help-contents.png")),
                triggered=self.helpAction)

        # create the scientific basis action
        self.scientificBasis = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "applications-science.png")),
                triggered=self.scientificAction)

        # create the help about action
        self.helpAbout = QAction(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "dialog-information.png")),
                triggered=self.aboutAction)

    def createMainMenu(self):
        # create main menu bar
        self.menu = self.menuBar()

        # create file menu
        self.calculationMenu = QMenu(self)
        self.calculationMenu.addAction(self.calculationNew)
        self.calculationMenu.addAction(self.calculationOpen)

        self.examplesMenu = QMenu(self, icon=QIcon(os.path.join(self._OXYGEN_PATH_32, "folder-documents.png")))
        self.examplesMenu.addAction(self.examplesMaple)
        self.examplesMenu.addAction(self.examplesDouglas)
        self.calculationMenu.addMenu(self.examplesMenu)
        self.calculationMenu.addSeparator()

        self.calculationMenu.addAction(self.calculationSave)
        self.calculationMenu.addAction(self.calculationSaveAs)
        self.calculationMenu.addAction(self.calculationExport)
        self.calculationMenu.addSeparator()

        #self.calculationMenu.addAction(self.calculationPrint)
        #self.calculationMenu.addSeparator()

        self.calculationMenu.addAction(self.calculationQuit)

        self.menu.addMenu(self.calculationMenu)

        # create language menu
        self.languageMenu = QMenu(self)
        self.languageMenu.addAction(self.languageGerman)
        self.languageMenu.addAction(self.languageEnglish)
        self.menu.addMenu(self.languageMenu)

        # create help menu
        self.helpMenu = QMenu(self)
        self.helpMenu.addAction(self.helpContents)
        self.helpMenu.addAction(self.scientificBasis)
        self.helpMenu.addSeparator()
        self.helpMenu.addAction(self.helpAbout)
        self.menu.addMenu(self.helpMenu)

    def createEditBox(self):
        # create group box
        self.editBox = QGroupBox()
        self.editBox.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum)
        gridLayout = QGridLayout(self.editBox)

        # create input fields
        self.operationInput = QLineEdit(self.model.projectData("operation"))
        self.operationInput.setFixedWidth(self._INPUTS_FIXED_WIDTH)
        self.operationInput.setClearButtonEnabled(True)
        self.operationLabel = QLabel()
        self.operationLabel.setBuddy(self.operationInput)
        gridLayout.addWidget(self.operationLabel, 0, 0)
        gridLayout.addWidget(self.operationInput, 0, 1)

        self.districtInput = QLineEdit(self.model.projectData("district"))
        self.districtInput.setFixedWidth(self._INPUTS_FIXED_WIDTH)
        self.districtInput.setClearButtonEnabled(True)
        self.districtLabel = QLabel()
        self.districtLabel.setBuddy(self.districtInput)
        gridLayout.addWidget(self.districtLabel, 0, 2)
        gridLayout.addWidget(self.districtInput, 0, 3)

        self.managerInput = QLineEdit(self.model.projectData("manager"))
        self.managerInput.setFixedWidth(self._INPUTS_FIXED_WIDTH)
        self.managerInput.setClearButtonEnabled(True)
        self.managerLabel = QLabel()
        self.managerLabel.setBuddy(self.managerInput)
        gridLayout.addWidget(self.managerLabel, 1, 0)
        gridLayout.addWidget(self.managerInput, 1, 1)

        self.locationInput = QLineEdit(self.model.projectData("location"))
        self.locationInput.setFixedWidth(self._INPUTS_FIXED_WIDTH)
        self.locationInput.setClearButtonEnabled(True)
        self.locationLabel = QLabel()
        self.locationLabel.setBuddy(self.locationInput)
        gridLayout.addWidget(self.locationLabel, 1, 2)
        gridLayout.addWidget(self.locationInput, 1, 3)

        lineFrame = QFrame(frameShadow=QFrame.Sunken, frameShape=QFrame.VLine)
        gridLayout.addWidget(lineFrame, 0, 4, 2, 1)

        self.taxInput = QCheckBox()
        self.taxInput.setChecked(self.model.projectData("tax"))
        self.taxLabel = QLabel()
        self.taxLabel.setBuddy(self.taxInput)
        self.taxHint = ToolTipLabel()
        gridLayout.addWidget(self.taxLabel, 0, 5)
        gridLayout.addWidget(self.taxInput, 0, 6)
        gridLayout.addWidget(self.taxHint, 0, 7, Qt.AlignRight)
        gridLayout.setColumnMinimumWidth(7, 40)

        rightSpacer = QSpacerItem(20, 0, QSizePolicy.Expanding, QSizePolicy.Fixed)
        gridLayout.addItem(rightSpacer, 0, 8, 2, 1)

        self.dataWidget = EnhancedTreeWidget()
        gridLayout.addWidget(self.dataWidget, 2, 0, 1, 9)

        return self.editBox

    def retranslateUi(self):
        # main window title
        self.setWindowTitle(QApplication.translate("MainWindow", "Wuchshüllenrechner"))     # Wuchshüllenrechner

        # all menu actions
        # first the calculation menu
        self.calculationMenu.setTitle(QApplication.translate("MainWindow", "&Calculation"))     # Kalkulation
        self.calculationNew.setText(QApplication.translate("MainWindow", "&New"))               # &Neu
        self.calculationOpen.setText(QApplication.translate("MainWindow", "&Open"))             # Ö&ffnen
        self.calculationSave.setText(QApplication.translate("MainWindow", "&Save"))             # &Speichern
        self.calculationSaveAs.setText(QApplication.translate("MainWindow", "&Save as"))        # &Speichern unter
        #self.calculationPrint.setText(QApplication.translate("MainWindow", "&Print"))           # &Drucken
        self.calculationExport.setText(QApplication.translate("MainWindow", "&Export"))         # &Exportieren
        self.calculationQuit.setText(QApplication.translate("MainWindow", "E&xit"))             # &Beenden

        # now the examples menu
        self.examplesMenu.setTitle(QApplication.translate("MainWindow", "E&xamples"))           # &Beispiele
        self.examplesMaple.setText(QApplication.translate("MainWindow",
                "&Calculation example for great &maple"))                                       # Berechnungsbeispiel für Berg-&Ahorn
        self.examplesDouglas.setText(QApplication.translate("MainWindow",
                "&Calculation example for &douglas fir"))                                       # Berechnungsbeispiel für Küsten-Douglasie

        # the language menu
        self.languageMenu.setTitle(QApplication.translate("MainWindow", "&Language"))           # &Sprache
        self.languageGerman.setText(QApplication.translate("MainWindow", "&German"))            # &Deutsch
        self.languageEnglish.setText(QApplication.translate("MainWindow", "&English"))          # &Englisch

        # the help menu
        self.helpMenu.setTitle(QApplication.translate("MainWindow", "&Help"))                   # &Hilfe
        self.helpContents.setText(QApplication.translate("MainWindow", "Help &Contents"))       # &Hilfe anzeigen
        self.scientificBasis.setText(QApplication.translate("MainWindow", "Specialist &articles")) # &Fachbeiträge
        self.helpAbout.setText(QApplication.translate("MainWindow",
                "&About Wuchshüllenrechner"))                                                   # Über &Wuchshüllenrechner

        # the edit box for data collection
        self.editBox.setTitle(QApplication.translate("MainWindow", "Data collection"))          # Datenerfassung
        self.operationLabel.setText(QApplication.translate("MainWindow",
                "Forestry &operation") + ":")                                                   # Forst&betrieb
        self.districtLabel.setText(QApplication.translate("MainWindow",
                "Forest &district") + ":")                                                      # Forst&revier
        self.managerLabel.setText(QApplication.translate("MainWindow",
                "&District manager") + ":")                                                     # &Revierleiter
        self.locationLabel.setText(QApplication.translate("MainWindow",
                "&Location") + ":")                                                             # &Waldort
        self.taxLabel.setText(QApplication.translate("MainWindow",
                "&Sales tax") + ":")                                                            # Mehrwert&steuer
        self.taxHint.setToolTip(QApplication.translate("MainWindow",
                "Wirkt sich auf Vorgabewerte aus und kann nach\nAnlegen einer "
                "Variante nicht geändert werden."))

        # update the data widget
        self.dataWidget.retranslateUi()

        # update the chart and result view
        self.plotWidget.retranslateUi()