Example #1
0
def resetScriptFolder(folder):
    """Check if script folder exist. If not, notify and try to check if it is absolute to another user setting.
    If so, modify folder to change user setting to the current user setting."""

    newFolder = folder
    if os.path.exists(newFolder):
        return newFolder

    QgsMessageLog.logMessage(
        QgsApplication.translate(
            "loadAlgorithms",
            "Script folder {} does not exist").format(newFolder),
        QgsApplication.translate("loadAlgorithms", "Processing"), Qgis.Warning)

    if not os.path.isabs(newFolder):
        return None

    # try to check if folder is absolute to other QgsApplication.qgisSettingsDirPath()

    # isolate "QGIS3/profiles/"
    appIndex = -4
    profileIndex = -3
    currentSettingPath = QDir.toNativeSeparators(
        QgsApplication.qgisSettingsDirPath())
    paths = currentSettingPath.split(os.sep)
    commonSettingPath = os.path.join(paths[appIndex], paths[profileIndex])

    if commonSettingPath in newFolder:
        # strip not common folder part. e.g. preserve the profile path
        # stripping the heading part that come from another location
        tail = newFolder[newFolder.find(commonSettingPath):]
        # tail folder with the actual userSetting path
        header = os.path.join(os.sep, os.path.join(*paths[:appIndex]))
        newFolder = os.path.join(header, tail)

        # skip if it does not exist
        if not os.path.exists(newFolder):
            return None

        QgsMessageLog.logMessage(
            QgsApplication.translate(
                "loadAlgorithms",
                "Script folder changed into {}").format(newFolder),
            QgsApplication.translate("loadAlgorithms", "Processing"),
            Qgis.Warning)

    return newFolder
Example #2
0
def resetScriptFolder(folder):
    """Check if script folder exist. If not, notify and try to check if it is absolute to another user setting.
    If so, modify folder to change user setting to the current user setting."""

    newFolder = folder
    if os.path.exists(newFolder):
        return newFolder

    QgsMessageLog.logMessage(QgsApplication .translate("loadAlgorithms", "Script folder {} does not exist").format(newFolder),
                             QgsApplication.translate("loadAlgorithms", "Processing"),
                             Qgis.Warning)

    if not os.path.isabs(newFolder):
        return None

    # try to check if folder is absolute to other QgsApplication.qgisSettingsDirPath()

    # isolate "QGIS3/profiles/"
    appIndex = -4
    profileIndex = -3
    currentSettingPath = QgsApplication.qgisSettingsDirPath()
    paths = currentSettingPath.split(os.sep)
    commonSettingPath = os.path.join(paths[appIndex], paths[profileIndex])

    if commonSettingPath in newFolder:
        # strip not common folder part. e.g. preserve the profile path
        # stripping the heading part that come from another location
        tail = newFolder[newFolder.find(commonSettingPath):]
        # tail folder with the actual userSetting path
        header = os.path.join(os.sep, os.path.join(*paths[:appIndex]))
        newFolder = os.path.join(header, tail)

        # skip if it does not exist
        if not os.path.exists(newFolder):
            return None

        QgsMessageLog.logMessage(QgsApplication .translate("loadAlgorithms", "Script folder changed into {}").format(newFolder),
                                 QgsApplication.translate("loadAlgorithms", "Processing"),
                                 Qgis.Warning)

    return newFolder
    def onFileSearchPressed(self, row: int):
        """Open file browser to allow user pick a QGIS project file. \
        Trigered when edit button is pressed for a project with type_storage == file.

        :param row: row indice
        :type row: int
        """
        file_widget = self.tableWidget.cellWidget(row, self.cols.uri)
        item = self.tableWidget.item(row, 1)

        filePath = QFileDialog.getOpenFileName(
            self,
            QgsApplication.translate("menu_from_project",
                                     "Projects configuration", None),
            file_widget.text(),
            QgsApplication.translate("menu_from_project",
                                     "QGIS projects (*.qgs *.qgz)", None),
        )

        try:
            if filePath[0]:
                file_widget.setText(filePath[0])

                name_widget = self.tableWidget.cellWidget(row, self.cols.name)
                name = name_widget.text()
                if not name:
                    try:
                        name = filePath[0].split("/")[-1]
                        name = name.split(".")[0]
                    except Exception:
                        name = ""

                    name_widget.setText(name)

        except Exception:
            pass
    def __init__(self, parent, plugin):
        self.plugin = plugin
        self.parent = parent
        QDialog.__init__(self, parent)
        self.setupUi(self)
        self.defaultcursor = self.cursor
        self.setWindowTitle(self.windowTitle() +
                            " - {} v{}".format(__title__, __version__))
        self.setWindowIcon(QIcon(str(DIR_PLUGIN_ROOT /
                                     "resources/gear.svg")), )

        # column order reference
        self.cols = TABLE_COLUMNS_ORDER(edit=0,
                                        name=1,
                                        type_menu_location=3,
                                        type_storage=2,
                                        uri=4)

        # menu locations
        self.LOCATIONS = {
            "new": {
                "index": 0,
                "label": QgsApplication.translate("ConfDialog", "New menu",
                                                  None),
            },
            "layer": {
                "index":
                1,
                "label":
                QgsApplication.translate("ConfDialog", "Add layer menu", None),
            },
            "merge": {
                "index":
                2,
                "label":
                QgsApplication.translate("ConfDialog", "Merge with previous",
                                         None),
            },
        }

        # -- Configured projects (table and related buttons)
        self.tableWidget.horizontalHeader().setSectionResizeMode(
            QHeaderView.ResizeToContents)
        self.tableWidget.setRowCount(len(self.plugin.projects))
        self.buttonBox.accepted.connect(self.onAccepted)
        self.btnDelete.clicked.connect(self.onDelete)
        self.btnDelete.setText(None)
        self.btnDelete.setIcon(
            QIcon(QgsApplication.iconPath("mActionDeleteSelected.svg")))
        self.btnUp.clicked.connect(self.onMoveUp)
        self.btnUp.setText(None)
        self.btnUp.setIcon(QIcon(
            QgsApplication.iconPath("mActionArrowUp.svg")))
        self.btnDown.clicked.connect(self.onMoveDown)
        self.btnDown.setText(None)
        self.btnDown.setIcon(
            QIcon(QgsApplication.iconPath("mActionArrowDown.svg")))

        # add button
        self.btnAdd.setIcon(QIcon(QgsApplication.iconPath("mActionAdd.svg")))
        self.addMenu = QMenu(self.btnAdd)
        add_option_file = QAction(
            QIcon(QgsApplication.iconPath("mIconFile.svg")),
            self.tr("Add from file"),
            self.addMenu,
        )
        add_option_pgdb = QAction(
            QIcon(QgsApplication.iconPath("mIconPostgis.svg")),
            self.tr("Add from PostgreSQL database"),
            self.addMenu,
        )
        add_option_http = QAction(
            QIcon(str(DIR_PLUGIN_ROOT / "resources/globe.svg")),
            self.tr("Add from URL"),
            self.addMenu,
        )
        add_option_file.triggered.connect(partial(self.onAdd, "file"))
        add_option_http.triggered.connect(partial(self.onAdd, "http"))
        add_option_pgdb.triggered.connect(partial(self.onAdd, "database"))
        self.addMenu.addAction(add_option_file)
        self.addMenu.addAction(add_option_pgdb)
        self.addMenu.addAction(add_option_http)
        self.btnAdd.setMenu(self.addMenu)

        for idx, project in enumerate(self.plugin.projects):
            # edit project
            self.addEditButton(idx, guess_type_from_uri(project.get("file")))

            # project name
            itemName = QTableWidgetItem(project.get("name"))
            itemName.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
            self.tableWidget.setItem(idx, self.cols.name, itemName)
            le = QLineEdit()
            le.setText(project.get("name"))
            le.setPlaceholderText(self.tr("Use project title"))
            self.tableWidget.setCellWidget(idx, self.cols.name, le)

            # project storage type
            self.tableWidget.setCellWidget(
                idx,
                self.cols.type_storage,
                self.mk_prj_storage_icon(
                    guess_type_from_uri(project.get("file"))),
            )

            # project menu location
            location_combo = QComboBox()
            for pk in self.LOCATIONS:
                if not (pk == "merge" and idx == 0):
                    location_combo.addItem(self.LOCATIONS[pk]["label"], pk)

            try:
                location_combo.setCurrentIndex(
                    self.LOCATIONS[project["location"]]["index"])
            except Exception:
                location_combo.setCurrentIndex(0)
            self.tableWidget.setCellWidget(idx, self.cols.type_menu_location,
                                           location_combo)

            # project path (guess type stored into data)
            itemFile = QTableWidgetItem(project.get("file"))
            itemFile.setData(Qt.UserRole,
                             guess_type_from_uri(project.get("file")))
            itemFile.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
            self.tableWidget.setItem(idx, self.cols.uri, itemFile)
            le = QLineEdit()
            le.setText(project.get("file"))
            try:
                le.setStyleSheet("color: {};".format(
                    "black" if project["valid"] else "red"))
            except Exception:
                le.setStyleSheet("color: {};".format("black"))

            self.tableWidget.setCellWidget(idx, self.cols.uri, le)
            le.textChanged.connect(self.onTextChanged)

        # -- Options
        self.cbxLoadAll.setChecked(self.plugin.optionLoadAll)
        self.cbxLoadAll.setTristate(False)

        self.cbxCreateGroup.setCheckState(self.plugin.optionCreateGroup)
        self.cbxCreateGroup.setTristate(False)

        self.cbxShowTooltip.setCheckState(self.plugin.optionTooltip)
        self.cbxShowTooltip.setTristate(False)

        self.tableTunning()
Example #5
0
 def tr(self, string, context=''):
     if context == '':
         context = 'PDALtoolsAlgorithmProvider'
     return QgsApplication.translate(context, string)