Exemplo n.º 1
0
 def selectOutp(self):
     msgBox = QMessageBox()
     msgBox.setIcon(QMessageBox.Warning)
     msgBox.setWindowTitle('Warning')
     msgBox.setText('Please define Epanet Inp File location.')
     msgBox.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowStaysOnTopHint | Qt.WindowCloseButtonHint)
     msgBox.exec_()
     return True
Exemplo n.º 2
0
 def run(self):
     filePath = QFileDialog.getOpenFileName(self.iface.mainWindow(), "Choose EPANET Input file",
                                            os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop'),
                                            "Epanet Inp File (*.inp)")
     if filePath[0] == "":
         return
     epa2gis(filePath[0])
     self.iface.messageBar().clearWidgets()
     msgBox = QMessageBox()
     msgBox.setWindowTitle('ImportEpanetInpFiles')
     msgBox.setText('Shapefiles have been created successfully in folder "_shapefiles_".')
     msgBox.exec_()
 def on_checkBox_stateChanged(self,state):
     if(self.accessControl.checkUserIsAdmin() == True or self.accessControl.checkAccessTable() == False):
         if(state == Qt.Unchecked):
             self.disableAccessControl()
         elif(state == Qt.Checked):
             self.enableAccessControl()
     elif(self.checkBox.checkState() == Qt.Checked and self.checkBox.isEnabled() == True):
         self.checkBox.setCheckState(Qt.Unchecked)
         message = QMessageBox()
         message.setWindowTitle("Zugriffskontrolle")
         message.setText(u"Sie besitzen keine Administrationsrechte für die Zugriffskontrolle.")
         message.setInformativeText(u"Bitte wenden Sie sich an Ihren Administrator.")
         message.setIcon(QMessageBox.Critical)
         message.exec_()
Exemplo n.º 4
0
    def __init__(self):
        """ Initialize data objects, starts fetching if appropriate, and warn about/removes obsolete plugins """

        QObject.__init__(self)  # initialize QObject in order to to use self.tr()
        repositories.load()
        plugins.getAllInstalled()

        if repositories.checkingOnStart() and repositories.timeForChecking() and repositories.allEnabled():
            # start fetching repositories
            self.statusLabel = QLabel(self.tr("Looking for new plugins...") + " ", iface.mainWindow().statusBar())
            iface.mainWindow().statusBar().insertPermanentWidget(0, self.statusLabel)
            self.statusLabel.linkActivated.connect(self.showPluginManagerWhenReady)
            repositories.checkingDone.connect(self.checkingDone)
            for key in repositories.allEnabled():
                repositories.requestFetching(key)
        else:
            # no fetching at start, so mark all enabled repositories as requesting to be fetched.
            for key in repositories.allEnabled():
                repositories.setRepositoryData(key, "state", 3)

        # look for obsolete plugins (the user-installed one is newer than core one)
        for key in plugins.obsoletePlugins:
            plugin = plugins.localCache[key]
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Warning)
            msg.setWindowTitle(self.tr("QGIS Python Plugin Installer"))
            msg.addButton(self.tr("Uninstall (recommended)"), QMessageBox.AcceptRole)
            msg.addButton(self.tr("I will uninstall it later"), QMessageBox.RejectRole)
            msg.setText(
                "%s <b>%s</b><br/><br/>%s"
                % (
                    self.tr("Obsolete plugin:"),
                    plugin["name"],
                    self.tr(
                        "QGIS has detected an obsolete plugin that masks its more recent version shipped with this copy of QGIS. This is likely due to files associated with a previous installation of QGIS. Do you want to remove the old plugin right now and unmask the more recent version?"
                    ),
                )
            )
            msg.exec_()
            if not msg.result():
                # uninstall, update utils and reload if enabled
                self.uninstallPlugin(key, quiet=True)
                updateAvailablePlugins()
                settings = QSettings()
                if settings.value("/PythonPlugins/" + key, False, type=bool):
                    settings.setValue("/PythonPlugins/watchDog/" + key, True)
                    loadPlugin(key)
                    startPlugin(key)
                    settings.remove("/PythonPlugins/watchDog/" + key)
 def delete_map(self):
     name = self.ui.tabMaps.currentItem().text()
     map_id = self.ui.tabMaps.currentItem().data(Qt.UserRole)
     
     msgBox = QMessageBox()
     msgBox.setWindowTitle(self.tr("Delete QGIS Cloud map."))
     msgBox.setText(
         self.tr("Do you want to delete the map \"%s\"?") % name)
     msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
     msgBox.setDefaultButton(QMessageBox.Cancel)
     msgBox.setIcon(QMessageBox.Question)
     ret = msgBox.exec_()
     
     if ret == QMessageBox.Ok:
         self.ui.widgetServices.close()
         self.setCursor(Qt.WaitCursor)
         success = self.api.delete_map(map_id)
         
         if success:
             self.ui.btnMapDelete.setEnabled(False)
             self.refresh_maps()
         else:
             self.show_api_error(success)
             
         self.unsetCursor()
         self.ui.widgetServices.close()
     else:
         QMessageBox.warning(None,  self.tr('Warning'),  self.tr('Deletion of map "{name}" interrupted!').format(name=name))
Exemplo n.º 6
0
    def deLayer(self):
        if len(self.tableView_2.selectedIndexes())!=1:
            return

        msgBox = QMessageBox()
        msgBox.setInformativeText("Do you delete the layer?")
        msgBox.addButton(QMessageBox.Yes)
        msgBox.addButton(QMessageBox.No)
        msgBox.setDefaultButton(QMessageBox.No)
        msgBox.setWindowTitle("Confirmation")
        msgBox.setIcon(QMessageBox.Question)
        if msgBox.exec_() == QMessageBox.Yes:
            self.db.open()
            idx = self.tableView_2.selectionModel().selectedIndexes()[0]
            tname = str(self.model2.itemData(idx)[0])
            sql = "drop table %s" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns_auth where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns_field_infos where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns_statistics where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns_time where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            self.db.commit()
            self.db.close()
            self.loadLayers()
 def on_accessControlButtonDelete_released(self):
     user = PostNAS_AccessControl(self.getSelectedUser())
     message = QMessageBox()
     message.setWindowTitle("Zugriffskontrolle")
     message.setText(u"Möchten Sie den Benutzer wirklich löschen?")
     message.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
     message.setIcon(QMessageBox.Information)
     if(message.exec_() == QMessageBox.Yes):
         if(user.deleteUser() == True):
             self.loadAccessTable()
 def enableAccessControl(self):
     # Prüfen, ob die Access Tabelle in der Datenbank vorhanden ist.
     if(self.accessControl.checkAccessTable() == False):
         message = QMessageBox()
         message.setWindowTitle("Zugriffskontrolle")
         message.setText(u"Derzeit ist keine Tabelle für die Zugriffskontrolle in der Datenbank vorhanden.")
         message.setInformativeText(u"Möchten Sie die Tabelle in der Datenbank anlegen?")
         message.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
         message.setIcon(QMessageBox.Information)
         if(message.exec_() == QMessageBox.Yes):
             if(self.accessControl.createAccessTable() == False):
                 self.disableAccessControl()
             else:
                 self.enableAccessControl()
         else:
             self.checkBox.setCheckState(Qt.Unchecked)
             self.disableAccessControl()
     # Prüfen, ob ein Administrator für die Zugriffskontrolle eingetragen ist
     elif(self.accessControl.checkAccessTableHasAdmin() == False):
         message = QMessageBox()
         message.setWindowTitle("Zugriffskontrolle")
         message.setText(u"Derzeit ist kein Administrator für die Zugriffskontrolle hinterlegt.")
         message.setInformativeText(u"Möchten Ihren Benutzernamen als Administrator hinterlegen?")
         message.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
         message.setIcon(QMessageBox.Information)
         if(message.exec_() == QMessageBox.Yes):
             if(self.accessControl.insertAdminUser() == True):
                 self.enableAccessControl()
                 self.loadAccessTable()
             else:
                 QMessageBox.critical(None,"Fehler","Es ist ein Fehler aufgetreten.")
                 self.disableAccessControl()
     # Prüfen, ob angemeldeter Benutzer ein Admin ist
     elif(self.accessControl.checkUserIsAdmin() == True):
         self.checkBox.setCheckState(Qt.Checked)
         self.userTable.setEnabled(True)
         self.accessControlButtonAdd.setEnabled(True)
         self.accessControlButtonAdd.setEnabled(True)
         self.accessControlButtonEdit.setEnabled(False)
         self.accessControlButtonDelete.setEnabled(False)
Exemplo n.º 9
0
    def count_elements(self):

        jun_count = self.params.junctions_vlay.featureCount()
        res_count = self.params.reservoirs_vlay.featureCount()
        tan_count = self.params.tanks_vlay.featureCount()
        pip_count = self.params.pipes_vlay.featureCount()
        pum_count = self.params.pumps_vlay.featureCount()
        val_count = self.params.valves_vlay.featureCount()

        text = 'Load OK. ' +\
               str(jun_count) + ' junction(s), ' + \
               str(res_count) + ' reservoir(s), ' + \
               str(tan_count) + ' tank(s), ' + \
               str(pip_count) + ' pipe(s), ' + \
               str(pum_count) + ' pump(s) and ' + \
               str(val_count) + ' valve(s) were loaded.'

        msg_box = QMessageBox(self)
        msg_box.setWindowTitle(Parameters.plug_in_name)
        msg_box.setIcon(QMessageBox.Information)
        msg_box.setText(text)
        msg_box.exec_()
Exemplo n.º 10
0
 def setLayer(self, layer):
     self.model.setLayer(layer)
     if self.model.rowCount() == 0:
         self.on_resetButton_clicked()
     else:
         dlg = QMessageBox(self)
         dlg.setText("Do you want to reset the field mapping?")
         dlg.setStandardButtons(
             QMessageBox.StandardButtons(QMessageBox.Yes |
                                         QMessageBox.No))
         dlg.setDefaultButton(QMessageBox.No)
         if dlg.exec_() == QMessageBox.Yes:
             self.on_resetButton_clicked()
Exemplo n.º 11
0
 def deleteOperation(self):
     message = QMessageBox()
     message.setModal(True)
     message.setWindowTitle(u'Remover operação?')
     message.setIcon(QMessageBox.Warning)
     message.setText(u"Tem a certeza que pretende remover a operação?")
     message.addButton(u'Remover', QMessageBox.AcceptRole)
     message.addButton(u'Cancelar', QMessageBox.RejectRole)
     ret = message.exec_()
     if ret == QMessageBox.AcceptRole:
         self.parent.remove_operation(self)
     else:
         return
Exemplo n.º 12
0
 def setLayer(self, layer):
     self.model.setLayer(layer)
     if self.model.rowCount() == 0:
         self.on_resetButton_clicked()
     else:
         dlg = QMessageBox(self)
         dlg.setText("Do you want to reset the field mapping?")
         dlg.setStandardButtons(
             QMessageBox.StandardButtons(QMessageBox.Yes |
                                         QMessageBox.No))
         dlg.setDefaultButton(QMessageBox.No)
         if dlg.exec_() == QMessageBox.Yes:
             self.on_resetButton_clicked()
    def call_allocate_parcels_to_receiver_panel(self):
        # Make sure that all selected items are not yet allocated, otherwise, allow users to deallocate selected
        already_allocated = list()  # [parcel_fid1, ...]
        for parcel_fid, parcel_number in self.__selected_items.items():
            if parcel_fid in self._parcel_data():
                if self._parcel_data()[parcel_fid][1]:  # surveyor_name
                    already_allocated.append(parcel_fid)

        if already_allocated:
            msg = QMessageBox(self)
            msg.setIcon(QMessageBox.Question)
            msg.setText(QCoreApplication.translate("AllocateParcelsFieldDataCapturePanelWidget",
                                                   "Some selected parcels are already allocated!\n\nWhat would you like to do with selected parcels that are already allocated?"))
            msg.setWindowTitle(QCoreApplication.translate("AllocateParcelsFieldDataCapturePanelWidget", "Warning"))
            msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
            msg.button(QMessageBox.Yes).setText(
                QCoreApplication.translate("AllocateParcelsFieldDataCapturePanelWidget", "Deselect them and continue"))
            msg.button(QMessageBox.No).setText(
                QCoreApplication.translate("AllocateParcelsFieldDataCapturePanelWidget", "Reallocate and continue"))
            reply = msg.exec_()

            if reply == QMessageBox.Yes:  # Ignore
                # Remove selection of allocated parcels, reload table widget data and continue
                for allocated_parcel_id in already_allocated:
                    if allocated_parcel_id in self._parcel_data():
                        items = self.tbl_parcels.findItems(self._parcel_data()[allocated_parcel_id][0], Qt.MatchExactly)
                        if items:  # Item is currently shown, so deselect it in GUI
                            items[0].setSelected(False)  # Deselect item in column 0
                            self.tbl_parcels.item(items[0].row(), self.STATUS_COL).setSelected(False)  # Deselect item in column 1
                        else:  # Item is not currently shown, deselected in internal selection dict
                            if allocated_parcel_id in self.__selected_items:
                                del self.__selected_items[allocated_parcel_id]

                self.fill_data()

                if not self.__selected_items:
                    self.logger.warning_msg(__name__, QCoreApplication.translate("AllocateParcelsFieldDataCapturePanelWidget",
                                                                       "Ignoring selected parcels, there are none to be allocated! First select some!"), 10)
                    return

            elif reply == QMessageBox.No:  # Reallocate
                # Preserve the selected_items dict, but remove allocation before continuing
                if not self.discard_parcel_allocation(already_allocated):
                    return
            else:  # QMessageBox.Cancel
                return

        if self.__selected_items:
            self.allocate_parcels_to_receiver_panel_requested.emit(self.__selected_items)
        else:
            self.logger.warning_msg(__name__, QCoreApplication.translate("AllocateParcelsFieldDataCapturePanelWidget", "First select some parcels to be allocated."), 5)
Exemplo n.º 14
0
 def remove_postgis_search(self):
     sel = self.selected_postgis_search_ids()
     if len(sel) == 0:
         return
     box = QMessageBox(
         QMessageBox.Warning, "Quick Finder",
         QCoreApplication.translate(
             "Configuration dialog",
             "Are you sure to remove {0} search(es) ? ").format(len(sel)),
         QMessageBox.Yes | QMessageBox.Cancel, self)
     ret = box.exec_()
     if ret == QMessageBox.Cancel:
         return
     self.postgis_search_model.removeSearches(sel)
Exemplo n.º 15
0
 def setLayer(self, layer):
     if layer is None or self.layer == layer:
         return
     self.layer = layer
     if self.model.rowCount(QModelIndex()) == 0:
         self.on_resetButton_clicked()
         return
     dlg = QMessageBox(self)
     dlg.setText(self.tr("Do you want to reset the field mapping?"))
     dlg.setStandardButtons(
         QMessageBox.StandardButtons(QMessageBox.Yes | QMessageBox.No))
     dlg.setDefaultButton(QMessageBox.No)
     if dlg.exec_() == QMessageBox.Yes:
         self.on_resetButton_clicked()
Exemplo n.º 16
0
 def ask(parent, title, question, box_icon=QMessageBox.Question):
     """Ask for operation confirmation."""
     msg_box = QMessageBox(parent)
     msg_box.setIcon(box_icon)
     msg_box.setWindowTitle(title)
     msg_box.setTextFormat(Qt.RichText)
     msg_box.setText(question)
     msg_box.setStandardButtons(QMessageBox.No | QMessageBox.Yes)
     msg_box.setDefaultButton(QMessageBox.No)
     res = msg_box.exec_()
     if res == QMessageBox.No:
         return False
     else:
         return True
Exemplo n.º 17
0
    def qmb_reanalysis():
        """
        :return:
        """
        msgBox = QMessageBox()
        msgBox.setText(
            'You have selected an old date. \nWould you like to load raw data or old data from a previous production work?'
        )
        msgBox.setWindowTitle("BAWS (%s) Question" % __version__)
        raw_button = msgBox.addButton('Raw data', QMessageBox.YesRole)
        old_button = msgBox.addButton('Old production data',
                                      QMessageBox.NoRole)
        cancel_button = msgBox.addButton('Cancel', QMessageBox.RejectRole)
        msgBox.exec_()

        if msgBox.clickedButton() == raw_button:
            return 'raw'
        elif msgBox.clickedButton() == old_button:
            return 'old'
        elif msgBox.clickedButton() == cancel_button:
            return None
        else:
            return None
 def prepare_database(self):
     if self.check_connected():
         msgBox = QMessageBox()
         msgBox.setText(
             "DON'T USE THIS PARTWAY THROUGH THE JOB! because this will erase any data in tables used by this plugin."
         )
         msgBox.setInformativeText("Continue?")
         msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
         msgBox.setDefaultButton(QMessageBox.No)
         i = msgBox.exec_()
         if i == QMessageBox.Yes:
             self.dd.setup_database()
             iface.messageBar().pushMessage(
                 'fitting tool: prepared database')
Exemplo n.º 19
0
    def exportVersion(self, repo, layer, commitId):
        trackedlayer = getTrackingInfoForGeogigLayer(repo.url, layer)
        if trackedlayer:
            if os.path.exists(trackedlayer.geopkg):
                try:
                    con = sqlite3.connect(trackedlayer.geopkg)
                    cursor = con.cursor()
                    cursor.execute(
                        "SELECT commit_id FROM geogig_audited_tables WHERE table_name='%s';"
                        % layer)
                    currentCommitId = cursor.fetchone()[0]
                    cursor.close()
                    con.close()
                    if commitId != currentCommitId:
                        msgBox = QMessageBox()
                        msgBox.setWindowTitle("Layer was already exported")
                        msgBox.setText(
                            "This layer was exported already at a different commit.\n"
                            "Which one would you like to add to your QGIS project?"
                        )
                        msgBox.addButton(
                            QPushButton('Use previously exported commit'),
                            QMessageBox.YesRole)
                        msgBox.addButton(
                            QPushButton('Use latest commit from this branch'),
                            QMessageBox.NoRole)
                        msgBox.addButton(QPushButton('Cancel'),
                                         QMessageBox.RejectRole)
                        QApplication.restoreOverrideCursor()
                        ret = msgBox.exec_()
                        if ret == 0:
                            checkoutLayer(repo, layer, None, currentCommitId)
                        elif ret == 1:
                            try:
                                layer = checkoutLayer(repo, layer, None,
                                                      commitId)
                                repoWatcher.layerUpdated.emit(layer)
                            except HasLocalChangesError:
                                QMessageBox.warning(
                                    config.iface.mainWindow(),
                                    'Cannot export this commit',
                                    "The layer has local changes that would be overwritten.\n"
                                    "Either sync layer with branch or revert local changes "
                                    "before changing commit", QMessageBox.Ok)
                except:
                    checkoutLayer(repo, layer, None, currentCommitId)

        else:
            checkoutLayer(repo, layer, None, commitId)
Exemplo n.º 20
0
    def mergePullRequest(self):
        # import pydevd
        # pydevd.settrace('localhost', port=65432, stdoutToServer=True, stderrToServer=True,
        #                 trace_only_current_thread=False, overwrite_prev_trace=True, patch_multiprocessing=True,
        #                 suspend=False)
        pr = self.prs[self.comboPr.currentText()]
        merged = self.server.mergePullRequest(self.user, self.repo, pr["id"])
        if not merged:
            msgBox = QMessageBox()
            msgBox.setText("Conflicts in PR")
            msgBox.setInformativeText(
                "There are conflicts.\n"
                "Source repo needs to be synchronized and conflicts solved.\n"
                "This will modify the source repo.")
            msgBox.setIcon(QMessageBox.Warning)
            syncButton = msgBox.addButton("Continue", QMessageBox.ActionRole)
            abortButton = msgBox.addButton(QMessageBox.Abort)

            msgBox.exec_()

            if msgBox.clickedButton() == syncButton:
                # do sync
                syncDialog = SynchronizeDialog(
                    pr["sourceRepo"]["owner"]["identity"],
                    pr["sourceRepo"]["identity"], self.server,
                    pr["targetRepo"]["owner"]["identity"],
                    pr["targetRepo"]["identity"], False)
                syncDialog.exec_()
                if syncDialog.synced:
                    self.mergePullRequest()
                else:
                    self.close()
            elif msgBox.clickedButton() == abortButton:
                self.close()
        else:
            self.close()
Exemplo n.º 21
0
    def show_message_associate_geometry_creation(message) -> bool:
        # TODO parent in message box
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Question)
        msg.setText(message)
        msg.setWindowTitle(
            QCoreApplication.translate("WizardTranslations",
                                       "Continue editing?"))
        msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg.button(QMessageBox.No).setText(
            QCoreApplication.translate("WizardTranslations",
                                       "No, close the wizard"))
        reply = msg.exec_()

        return reply != QMessageBox.No
def showdialog(title: str, help_message: str):
    msg = QMessageBox()
    #msg.setIcon(QMessageBox.Information)

    #msg.setText("This is a message box")
    msg.setText(help_message)
    #msg.setInformativeText("This is additional information")
    #msg.setWindowTitle("MessageBox demo")
    msg.setWindowTitle(title)
    #msg.setDetailedText("The details are as follows:")
    #msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
    msg.setStandardButtons(QMessageBox.Ok)
    msg.buttonClicked.connect(msgbtn)

    retval = msg.exec_()
 def solveModifyAndDelete(self, path, modified):
     msgBox = QMessageBox()
     msgBox.setText("The feature has been modified in one version and deleted in the other one.\n"
                    "How do you want to solve the conflict?")
     msgBox.addButton(QPushButton('Modify'), QMessageBox.YesRole)
     msgBox.addButton(QPushButton('Delete'), QMessageBox.NoRole)
     msgBox.addButton(QPushButton('Cancel'), QMessageBox.RejectRole)
     ret = msgBox.exec_()
     if ret == 0:
         self.resolvedConflicts[path] = modified
         self._afterSolve()
     elif ret == 1:
         self.resolvedConflicts[path] = self.DELETE
         self._afterSolve()
     else:
         pass
Exemplo n.º 24
0
 def solveModifyAndDelete(self, path, modified):
     msgBox = QMessageBox()
     msgBox.setText(
         "The feature has been modified in one version and deleted in the other one.\n"
         "How do you want to solve the conflict?")
     msgBox.addButton(QPushButton('Modify'), QMessageBox.YesRole)
     msgBox.addButton(QPushButton('Delete'), QMessageBox.NoRole)
     msgBox.addButton(QPushButton('Cancel'), QMessageBox.RejectRole)
     ret = msgBox.exec_()
     if ret == 0:
         self.resolvedConflicts[path] = modified
         self._afterSolve()
     elif ret == 1:
         self.resolvedConflicts[path] = self.DELETE
         self._afterSolve()
     else:
         pass
Exemplo n.º 25
0
 def setLayer(self, layer):
     if self.model.layer() == layer:
         return
     self.model.setLayer(layer)
     if layer is None:
         return
     if self.model.rowCount() == 0:
         self.on_resetButton_clicked()
         return
     dlg = QMessageBox(self)
     dlg.setText(self.tr("Do you want to reset the field mapping?"))
     dlg.setStandardButtons(
         QMessageBox.StandardButtons(QMessageBox.Yes |
                                     QMessageBox.No))
     dlg.setDefaultButton(QMessageBox.No)
     if dlg.exec_() == QMessageBox.Yes:
         self.on_resetButton_clicked()
 def check_project_saved(self):
     project = QgsProject.instance()
     fname = project.fileName()
     if project.isDirty() or fname == '':
         msgBox = QMessageBox()
         msgBox.setWindowTitle(self.tr("Project Modified"))
         msgBox.setText(self.tr("The project has been modified."))
         msgBox.setInformativeText(
             self.tr("The project needs to be saved before it can be published. Proceed?"))
         msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Cancel)
         msgBox.setDefaultButton(QMessageBox.Save)
         if msgBox.exec_() == QMessageBox.Save:
             self.iface.actionSaveProject().trigger()
             return not project.isDirty()
         else:
             return False
     return True
Exemplo n.º 27
0
    def show_message_clean_layers_panel(self, message):
        msg = QMessageBox(self)
        msg.setIcon(QMessageBox.Question)
        msg.setText(message)
        msg.setWindowTitle(QCoreApplication.translate("ChangeDetectionSettingsDialog", "Remove layers?"))
        msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
        msg.button(QMessageBox.Yes).setText(QCoreApplication.translate("ChangeDetectionSettingsDialog", "Yes, remove layers"))
        msg.button(QMessageBox.No).setText(QCoreApplication.translate("ChangeDetectionSettingsDialog", "No, don't remove"))
        reply = msg.exec_()

        if reply == QMessageBox.Yes:
            QgsProject.instance().layerTreeRoot().removeAllChildren()
            self.close_dialog(QDialog.Accepted)
        elif reply == QMessageBox.No:
            self.close_dialog(QDialog.Accepted)
        elif reply == QMessageBox.Cancel:
            pass  # Continue config db connections
 def check_project_saved(self):
     project = QgsProject.instance()
     fname = project.fileName()
     if project.isDirty() or fname == '':
         msgBox = QMessageBox()
         msgBox.setWindowTitle(self.tr("Project Modified"))
         msgBox.setText(self.tr("The project has been modified."))
         msgBox.setInformativeText(
             self.tr("The project needs to be saved before it can be published. Proceed?"))
         msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Cancel)
         msgBox.setDefaultButton(QMessageBox.Save)
         if msgBox.exec_() == QMessageBox.Save:
             self.iface.actionSaveProject().trigger()
             return not project.isDirty()
         else:
             return False
     return True
Exemplo n.º 29
0
    def showDialog(self):
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Critical)

        msg.setText("Vector data path has not been set")
        msg.setInformativeText(
            "Please set the vector data path before running the import. This only has to be set once"
        )
        msg.setWindowTitle("Path not Set")
        #msg.setDetailedText("The details are as follows:")
        #msg.addButton(QPushButton)
        msg.setStandardButtons(QMessageBox.Open)
        #msg.buttonClicked.connect(msgbtn)
        retval = msg.exec_()
        print("value of pressed message box button:", retval)

        if (retval == 8192):
            self.selectDir()
Exemplo n.º 30
0
    def deLayer(self):
        if len(self.tableView_layers.selectedIndexes()) != 1:
            return

        msgBox = QMessageBox()
        msgBox.setInformativeText("Do you delete the layer?")
        msgBox.addButton(QMessageBox.Yes)
        msgBox.addButton(QMessageBox.No)
        msgBox.setDefaultButton(QMessageBox.No)
        msgBox.setWindowTitle("Confirmation")
        msgBox.setIcon(QMessageBox.Question)
        if msgBox.exec_() == QMessageBox.Yes:
            self.db.open()
            idx = self.tableView_layers.selectionModel().selectedIndexes()[0]
            tname = str(self.model2.itemData(idx)[0])

            #Check if layer is loaded on the TOC.
            #If layer is present will be removed and added from the TOC
            lyrs = [
                layer for layer in QgsProject.instance().mapLayers().values()
            ]

            for l in lyrs:
                if type(l).__name__ == 'QgsVectorLayer':
                    if tname == l.name():
                        #TODO: how get the self.db path or database name?
                        if l.providerType() == 'spatialite':
                            QgsProject.instance().removeMapLayer(l.id())

            sql = "drop table %s" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns_auth where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns_field_infos where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns_statistics where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            sql = "delete from geometry_columns_time where f_table_name='%s'" % tname
            q = self.db.exec_(sql)
            self.db.commit()
            self.db.close()
            self.loadLayers()
Exemplo n.º 31
0
    def delete_database(self):
        name = self.ui.tabDatabases.currentItem().text()

        answer = False

        for layer in list(self.PROJECT_INSTANCE.mapLayers().values()):
            if QgsDataSourceUri(layer.publicSource()).database() == name:

                if not answer:
                    answer = QMessageBox.question(
                        self, self.tr("Warning"),
                        self.
                        tr('You have layers from database "{name}" loaded in your project! Do you want to remove them before you delete database "{name}"?'
                           ).format(name=name),
                        QMessageBox.StandardButtons(QMessageBox.Cancel
                                                    | QMessageBox.Yes))

                if answer == QMessageBox.Yes:
                    self.PROJECT_INSTANCE.removeMapLayer(layer.id())

        if answer == QMessageBox.Cancel:
            QMessageBox.warning(
                None, self.tr('Warning'),
                self.tr('Deletion of database "{name}" interrupted!').format(
                    name=name))
            return

        msgBox = QMessageBox()
        msgBox.setWindowTitle(self.tr("Delete QGIS Cloud database."))
        msgBox.setText(
            self.tr("Do you want to delete the database \"%s\"?") % name)
        msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        msgBox.setDefaultButton(QMessageBox.Cancel)
        msgBox.setIcon(QMessageBox.Question)
        ret = msgBox.exec_()

        if ret == QMessageBox.Ok:
            self.setCursor(Qt.WaitCursor)
            result = self.api.delete_database(name)
            self.show_api_error(result)
            self.ui.btnDbDelete.setEnabled(False)
            time.sleep(2)
            self.refresh_databases()
            self.unsetCursor()
Exemplo n.º 32
0
 def close_and_control(self):
     self.project_finder.close()
     for search in list(self.project_finder.searches.values()):
         if search.dateEvaluated is None:
             box = QMessageBox(
                 QMessageBox.Warning, "Quick Finder",
                 QCoreApplication.translate(
                     "Configuration dialog",
                     "Some searches are still not recorded to the file. Do you want to record them now ? "
                 ),
                 QMessageBox.Cancel | QMessageBox.Yes | QMessageBox.Close,
                 self)
             ret = box.exec_()
             if ret == QMessageBox.Cancel:
                 return False
             elif ret == QMessageBox.Yes:
                 self.refresh_project_search()
                 return False
     return True
    def show_message_associate_geometry_creation(self, message):
        msg = QMessageBox(self)
        msg.setIcon(QMessageBox.Question)
        msg.setText(message)
        msg.setWindowTitle(QCoreApplication.translate("WizardTranslations", "Continue editing?"))
        msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg.button(QMessageBox.No).setText(QCoreApplication.translate("WizardTranslations", "No, close the wizard"))
        reply = msg.exec_()

        if reply == QMessageBox.No:
            # stop edition in close_wizard crash qgis
            if self._layers[self.EDITING_LAYER_NAME][LAYER].isEditable():
                self._layers[self.EDITING_LAYER_NAME][LAYER].rollBack()

            message = QCoreApplication.translate("WizardTranslations", "'{}' tool has been closed.").format(
                self.WIZARD_TOOL_NAME)
            self.close_wizard(message)
        else:
            # Continue creating geometry
            pass
    def delete_database(self):
        name = self.ui.tabDatabases.currentItem().text()
        
        answer = False
        
        for layer in list(self.PROJECT_INSTANCE.mapLayers().values()):            
            if QgsDataSourceUri(layer.publicSource()).database() == name:
                
                if not answer:
                    answer = QMessageBox.question(
                        self,
                        self.tr("Warning"),
                        self.tr('You have layers from database "{name}" loaded in your project! Do you want to remove them before you delete database "{name}"?').format(name=name),
                        QMessageBox.StandardButtons(
                            QMessageBox.Cancel |
                            QMessageBox.Yes))

                if answer == QMessageBox.Yes:
                     self.PROJECT_INSTANCE.removeMapLayer(layer.id())
                     
        if answer == QMessageBox.Cancel:
            QMessageBox.warning(None,  self.tr('Warning'),  self.tr('Deletion of database "{name}" interrupted!').format(name=name))
            return
            
        msgBox = QMessageBox()
        msgBox.setWindowTitle(self.tr("Delete QGIS Cloud database."))
        msgBox.setText(
            self.tr("Do you want to delete the database \"%s\"?") % name)
        msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        msgBox.setDefaultButton(QMessageBox.Cancel)
        msgBox.setIcon(QMessageBox.Question)
        ret = msgBox.exec_()
        
        if ret == QMessageBox.Ok:
            self.setCursor(Qt.WaitCursor)
            result = self.api.delete_database(name)
            self.show_api_error(result)
            self.ui.btnDbDelete.setEnabled(False)
            time.sleep(2)
            self.refresh_databases()
            self.unsetCursor()
Exemplo n.º 35
0
 def createPullRequest(self):
     user2, repo2 = self.comboTarget.currentText().split(":")
     name = self.txtPrName.text()
     if name:
         conflicts = self.server.branchConflicts(self.user, self.repo,
                                                 "master", user2, repo2,
                                                 "master")
         if conflicts:
             ret = QMessageBox.warning(
                 self, "Conflicts with target repository",
                 "This Pull Request is in conflict with the target repo.\n"
                 "Please SYNC changes from target repo (and resolve conflicts) before making a PR!",
                 QMessageBox.Ok, QMessageBox.Ok)
             return
         # this assumes sending to parent
         commitsBehind, commitsAhead = self.server.compareHistories(
             self.user, self.repo, user2, repo2)
         if commitsBehind > 0:
             msgBox = QMessageBox()
             msgBox.setText("Target repo has changes")
             msgBox.setInformativeText(
                 "The target repo has changes not in this repo - we recommend SYNC those changes before creating a PR"
             )
             msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Abort)
             msgBox.setDefaultButton(QMessageBox.Abort)
             ret = msgBox.exec_()
             if ret == QMessageBox.Abort:
                 return
         self.server.createPullRequest(self.user,
                                       self.repo,
                                       user2,
                                       repo2,
                                       name,
                                       "master",
                                       "master",
                                       description=self.prDescription)
         self.close()
     else:
         self.bar.pushMessage("Error",
                              "Enter a valid pull request name",
                              level=Qgis.Warning)
Exemplo n.º 36
0
    def runModel(self):
        valid, errors = self.model().validate()
        if not valid:
            message_box = QMessageBox()
            message_box.setWindowTitle(self.tr('Model is Invalid'))
            message_box.setIcon(QMessageBox.Warning)
            message_box.setText(
                self.
                tr('This model is not valid and contains one or more issues. Are you sure you want to run it in this state?'
                   ))
            message_box.setStandardButtons(QMessageBox.Yes
                                           | QMessageBox.Cancel)
            message_box.setDefaultButton(QMessageBox.Cancel)

            error_string = ''
            for e in errors:
                e = re.sub(r'<[^>]*>', '', e)
                error_string += '• {}\n'.format(e)

            message_box.setDetailedText(error_string)
            if message_box.exec_() == QMessageBox.Cancel:
                return

        def on_finished(successful, results):
            self.setLastRunChildAlgorithmResults(dlg.results().get(
                'CHILD_RESULTS', {}))
            self.setLastRunChildAlgorithmInputs(dlg.results().get(
                'CHILD_INPUTS', {}))

        dlg = AlgorithmDialog(self.model().create(), parent=self)
        dlg.setLogLevel(QgsProcessingContext.Verbose)
        dlg.setParameters(self.model().designerParameterValues())
        dlg.algorithmFinished.connect(on_finished)
        dlg.exec_()

        if dlg.wasExecuted():
            self.model().setDesignerParameterValues(
                dlg.createProcessingParameters(
                    flags=QgsProcessingParametersGenerator.Flags(
                        QgsProcessingParametersGenerator.Flag.
                        SkipDefaultValueParameters)))
Exemplo n.º 37
0
    def showModalDialog(self, dialogTitle, dialogDescription, iconFlag,
                        okButtonFlag, cancelButtonFlag):

        # Shows a modal dialog with title and description

        # Parameters:
        #    dialogTitle: Dialog title
        #    dialogDescription: Dialog description
        #    iconFlag: Integer to select the type of icon for the dialog
        #    okButtonFlag: Boolean to show or not the ok button on the dialog
        #    cancelButtonFlag: Boolean to show or not the cancel button on the dialog

        modalDialog = QMessageBox()

        # icon flag, 0: question, 1: information, 2: warning, 3: critical
        if (iconFlag == 0):
            modalDialog.setIcon(QMessageBox.Question)
        elif (iconFlag == 1):
            modalDialog.setIcon(QMessageBox.Information)
        elif (iconFlag == 2):
            modalDialog.setIcon(QMessageBox.Warning)
        elif (iconFlag == 3):
            modalDialog.setIcon(QMessageBox.Critical)
        else:
            modalDialog.setIcon(QMessageBox.Warning)

        modalDialog.setText(dialogTitle)
        modalDialog.setInformativeText(dialogDescription)

        if (okButtonFlag and cancelButtonFlag):
            modalDialog.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        elif (okButtonFlag):
            modalDialog.setStandardButtons(QMessageBox.Ok)
        elif (cancelButtonFlag):
            modalDialog.setStandardButtons(QMessageBox.Cancel)

        retval = modalDialog.exec_()

        if (retval == 1024 or retval == 4194304):
            self.endAllProcesses()
Exemplo n.º 38
0
    def CustomMessage(title, msg, informative="", icon="Critical"):
        ''' Custom Informative Message '''
        d = QMessageBox()
        d.setTextFormat(Qt.RichText)
        d.setWindowTitle(title)
        d.setWindowIcon(QIcon(QPixmap(":/imgFMV/images/icon.png")))
        d.setText(msg)
        d.setInformativeText(informative)
        d.setIconPixmap(QgsUtils.GetIcon(icon))
        d.addButton(QMessageBox.Yes)
        d.addButton(QMessageBox.No)
        d.setDefaultButton(QMessageBox.No)

        # Trick resize QMessageBox
        horizontalSpacer = QSpacerItem(500, 0, QSizePolicy.Minimum,
                                       QSizePolicy.Expanding)
        layout = d.layout()
        layout.addItem(horizontalSpacer, layout.rowCount(), 0, 1,
                       layout.columnCount())

        ret = d.exec_()
        return ret
Exemplo n.º 39
0
 def __init__(self, question="YesNo", msg = '', dialogtitle='User input needed', parent=None):
     self.result = ''
     if question == 'YesNo':         #  Yes/No dialog
         reply = QMessageBox.information(parent, dialogtitle, msg, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
         if reply==QMessageBox.Yes:
             self.result = 1 #1 = "yes"
         else:
             self.result = 0  #0="no"
     elif question == 'AllSelected': # All or Selected Dialog
         btnAll = QPushButton("All")   # = "0"
         btnSelected = QPushButton("Selected")     # = "1"
         #btnAll.clicked.connect(lambda x: self.DoForAll())
         #btnSelected.clicked.connect(lambda x: self.DoForSelected())
         msgBox = QMessageBox(parent)
         msgBox.setText(msg)
         msgBox.setWindowTitle(dialogtitle)
         #msgBox.setWindowModality(Qt.ApplicationModal)
         msgBox.addButton(btnAll, QMessageBox.ActionRole)
         msgBox.addButton(btnSelected, QMessageBox.ActionRole)
         msgBox.addButton(QMessageBox.Cancel)
         reply = msgBox.exec_()
         self.result = reply  # ALL=0, SELECTED=1
     elif question == 'DateShift':
         supported_units = ['microseconds', 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks']
         while True:
             answer = str(QInputDialog.getText(None, "User input needed", "Give needed adjustment of date/time for the data.\nSupported format: +- X <resolution>\nEx: 1 hours, -1 hours, -1 days\nSupported units:\n%s"%', '.join(supported_units), QLineEdit.Normal, '0 hours')[0])
             if not answer:
                 self.result = 'cancel'
                 break
             else:
                 adjustment_unit = answer.split()
                 if len(adjustment_unit) == 2:
                     if adjustment_unit[1] in supported_units:
                         self.result = adjustment_unit
                         break
                     else:
                         pop_up_info("Failure:\nOnly support resolutions\n%s"%', '.join(supported_units))
                 else:
                     pop_up_info("Failure:\nMust write time resolution also.\n")
Exemplo n.º 40
0
def show_question(text,
                  title=None,
                  inf_text=None,
                  context_name=None,
                  parameter=None,
                  force_action=False):
    """ Ask question to the user """

    # Expert mode does not ask and accept all actions
    if global_vars.user_level['level'] not in (None,
                                               'None') and not force_action:
        if global_vars.user_level['level'] not in global_vars.user_level[
                'showquestion']:
            return True

    msg_box = QMessageBox()
    msg = tr(text, context_name)
    if parameter:
        msg += ": " + str(parameter)
    if len(msg) > 750:
        msg = msg[:750] + "\n[...]"
    msg_box.setText(msg)
    if title:
        title = tr(title, context_name)
        msg_box.setWindowTitle(title)
    if inf_text:
        inf_text = tr(inf_text, context_name)
        if len(inf_text) > 500:
            inf_text = inf_text[:500] + "\n[...]"
        msg_box.setInformativeText(inf_text)
    msg_box.setStandardButtons(QMessageBox.Cancel | QMessageBox.Ok)
    msg_box.setDefaultButton(QMessageBox.Ok)
    msg_box.setWindowFlags(Qt.WindowStaysOnTopHint)
    ret = msg_box.exec_()
    if ret == QMessageBox.Ok:
        return True
    elif ret == QMessageBox.Discard:
        return False
Exemplo n.º 41
0
    def do_delete_data_action(self, count_id):
        dlg = DeleteDialog(self.iface)
        tz = pytz.timezone("Europe/Zurich")

        if dlg.exec_():
            start = tz.localize(dlg.start_date.dateTime().toPyDateTime())
            end = tz.localize(dlg.end_date.dateTime().toPyDateTime())
            definitive = dlg.definitive.isChecked()
            quarantine = dlg.quarantine.isChecked()
            count = models.Count.objects.get(id=count_id)

            msg = QMessageBox()
            msg.setIcon(QMessageBox.Warning)
            msg.setText("Effacement des données")
            # msg.setInformativeText(f"Effacement des données")
            msg.setDetailedText("Les données suivantes seront supprimées:\n"
                                f"comptage: {count_id}\n"
                                f"de: {start.strftime('%d.%m.%Y')}\n"
                                f"à: {end.strftime('%d.%m.%Y')} inclus\n"
                                f"provisoires: {quarantine}\n"
                                f"définitives: {definitive}")
            msg.setWindowTitle("Effacement des données")
            msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
            retval = msg.exec_()
            if retval == QMessageBox.Ok:
                qs = models.CountDetail.objects.filter(
                    id_count=count,
                    timestamp__range=(start, end + timedelta(days=1)),
                )

                if not definitive:
                    qs = qs.filter(status=definitions.IMPORT_STATUS_QUARANTINE)

                if not quarantine:
                    qs = qs.filter(status=definitions.IMPORT_STATUS_DEFINITIVE)

                qs.delete()
    def exportVersion(self, repo, layer, commitId):
        trackedlayer = getTrackingInfoForGeogigLayer(repo.url, layer)
        if trackedlayer:
            if os.path.exists(trackedlayer.geopkg):
                try:
                    con = sqlite3.connect(trackedlayer.geopkg)
                    cursor = con.cursor()
                    cursor.execute("SELECT commit_id FROM geogig_audited_tables WHERE table_name='%s';" % layer)
                    currentCommitId = cursor.fetchone()[0]
                    cursor.close()
                    con.close()
                    if commitId != currentCommitId:
                        msgBox = QMessageBox()
                        msgBox.setWindowTitle("Layer was already exported")
                        msgBox.setText("This layer was exported already at a different commit.\n"
                                       "Which one would you like to add to your QGIS project?")
                        msgBox.addButton(QPushButton('Use previously exported commit'), QMessageBox.YesRole)
                        msgBox.addButton(QPushButton('Use latest commit from this branch'), QMessageBox.NoRole)
                        msgBox.addButton(QPushButton('Cancel'), QMessageBox.RejectRole)
                        QApplication.restoreOverrideCursor()
                        ret = msgBox.exec_()
                        if ret == 0:
                            checkoutLayer(repo, layer, None, currentCommitId)
                        elif ret == 1:
                            try:
                                layer = checkoutLayer(repo, layer, None, commitId)
                                repoWatcher.layerUpdated.emit(layer)
                            except HasLocalChangesError:
                                QMessageBox.warning(config.iface.mainWindow(), 'Cannot export this commit',
                                                    "The layer has local changes that would be overwritten.\n"
                                                    "Either sync layer with branch or revert local changes "
                                                    "before changing commit",QMessageBox.Ok)
                except:
                    checkoutLayer(repo, layer, None, currentCommitId)

        else:
            checkoutLayer(repo, layer, None, commitId)
Exemplo n.º 43
0
    def _map_tool_changed(self, args: MapToolChangedArgs):
        # TODO parent was removed
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Question)
        msg.setText(
            QCoreApplication.translate(
                "WizardTranslations",
                "Do you really want to change the map tool?"))
        msg.setWindowTitle(
            QCoreApplication.translate("WizardTranslations",
                                       "CHANGING MAP TOOL?"))
        msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        msg.button(QMessageBox.Yes).setText(
            QCoreApplication.translate("WizardTranslations",
                                       "Yes, and close the wizard"))
        msg.button(QMessageBox.No).setText(
            QCoreApplication.translate("WizardTranslations",
                                       "No, continue editing"))
        reply = msg.exec_()

        if reply == QMessageBox.Yes:
            args.change_map_tool = True
            self._wizard_messages.show_map_tool_changed_msg()
            self.close_wizard()
Exemplo n.º 44
0
    def ok(self):
        # Here check if select OK button, get the layer fields
        # Initialize
        # [JUNCTIONS]
        if self.dlg.out.text() == '':
            if self.selectOutp():
                return
        elif os.path.isabs(self.dlg.out.text()) == False:
            if self.selectOutp():
                return

        self.outEpanetName = self.dlg.out.text()

        try:
            f = open(self.outEpanetName, "w")
            f.close()
        except:
            msgBox = QMessageBox()
            msgBox.setIcon(QMessageBox.Warning)
            msgBox.setWindowTitle('Warning')
            msgBox.setText('Please define Epanet Inp File location.')
            msgBox.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowStaysOnTopHint | Qt.WindowCloseButtonHint)
            msgBox.exec_()
            return

        if not self.layers:
            msgBox = QMessageBox()
            msgBox.setIcon(QMessageBox.Warning)
            msgBox.setWindowTitle('Warning')
            msgBox.setText('No layers selected.')
            msgBox.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowStaysOnTopHint | Qt.WindowCloseButtonHint)
            msgBox.exec_()
            return
        xypipes_id = []
        xypipesvert = []
        for sect in self.sections:
            exec('sect' + sect + '=[]') in globals(), locals()
            exec('xy' + sect + '=[]') in globals(), locals()
            if eval('self.dlg.sect_' + sect + '.currentText()') != 'NONE':
                # Get layer field names
                indLayerName = self.layer_list.index(eval('self.dlg.sect_' + sect + '.currentText()')) - 1
                provider = self.layers[indLayerName].dataProvider()
                fields = provider.fields()
                field_names = [field.name() for field in fields]
                for elem in self.layers[indLayerName].getFeatures():
                    eval('sect' + sect + '.append(dict(zip(field_names, elem.attributes())))')
                    if any(sect in s for s in self.sections[0:5]):
                        geom = elem.geometry()
                        if self.layers[indLayerName].geometryType() == 0:
                            eval('xy' + sect + '.append(geom.asPoint())')
                        elif self.layers[indLayerName].geometryType() == 1:
                            eval('xy' + sect + '.append(geom.asPolyline())')
                            if sect == 'pipes':
                                if len(geom.asPolyline()) > 2:
                                    for pp in range(len(geom.asPolyline())-2):
                                        xypipes_id.append(elem.attributes()[0])
                                        xypipesvert.append(geom.asPolyline()[pp])
                    if sect == 'junctions':
                        if 'Elevation' not in locals()['sect' + sect][0].keys():
                            QMessageBox.warning(QWidget(), "Message", "Elevation field is missing.")
        # (myDirectory,nameFile) = os.path.split(self.iface.activeLayer().dataProvider().dataSourceUri())
        my_directory = ''
        f = open(self.outEpanetName, 'wt')
        f.write('[TITLE]\n')
        f.write('Export input file via plugin ExportEpanetInpFiles. \n\n')
        f.write('[JUNCTIONS]\n')
        node_i_ds = []
        for i in range(len(locals()['sectjunctions'])):
            node_i_ds.append(locals()['sectjunctions'][i]['ID'])
            f.write(locals()['sectjunctions'][i]['ID'] + '   ' + str(locals()['sectjunctions'][i]['Elevation']) + '\n')
        f.write('\n[RESERVOIRS]\n')
        for i in range(len(locals()['sectreservoirs'])):
            node_i_ds.append(locals()['sectreservoirs'][i]['ID'])
            f.write(locals()['sectreservoirs'][i]['ID'] + '   ' + str(locals()['sectreservoirs'][i]['Head']) + '\n')
        f.write('\n[TANKS]\n')
        for i in range(len(locals()['secttanks'])):
            node_i_ds.append(locals()['secttanks'][i]['ID'])
            if locals()['secttanks'][i]['VolumeCurv'] == None:
                locals()['secttanks'][i]['VolumeCurv'] = ""
            f.write(locals()['secttanks'][i]['ID'] + '   ' + str(locals()['secttanks'][i]['Elevation']) + '   ' + str(locals()['secttanks'][i]['InitLevel'])
                    + '   ' + str(locals()['secttanks'][i]['MinLevel']) + '   ' + str(locals()['secttanks'][i]['MaxLevel']) + '   ' + str(
                locals()['secttanks'][i]['Diameter'])
                    + '   ' + str(locals()['secttanks'][i]['MinVolume']) + '   ' + str(locals()['secttanks'][i]['VolumeCurv']) + '   ' + '\n')
        f.write('\n[PIPES]\n')
        for i in range(len(locals()['sectpipes'])):
            if (locals()['sectpipes'][i]['NodeFrom'] in node_i_ds) and (locals()['sectpipes'][i]['NodeTo'] in node_i_ds):
                f.write(locals()['sectpipes'][i]['ID'] + '   ' + locals()['sectpipes'][i]['NodeFrom']
                        + '   ' + locals()['sectpipes'][i]['NodeTo'] + '   ' + str(locals()['sectpipes'][i]['Length']) + '   ' + str(
                    locals()['sectpipes'][i]['Diameter'])
                        + '   ' + str(locals()['sectpipes'][i]['Roughness']) + '   ' + str(locals()['sectpipes'][i]['MinorLoss']) + '   ' +
                        locals()['sectpipes'][i]['Status'] + '\n')
        f.write('\n[PUMPS]\n')
        for i in range(len(locals()['sectpumps'])):
            if locals()['sectpumps'][i]['Curve'] != 'NULL':
                try:
                    locals()['sectpumps'][i]['Curve'] = 'HEAD ' + locals()['sectpumps'][i]['Curve']
                except:
                    locals()['sectpumps'][i]['Curve'] = 'HEAD '
            else:
                locals()['sectpumps'][i]['Curve'] = ''
            if locals()['sectpumps'][i]['Pattern'] != 'NULL':
                try:
                    locals()['sectpumps'][i]['Pattern'] = 'PATTERN ' + locals()['sectpumps'][i]['Pattern']
                except:
                    locals()['sectpumps'][i]['Pattern'] = "PATTERN "
            else:
                locals()['sectpumps'][i]['Pattern'] = ''
            if locals()['sectpumps'][i]['Power'] != 'NULL':
                try:
                    locals()['sectpumps'][i]['Power'] = 'POWER ' + locals()['sectpumps'][i]['Power']
                except:
                    locals()['sectpumps'][i]['Power'] = "POWER "
            else:
                locals()['sectpumps'][i]['Power'] = ''

            try:
                f.write(locals()['sectpumps'][i]['ID'] + '   ' + locals()['sectpumps'][i]['NodeFrom']
                        + '   ' + locals()['sectpumps'][i]['NodeTo'] + '   ' + locals()['sectpumps'][i]['Curve']
                        + '   ' + str(locals()['sectpumps'][i]['Pattern']) + '   ' + str(locals()['sectpumps'][i]['Power']) + '\n')
            except:
                f.write(locals()['sectpumps'][i]['ID']  +'\n')

        f.write('\n[VALVES]\n')
        if self.dlg.sect_valves.currentText() != 'NONE':
            for i in range(len(locals()['sectvalves'])):
                try:
                    locals()['sectvalves'][i]['NodeFrom'] = locals()['sectvalves'][i]['NodeFrom'] + ''
                except:
                    locals()['sectvalves'][i]['NodeFrom'] = ""

                try:
                    locals()['sectvalves'][i]['NodeTo'] = locals()['sectvalves'][i]['NodeTo'] + ''
                except:
                    locals()['sectvalves'][i]['NodeTo'] = ""
                f.write("{}   {}   {}   {}    {}    {}    {}\n".format(locals()['sectvalves'][i]['ID'], locals()['sectvalves'][i]['NodeFrom'],
                                                                       locals()['sectvalves'][i]['NodeTo'],
                                                                       str(locals()['sectvalves'][i]['Diameter']),
                                                                       locals()['sectvalves'][i]['Type'],
                                                                       str(locals()['sectvalves'][i]['Setting']),
                                                                       str(locals()['sectvalves'][i]['MinorLoss'])))

        f.write('\n[DEMANDS]\n')
        for i in range(len(locals()['sectjunctions'])):
            for u in range(int((len(locals()['sectjunctions'][i]) - 2) / 2)):
                if locals()['sectjunctions'][i]['Demand' + str(u + 1)] == 0 and str(
                        locals()['sectjunctions'][i]['Pattern' + str(u + 1)]) == 'None':
                    continue
                if str(locals()['sectjunctions'][i]['Pattern' + str(u + 1)]) == 'NULL' or str(
                        locals()['sectjunctions'][i]['Pattern' + str(u + 1)]) == 'None':
                    locals()['sectjunctions'][i]['Pattern' + str(u + 1)] = ''
                f.write(locals()['sectjunctions'][i]['ID'] + '   ' + str(locals()['sectjunctions'][i]['Demand' + str(u + 1)])
                        + '   ' + str(locals()['sectjunctions'][i]['Pattern' + str(u + 1)]) + '\n')

        f.write('\n[STATUS]\n')
        for i in range(len(locals()['sectSTATUS'])):
            f.write("{}   {}\n".format(locals()['sectSTATUS'][i]['Link_ID'], locals()['sectSTATUS'][i]['Status/Set']))

        f.write('\n[PATTERNS]\n')
        for i in range(len(locals()['sectPATTERNS'])):
            f.write("{}   {}\n".format(locals()['sectPATTERNS'][i]['Pattern_ID'], locals()['sectPATTERNS'][i]['Multiplier']))

        f.write('\n[CURVES]\n')
        for i in range(len(locals()['sectCURVES'])):
            f.write(";{}:\n   {}   {}   {}\n".format(locals()['sectCURVES'][i]['Type'], locals()['sectCURVES'][i]['Curve_ID'],
                                                     str(locals()['sectCURVES'][i]['X-Value']),str(locals()['sectCURVES'][i]['Y-Value'])))

        f.write('\n[CONTROLS]\n')
        for i in range(len(locals()['sectCONTROLS'])):
            f.write("{}\n".format(locals()['sectCONTROLS'][i]['Controls']))

        f.write('\n[RULES]\n')
        for i in range(len(locals()['sectRULES'])):
            f.write("RULE {}\n   {}\n".format(locals()['sectRULES'][i]['Rule_ID'], locals()['sectRULES'][i]['Rule']))

        f.write('\n[ENERGY]\n')
        if locals()['sectENERGY']:
            try: f.write('Global Efficiency   ' + str(locals()['sectENERGY'][0]['GlobalEffi']) + '\n')
            except: pass
            try: f.write('Global Price    ' + str(locals()['sectENERGY'][0]['GlobalPric']) + '\n')
            except: pass
            try: f.write('Demand Charge   ' + str(locals()['sectENERGY'][0]['DemCharge']) + '\n')
            except: pass

        f.write('\n[REACTIONS]\n')
        if locals()['sectREACTIONS']:
            try: f.write('Order Bulk   ' + str(locals()['sectREACTIONS'][0]['OrderBulk']) + '\n')
            except: pass
            try: f.write('Order Tank    ' + str(locals()['sectREACTIONS'][0]['OrderTank']) + '\n')
            except: pass
            try: f.write('Order Wall   ' + str(locals()['sectREACTIONS'][0]['OrderWall']) + '\n')
            except: pass
            try: f.write('Global Bulk   ' + str(locals()['sectREACTIONS'][0]['GlobalBulk']) + '\n')
            except: pass
            try: f.write('Global Wall   ' + str(locals()['sectREACTIONS'][0]['GlobalWall']) + '\n')
            except: pass
            try: f.write('Limiting Potential   ' + str(locals()['sectREACTIONS'][0]['LimPotent']) + '\n')
            except: pass
            try: f.write('Roughness Correlation   ' + str(locals()['sectREACTIONS'][0]['RoughCorr']) + '\n')
            except: pass

        f.write('\n[REACTIONS]\n')
        for i in range(len(locals()['sectREACTIONS_I'])):
            f.write('{}    {}    {} \n'.format(locals()['sectREACTIONS_I'][i]['Type'],
                                               locals()['sectREACTIONS_I'][i]['Pipe/Tank'], str(locals()['sectREACTIONS_I'][i]['Coeff.'])))
        f.write('\n[EMITTERS]\n')
        for i in range(len(locals()['sectEMITTERS'])):
            f.write('{}    {}\n'.format(locals()['sectEMITTERS'][i]['Junc_ID'], str(locals()['sectEMITTERS'][i]['Coeff.'])))

        f.write('\n[SOURCES]\n')
        for i in range(len(locals()['sectSOURCES'])):
            try:
                locals()['sectSOURCES'][i]['Pattern'] = locals()['sectSOURCES'][i]['Pattern']  + ''
            except:
                locals()['sectSOURCES'][i]['Pattern'] = ''
            f.write("{}   {}   {}   {}\n".format(locals()['sectSOURCES'][i]['Node_ID'], locals()['sectSOURCES'][i]['Type'],
                                                                   str(locals()['sectSOURCES'][i]['Strength']),
                                                                   locals()['sectSOURCES'][i]['Pattern']))

        f.write('\n[MIXING]\n')
        for i in range(len(locals()['sectMIXING'])):
            f.write('{}    {}    {} \n'.format(locals()['sectMIXING'][i]['Tank_ID'],
                                               locals()['sectMIXING'][i]['Model'], str(locals()['sectMIXING'][i]['Fraction'])))

        f.write('\n[TIMES]\n')
        if locals()['sectTIMES']:
            try: f.write('Duration   ' + str(locals()['sectTIMES'][0]['Duration']) + '\n')
            except: pass
            try: f.write('Hydraulic Timestep    ' + str(locals()['sectTIMES'][0]['HydStep']) + '\n')
            except: pass
            try: f.write('Quality Timestep   ' + str(locals()['sectTIMES'][0]['QualStep']) + '\n')
            except: pass
            try: f.write('Pattern Timestep   ' + str(locals()['sectTIMES'][0]['PatStep']) + '\n')
            except: pass
            try: f.write('Pattern Start   ' + str(locals()['sectTIMES'][0]['PatStart']) + '\n')
            except: pass
            try: f.write('Report Timestep   ' + str(locals()['sectTIMES'][0]['RepStep']) + '\n')
            except: pass
            try: f.write('Report Start   ' + str(locals()['sectTIMES'][0]['RepStart']) + '\n')
            except: pass
            try: f.write('Start ClockTime   ' + str(locals()['sectTIMES'][0]['StartClock']) + '\n')
            except: pass
            try: f.write('Statistic   ' + str(locals()['sectTIMES'][0]['Statistic']) + '\n')
            except: pass

        f.write('\n[REPORT]\n')
        if locals()['sectREPORT']:
            try: f.write('Status   ' + locals()['sectREPORT'][0]['Status'] + '\n')
            except: pass
            try: f.write('Summary    ' + locals()['sectREPORT'][0]['Summary'] + '\n')
            except: pass
            try: f.write('Page   ' + locals()['sectREPORT'][0]['Page'] + '\n')
            except: pass

        f.write('\n[OPTIONS]\n')
        if locals()['sectOPTIONS']:
            try: f.write('Units   ' + str(locals()['sectOPTIONS'][0]['Units']) + '\n');
            except: pass
            try: f.write('Headloss    ' + str(locals()['sectOPTIONS'][0]['Headloss']) + '\n')
            except: pass
            try: f.write('Specific Gravity   ' + str(locals()['sectOPTIONS'][0]['SpecGrav']) + '\n')
            except: pass
            try: f.write('Viscosity   ' + str(locals()['sectOPTIONS'][0]['Viscosity']) + '\n')
            except: pass
            try: f.write('Trials   ' + str(locals()['sectOPTIONS'][0]['Trials']) + '\n')
            except: pass
            try: f.write('Accuracy   ' + str(locals()['sectOPTIONS'][0]['Accuracy']) + '\n')
            except: pass
            try: f.write('CHECKFREQ   ' + str(locals()['sectOPTIONS'][0]['CheckFreq']) + '\n')
            except: pass
            try: f.write('MAXCHECK   ' + str(locals()['sectOPTIONS'][0]['MaxCheck']) + '\n')
            except: pass
            try: f.write('DAMPLIMIT   ' + str(locals()['sectOPTIONS'][0]['DampLimit']) + '\n')
            except: pass
            try: f.write('Unbalanced   ' + str(locals()['sectOPTIONS'][0]['Unbalanced']) + '\n')
            except: pass
            try: f.write('Pattern   ' + str(locals()['sectOPTIONS'][0]['PatID']) + '\n')
            except: pass
            try: f.write('Demand Multiplier   ' + str(locals()['sectOPTIONS'][0]['DemMult']) + '\n')
            except: pass
            try: f.write('Emitter Exponent   ' + str(locals()['sectOPTIONS'][0]['EmitExp']) + '\n')
            except: pass
            try: f.write('Quality   ' + str(locals()['sectOPTIONS'][0]['Quality']) + '\n')
            except: pass
            try: f.write('Diffusivity   ' + str(locals()['sectOPTIONS'][0]['Diffusivit']) + '\n')
            except: pass
            try: f.write('Tolerance   ' + str(locals()['sectOPTIONS'][0]['Tolerance']) + '\n')
            except: pass

        f.write('\n[COORDINATES]\n')
        for i in range(len(locals()['sectjunctions'])):
            f.write(locals()['sectjunctions'][i]['ID'] + '   ' + str(locals()['xyjunctions'][i][0]) + '   ' + str(locals()['xyjunctions'][i][1]) + '\n')
        for i in range(len(locals()['sectreservoirs'])):
            f.write(locals()['sectreservoirs'][i]['ID'] + '   ' + str(locals()['xyreservoirs'][i][0]) + '   ' + str(locals()['xyreservoirs'][i][1]) + '\n')
        for i in range(len(locals()['secttanks'])):
            f.write(locals()['secttanks'][i]['ID'] + '   ' + str(locals()['xytanks'][i][0]) + '   ' + str(locals()['xytanks'][i][1]) + '\n')

        f.write('\n[VERTICES]\n')

        for l in range(len(xypipes_id)):
                f.write(xypipes_id[l] + '   ' + str(xypipesvert[l][0]) + '   ' + str(xypipesvert[l][1]) + '\n')

        f.write('\n[END]\n')

        f.close()

        self.cancel()
        msgBox = QMessageBox()
        msgBox.setWindowTitle('Export Options')
        msgBox.setText('Export Epanet Inp File "' + self.outEpanetName + '" succesful.')
        msgBox.exec_()
    def installBaseMap(self):
        authcfg = get_oauth_authcfg()
        if authcfg is None:
            self._showMessage('Could not find a valid authentication configuration!',
                              QgsMessageBar.WARNING)
            return False

        authId = authcfg.id()
        mapBoxStreets = basemaputils.getMapBoxStreetsMap(self.token)

        if os.path.isfile(basemaputils.defaultProjectPath()):
            # default project already exists, make a backup copy
            backup = basemaputils.defaultProjectPath().replace(
                '.qgs', '-%s.qgs' % datetime.now().strftime('%Y-%m-%d-%H_%M_%S'))
            shutil.copy2(basemaputils.defaultProjectPath(), backup)
            self._showMessage("A backup copy of the previous default project "
                              "has been saved to {}".format(backup))

            msgBox = QMessageBox()
            msgBox.setIcon(QMessageBox.Question)
            msgBox.setText("A default project already exists.  Do you "
                           "wish to add the Boundless basemap to your "
                           "existing default project or create a new "
                           "default project?")
            btnAdd = msgBox.addButton("Add", QMessageBox.ActionRole)
            btnCreateNew = msgBox.addButton("Create New", QMessageBox.ActionRole)
            msgBox.exec_()
            if msgBox.clickedButton() == btnAdd:
                if not basemaputils.addToDefaultProject([mapBoxStreets], ["Mapbox Streets"], authId):
                    self._showMessage("Could not update default project with basemap!",
                                      QgsMessageBar.WARNING)
                    return False
            elif msgBox.clickedButton() == btnCreateNew:
                template = basemaputils.PROJECT_DEFAULT_TEMPLATE

                prj = basemaputils.createDefaultProject([mapBoxStreets], ["Mapbox Streets"],
                                                        template, authId)
                if prj is None or prj == '':
                    self._showMessage("Could not create a valid default project from the template '{}'!".format(template),
                                      QgsMessageBar.WARNING)
                    return False

                if not basemaputils.writeDefaultProject(prj):
                    self._showMessage("Could not write the default project on disk!",
                                      QgsMessageBar.WARNING)
                    return False
        else:
            # no default project, create one
            template = basemaputils.PROJECT_DEFAULT_TEMPLATE

            prj = basemaputils.createDefaultProject([mapBoxStreets], ["Mapbox Streets"],
                                               template, authId)
            if prj is None or prj == '':
                self._showMessage("Could not create a valid default project from the template '{}'!".format(template),
                                  QgsMessageBar.WARNING)
                return False

            if not basemaputils.writeDefaultProject(prj):
                self._showMessage("Could not write the default project on disk!",
                                  QgsMessageBar.WARNING)
                return False

        self._showMessage("Basemap added to the default project.")
        return True
Exemplo n.º 46
0
 def msgbox(self, texto, icon=QMessageBox.Information):
     msg = QMessageBox()
     msg.setText(str(texto))
     msg.setIcon(icon)
     msg.exec_()
Exemplo n.º 47
0
class ExportDialog(QDialog, DIALOG_UI):
    ValidExtensions = ["xtf", "XTF", "itf", "ITF", "gml", "GML", "xml", "XML"]

    def __init__(self, base_config, parent=None):
        QDialog.__init__(self, parent)
        self.setupUi(self)
        self.db_simple_factory = DbSimpleFactory()
        QgsGui.instance().enableAutoGeometryRestore(self)
        self.buttonBox.accepted.disconnect()
        self.buttonBox.clear()
        self.buttonBox.addButton(QDialogButtonBox.Cancel)
        self.buttonBox.addButton(QDialogButtonBox.Help)
        self.buttonBox.helpRequested.connect(self.help_requested)

        self.export_text = self.tr("Export")
        self.set_button_to_export_action = QAction(self.export_text, None)
        self.set_button_to_export_action.triggered.connect(
            self.set_button_to_export)

        self.export_without_validation_text = self.tr(
            "Export without validation")
        self.set_button_to_export_without_validation_action = QAction(
            self.export_without_validation_text, None)
        self.set_button_to_export_without_validation_action.triggered.connect(
            self.set_button_to_export_without_validation)

        self.edit_command_action = QAction(self.tr("Edit ili2db command"),
                                           None)
        self.edit_command_action.triggered.connect(self.edit_command)

        self.export_tool_button.addAction(
            self.set_button_to_export_without_validation_action)
        self.export_tool_button.addAction(self.edit_command_action)
        self.export_tool_button.setText(self.export_text)
        self.export_tool_button.clicked.connect(self.accepted)

        self.xtf_file_browse_button.clicked.connect(
            make_save_file_selector(
                self.xtf_file_line_edit,
                title=self.tr("Save in XTF Transfer File"),
                file_filter=self.
                tr("XTF Transfer File (*.xtf *XTF);;Interlis 1 Transfer File (*.itf *ITF);;XML (*.xml *XML);;GML (*.gml *GML)"
                   ),
                extension=".xtf",
                extensions=["." + ext for ext in self.ValidExtensions],
            ))
        self.xtf_file_browse_button.clicked.connect(
            self.xtf_browser_opened_to_true)
        self.xtf_browser_was_opened = False

        self.type_combo_box.clear()
        self._lst_panel = dict()

        for db_id in self.db_simple_factory.get_db_list(False):
            self.type_combo_box.addItem(displayDbIliMode[db_id], db_id)
            db_factory = self.db_simple_factory.create_factory(db_id)
            item_panel = db_factory.get_config_panel(self, DbActionType.EXPORT)
            self._lst_panel[db_id] = item_panel
            self.db_layout.addWidget(item_panel)

        self.validators = Validators()

        fileValidator = FileValidator(
            pattern=["*." + ext for ext in self.ValidExtensions],
            allow_non_existing=True,
        )

        self.xtf_file_line_edit.setValidator(fileValidator)
        self.xtf_file_line_edit.textChanged.connect(
            self.validators.validate_line_edits)
        self.xtf_file_line_edit.textChanged.connect(
            self.xtf_browser_opened_to_false)
        self.xtf_file_line_edit.textChanged.emit(
            self.xtf_file_line_edit.text())

        # Reset to export as default text
        self.xtf_file_line_edit.textChanged.connect(self.set_button_to_export)

        # refresh the models on changing values but avoid massive db connects by timer
        self.refreshTimer = QTimer()
        self.refreshTimer.setSingleShot(True)
        self.refreshTimer.timeout.connect(self.refresh_models)

        for key, value in self._lst_panel.items():
            value.notify_fields_modified.connect(
                self.request_for_refresh_models)

        self.validate_data = True  # validates exported data by default, We use --disableValidation when is False
        self.base_configuration = base_config
        self.restore_configuration()

        self.export_models_model = ExportModels()
        self.export_models_view.setModel(self.export_models_model)
        self.export_models_view.clicked.connect(self.export_models_model.check)
        self.export_models_view.space_pressed.connect(
            self.export_models_model.check)
        self.request_for_refresh_models()

        self.type_combo_box.currentIndexChanged.connect(self.type_changed)

        self.bar = QgsMessageBar()
        self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.txtStdout.setLayout(QGridLayout())
        self.txtStdout.layout().setContentsMargins(0, 0, 0, 0)
        self.txtStdout.layout().addWidget(self.bar, 0, 0, Qt.AlignTop)

    def request_for_refresh_models(self):
        # hold refresh back
        self.refreshTimer.start(500)

    def refresh_models(self):
        self.refreshed_export_models_model()

    def refreshed_export_models_model(self):
        tool = self.type_combo_box.currentData() & ~DbIliMode.ili

        configuration = self.updated_configuration()
        schema = configuration.dbschema

        db_factory = self.db_simple_factory.create_factory(tool)
        config_manager = db_factory.get_db_command_config_manager(
            configuration)
        uri_string = config_manager.get_uri(configuration.db_use_super_login)

        db_connector = None

        try:
            db_connector = db_factory.get_db_connector(uri_string, schema)
        except (DBConnectorError, FileNotFoundError):
            # when wrong connection parameters entered, there should just be returned an empty model - so let it pass
            pass

        self.export_models_model.refresh_models(db_connector)

    def db_ili_version(self, configuration):
        """
        Returns the ili2db version the database has been created with or None if the database
        could not be detected as a ili2db database
        """
        schema = configuration.dbschema

        db_factory = self.db_simple_factory.create_factory(configuration.tool)
        config_manager = db_factory.get_db_command_config_manager(
            configuration)
        uri_string = config_manager.get_uri(configuration.db_use_super_login)

        db_connector = None

        try:
            db_connector = db_factory.get_db_connector(uri_string, schema)
            return db_connector.ili_version()
        except (DBConnectorError, FileNotFoundError):
            return None

    def set_button_to_export(self):
        """
        Changes the text of the button to export (with validation) and sets the validate_data to true.
        So on clicking the button the export will start with validation.
        The buttons actions are changed to be able to switch the without-validation mode.
        """
        self.validate_data = True
        self.export_tool_button.removeAction(self.set_button_to_export_action)
        self.export_tool_button.removeAction(self.edit_command_action)
        self.export_tool_button.addAction(
            self.set_button_to_export_without_validation_action)
        self.export_tool_button.addAction(self.edit_command_action)
        self.export_tool_button.setText(self.export_text)

    def set_button_to_export_without_validation(self):
        """
        Changes the text of the button to export without validation and sets the validate_data to false.
        So on clicking the button the export will start without validation.
        The buttons actions are changed to be able to switch the with-validation mode.
        """
        self.validate_data = False
        self.export_tool_button.removeAction(
            self.set_button_to_export_without_validation_action)
        self.export_tool_button.removeAction(self.edit_command_action)
        self.export_tool_button.addAction(self.set_button_to_export_action)
        self.export_tool_button.addAction(self.edit_command_action)
        self.export_tool_button.setText(self.export_without_validation_text)

    def edit_command(self):
        """
        A dialog opens giving the user the possibility to edit the ili2db command used for the export
        """
        exporter = iliexporter.Exporter()
        exporter.tool = self.type_combo_box.currentData()
        exporter.configuration = self.updated_configuration()
        command = exporter.command(True)
        edit_command_dialog = EditCommandDialog(self)
        edit_command_dialog.command_edit.setPlainText(command)
        if edit_command_dialog.exec_():
            edited_command = edit_command_dialog.command_edit.toPlainText()
            self.accepted(edited_command)

    def accepted(self, edited_command=None):
        db_id = self.type_combo_box.currentData()

        res, message = self._lst_panel[db_id].is_valid()

        if not res:
            self.txtStdout.setText(message)
            return

        configuration = self.updated_configuration()

        if not edited_command:
            if (not self.xtf_file_line_edit.validator().validate(
                    configuration.xtffile, 0)[0] == QValidator.Acceptable):
                self.txtStdout.setText(
                    self.
                    tr("Please set a valid INTERLIS XTF file before exporting data."
                       ))
                self.xtf_file_line_edit.setFocus()
                return
            if not configuration.ilimodels:
                self.txtStdout.setText(
                    self.tr("Please set a model before exporting data."))
                self.export_models_view.setFocus()
                return

        # If xtf browser was opened and the file exists, the user already chose
        # to overwrite the file
        if (os.path.isfile(self.xtf_file_line_edit.text().strip())
                and not self.xtf_browser_was_opened):
            self.msg = QMessageBox()
            self.msg.setIcon(QMessageBox.Warning)
            self.msg.setText(
                self.tr(
                    "{filename} already exists.\nDo you want to replace it?").
                format(filename=os.path.basename(
                    self.xtf_file_line_edit.text().strip())))
            self.msg.setWindowTitle(self.tr("Save in XTF Transfer File"))
            self.msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            msg_box = self.msg.exec_()
            if msg_box == QMessageBox.No:
                return

        with OverrideCursor(Qt.WaitCursor):
            self.progress_bar.show()
            self.progress_bar.setValue(0)

            self.disable()
            self.txtStdout.setTextColor(QColor(LogColor.COLOR_INFO))
            self.txtStdout.clear()

            exporter = iliexporter.Exporter()
            exporter.tool = self.type_combo_box.currentData()
            exporter.configuration = configuration

            self.save_configuration(configuration)

            exporter.stdout.connect(self.print_info)
            exporter.stderr.connect(self.on_stderr)
            exporter.process_started.connect(self.on_process_started)
            exporter.process_finished.connect(self.on_process_finished)

            self.progress_bar.setValue(25)

            try:
                if exporter.run(
                        edited_command) != iliexporter.Exporter.SUCCESS:
                    if configuration.db_ili_version == 3:
                        # failed with a db created by ili2db version 3
                        if not edited_command:
                            # fallback because of issues with --export3 argument
                            self.show_message(
                                Qgis.Warning,
                                self.
                                tr("Tried export with ili2db version 3.x.x (fallback)"
                                   ),
                            )

                            exporter.version = 3
                            # ... and enforce the Exporter to use ili2db version 3.x.x
                            if exporter.run() != iliexporter.Exporter.SUCCESS:
                                self.enable()
                                self.progress_bar.hide()
                                return
                        else:
                            self.show_message(
                                Qgis.Warning,
                                self.
                                tr("Tried export with ili2db version 3.x.x (no fallback with editted command)"
                                   ),
                            )
                            return
                    else:
                        self.enable()
                        self.progress_bar.hide()
                        return
            except JavaNotFoundError as e:
                self.txtStdout.setTextColor(QColor(LogColor.COLOR_INFO))
                self.txtStdout.clear()
                self.txtStdout.setText(e.error_string)
                self.enable()
                self.progress_bar.hide()
                return

            self.buttonBox.clear()
            self.buttonBox.setEnabled(True)
            self.buttonBox.addButton(QDialogButtonBox.Close)
            self.progress_bar.setValue(100)

    def print_info(self, text):
        self.txtStdout.setTextColor(QColor(LogColor.COLOR_INFO))
        self.txtStdout.append(text)
        QCoreApplication.processEvents()

    def on_stderr(self, text):
        color_log_text(text, self.txtStdout)
        self.advance_progress_bar_by_text(text)
        QCoreApplication.processEvents()

    def show_message(self, level, message):
        if level == Qgis.Warning:
            self.bar.pushMessage(message, Qgis.Info, 10)
        elif level == Qgis.Critical:
            self.bar.pushMessage(message, Qgis.Warning, 10)

    def on_process_started(self, command):
        self.disable()
        self.txtStdout.setTextColor(QColor(LogColor.COLOR_INFO))
        self.txtStdout.clear()
        self.txtStdout.setText(command)
        QCoreApplication.processEvents()

    def on_process_finished(self, exit_code, result):
        color = "#004905" if exit_code == 0 else "#aa2222"
        self.txtStdout.setTextColor(QColor(color))
        self.txtStdout.append(self.tr("Finished ({})".format(exit_code)))
        if result == iliexporter.Exporter.SUCCESS:
            self.buttonBox.clear()
            self.buttonBox.setEnabled(True)
            self.buttonBox.addButton(QDialogButtonBox.Close)
        else:
            if self.export_without_validate():
                self.set_button_to_export_without_validation()
            self.enable()

    def export_without_validate(self):
        """
        Valid if an error occurred that prevents executing the export without validations
        :return: True if you can execute the export without validations, False in other case
        """
        log = self.txtStdout.toPlainText().lower()
        if "permission denied" in log or "access is denied" in log:
            return False
        return True

    def updated_configuration(self):
        """
        Get the configuration that is updated with the user configuration changes on the dialog.
        :return: Configuration
        """
        configuration = ili2dbconfig.ExportConfiguration()

        mode = self.type_combo_box.currentData()
        self._lst_panel[mode].get_fields(configuration)

        configuration.tool = mode
        configuration.xtffile = self.xtf_file_line_edit.text().strip()
        configuration.ilimodels = ";".join(
            self.export_models_model.checked_models())
        configuration.base_configuration = self.base_configuration
        configuration.db_ili_version = self.db_ili_version(configuration)

        if not self.validate_data:
            configuration.disable_validation = True
        return configuration

    def save_configuration(self, configuration):
        settings = QSettings()
        settings.setValue("QgisModelBaker/ili2pg/xtffile_export",
                          configuration.xtffile)
        settings.setValue("QgisModelBaker/importtype",
                          self.type_combo_box.currentData().name)

        mode = self.type_combo_box.currentData()
        db_factory = self.db_simple_factory.create_factory(mode)
        config_manager = db_factory.get_db_command_config_manager(
            configuration)
        config_manager.save_config_in_qsettings()

    def restore_configuration(self):
        settings = QSettings()

        for db_id in self.db_simple_factory.get_db_list(False):
            configuration = iliexporter.ExportConfiguration()
            db_factory = self.db_simple_factory.create_factory(db_id)
            config_manager = db_factory.get_db_command_config_manager(
                configuration)
            config_manager.load_config_from_qsettings()
            self._lst_panel[db_id].set_fields(configuration)

        mode = settings.value("QgisModelBaker/importtype")
        mode = DbIliMode[
            mode] if mode else self.db_simple_factory.default_database
        mode = mode & ~DbIliMode.ili

        self.type_combo_box.setCurrentIndex(self.type_combo_box.findData(mode))
        self.refresh_db_panel()

    def disable(self):
        self.type_combo_box.setEnabled(False)
        for key, value in self._lst_panel.items():
            value.setEnabled(False)
        self.ili_config.setEnabled(False)
        self.buttonBox.setEnabled(False)

    def enable(self):
        self.type_combo_box.setEnabled(True)
        for key, value in self._lst_panel.items():
            value.setEnabled(True)
        self.ili_config.setEnabled(True)
        self.buttonBox.setEnabled(True)

    def type_changed(self):
        self.txtStdout.clear()
        self.set_button_to_export()
        self.refresh_db_panel()
        self.refresh_models()
        self.txtStdout.clear()

    def refresh_db_panel(self):
        self.progress_bar.hide()

        db_id = self.type_combo_box.currentData()
        self.db_wrapper_group_box.setTitle(displayDbIliMode[db_id])

        # Refresh panels
        for key, value in self._lst_panel.items():
            value.interlis_mode = False
            is_current_panel_selected = db_id == key
            value.setVisible(is_current_panel_selected)
            if is_current_panel_selected:
                value._show_panel()

    def link_activated(self, link):
        if link.url() == "#configure":
            cfg = OptionsDialog(self.base_configuration)
            if cfg.exec_():
                settings = QSettings()
                settings.beginGroup("QgisModelBaker/ili2db")
                self.base_configuration.save(settings)
        else:
            QDesktopServices.openUrl(link)

    def help_requested(self):
        os_language = QLocale(
            QSettings().value("locale/userLocale")).name()[:2]
        if os_language in ["es", "de"]:
            webbrowser.open(
                "https://opengisch.github.io/QgisModelBaker/docs/{}/user-guide.html#export-an-interlis-transfer-file-xtf"
                .format(os_language))
        else:
            webbrowser.open(
                "https://opengisch.github.io/QgisModelBaker/docs/user-guide.html#export-an-interlis-transfer-file-xtf"
            )

    def xtf_browser_opened_to_true(self):
        """
        Slot. Sets a flag to true to eventually avoid asking a user whether to overwrite a file.
        """
        self.xtf_browser_was_opened = True

    def xtf_browser_opened_to_false(self):
        """
        Slot. Sets a flag to false to eventually ask a user whether to overwrite a file.
        """
        self.xtf_browser_was_opened = False

    def advance_progress_bar_by_text(self, text):
        if text.strip() == "Info: compile models…":
            self.progress_bar.setValue(50)
        elif text.strip() == "Info: create table structure…":
            self.progress_bar.setValue(75)
Exemplo n.º 48
0
    def main(self):

        # If attribute already exists
        if not self.layer.dataProvider().fields().indexFromName( self.dlg.fieldNameLineEdit.text() ) == -1:
            messageBox = QMessageBox()
            messageBox.setWindowTitle( "Sort and Number" )
            messageBox.setText( self.tr("Field name already exists!") )
            messageBox.setInformativeText( self.tr("Continue anyway? Existing values will be lost.") )
            messageBox.setIcon( QMessageBox.Warning )
            messageBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel);
            res = messageBox.exec_()

            # Cancel overwrite
            if res == QMessageBox.Cancel:
                # self.dlg.runButton.setEnabled(True)
                # self.dlg.closeButton.setEnabled(True)
                return
            # Continue and overwrite
            else:
                attrIdx = self.layer.dataProvider().fields().indexFromName( self.dlg.fieldNameLineEdit.text() )
                # Reset order values to NULL
                self.layer.startEditing()
                for f in list( self.layer.getFeatures() ):
                    self.layer.changeAttributeValue(f.id(), attrIdx, None)
                self.layer.commitChanges()


        else:
            # Add new attribute
            self.layer.dataProvider().addAttributes( [QgsField(self.dlg.fieldNameLineEdit.text(), QVariant.Int)] )
            attrIdx = self.layer.dataProvider().fields().indexFromName( self.dlg.fieldNameLineEdit.text() )
            self.layer.updateFields() # tell the vector layer to fetch changes from the provider


        self.dlg.runButton.setEnabled(False)

        # Get params
        field1 = self.dlg.attributeComboBox1.itemData( self.dlg.attributeComboBox1.currentIndex() )
        field1Id = self.layer.dataProvider().fields().indexFromName( field1.name() )
        isInv1 = self.dlg.invCheckBox1.isChecked()

        if self.dlg.attributeComboBox2.currentIndex() > 0:
            field2 = self.dlg.attributeComboBox2.itemData( self.dlg.attributeComboBox2.currentIndex() )
            field2Id = self.layer.dataProvider().fields().indexFromName( field2.name() )
            isInv2 = self.dlg.invCheckBox2.isChecked()
        else:
            field2 = None
            field2Id = None

        if self.dlg.attributeComboBox3.currentIndex() > 0:
            field3 = self.dlg.attributeComboBox3.itemData( self.dlg.attributeComboBox3.currentIndex() )
            field3Id = self.layer.dataProvider().fields().indexFromName( field3.name() )
            isInv3 = self.dlg.invCheckBox3.isChecked()
        else:
            field3 = None
            field3Id = None

        #locale.setlocale(locale.LC_ALL, "") # alphabetical sort

        if self.dlg.selFeatureCheckBox.isChecked():
            featureList = list( self.layer.selectedFeatures() )
            # Message to Log Messages Panel for debugging		
            #QgsMessageLog.logMessage( "Use selected features only.", "QGISSortAndNumber", 0 )
        else:
            featureList = list( self.layer.getFeatures() )
            #QgsMessageLog.logMessage( "Use all features.", "QGISSortAndNumber", 0 )

        if field3Id != None:
            featureList = sorted(featureList, key=lambda f: f[field3Id], reverse=isInv3)

        if field2Id != None:
            featureList = sorted(featureList, key=lambda f: f[field2Id], reverse=isInv2)

        featureList = sorted(featureList, key=lambda f: f[field1Id], reverse=isInv1)

        # add numbering field to layer
        self.layer.startEditing()
        
        for i, f in enumerate(featureList):
            #print f.id()
            self.layer.changeAttributeValue(f.id(), attrIdx, i+1)

        self.layer.commitChanges()

        # "Done" message
        messageBox = QMessageBox()
        messageBox.setWindowTitle( "Sort and Number" )
        messageBox.setText( self.tr("Done") )
        messageBox.setIcon( QMessageBox.Information )
        messageBox.setStandardButtons(QMessageBox.Ok);
        res = messageBox.exec_()

        self.dlg.runButton.setEnabled(True)