Ejemplo n.º 1
0
    def __init__(self, layer, geometry, parent=None):
        GeomEditor.__init__(self, layer, geometry)
        QTextEdit.__init__(self, parent)

        self.setGeom(geometry)
        self.textChanged.connect(self.geomChanged)
        self.layerEditable()
Ejemplo n.º 2
0
    def __init__(self, layer, geometry, parent=None):
        GeomEditor.__init__(self, layer, geometry)
        QTextEdit.__init__(self, parent)

        self.setGeom(geometry)
        self.cursorPositionChanged.connect(self.emitCurrentPoint)
        self.textChanged.connect(self.geomChanged)
        self.layerEditable()
 def __init__(self, commit):
     QTextEdit.__init__(self)
     self.setWordWrap(False)
     self.commit = commit
     size = self.font().pointSize()
     text = ('<b><font style="font-size:%spt">%s</font></b>'
         '<br><font color="#5f6b77" style="font-size:%spt"><b>%s</b> by <b>%s</b></font> '
         '<font color="#5f6b77" style="font-size:%spt; background-color:rgb(225,225,225)"> %s </font>' %
         (str(size), self.commit.message.splitlines()[0], str(size - 1),
          self.commit.authorprettydate(), self.commit.authorname, str(size - 1), self.commit.id[:10]))
     self.setText(text)
Ejemplo n.º 4
0
    def testStringWithMaxLen(self):
        """ tests that text edit wrappers correctly handle string fields with a maximum length """
        layer = QgsVectorLayer("none?field=fldint:integer", "layer", "memory")
        assert layer.isValid()
        layer.dataProvider().addAttributes([QgsField('max', QVariant.String, 'string', 10),
                                            QgsField('nomax', QVariant.String, 'string', 0)])
        layer.updateFields()
        QgsProject.instance().addMapLayer(layer)

        reg = QgsGui.editorWidgetRegistry()
        config = {'IsMultiline': 'True'}

        # first test for field without character limit
        editor = QTextEdit()
        editor.setPlainText('this_is_a_long_string')
        w = reg.create('TextEdit', layer, 2, config, editor, None)
        self.assertEqual(w.value(), 'this_is_a_long_string')

        # next test for field with character limit
        editor = QTextEdit()
        editor.setPlainText('this_is_a_long_string')
        w = reg.create('TextEdit', layer, 1, config, editor, None)

        self.assertEqual(w.value(), 'this_is_a_')

        QgsProject.instance().removeAllMapLayers()
Ejemplo n.º 5
0
 def __init__(self, item_widget):
     super().__init__(item_widget)
     self._item_widget = item_widget
     self._value_text_edit = QTextEdit()
     self._value_text_edit.setLineWrapMode(QTextEdit.WidgetWidth)
     self._value_text_edit.textChanged.connect(self._on_code_value_changed)
     self._exp_btn = QPushButton(self.tr('Insert expression...'))
     self._exp_btn.setIcon(
         QgsApplication.getThemeIcon('/mIconExpression.svg'))
     self._exp_btn.clicked.connect(self._on_insert_expression)
     layout = QVBoxLayout()
     layout.addWidget(self._value_text_edit)
     layout.addWidget(self._exp_btn)
     self.setLayout(layout)
 def __init__(self):
     QDialog.__init__(self)
     self.bar_ = QgsMessageBar()
     self.bar_.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
     self.setLayout(QGridLayout())
     self.layout().setContentsMargins(0, 0, 0, 0)
     self.buttonbox = QDialogButtonBox(QDialogButtonBox.Ok)
     self.buttonbox.accepted.connect(self.run)
     self.text_edit = QTextEdit()
     self.text_edit.setHtml(
         "<font color='grey' size='6'><green>PyQGIS PyQt!\nHello</font>")
     self.layout().addWidget(self.text_edit, 1, 0, -1, 1)
     self.layout().addWidget(self.buttonbox, 0, 0, 0, 1)
     self.layout().addWidget(self.bar_, 0, 0, 1, 1)
Ejemplo n.º 7
0
 def __init__(self, s):
     QDialog.__init__(self)
     self.setModal(True)
     self.resize(600, 400)
     self.setWindowTitle(self.tr('Unit Test'))
     layout = QVBoxLayout()
     self.text = QTextEdit()
     self.text.setFontFamily("monospace")
     self.text.setEnabled(True)
     # Add two spaces in front of each text for faster copy/paste
     self.text.setText('  {}'.format(s.replace('\n', '\n  ')))
     layout.addWidget(self.text)
     self.setLayout(layout)
     QMetaObject.connectSlotsByName(self)
Ejemplo n.º 8
0
    def get_widget_with_party_info_formatted(self, party_info):
        widget = QTextEdit()

        if party_info:
            html = list()
            html.append("<b>{}</b>".format(
                party_info[DICT_KEY_PARTY_T_NAME_F]))
            html.append("<i>{}</i>: <b>{}</b>".format(
                party_info[DICT_KEY_PARTY_T_DOCUMENT_TYPE_F],
                party_info[DICT_KEY_PARTY_T_DOCUMENT_ID_F]))
            html.append("<i>Derecho</i>: <b>{}</b>".format(
                party_info[DICT_KEY_PARTY_T_RIGHT]))
            widget.setHtml("<br>".join(html))

        return widget
Ejemplo n.º 9
0
 def _highlight(line_no, color):
     block = self.document().findBlockByLineNumber(line_no)
     highlight = QTextEdit.ExtraSelection()
     highlight.cursor = QTextCursor(block)
     highlight.format.setProperty(QTextFormat.FullWidthSelection, True)
     highlight.format.setBackground(color)
     return highlight
Ejemplo n.º 10
0
 def setupUi(self):
     self.valueItems = {}
     self.dependentItems = {}
     self.resize(650, 450)
     self.buttonBox = QDialogButtonBox()
     self.buttonBox.setOrientation(Qt.Horizontal)
     self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel
                                       | QDialogButtonBox.Ok)
     self.infoText = QTextEdit()
     numbers = self.getAvailableValuesOfType(ParameterNumber, OutputNumber)
     text = self.tr(
         'You can refer to model values in your formula, using '
         'single-letter variables, as follows:\n',
         'CalculatorModelerParametersDialog')
     ichar = 97
     if numbers:
         for number in numbers:
             text += chr(ichar) + '->' + self.resolveValueDescription(
                 number) + '\n'
             ichar += 1
     else:
         text += self.tr('\n - No numerical variables are available.',
                         'CalculatorModelerParametersDialog')
     self.infoText.setText(text)
     self.infoText.setEnabled(False)
     self.formulaText = QLineEdit()
     if hasattr(self.formulaText, 'setPlaceholderText'):
         self.formulaText.setPlaceholderText(
             self.tr('[Enter your formula here]',
                     'CalculatorModelerParametersDialog'))
     if self._algName is not None:
         alg = self.model.algs[self._algName]
         self.formulaText.setText(alg.params[FORMULA])
     self.setWindowTitle(
         self.tr('Calculator', 'CalculatorModelerParametersDialog'))
     self.verticalLayout = QVBoxLayout()
     self.verticalLayout.setSpacing(2)
     self.verticalLayout.setMargin(0)
     self.verticalLayout.addWidget(self.infoText)
     self.verticalLayout.addWidget(self.formulaText)
     self.verticalLayout.addWidget(self.buttonBox)
     self.setLayout(self.verticalLayout)
     self.buttonBox.accepted.connect(self.okPressed)
     self.buttonBox.rejected.connect(self.cancelPressed)
     QMetaObject.connectSlotsByName(self)
Ejemplo n.º 11
0
    def attrPrinter(self, fieldsList: object, feature: QgsFeature,
                    place: QTextEdit):
        """print the attribute table on preview panel

        :param fieldsList object: List the attribute value of feature
        :param feature QgsFeature: The feature will be printed
        :param place QTextEdit: The place for editing text
        
        """
        temp = ''

        for f in fieldsList:
            temp += f.name()
            temp += ' : '
            temp += str(feature.attribute(f.name()))
            temp += '\n'
        # print(place)
        place.setText(temp)
Ejemplo n.º 12
0
    def __init__(self, text):
        super(TextEditorDialog, self).__init__()

        self.text = text

        self.resize(600, 350)
        self.setWindowFlags(self.windowFlags() | Qt.WindowSystemMenuHint |
                                                 Qt.WindowMinMaxButtonsHint)
        self.setWindowTitle("Editor")

        layout = QVBoxLayout()
        buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        self.editor = QTextEdit()
        self.editor.setPlainText(text)
        layout.addWidget(self.editor)
        layout.addWidget(buttonBox)
        self.setLayout(layout)

        buttonBox.accepted.connect(self.okPressed)
        buttonBox.rejected.connect(self.cancelPressed)
 def highlightCurrentLine(self):
     extraSelections = []
     if not self.isReadOnly():
         selection = QTextEdit.ExtraSelection()
         lineColor = QColor(Qt.blue).lighter(190)
         selection.format.setBackground(lineColor)
         selection.format.setProperty(QTextFormat.FullWidthSelection, True)
         selection.cursor = self.textCursor()
         selection.cursor.clearSelection()
         extraSelections.append(selection)
     self.setExtraSelections(extraSelections)
class MyDialog(QDialog):
    """agrdgadgas  """
    def __init__(self):
        QDialog.__init__(self)
        self.bar_ = QgsMessageBar()
        self.bar_.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.setLayout(QGridLayout())
        self.layout().setContentsMargins(0, 0, 0, 0)
        self.buttonbox = QDialogButtonBox(QDialogButtonBox.Ok)
        self.buttonbox.accepted.connect(self.run)
        self.text_edit = QTextEdit()
        self.text_edit.setHtml(
            "<font color='grey' size='6'><green>PyQGIS PyQt!\nHello</font>")
        self.layout().addWidget(self.text_edit, 1, 0, -1, 1)
        self.layout().addWidget(self.buttonbox, 0, 0, 0, 1)
        self.layout().addWidget(self.bar_, 0, 0, 1, 1)

    def run(self):
        """ASFDSG"""
        self.bar_.pushMessage("Hello", "World", level=Qgis.Info)
Ejemplo n.º 15
0
    def testStringWithMaxLen(self):
        """ tests that text edit wrappers correctly handle string fields with a maximum length """
        layer = QgsVectorLayer("none?field=fldint:integer", "layer", "memory")
        assert layer.isValid()
        layer.dataProvider().addAttributes([QgsField('max', QVariant.String, 'string', 10),
                                            QgsField('nomax', QVariant.String, 'string', 0)])
        layer.updateFields()
        QgsMapLayerRegistry.instance().addMapLayer(layer)

        reg = QgsEditorWidgetRegistry.instance()
        config = {'IsMultiline': 'True'}

        # first test for field without character limit
        editor = QTextEdit()
        editor.setPlainText('this_is_a_long_string')
        w = reg.create('TextEdit', layer, 2, config, editor, None)
        self.assertEqual(w.value(), 'this_is_a_long_string')

        # next test for field with character limit
        editor = QTextEdit()
        editor.setPlainText('this_is_a_long_string')
        w = reg.create('TextEdit', layer, 1, config, editor, None)

        self.assertEqual(w.value(), 'this_is_a_')

        QgsMapLayerRegistry.instance().removeAllMapLayers()
Ejemplo n.º 16
0
def save_pdf_format(qgis_utils, settings_path, title, text):
    settings = QSettings()
    new_filename, filter = QFileDialog.getSaveFileName(
        None,
        QCoreApplication.translate('Asistente-LADM_COL', 'Export to PDF'),
        settings.value(settings_path, '.'),
        filter="PDF (*.pdf)")

    if new_filename:
        settings.setValue(settings_path, os.path.dirname(new_filename))
        new_filename = new_filename if new_filename.lower().endswith(
            ".pdf") else "{}.pdf".format(new_filename)

        txt_log = QTextEdit()
        txt_log.setHtml("{}<br>{}".format(title, text))

        printer = QPrinter()
        printer.setPageSize(QPrinter.Letter)
        printer.setOutputFormat(QPrinter.PdfFormat)
        printer.setOutputFileName(new_filename)
        txt_log.print(printer)

        msg = QCoreApplication.translate(
            "Asistente-LADM_COL",
            "Report successfully generated in folder <a href='file:///{normalized_path}'>{path}</a>!"
        ).format(normalized_path=normalize_local_url(new_filename),
                 path=new_filename)
        qgis_utils.message_with_duration_emitted.emit(msg, Qgis.Success, 0)
Ejemplo n.º 17
0
 def __init__(self, s):
     QDialog.__init__(self)
     self.setModal(True)
     self.resize(600, 400)
     self.setWindowTitle(self.tr('Unit test'))
     layout = QVBoxLayout()
     self.text = QTextEdit()
     self.text.setFontFamily("monospace")
     self.text.setEnabled(True)
     self.text.setText(s)
     layout.addWidget(self.text)
     self.setLayout(layout)
     QMetaObject.connectSlotsByName(self)
Ejemplo n.º 18
0
    def __init__(self,
                 plugIn,
                 parent,
                 size,
                 transparency,
                 windowFlags=Qt.Widget):
        self.plugIn = plugIn
        self.size = size
        self.transparency = transparency
        QWidget.__init__(self, parent, windowFlags)

        self.edit1 = QTextEdit(self)
        self.edit1.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.edit1.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.edit1.insertPlainText("12.3456")
        self.edit1.setReadOnly(True)

        self.edit2 = QTextEdit(self)
        self.edit2.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.edit2.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.edit2.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.edit2.insertPlainText("78.9012")
        self.edit2.setReadOnly(True)
Ejemplo n.º 19
0
 def __init__(self, s):
     QDialog.__init__(self)
     self.setModal(True)
     self.resize(600, 400)
     self.setWindowTitle(self.tr('Unit test'))
     layout = QVBoxLayout()
     self.text = QTextEdit()
     self.text.setFontFamily("monospace")
     self.text.setEnabled(True)
     # Add two spaces in front of each text for faster copy/paste
     self.text.setText('  {}'.format(s.replace('\n', '\n  ')))
     layout.addWidget(self.text)
     self.setLayout(layout)
     QMetaObject.connectSlotsByName(self)
def export_title_text_to_pdf(filepath, title, text):
    filepath = filepath if filepath.lower().endswith(
        ".pdf") else "{}.pdf".format(filepath)

    txt_log = QTextEdit()
    txt_log.setHtml("{}<br>{}".format(title, text))

    printer = QPrinter()
    printer.setPageSize(QPrinter.Letter)
    printer.setOutputFormat(QPrinter.PdfFormat)
    printer.setOutputFileName(filepath)
    txt_log.print(printer)
Ejemplo n.º 21
0
class ShowTestDialog(QDialog):
    def __init__(self, s):
        QDialog.__init__(self)
        self.setModal(True)
        self.resize(600, 400)
        self.setWindowTitle(self.tr('Unit test'))
        layout = QVBoxLayout()
        self.text = QTextEdit()
        self.text.setFontFamily("monospace")
        self.text.setEnabled(True)
        self.text.setText(s)
        layout.addWidget(self.text)
        self.setLayout(layout)
        QMetaObject.connectSlotsByName(self)
Ejemplo n.º 22
0
 def setupUi(self):
     self.valueItems = {}
     self.dependentItems = {}
     self.resize(650, 450)
     self.buttonBox = QDialogButtonBox()
     self.buttonBox.setOrientation(Qt.Horizontal)
     self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel
                                       | QDialogButtonBox.Ok)
     self.infoText = QTextEdit()
     numbers = self.getAvailableValuesOfType(ParameterNumber, OutputNumber)
     text = self.tr('You can refer to model values in your formula, using '
                    'single-letter variables, as follows:\n', 'CalculatorModelerParametersDialog')
     ichar = 97
     if numbers:
         for number in numbers:
             text += chr(ichar) + '->' + self.resolveValueDescription(number) + '\n'
             ichar += 1
     else:
         text += self.tr('\n - No numerical variables are available.', 'CalculatorModelerParametersDialog')
     self.infoText.setText(text)
     self.infoText.setEnabled(False)
     self.formulaText = QLineEdit()
     if hasattr(self.formulaText, 'setPlaceholderText'):
         self.formulaText.setPlaceholderText(self.tr('[Enter your formula here]', 'CalculatorModelerParametersDialog'))
     if self._algName is not None:
         alg = self.model.algs[self._algName]
         self.formulaText.setText(alg.params[FORMULA])
     self.setWindowTitle(self.tr('Calculator', 'CalculatorModelerParametersDialog'))
     self.verticalLayout = QVBoxLayout()
     self.verticalLayout.setSpacing(2)
     self.verticalLayout.setMargin(0)
     self.verticalLayout.addWidget(self.infoText)
     self.verticalLayout.addWidget(self.formulaText)
     self.verticalLayout.addWidget(self.buttonBox)
     self.setLayout(self.verticalLayout)
     self.buttonBox.accepted.connect(self.okPressed)
     self.buttonBox.rejected.connect(self.cancelPressed)
     QMetaObject.connectSlotsByName(self)
Ejemplo n.º 23
0
class AboutBox(QDialog):
    """
    About box of the plugin
    """
    def __init__(self, parent=None):

        QWidget.__init__(self, parent)

        mainLayout = QVBoxLayout()

        logo_file_path = PluginGlobals.instance().logo_file_path
        self.logo = QLabel()
        self.logo.setPixmap(QPixmap(logo_file_path))
        mainLayout.addWidget(self.logo)

        title = u"À propos de l'extension GéoGrandEst…"
        description = u"""Extension pour QGIS donnant un accès simplifié aux ressources géographiques utiles aux 
partenaires de GéoGrandEst
Version {0}
Plus d'informations à l'adresse suivante :
{1}
        """.format(PluginGlobals.instance().PLUGIN_VERSION,
                   PluginGlobals.instance().PLUGIN_SOURCE_REPOSITORY)

        self.textArea = QTextEdit()
        self.textArea.setReadOnly(True)
        self.textArea.setText(description)
        self.textArea.setFrameShape(QFrame.NoFrame)
        mainLayout.addWidget(self.textArea)

        self.setModal(True)
        self.setSizeGripEnabled(False)

        self.setLayout(mainLayout)

        self.setFixedSize(400, 250)
        self.setWindowTitle(title)
Ejemplo n.º 24
0
class CalculatorModelerParametersDialog(ModelerParametersDialog):

    def setupUi(self):
        self.valueItems = {}
        self.dependentItems = {}
        self.resize(650, 450)
        self.buttonBox = QDialogButtonBox()
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel
                                          | QDialogButtonBox.Ok)
        self.infoText = QTextEdit()
        numbers = self.getAvailableValuesOfType(ParameterNumber, OutputNumber)
        text = self.tr('You can refer to model values in your formula, using '
                       'single-letter variables, as follows:\n', 'CalculatorModelerParametersDialog')
        ichar = 97
        if numbers:
            for number in numbers:
                text += chr(ichar) + '->' + self.resolveValueDescription(number) + '\n'
                ichar += 1
        else:
            text += self.tr('\n - No numerical variables are available.', 'CalculatorModelerParametersDialog')
        self.infoText.setText(text)
        self.infoText.setEnabled(False)
        self.formulaText = QLineEdit()
        if hasattr(self.formulaText, 'setPlaceholderText'):
            self.formulaText.setPlaceholderText(self.tr('[Enter your formula here]', 'CalculatorModelerParametersDialog'))
        if self._algName is not None:
            alg = self.model.algs[self._algName]
            self.formulaText.setText(alg.params[FORMULA])
        self.setWindowTitle(self.tr('Calculator', 'CalculatorModelerParametersDialog'))
        self.verticalLayout = QVBoxLayout()
        self.verticalLayout.setSpacing(2)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.addWidget(self.infoText)
        self.verticalLayout.addWidget(self.formulaText)
        self.verticalLayout.addWidget(self.buttonBox)
        self.setLayout(self.verticalLayout)
        self.buttonBox.accepted.connect(self.okPressed)
        self.buttonBox.rejected.connect(self.cancelPressed)
        QMetaObject.connectSlotsByName(self)

    def createAlgorithm(self):
        alg = Algorithm('modelertools:calculator')
        alg.setName(self.model)
        alg.description = self.tr('Calculator', 'CalculatorModelerParametersDialog')

        formula = self.formulaText.text()
        alg.params[FORMULA] = formula

        for i in range(AVAILABLE_VARIABLES):
            paramname = NUMBER + str(i)
            alg.params[paramname] = None

        numbers = self.getAvailableValuesOfType(ParameterNumber, OutputNumber)
        used = []
        for i in range(len(numbers)):
            if str(chr(i + 97)) in formula:
                used.append(numbers[i])

        for i, variable in enumerate(used):
            paramname = NUMBER + str(i)
            alg.params[paramname] = variable

        # TODO check formula is correct
        return alg
Ejemplo n.º 25
0
class MainPSWindow(QMainWindow):
    click_ref = pyqtSignal(QgsPoint, Qt.MouseButton)
    close = pyqtSignal()

    def __init__(self, iface, parent=None):
        QMainWindow.__init__(self, parent=parent)

        # build ui
        self.ui = Ui_Form()  #Appelle la fenêtre
        self.ui.setupUi(self)  #Construit l'interface
        self.setCentralWidget(self.ui.PS_Time_Viewer
                              )  #Définit la fenêtre principal de l'interface
        self.first_point = True

        # Set up the user interface from Designer.
        self.iface = iface
        self.canvas = iface.mapCanvas()  #Lie QGIS et la fenêtre

        # connect signals
        self.make_connection()  #Relie les boutons aux actions

    def set_ps_layer(self, ps_layer):
        self.ps_layer = ps_layer

    def setupUi2(self, Form):
        """Ajoutons ce qu'il manque à la fenêtre"""
        self.ui.setupUi(self, Form)
        Form.closeEvent = self.close_Event

    def closeEvent(self, event):
        result = QMessageBox.question(self, "Confirm Exit...",
                                      "Are you sure you want to exit ?",
                                      QMessageBox.Yes | QMessageBox.No)
        event.ignore()

        if result == QMessageBox.Yes:
            self.close.emit()
            event.accept()

    def addDlg(self, dlg):
        self.dlg = dlg
        self.ui.graph_loc.addWidget(self.dlg.toolbar, 0,
                                    Qt.AlignTop)  #gridLayout_21
        self.ui.graph_loc.addWidget(self.dlg.plot, 40,
                                    Qt.AlignTop)  #verticalLayout_2
        self.ui.graph_loc.addWidget(self.dlg.nav, 2,
                                    Qt.AlignTop)  #verticalLayout_2

    def get_diff(self, list_item):
        x, y = [], []  # lists containg x,y values
        idx_selected = []

        for i in range(len(list_item)):
            if list_item[i].isSelected() == True:
                idx_selected.append(i)

        if len(idx_selected) == 2:
            for elem in range(2):
                #fid.append(int(str(elem.text()).split()[-1]))
                idx = idx_selected[elem]
                x.append(self.dlg.plot.collections[idx].x)
                y.append(self.dlg.plot.collections[idx].y)
        else:
            QMessageBox.information(self.iface.mainWindow(),
                                    "PS Time Series Viewer",
                                    "Sélectionner 2 points", QMessageBox.Ok)

        return x, y

    def plot_diff(self):
        list_item = []
        for i in range(self.ui.list_series.count()):
            list_item.append(self.ui.list_series.item(i))
        selected = self.get_diff(list_item)

        #Transformer les datetime
        yeari = []
        monthi = []
        dayi = []
        for j in range(len(selected[0])):
            for i in range(len(selected[0][0])):
                yeari.append(selected[0][j][i].year)
                monthi.append(selected[0][j][i].month)
                dayi.append(selected[0][j][i].day)

        if yeari[0:int(len(yeari) / 2)] == yeari[int(len(yeari) / 2):]:
            xdiff = selected[0][0]
            ydiff = []
            for i in range(len(selected[1][0])):
                ydiff.append(
                    float(selected[1][0][i]) - float(selected[1][1][i]))
        else:
            QMessageBox.warning(self.iface.mainWindow(),
                                "PS Time Series Viewer",
                                "No match in time." % self.ts_tablename)

        self.nb_series = 0
        layer = self.iface.activeLayer()
        infoFields = {}
        ps_fields = layer.dataProvider().fields()
        for idx, fld in enumerate(ps_fields):
            infoFields[idx] = fld

        if self.nb_series == 0 or self.first_point == True:
            self.dlg = PSTimeSeries_Dlg(layer, infoFields)
            self.dlg.plot.setData(xdiff, ydiff)
            self.dlg.addPlotPS(xdiff, ydiff)
            self.dlg.plot._updateLists()
            self.addDlg(self.dlg)
            self.nb_series += 1
            self.first_point = False

        else:
            self.window.dlg.addLayer(layer, infoFields)
            self.window.dlg.plot.setData(xdiff, ydiff)
            self.window.dlg.addPlotPS(xdiff, ydiff)
            self.window.dlg.plot._updateLists()
            self.window.dlg.refresh()
            self.nb_series += 1

    ##### Buttons#####################################################################################################

    def search_time_series(self):
        self.dlg_files = QFileDialog()
        directory = QFileDialog.getOpenFileName(None, "Select a directory", "",
                                                "Shapefile (*.shp)")  #,
        QgsMessageLog.logMessage(str(directory))
        if not (directory == ""):
            self.ui.time_series.setText(directory[0])
            print("ok")
        else:
            print("")

    def search_metadata(self):
        self.dlg_files = QFileDialog()
        directory = QFileDialog.getOpenFileName(None, "Select a directory", "",
                                                "CSV (*.csv)")  #,
        QgsMessageLog.logMessage(str(directory))
        if not (directory == ""):
            self.ui.metadata.setText(directory[0])
            print("ok")
        else:
            print("")

    def search_gnss(self):
        self.dlg_files = QFileDialog()
        directory = QFileDialog.getOpenFileName(None, "Select a directory", "",
                                                "CSV (*.csv)")  #,
        QgsMessageLog.logMessage(str(directory))
        if not (directory == ""):
            self.ui.gnss_2.setText(directory[0])
            print("ok")
        else:
            print("")

    def search_ref(self):
        self.dlg_files = QFileDialog()
        directory = QFileDialog.getOpenFileName(None, "Select a directory", "",
                                                "Shapefile (*.shp)")  #,
        QgsMessageLog.logMessage(str(directory))
        if not (directory == ""):
            self.ui.ref_2.setText(directory[0])
            print("ok")
        else:
            print("")

    def load_time_series(self):
        self.ui.layers_for_options.addItem(self.ui.time_series.toPlainText())
        self.ui.list_series.addItem(self.ui.time_series.toPlainText())
        self.ui.list_time_series_with_new_ref.addItem(
            self.ui.time_series.toPlainText())

    def load_gnss(self):
        self.ui.gnss_selection_list.addItem(self.ui.gnss_2.toPlainText())

    def load_ref(self):
        self.ui.ref_list.addItem(self.ui.ref_2.toPlainText())
        layer = self.iface.addVectorLayer(self.ui.ref_2.toPlainText(), "ref",
                                          "ogr")
        if not layer:
            print("Layer failed to load!")
        click_ref = pyqtSignal(QgsPoint, Qt.MouseButton)

    def remove_ts(self):
        toRemove = self.ui.list_series.selectedItems()
        if toRemove != []:
            #QgsMessageLog.logMessage("Items selectionnés mais pas supprimés")
            for elem in toRemove:
                idx = self.ui.list_series.row(elem)
                self.ui.layers_for_options.takeItem(idx)
                self.ui.list_series.takeItem(idx)
                self.ui.list_time_series_with_new_ref.takeItem(idx)
                self.dlg.plot._removeCollection(self.dlg.plot.collections[idx])
                self.dlg.refresh()
                QgsMessageLog.logMessage(str(idx) + "   " + str(len(toRemove)))

    def new_ref(self):
        self.dlg_ref = QFileDialog()
        directory = QFileDialog.getExistingDirectory(None, 'Select a folder:',
                                                     '',
                                                     QFileDialog.ShowDirsOnly)

        QgsMessageLog.logMessage(str(directory))
        if not (directory == ""):
            self.ui.create_new_ref.setText(directory)
            print("ok")
        else:
            print("")
        # layer = iface.addVectorLayer(self.ui.create_new_ref.toPlainText(), "ref", "ogr")
        # if not layer:
        # 	print("Layer failed to load!")

    #run method that performs all the real work
    def create_new_ref(self):
        self.point = None
        self.w = QWidget()
        self.w.resize(250, 150)
        self.w.move(300, 300)
        self.w.setWindowTitle('New Reference Area')
        self.label = QLabel(self.tr(u'Set a radius value'))
        self.TextEdit = QTextEdit()
        self.label2 = QLabel(
            self.tr(u'Click on the QGIS interface to set area s center'))
        self.TextEdit2 = QTextEdit()
        self.btn = QPushButton('Ok', self.w)
        self.vbox = QVBoxLayout(self.w)
        self.vbox.addWidget(self.label)
        self.vbox.addWidget(self.TextEdit)
        self.vbox.addWidget(self.label2)
        self.vbox.addWidget(self.TextEdit2)
        self.vbox.addWidget(self.btn)
        self.w.setLayout(self.vbox)
        self.w.show()
        self.canvas = self.iface.mapCanvas()

        # out click tool will emit a QgsPoint on every click
        self.clickTool = QgsMapToolEmitPoint(self.canvas)

        # create our GUI dialog
        self.clickTool.canvasClicked.connect(self.handleMouseDown)
        self.btn.clicked.connect(self.draw_ref)
        self.canvas.setMapTool(self.clickTool)

    def handleMouseDown(self, point, button):
        self.point = point
        self.TextEdit2.setText(str(point.x()) + " , " + str(point.y()))

    def draw_ref(self):
        QMessageBox.information(self.iface.mainWindow(),
                                "PS Time Series Viewer", "Ok")
        try:
            self.radius = float(self.TextEdit.toPlainText())
        except:
            QMessageBox.information(self.iface.mainWindow(),
                                    "PS Time Series Viewer",
                                    "Please set a float")

        if self.point:
            pathText = self.ui.create_new_ref.toPlainText()
            if pathText == "":
                pathText = "D:"
            path = pathText + "/reference_area.shp"
            uri = path + "|referenceArea"
            vpoly = QgsVectorLayer(uri, 'referenceArea', "ogr")  #
            feature = QgsFeature()
            feature.setGeometry(
                QgsGeometry.fromPointXY(self.point).buffer(self.radius, 100))
            provider = vpoly.dataProvider()
            vpoly.startEditing()
            provider.addFeatures([feature])
            vpoly.commitChanges()
        else:
            QMessageBox.information(self.iface.mainWindow(),
                                    "PS Time Series Viewer", "No point ")

        #QMessageBox.information( self.iface.mainWindow(),"Info", "X,Y = %s,%s" % (str(point.x()),str(point.y())) )

    # def plot_legend(self):
    # 	path=self.ui.layers_for_options.selectedItems()
    # 	myVectorLayer = QgsVectorLayer(path+"|layer", "layer", 'ogr')
    # 	myTargetField = 'target_field'
    # 	myRangeList = []
    # 	myOpacity = 1
    # 	# Make our first symbol and range...
    # 	myMin = 0.0
    # 	myMax = 50.0
    # 	myLabel = 'Group 1'
    # 	myColour = QtGui.QColor('#ffee00')
    # 	mySymbol1 = QgsSymbol.defaultSymbol(myVectorLayer.geometryType())
    # 	mySymbol1.setColor(myColour)
    # 	mySymbol1.setOpacity(myOpacity)
    # 	myRange1 = QgsRendererRange(myMin, myMax, mySymbol1, myLabel)
    # 	myRangeList.append(myRange1)
    # 	#now make another symbol and range...
    # 	myMin = 50.1
    # 	myMax = 100
    # 	myLabel = 'Group 2'
    # 	myColour = QtGui.QColor('#00eeff')
    # 	mySymbol2 = QgsSymbol.defaultSymbol(
    # 		myVectorLayer.geometryType())
    # 	mySymbol2.setColor(myColour)
    # 	mySymbol2.setOpacity(myOpacity)
    # 	myRange2 = QgsRendererRange(myMin, myMax, mySymbol2, myLabel)
    # 	myRangeList.append(myRange2)
    # 	myRenderer = QgsGraduatedSymbolRenderer('', myRangeList)
    # 	myRenderer.setMode(QgsGraduatedSymbolRenderer.EqualInterval)
    # 	myRenderer.setClassAttribute(myTargetField)
    #
    # 	myVectorLayer.setRenderer(myRenderer)
    # 	QgsProject.instance().addMapLayer(myVectorLayer)
    # 	for legendLyr in self.iface.mapCanvas().layers():
    # 		if legendLyr.name() != "os1250_line" and legendLyr.name() != "os1250_text":
    # 			renderer = legendLyr.rendererV2()
    # 			if renderer.type() == "categorizedSymbol":
    # 				myRenderer = renderer.clone()
    # 				idx=0
    # 				for cat in myRenderer.categories():
    # 					myRenderer.updateCategoryLabel (idx,"foo")
    # 					idx+=1
    # 				legendLyr.setRendererV2(myRenderer)
    # 				legendLyr.triggerRepaint()


#######################################################################################################################

    def make_connection(self):
        """
		Create connection for window item
		"""

        #searching file for loading
        self.ui.time_series_search.clicked.connect(
            self.search_time_series)  #(self.ui.time_series)
        self.ui.gnss_search.clicked.connect(self.search_gnss)
        self.ui.ref_search.clicked.connect(self.search_ref)

        #pushing files
        self.ui.time_series_push.clicked.connect(self.load_time_series)
        self.ui.gnss_push.clicked.connect(self.load_gnss)
        self.ui.ref_push.clicked.connect(self.load_ref)
        self.ui.remove_push.clicked.connect(self.remove_ts)
        self.ui.plot_difference.clicked.connect(self.plot_diff)
        self.ui.new_ref.clicked.connect(self.new_ref)
        self.ui.create_new_ref_push.clicked.connect(self.create_new_ref)
Ejemplo n.º 26
0
class QadPreview(QWidget):
    def __init__(self,
                 plugIn,
                 parent,
                 size,
                 transparency,
                 windowFlags=Qt.Widget):
        self.plugIn = plugIn
        self.size = size
        self.transparency = transparency
        QWidget.__init__(self, parent, windowFlags)

        self.edit1 = QTextEdit(self)
        self.edit1.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.edit1.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.edit1.insertPlainText("12.3456")
        self.edit1.setReadOnly(True)

        self.edit2 = QTextEdit(self)
        self.edit2.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.edit2.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.edit2.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.edit2.insertPlainText("78.9012")
        self.edit2.setReadOnly(True)

    def refresh(self, size, transparency):
        self.size = size
        self.transparency = transparency
        self.update()  # forzo il disegno del preview

    def paintEvent(self, event):
        self.paint_preview()


    def setEdit(self, editWidget, foregroundColor, backGroundColor, borderColor, \
                selectionColor, selectionBackGroundColor, opacity):
        # se i colori sono None allora non vengono alterati
        # caso particolare per borderColor = "" non viene disegnato
        # opacity = 0-100
        oldFmt = self.styleSheet().split(";")
        fmt = "rgba({0},{1},{2},{3}%)"

        c = QColor(foregroundColor)
        rgbStrForeColor = "color: " + fmt.format(str(c.red()), str(
            c.green()), str(c.blue()), str(opacity)) + ";"

        c = QColor(backGroundColor)
        rgbStrBackColor = "background-color: " + fmt.format(
            str(c.red()), str(c.green()), str(c.blue()), str(opacity)) + ";"

        c = QColor(borderColor)
        rgbStrBorderColor = "border-color: " + fmt.format(
            str(c.red()), str(c.green()), str(c.blue()), str(opacity)) + ";"
        fmtBorder = "border:1px;border-style:solid;"

        c = QColor(selectionColor)
        rgbStrSelectionColor = "selection-color: " + fmt.format(
            str(c.red()), str(c.green()), str(c.blue()), str(opacity)) + ";"

        c = QColor(selectionBackGroundColor)
        rgbStrSelectionBackColor = "selection-background-color: " + fmt.format(
            str(c.red()), str(c.green()), str(c.blue()), str(opacity)) + ";"

        fontSize = 8 + self.size

        fmt = rgbStrForeColor + \
              rgbStrBackColor + \
              fmtBorder + \
              rgbStrBorderColor + \
              rgbStrSelectionColor + \
              rgbStrSelectionBackColor + \
              "font-size: " + str(fontSize) + "pt;"

        editWidget.setStyleSheet(fmt)

    def paint_preview(self):
        rect = self.rect()
        painter = QPainter(self)
        painter.fillRect(rect, self.plugIn.canvas.canvasColor())
        painter.setRenderHint(QPainter.Antialiasing)

        foregroundColor = QColor(
            QadVariables.get(
                QadMsg.translate("Environment variables", "DYNEDITFORECOLOR")))
        backGroundColor = QColor(
            QadVariables.get(
                QadMsg.translate("Environment variables", "DYNEDITBACKCOLOR")))
        borderColor = QColor(
            QadVariables.get(
                QadMsg.translate("Environment variables",
                                 "DYNEDITBORDERCOLOR")))
        opacity = 100 - self.transparency
        font_size = 8 + self.size
        height = font_size + 15

        selectionColor = QColor(Qt.white)
        selectionBackGroundColor = QColor(51, 153,
                                          255)  # azzurro (R=51 G=153 B=255)
        self.setEdit(self.edit1, foregroundColor, backGroundColor, borderColor,
                     selectionColor, selectionBackGroundColor, opacity)
        fm = QFontMetrics(self.edit1.currentFont())
        width1 = fm.width(self.edit1.toPlainText() + "__") + 2

        self.edit1.resize(width1, height)
        self.edit1.selectAll()  # seleziono tutto il testo

        self.setEdit(self.edit2, foregroundColor, backGroundColor, borderColor,
                     backGroundColor, foregroundColor, opacity)
        fm = QFontMetrics(self.edit2.currentFont())
        width2 = fm.width(self.edit2.toPlainText() + "__") + 2
        self.edit2.resize(width2, height)

        offset = height / 3
        x = (rect.width() - (width1 + offset + width2)) / 2
        y = (rect.height() - height) / 2
        self.edit1.move(x, y)
        self.edit2.move(x + width1 + offset, y)
Ejemplo n.º 27
0
 def __init__(self, layer, geometry, parent=None):
     GeomEditor.__init__(self, layer, geometry)
     QTextEdit.__init__(self, parent)
Ejemplo n.º 28
0
class ModelerParameterDefinitionDialog(QDialog):

    @staticmethod
    def use_legacy_dialog(param=None, paramType=None):
        if paramType in (parameters.PARAMETER_TABLE_FIELD,
                         parameters.PARAMETER_BAND,
                         parameters.PARAMETER_VECTOR,
                         parameters.PARAMETER_TABLE,
                         parameters.PARAMETER_MULTIPLE,
                         parameters.PARAMETER_NUMBER,
                         parameters.PARAMETER_DISTANCE,
                         parameters.PARAMETER_SCALE,
                         parameters.PARAMETER_ENUM,
                         parameters.PARAMETER_MATRIX,
                         parameters.PARAMETER_MAP_LAYER):
            return True
        elif isinstance(param, (QgsProcessingParameterField,
                                QgsProcessingParameterBand,
                                QgsProcessingParameterFeatureSource,
                                QgsProcessingParameterVectorLayer,
                                QgsProcessingParameterMultipleLayers,
                                QgsProcessingParameterNumber,
                                QgsProcessingParameterDistance,
                                QgsProcessingParameterScale,
                                QgsProcessingParameterEnum,
                                QgsProcessingParameterMatrix,
                                QgsProcessingParameterMapLayer,
                                QgsProcessingDestinationParameter)):
            return True

        # yay, use new API!
        return False

    def __init__(self, alg, paramType=None, param=None):
        self.alg = alg
        self.paramType = paramType
        self.param = param
        QDialog.__init__(self)
        self.setModal(True)
        self.setupUi()
        settings = QgsSettings()
        self.restoreGeometry(settings.value("/Processing/modelParametersDefinitionDialogGeometry", QByteArray()))

    def closeEvent(self, event):
        settings = QgsSettings()
        settings.setValue("/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry())
        super(ModelerParameterDefinitionDialog, self).closeEvent(event)

    def switchToCommentTab(self):
        self.tab.setCurrentIndex(1)
        self.commentEdit.setFocus()
        self.commentEdit.selectAll()

    def setupUi(self):
        type_metadata = QgsApplication.processingRegistry().parameterType(self.param.type() if self.param else self.paramType)
        self.setWindowTitle(self.tr('{} Parameter Definition').format(type_metadata.name()))

        self.mainLayout = QVBoxLayout()
        self.tab = QTabWidget()
        self.mainLayout.addWidget(self.tab)

        self.setMinimumWidth(300)

        self.verticalLayout = QVBoxLayout()

        self.label = QLabel(self.tr('Parameter name'))
        self.verticalLayout.addWidget(self.label)
        self.nameTextBox = QLineEdit()
        self.verticalLayout.addWidget(self.nameTextBox)

        if isinstance(self.param, QgsProcessingParameterDefinition):
            self.nameTextBox.setText(self.param.description())

        if self.paramType == parameters.PARAMETER_TABLE_FIELD or \
                isinstance(self.param, QgsProcessingParameterField):
            self.verticalLayout.addWidget(QLabel(self.tr('Parent layer')))
            self.parentCombo = QComboBox()
            idx = 0
            for param in list(self.alg.parameterComponents().values()):
                definition = self.alg.parameterDefinition(param.parameterName())
                if isinstance(definition, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer)):
                    self.parentCombo.addItem(definition.description(), definition.name())
                    if self.param is not None:
                        if self.param.parentLayerParameterName() == definition.name():
                            self.parentCombo.setCurrentIndex(idx)
                    idx += 1
            self.verticalLayout.addWidget(self.parentCombo)

            # add the datatype selector
            self.verticalLayout.addWidget(QLabel(self.tr('Allowed data type')))
            self.datatypeCombo = QComboBox()
            self.datatypeCombo.addItem(self.tr('Any'), -1)
            self.datatypeCombo.addItem(self.tr('Number'), 0)
            self.datatypeCombo.addItem(self.tr('String'), 1)
            self.datatypeCombo.addItem(self.tr('Date/time'), 2)
            self.verticalLayout.addWidget(self.datatypeCombo)

            if self.param is not None and self.param.dataType() is not None:
                # QComboBoxes indexes start at 0,
                # self.param.datatype start with -1 that is why I need to do +1
                datatypeIndex = self.param.dataType() + 1
                self.datatypeCombo.setCurrentIndex(datatypeIndex)

            self.multipleCheck = QCheckBox()
            self.multipleCheck.setText(self.tr('Accept multiple fields'))
            self.multipleCheck.setChecked(False)
            if self.param is not None:
                self.multipleCheck.setChecked(self.param.allowMultiple())
            self.verticalLayout.addWidget(self.multipleCheck)

            self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
            self.defaultTextBox = QLineEdit()
            self.defaultTextBox.setToolTip(
                self.tr('Default field name, or ; separated list of field names for multiple field parameters'))
            if self.param is not None:
                default = self.param.defaultValue()
                if default is not None:
                    self.defaultTextBox.setText(str(default))
            self.verticalLayout.addWidget(self.defaultTextBox)

        elif self.paramType == parameters.PARAMETER_BAND or \
                isinstance(self.param, QgsProcessingParameterBand):
            self.verticalLayout.addWidget(QLabel(self.tr('Parent layer')))
            self.parentCombo = QComboBox()
            idx = 0
            for param in list(self.alg.parameterComponents().values()):
                definition = self.alg.parameterDefinition(param.parameterName())
                if isinstance(definition, (QgsProcessingParameterRasterLayer)):
                    self.parentCombo.addItem(definition.description(), definition.name())
                    if self.param is not None:
                        if self.param.parentLayerParameterName() == definition.name():
                            self.parentCombo.setCurrentIndex(idx)
                    idx += 1
            self.verticalLayout.addWidget(self.parentCombo)
        elif (self.paramType in (
                parameters.PARAMETER_VECTOR, parameters.PARAMETER_TABLE) or
                isinstance(self.param, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer))):
            self.verticalLayout.addWidget(QLabel(self.tr('Geometry type')))
            self.shapetypeCombo = QComboBox()
            self.shapetypeCombo.addItem(self.tr('Geometry Not Required'), QgsProcessing.TypeVector)
            self.shapetypeCombo.addItem(self.tr('Point'), QgsProcessing.TypeVectorPoint)
            self.shapetypeCombo.addItem(self.tr('Line'), QgsProcessing.TypeVectorLine)
            self.shapetypeCombo.addItem(self.tr('Polygon'), QgsProcessing.TypeVectorPolygon)
            self.shapetypeCombo.addItem(self.tr('Any Geometry Type'), QgsProcessing.TypeVectorAnyGeometry)
            if self.param is not None:
                self.shapetypeCombo.setCurrentIndex(self.shapetypeCombo.findData(self.param.dataTypes()[0]))
            self.verticalLayout.addWidget(self.shapetypeCombo)
        elif (self.paramType == parameters.PARAMETER_MULTIPLE
              or isinstance(self.param, QgsProcessingParameterMultipleLayers)):
            self.verticalLayout.addWidget(QLabel(self.tr('Data type')))
            self.datatypeCombo = QComboBox()
            self.datatypeCombo.addItem(self.tr('Any Map Layer'), QgsProcessing.TypeMapLayer)
            self.datatypeCombo.addItem(self.tr('Vector (No Geometry Required)'), QgsProcessing.TypeVector)
            self.datatypeCombo.addItem(self.tr('Vector (Point)'), QgsProcessing.TypeVectorPoint)
            self.datatypeCombo.addItem(self.tr('Vector (Line)'), QgsProcessing.TypeVectorLine)
            self.datatypeCombo.addItem(self.tr('Vector (Polygon)'), QgsProcessing.TypeVectorPolygon)
            self.datatypeCombo.addItem(self.tr('Vector (Any Geometry Type)'), QgsProcessing.TypeVectorAnyGeometry)
            self.datatypeCombo.addItem(self.tr('Raster'), QgsProcessing.TypeRaster)
            self.datatypeCombo.addItem(self.tr('File'), QgsProcessing.TypeFile)
            if self.param is not None:
                self.datatypeCombo.setCurrentIndex(self.datatypeCombo.findData(self.param.layerType()))
            self.verticalLayout.addWidget(self.datatypeCombo)
        elif (self.paramType == parameters.PARAMETER_MAP_LAYER or
              isinstance(self.param, QgsProcessingParameterMapLayer)):
            self.verticalLayout.addWidget(QLabel(self.tr('Data type')))
            self.datatypeCombo = QComboBox()
            self.datatypeCombo.addItem(self.tr('Any Map Layer'), QgsProcessing.TypeMapLayer)
            self.datatypeCombo.addItem(self.tr('Vector (Point)'), QgsProcessing.TypeVectorPoint)
            self.datatypeCombo.addItem(self.tr('Vector (Line)'), QgsProcessing.TypeVectorLine)
            self.datatypeCombo.addItem(self.tr('Vector (Polygon)'), QgsProcessing.TypeVectorPolygon)
            self.datatypeCombo.addItem(self.tr('Vector (Any Geometry Type)'), QgsProcessing.TypeVectorAnyGeometry)
            self.datatypeCombo.addItem(self.tr('Raster'), QgsProcessing.TypeRaster)
            self.datatypeCombo.addItem(self.tr('Mesh'), QgsProcessing.TypeMesh)
            if self.param is not None:
                self.datatypeCombo.setCurrentIndex(self.datatypeCombo.findData(self.param.dataTypes()[0]))
            self.verticalLayout.addWidget(self.datatypeCombo)
        elif (self.paramType in (parameters.PARAMETER_NUMBER, parameters.PARAMETER_DISTANCE, parameters.PARAMETER_SCALE)
              or isinstance(self.param, (QgsProcessingParameterNumber, QgsProcessingParameterDistance, QgsProcessingParameterScale))):

            if (self.paramType == parameters.PARAMETER_DISTANCE
                    or isinstance(self.param, QgsProcessingParameterDistance)):
                self.verticalLayout.addWidget(QLabel(self.tr('Linked input')))
                self.parentCombo = QComboBox()
                self.parentCombo.addItem('', '')
                idx = 1
                for param in list(self.alg.parameterComponents().values()):
                    definition = self.alg.parameterDefinition(param.parameterName())
                    if isinstance(definition, (QgsProcessingParameterFeatureSource,
                                               QgsProcessingParameterVectorLayer,
                                               QgsProcessingParameterMapLayer,
                                               QgsProcessingParameterCrs)):
                        self.parentCombo.addItem(definition.description(), definition.name())
                        if self.param is not None:
                            if self.param.parentParameterName() == definition.name():
                                self.parentCombo.setCurrentIndex(idx)
                        idx += 1
                self.verticalLayout.addWidget(self.parentCombo)
            elif (self.paramType != parameters.PARAMETER_SCALE and not
                    isinstance(self.param, QgsProcessingParameterScale)):
                self.verticalLayout.addWidget(QLabel(self.tr('Number type')))
                self.type_combo = QComboBox()
                self.type_combo.addItem(self.tr('Float'), QgsProcessingParameterNumber.Double)
                self.type_combo.addItem(self.tr('Integer'), QgsProcessingParameterNumber.Integer)
                if self.param:
                    self.type_combo.setCurrentIndex(self.type_combo.findData(self.param.dataType()))
                self.verticalLayout.addWidget(self.type_combo)

            if (self.paramType != parameters.PARAMETER_SCALE and not
                    isinstance(self.param, QgsProcessingParameterScale)):
                self.verticalLayout.addWidget(QLabel(self.tr('Min value')))
                self.minTextBox = QLineEdit()
                self.verticalLayout.addWidget(self.minTextBox)
                self.verticalLayout.addWidget(QLabel(self.tr('Max value')))
                self.maxTextBox = QLineEdit()
                self.verticalLayout.addWidget(self.maxTextBox)
                if self.param is not None:
                    self.minTextBox.setText(str(self.param.minimum()))
                    self.maxTextBox.setText(str(self.param.maximum()))
            self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
            self.defaultTextBox = QLineEdit()
            self.defaultTextBox.setText(self.tr('0'))
            if self.param is not None:
                default = self.param.defaultValue()
                if self.param.dataType() == QgsProcessingParameterNumber.Integer:
                    default = int(math.floor(float(default)))
                if default:
                    self.defaultTextBox.setText(str(default))
            self.verticalLayout.addWidget(self.defaultTextBox)
        elif self.paramType == parameters.PARAMETER_ENUM or \
                isinstance(self.param, QgsProcessingParameterEnum):
            self.widget = EnumModelerWidget(self)
            if self.param is not None:
                self.widget.setAllowMultiple(bool(self.param.allowMultiple()))
                self.widget.setOptions(self.param.options())
                self.widget.setDefault(self.param.defaultValue())
            self.verticalLayout.addWidget(self.widget)
        elif self.paramType == parameters.PARAMETER_MATRIX or \
                isinstance(self.param, QgsProcessingParameterMatrix):
            self.widget = MatrixModelerWidget(self)
            if self.param is not None:
                self.widget.setValue(self.param.headers(), self.param.defaultValue())
                self.widget.setFixedRows(self.param.hasFixedNumberRows())
            self.verticalLayout.addWidget(self.widget)

        elif isinstance(self.param, QgsProcessingDestinationParameter):
            self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
            self.defaultWidget = QgsProcessingLayerOutputDestinationWidget(self.param, defaultSelection=True)
            self.verticalLayout.addWidget(self.defaultWidget)

        self.verticalLayout.addSpacing(20)
        self.requiredCheck = QCheckBox()
        self.requiredCheck.setText(self.tr('Mandatory'))
        self.requiredCheck.setChecked(True)
        if self.param is not None:
            self.requiredCheck.setChecked(not self.param.flags() & QgsProcessingParameterDefinition.FlagOptional)
        self.verticalLayout.addWidget(self.requiredCheck)

        self.advancedCheck = QCheckBox()
        self.advancedCheck.setText(self.tr('Advanced'))
        self.advancedCheck.setChecked(False)
        if self.param is not None:
            self.advancedCheck.setChecked(self.param.flags() & QgsProcessingParameterDefinition.FlagAdvanced)
        self.verticalLayout.addWidget(self.advancedCheck)

        # If child algorithm output is mandatory, disable checkbox
        if isinstance(self.param, QgsProcessingDestinationParameter):
            provider_name, child_name, output_name = self.param.name().split(':')
            child = self.alg.childAlgorithms()['{}:{}'.format(provider_name, child_name)]
            model_output = child.modelOutput(output_name)
            param_def = child.algorithm().parameterDefinition(model_output.childOutputName())
            if not (param_def.flags() & QgsProcessingParameterDefinition.FlagOptional):
                self.requiredCheck.setEnabled(False)
                self.requiredCheck.setChecked(True)

            self.advancedCheck.setEnabled(False)
            self.advancedCheck.setChecked(False)

        self.verticalLayout.addStretch()

        w = QWidget()
        w.setLayout(self.verticalLayout)
        self.tab.addTab(w, self.tr('Properties'))

        self.commentLayout = QVBoxLayout()
        self.commentEdit = QTextEdit()
        self.commentEdit.setAcceptRichText(False)
        self.commentLayout.addWidget(self.commentEdit)
        w2 = QWidget()
        w2.setLayout(self.commentLayout)
        self.tab.addTab(w2, self.tr('Comments'))

        self.buttonBox = QDialogButtonBox(self)
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel
                                          | QDialogButtonBox.Ok)
        self.buttonBox.setObjectName('buttonBox')
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

        self.mainLayout.addWidget(self.buttonBox)

        self.setLayout(self.mainLayout)

    def setComments(self, text):
        self.commentEdit.setPlainText(text)

    def comments(self):
        return self.commentEdit.toPlainText()

    def accept(self):
        description = self.nameTextBox.text()
        if description.strip() == '':
            QMessageBox.warning(self, self.tr('Unable to define parameter'),
                                self.tr('Invalid parameter name'))
            return
        if self.param is None:
            validChars = \
                'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
            safeName = ''.join(c for c in description if c in validChars)
            name = safeName.lower()
            i = 2
            while self.alg.parameterDefinition(name):
                name = safeName.lower() + str(i)
                i += 1
        else:
            name = self.param.name()
        if (self.paramType == parameters.PARAMETER_TABLE_FIELD
                or isinstance(self.param, QgsProcessingParameterField)):
            if self.parentCombo.currentIndex() < 0:
                QMessageBox.warning(self, self.tr('Unable to define parameter'),
                                    self.tr('Wrong or missing parameter values'))
                return
            parent = self.parentCombo.currentData()
            datatype = self.datatypeCombo.currentData()
            default = self.defaultTextBox.text()
            if not default:
                default = None
            self.param = QgsProcessingParameterField(name, description, defaultValue=default,
                                                     parentLayerParameterName=parent, type=datatype,
                                                     allowMultiple=self.multipleCheck.isChecked())
        elif (self.paramType == parameters.PARAMETER_BAND
              or isinstance(self.param, QgsProcessingParameterBand)):
            if self.parentCombo.currentIndex() < 0:
                QMessageBox.warning(self, self.tr('Unable to define parameter'),
                                    self.tr('Wrong or missing parameter values'))
                return
            parent = self.parentCombo.currentData()
            self.param = QgsProcessingParameterBand(name, description, None, parent)
        elif (self.paramType == parameters.PARAMETER_MAP_LAYER
              or isinstance(self.param, QgsProcessingParameterMapLayer)):
            self.param = QgsProcessingParameterMapLayer(
                name, description, types=[self.datatypeCombo.currentData()])
        elif (self.paramType == parameters.PARAMETER_RASTER
              or isinstance(self.param, QgsProcessingParameterRasterLayer)):
            self.param = QgsProcessingParameterRasterLayer(
                name, description)
        elif (self.paramType == parameters.PARAMETER_TABLE
              or isinstance(self.param, QgsProcessingParameterVectorLayer)):
            self.param = QgsProcessingParameterVectorLayer(
                name, description,
                [self.shapetypeCombo.currentData()])
        elif (self.paramType == parameters.PARAMETER_VECTOR
              or isinstance(self.param, QgsProcessingParameterFeatureSource)):
            self.param = QgsProcessingParameterFeatureSource(
                name, description,
                [self.shapetypeCombo.currentData()])
        elif (self.paramType == parameters.PARAMETER_MULTIPLE
              or isinstance(self.param, QgsProcessingParameterMultipleLayers)):
            self.param = QgsProcessingParameterMultipleLayers(
                name, description,
                self.datatypeCombo.currentData())
        elif (self.paramType == parameters.PARAMETER_DISTANCE
              or isinstance(self.param, QgsProcessingParameterDistance)):
            self.param = QgsProcessingParameterDistance(name, description,
                                                        self.defaultTextBox.text())
            try:
                vmin = self.minTextBox.text().strip()
                if not vmin == '':
                    self.param.setMinimum(float(vmin))
                vmax = self.maxTextBox.text().strip()
                if not vmax == '':
                    self.param.setMaximum(float(vmax))
            except:
                QMessageBox.warning(self, self.tr('Unable to define parameter'),
                                    self.tr('Wrong or missing parameter values'))
                return

            if self.parentCombo.currentIndex() < 0:
                QMessageBox.warning(self, self.tr('Unable to define parameter'),
                                    self.tr('Wrong or missing parameter values'))
                return
            parent = self.parentCombo.currentData()
            if parent:
                self.param.setParentParameterName(parent)
        elif (self.paramType == parameters.PARAMETER_SCALE
              or isinstance(self.param, QgsProcessingParameterScale)):
            self.param = QgsProcessingParameterScale(name, description,
                                                     self.defaultTextBox.text())
        elif (self.paramType == parameters.PARAMETER_NUMBER
              or isinstance(self.param, QgsProcessingParameterNumber)):

            type = self.type_combo.currentData()
            self.param = QgsProcessingParameterNumber(name, description, type,
                                                      self.defaultTextBox.text())
            try:
                vmin = self.minTextBox.text().strip()
                if not vmin == '':
                    self.param.setMinimum(float(vmin))
                vmax = self.maxTextBox.text().strip()
                if not vmax == '':
                    self.param.setMaximum(float(vmax))
            except:
                QMessageBox.warning(self, self.tr('Unable to define parameter'),
                                    self.tr('Wrong or missing parameter values'))
                return
        elif (self.paramType == parameters.PARAMETER_EXTENT
              or isinstance(self.param, QgsProcessingParameterExtent)):
            self.param = QgsProcessingParameterExtent(name, description)
        elif (self.paramType == parameters.PARAMETER_ENUM
                or isinstance(self.param, QgsProcessingParameterEnum)):
            self.param = QgsProcessingParameterEnum(name, description, self.widget.options(), self.widget.allowMultiple(), self.widget.defaultOptions())
        elif (self.paramType == parameters.PARAMETER_MATRIX
                or isinstance(self.param, QgsProcessingParameterMatrix)):
            self.param = QgsProcessingParameterMatrix(name, description, hasFixedNumberRows=self.widget.fixedRows(), headers=self.widget.headers(), defaultValue=self.widget.value())

        # Destination parameter
        elif (isinstance(self.param, QgsProcessingParameterFeatureSink)):
            self.param = QgsProcessingParameterFeatureSink(
                name=name,
                description=self.param.description(),
                type=self.param.dataType(),
                defaultValue=self.defaultWidget.value())
        elif (isinstance(self.param, QgsProcessingParameterFileDestination)):
            self.param = QgsProcessingParameterFileDestination(
                name=name,
                description=self.param.description(),
                fileFilter=self.param.fileFilter(),
                defaultValue=self.defaultWidget.value())
        elif (isinstance(self.param, QgsProcessingParameterFolderDestination)):
            self.param = QgsProcessingParameterFolderDestination(
                name=name,
                description=self.param.description(),
                defaultValue=self.defaultWidget.value())
        elif (isinstance(self.param, QgsProcessingParameterRasterDestination)):
            self.param = QgsProcessingParameterRasterDestination(
                name=name,
                description=self.param.description(),
                defaultValue=self.defaultWidget.value())
        elif (isinstance(self.param, QgsProcessingParameterVectorDestination)):
            self.param = QgsProcessingParameterVectorDestination(
                name=name,
                description=self.param.description(),
                type=self.param.dataType(),
                defaultValue=self.defaultWidget.value())

        else:
            if self.paramType:
                typeId = self.paramType
            else:
                typeId = self.param.type()

            paramTypeDef = QgsApplication.instance().processingRegistry().parameterType(typeId)
            if not paramTypeDef:
                msg = self.tr('The parameter `{}` is not registered, are you missing a required plugin?'.format(typeId))
                raise UndefinedParameterException(msg)
            self.param = paramTypeDef.create(name)
            self.param.setDescription(description)
            self.param.setMetadata(paramTypeDef.metadata())

        if not self.requiredCheck.isChecked():
            self.param.setFlags(self.param.flags() | QgsProcessingParameterDefinition.FlagOptional)
        else:
            self.param.setFlags(self.param.flags() & ~QgsProcessingParameterDefinition.FlagOptional)

        if self.advancedCheck.isChecked():
            self.param.setFlags(self.param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
        else:
            self.param.setFlags(self.param.flags() & ~QgsProcessingParameterDefinition.FlagAdvanced)

        settings = QgsSettings()
        settings.setValue("/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry())

        QDialog.accept(self)

    def reject(self):
        self.param = None

        settings = QgsSettings()
        settings.setValue("/Processing/modelParametersDefinitionDialogGeometry", self.saveGeometry())

        QDialog.reject(self)
Ejemplo n.º 29
0
    def setupUi(self):
        type_metadata = QgsApplication.processingRegistry().parameterType(self.param.type() if self.param else self.paramType)
        self.setWindowTitle(self.tr('{} Parameter Definition').format(type_metadata.name()))

        self.mainLayout = QVBoxLayout()
        self.tab = QTabWidget()
        self.mainLayout.addWidget(self.tab)

        self.setMinimumWidth(300)

        self.verticalLayout = QVBoxLayout()

        self.label = QLabel(self.tr('Parameter name'))
        self.verticalLayout.addWidget(self.label)
        self.nameTextBox = QLineEdit()
        self.verticalLayout.addWidget(self.nameTextBox)

        if isinstance(self.param, QgsProcessingParameterDefinition):
            self.nameTextBox.setText(self.param.description())

        if self.paramType == parameters.PARAMETER_TABLE_FIELD or \
                isinstance(self.param, QgsProcessingParameterField):
            self.verticalLayout.addWidget(QLabel(self.tr('Parent layer')))
            self.parentCombo = QComboBox()
            idx = 0
            for param in list(self.alg.parameterComponents().values()):
                definition = self.alg.parameterDefinition(param.parameterName())
                if isinstance(definition, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer)):
                    self.parentCombo.addItem(definition.description(), definition.name())
                    if self.param is not None:
                        if self.param.parentLayerParameterName() == definition.name():
                            self.parentCombo.setCurrentIndex(idx)
                    idx += 1
            self.verticalLayout.addWidget(self.parentCombo)

            # add the datatype selector
            self.verticalLayout.addWidget(QLabel(self.tr('Allowed data type')))
            self.datatypeCombo = QComboBox()
            self.datatypeCombo.addItem(self.tr('Any'), -1)
            self.datatypeCombo.addItem(self.tr('Number'), 0)
            self.datatypeCombo.addItem(self.tr('String'), 1)
            self.datatypeCombo.addItem(self.tr('Date/time'), 2)
            self.verticalLayout.addWidget(self.datatypeCombo)

            if self.param is not None and self.param.dataType() is not None:
                # QComboBoxes indexes start at 0,
                # self.param.datatype start with -1 that is why I need to do +1
                datatypeIndex = self.param.dataType() + 1
                self.datatypeCombo.setCurrentIndex(datatypeIndex)

            self.multipleCheck = QCheckBox()
            self.multipleCheck.setText(self.tr('Accept multiple fields'))
            self.multipleCheck.setChecked(False)
            if self.param is not None:
                self.multipleCheck.setChecked(self.param.allowMultiple())
            self.verticalLayout.addWidget(self.multipleCheck)

            self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
            self.defaultTextBox = QLineEdit()
            self.defaultTextBox.setToolTip(
                self.tr('Default field name, or ; separated list of field names for multiple field parameters'))
            if self.param is not None:
                default = self.param.defaultValue()
                if default is not None:
                    self.defaultTextBox.setText(str(default))
            self.verticalLayout.addWidget(self.defaultTextBox)

        elif self.paramType == parameters.PARAMETER_BAND or \
                isinstance(self.param, QgsProcessingParameterBand):
            self.verticalLayout.addWidget(QLabel(self.tr('Parent layer')))
            self.parentCombo = QComboBox()
            idx = 0
            for param in list(self.alg.parameterComponents().values()):
                definition = self.alg.parameterDefinition(param.parameterName())
                if isinstance(definition, (QgsProcessingParameterRasterLayer)):
                    self.parentCombo.addItem(definition.description(), definition.name())
                    if self.param is not None:
                        if self.param.parentLayerParameterName() == definition.name():
                            self.parentCombo.setCurrentIndex(idx)
                    idx += 1
            self.verticalLayout.addWidget(self.parentCombo)
        elif (self.paramType in (
                parameters.PARAMETER_VECTOR, parameters.PARAMETER_TABLE) or
                isinstance(self.param, (QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer))):
            self.verticalLayout.addWidget(QLabel(self.tr('Geometry type')))
            self.shapetypeCombo = QComboBox()
            self.shapetypeCombo.addItem(self.tr('Geometry Not Required'), QgsProcessing.TypeVector)
            self.shapetypeCombo.addItem(self.tr('Point'), QgsProcessing.TypeVectorPoint)
            self.shapetypeCombo.addItem(self.tr('Line'), QgsProcessing.TypeVectorLine)
            self.shapetypeCombo.addItem(self.tr('Polygon'), QgsProcessing.TypeVectorPolygon)
            self.shapetypeCombo.addItem(self.tr('Any Geometry Type'), QgsProcessing.TypeVectorAnyGeometry)
            if self.param is not None:
                self.shapetypeCombo.setCurrentIndex(self.shapetypeCombo.findData(self.param.dataTypes()[0]))
            self.verticalLayout.addWidget(self.shapetypeCombo)
        elif (self.paramType == parameters.PARAMETER_MULTIPLE
              or isinstance(self.param, QgsProcessingParameterMultipleLayers)):
            self.verticalLayout.addWidget(QLabel(self.tr('Data type')))
            self.datatypeCombo = QComboBox()
            self.datatypeCombo.addItem(self.tr('Any Map Layer'), QgsProcessing.TypeMapLayer)
            self.datatypeCombo.addItem(self.tr('Vector (No Geometry Required)'), QgsProcessing.TypeVector)
            self.datatypeCombo.addItem(self.tr('Vector (Point)'), QgsProcessing.TypeVectorPoint)
            self.datatypeCombo.addItem(self.tr('Vector (Line)'), QgsProcessing.TypeVectorLine)
            self.datatypeCombo.addItem(self.tr('Vector (Polygon)'), QgsProcessing.TypeVectorPolygon)
            self.datatypeCombo.addItem(self.tr('Vector (Any Geometry Type)'), QgsProcessing.TypeVectorAnyGeometry)
            self.datatypeCombo.addItem(self.tr('Raster'), QgsProcessing.TypeRaster)
            self.datatypeCombo.addItem(self.tr('File'), QgsProcessing.TypeFile)
            if self.param is not None:
                self.datatypeCombo.setCurrentIndex(self.datatypeCombo.findData(self.param.layerType()))
            self.verticalLayout.addWidget(self.datatypeCombo)
        elif (self.paramType == parameters.PARAMETER_MAP_LAYER or
              isinstance(self.param, QgsProcessingParameterMapLayer)):
            self.verticalLayout.addWidget(QLabel(self.tr('Data type')))
            self.datatypeCombo = QComboBox()
            self.datatypeCombo.addItem(self.tr('Any Map Layer'), QgsProcessing.TypeMapLayer)
            self.datatypeCombo.addItem(self.tr('Vector (Point)'), QgsProcessing.TypeVectorPoint)
            self.datatypeCombo.addItem(self.tr('Vector (Line)'), QgsProcessing.TypeVectorLine)
            self.datatypeCombo.addItem(self.tr('Vector (Polygon)'), QgsProcessing.TypeVectorPolygon)
            self.datatypeCombo.addItem(self.tr('Vector (Any Geometry Type)'), QgsProcessing.TypeVectorAnyGeometry)
            self.datatypeCombo.addItem(self.tr('Raster'), QgsProcessing.TypeRaster)
            self.datatypeCombo.addItem(self.tr('Mesh'), QgsProcessing.TypeMesh)
            if self.param is not None:
                self.datatypeCombo.setCurrentIndex(self.datatypeCombo.findData(self.param.dataTypes()[0]))
            self.verticalLayout.addWidget(self.datatypeCombo)
        elif (self.paramType in (parameters.PARAMETER_NUMBER, parameters.PARAMETER_DISTANCE, parameters.PARAMETER_SCALE)
              or isinstance(self.param, (QgsProcessingParameterNumber, QgsProcessingParameterDistance, QgsProcessingParameterScale))):

            if (self.paramType == parameters.PARAMETER_DISTANCE
                    or isinstance(self.param, QgsProcessingParameterDistance)):
                self.verticalLayout.addWidget(QLabel(self.tr('Linked input')))
                self.parentCombo = QComboBox()
                self.parentCombo.addItem('', '')
                idx = 1
                for param in list(self.alg.parameterComponents().values()):
                    definition = self.alg.parameterDefinition(param.parameterName())
                    if isinstance(definition, (QgsProcessingParameterFeatureSource,
                                               QgsProcessingParameterVectorLayer,
                                               QgsProcessingParameterMapLayer,
                                               QgsProcessingParameterCrs)):
                        self.parentCombo.addItem(definition.description(), definition.name())
                        if self.param is not None:
                            if self.param.parentParameterName() == definition.name():
                                self.parentCombo.setCurrentIndex(idx)
                        idx += 1
                self.verticalLayout.addWidget(self.parentCombo)
            elif (self.paramType != parameters.PARAMETER_SCALE and not
                    isinstance(self.param, QgsProcessingParameterScale)):
                self.verticalLayout.addWidget(QLabel(self.tr('Number type')))
                self.type_combo = QComboBox()
                self.type_combo.addItem(self.tr('Float'), QgsProcessingParameterNumber.Double)
                self.type_combo.addItem(self.tr('Integer'), QgsProcessingParameterNumber.Integer)
                if self.param:
                    self.type_combo.setCurrentIndex(self.type_combo.findData(self.param.dataType()))
                self.verticalLayout.addWidget(self.type_combo)

            if (self.paramType != parameters.PARAMETER_SCALE and not
                    isinstance(self.param, QgsProcessingParameterScale)):
                self.verticalLayout.addWidget(QLabel(self.tr('Min value')))
                self.minTextBox = QLineEdit()
                self.verticalLayout.addWidget(self.minTextBox)
                self.verticalLayout.addWidget(QLabel(self.tr('Max value')))
                self.maxTextBox = QLineEdit()
                self.verticalLayout.addWidget(self.maxTextBox)
                if self.param is not None:
                    self.minTextBox.setText(str(self.param.minimum()))
                    self.maxTextBox.setText(str(self.param.maximum()))
            self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
            self.defaultTextBox = QLineEdit()
            self.defaultTextBox.setText(self.tr('0'))
            if self.param is not None:
                default = self.param.defaultValue()
                if self.param.dataType() == QgsProcessingParameterNumber.Integer:
                    default = int(math.floor(float(default)))
                if default:
                    self.defaultTextBox.setText(str(default))
            self.verticalLayout.addWidget(self.defaultTextBox)
        elif self.paramType == parameters.PARAMETER_ENUM or \
                isinstance(self.param, QgsProcessingParameterEnum):
            self.widget = EnumModelerWidget(self)
            if self.param is not None:
                self.widget.setAllowMultiple(bool(self.param.allowMultiple()))
                self.widget.setOptions(self.param.options())
                self.widget.setDefault(self.param.defaultValue())
            self.verticalLayout.addWidget(self.widget)
        elif self.paramType == parameters.PARAMETER_MATRIX or \
                isinstance(self.param, QgsProcessingParameterMatrix):
            self.widget = MatrixModelerWidget(self)
            if self.param is not None:
                self.widget.setValue(self.param.headers(), self.param.defaultValue())
                self.widget.setFixedRows(self.param.hasFixedNumberRows())
            self.verticalLayout.addWidget(self.widget)

        elif isinstance(self.param, QgsProcessingDestinationParameter):
            self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
            self.defaultWidget = QgsProcessingLayerOutputDestinationWidget(self.param, defaultSelection=True)
            self.verticalLayout.addWidget(self.defaultWidget)

        self.verticalLayout.addSpacing(20)
        self.requiredCheck = QCheckBox()
        self.requiredCheck.setText(self.tr('Mandatory'))
        self.requiredCheck.setChecked(True)
        if self.param is not None:
            self.requiredCheck.setChecked(not self.param.flags() & QgsProcessingParameterDefinition.FlagOptional)
        self.verticalLayout.addWidget(self.requiredCheck)

        self.advancedCheck = QCheckBox()
        self.advancedCheck.setText(self.tr('Advanced'))
        self.advancedCheck.setChecked(False)
        if self.param is not None:
            self.advancedCheck.setChecked(self.param.flags() & QgsProcessingParameterDefinition.FlagAdvanced)
        self.verticalLayout.addWidget(self.advancedCheck)

        # If child algorithm output is mandatory, disable checkbox
        if isinstance(self.param, QgsProcessingDestinationParameter):
            provider_name, child_name, output_name = self.param.name().split(':')
            child = self.alg.childAlgorithms()['{}:{}'.format(provider_name, child_name)]
            model_output = child.modelOutput(output_name)
            param_def = child.algorithm().parameterDefinition(model_output.childOutputName())
            if not (param_def.flags() & QgsProcessingParameterDefinition.FlagOptional):
                self.requiredCheck.setEnabled(False)
                self.requiredCheck.setChecked(True)

            self.advancedCheck.setEnabled(False)
            self.advancedCheck.setChecked(False)

        self.verticalLayout.addStretch()

        w = QWidget()
        w.setLayout(self.verticalLayout)
        self.tab.addTab(w, self.tr('Properties'))

        self.commentLayout = QVBoxLayout()
        self.commentEdit = QTextEdit()
        self.commentEdit.setAcceptRichText(False)
        self.commentLayout.addWidget(self.commentEdit)
        w2 = QWidget()
        w2.setLayout(self.commentLayout)
        self.tab.addTab(w2, self.tr('Comments'))

        self.buttonBox = QDialogButtonBox(self)
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel
                                          | QDialogButtonBox.Ok)
        self.buttonBox.setObjectName('buttonBox')
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

        self.mainLayout.addWidget(self.buttonBox)

        self.setLayout(self.mainLayout)
Ejemplo n.º 30
0
class TreeSettingItem(QTreeWidgetItem):

    comboStyle = '''QComboBox {
                 border: 1px solid gray;
                 border-radius: 3px;
                 padding: 1px 18px 1px 3px;
                 min-width: 6em;
             }

             QComboBox::drop-down {
                 subcontrol-origin: padding;
                 subcontrol-position: top right;
                 width: 15px;
                 border-left-width: 1px;
                 border-left-color: darkgray;
                 border-left-style: solid;
                 border-top-right-radius: 3px;
                 border-bottom-right-radius: 3px;
             }
            '''

    def _addTextEdit(self, editable=True):
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        self.textEdit = QTextEdit()
        if not editable:
            self.textEdit.setReadOnly(True)
        self.textEdit.setPlainText(self._value)
        layout.addWidget(self.textEdit)
        w = QWidget()
        w.setLayout(layout)
        self.tree.setItemWidget(self, 1, w)

    def _addTextBoxWithLink(self, text, func, editable=True):
        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        self.lineEdit = QLineEdit()
        if not editable:
            self.lineEdit.setReadOnly(True)
        self.lineEdit.setText(self._value)
        layout.addWidget(self.lineEdit)
        if text:
            self.linkLabel = QLabel()
            self.linkLabel.setText("<a href='#'> %s</a>" % text)
            self.linkLabel.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
            layout.addWidget(self.linkLabel)
            self.linkLabel.linkActivated.connect(func)
        w = QWidget()
        w.setLayout(layout)
        self.tree.setItemWidget(self, 1, w)

    def __init__(self, parent, tree, setting, namespace, value):
        QTreeWidgetItem.__init__(self, parent)
        self.parent = parent
        self.namespace = namespace
        self.tree = tree
        self._value = value
        self.setting = setting
        self.name = setting["name"]
        self.labelText = setting["label"]
        self.settingType = setting["type"]
        self.setText(0, self.labelText)
        if self.settingType == CRS:
            def edit():
                selector = QgsProjectionSelectionDialog()
                selector.setCrs(value);
                if selector.exec_():
                    crs = selector.crs()
                    if crs.upper().startswith("EPSG:"):
                        self.lineEdit.setText(crs)
            self._addTextBoxWithLink("Edit", edit, False)
        elif self.settingType == FILES:
            def edit():
                f = QFileDialog.getOpenFileNames(parent.treeWidget(), "Select file", "", "*.*")
                if f:
                    self.lineEdit.setText(",".join(f))
            self._addTextBoxWithLink("Browse", edit, True)
        elif self.settingType == FILE:
            def edit():
                f = QFileDialog.getOpenFileName(parent.treeWidget(), "Select file", "", "*.*")
                if f:
                    self.lineEdit.setText(f)
            self._addTextBoxWithLink("Browse", edit, True)
        elif self.settingType == FOLDER:
            def edit():
                f = QFileDialog.getExistingDirectory(parent.treeWidget(), "Select folder", "")
                if f:
                    self.lineEdit.setText(f)
            self._addTextBoxWithLink("Browse", edit, True)
        elif self.settingType == BOOL:
            if value:
                self.setCheckState(1, Qt.Checked)
            else:
                self.setCheckState(1, Qt.Unchecked)
        elif self.settingType == CHOICE:
            self.combo = QComboBox()
            self.combo.setStyleSheet(self.comboStyle)
            for option in setting["options"]:
                self.combo.addItem(option)
            self.tree.setItemWidget(self, 1, self.combo)
            idx = self.combo.findText(str(value))
            self.combo.setCurrentIndex(idx)
        elif self.settingType == TEXT:
            self._addTextEdit()
        elif self.settingType == STRING:
            self._addTextBoxWithLink(None, None)
        elif self.settingType == AUTHCFG:
            def edit():
                currentAuthCfg = self.value()
                dlg = AuthConfigSelectDialog(parent.treeWidget(), authcfg=currentAuthCfg)
                ret = dlg.exec_()
                if ret:
                    self.lineEdit.setText(dlg.authcfg)
            self._addTextBoxWithLink("Select", edit, True)
        else:
            self.setFlags(self.flags() | Qt.ItemIsEditable)
            self.setText(1, unicode(value))

    def saveValue(self):
        value = self.value()
        setPluginSetting(self.name, value, self.namespace)

    def value(self):
        self.setBackgroundColor(0, Qt.white)
        self.setBackgroundColor(1, Qt.white)
        try:
            if self.settingType == BOOL:
                return self.checkState(1) == Qt.Checked
            elif self.settingType == NUMBER:
                v = float(self.text(1))
                return v
            elif self.settingType == CHOICE:
                return self.combo.currentText()
            elif self.settingType in [TEXT]:
                return self.textEdit.toPlainText()
            elif self.settingType in [CRS, STRING, FILES, FOLDER, AUTHCFG]:
                return self.lineEdit.text()
            else:
                return self.text(1)
        except:
            self.setBackgroundColor(0, Qt.yellow)
            self.setBackgroundColor(1, Qt.yellow)
            raise WrongValueException()

    def setValue(self, value):
        if self.settingType == BOOL:
            if value:
                self.setCheckState(1, Qt.Checked)
            else:
                self.setCheckState(1, Qt.Unchecked)
        elif self.settingType == CHOICE:
            idx = self.combo.findText(str(value))
            self.combo.setCurrentIndex(idx)
        elif self.settingType in [TEXT, CRS, STRING, FILES, FOLDER, AUTHCFG]:
            self.lineEdit.setText(value)
        else:
            self.setText(1, unicode(value))

    def resetDefault(self):
        self.setValue(self.setting["default"])
Ejemplo n.º 31
0
class ConsoleWidget(QWidget):
    def __init__(self, exc_info, parent=None):
        QWidget.__init__(self, parent)

        self.compiler = code.CommandCompiler()  # for console

        self.tb = exc_info[2]
        self.entries = traceback.extract_tb(self.tb)

        self.console = QLineEdit()
        self.console.setPlaceholderText(">>> Python Console")
        self.console.returnPressed.connect(self.exec_console)
        self.console.setFont(QFont("Courier"))

        self.console_out = QTextEdit()
        self.console_out.setReadOnly(True)
        self.console_out.setFont(QFont("Courier"))
        self.console_out.setVisible(False)  # initially hidden

        self.console_outs = [''] * len(self.entries)

        self.frame_vars = [None] * len(self.entries)

        l = QVBoxLayout()
        l.addWidget(self.console_out)
        l.addWidget(self.console)
        l.setContentsMargins(0, 0, 0, 0)
        self.setLayout(l)

    def go_to_frame(self, index):
        self.console_out.setPlainText(self.console_outs[index])
        self.current_frame_index = index

    def exec_console(self):

        index = self.current_frame_index
        if index < 0: return

        # cache frame variables (globals and locals)
        # because every time we ask for frame.f_locals, a new dict instance
        # is created - we keep our local cache that may contain some changes
        if self.frame_vars[index] is None:
            #print "init", index
            frame = frame_from_traceback(self.tb, index)
            self.frame_vars[index] = (dict(frame.f_globals),
                                      dict(frame.f_locals))

        frame_vars = self.frame_vars[index]
        #print frame_vars[1]

        line = self.console.text()
        try:
            c = self.compiler(line, "<console>", "single")
        except (OverflowError, SyntaxError, ValueError) as e:
            QMessageBox.critical(self, "Error", str(e))
            return

        if c is None:
            QMessageBox.critical(self, "Error", "Code not complete")
            return

        import io
        io = io.StringIO() if sys.version_info.major >= 3 else io.BytesIO()
        try:
            with stdout_redirected(io):
                exec(c, frame_vars[0], frame_vars[1])
        except:
            etype, value, tb = sys.exc_info()
            QMessageBox.critical(self, "Error",
                                 etype.__name__ + "\n" + str(value))
            return

        stuff = self.console_outs[index]
        stuff += ">>> " + line + "\n"
        stuff += io.getvalue()
        self.console_outs[index] = stuff

        self.console_out.setPlainText(stuff)
        self.console_out.setVisible(True)
        # make sure we are at the end
        c = self.console_out.textCursor()
        c.movePosition(QTextCursor.End)
        self.console_out.setTextCursor(c)
        self.console_out.ensureCursorVisible()

        self.console.setText('')
    def setupUi(self):
        type_metadata = QgsApplication.processingRegistry().parameterType(
            self.param.type() if self.param else self.paramType)
        self.setWindowTitle(
            self.tr('{} Parameter Definition').format(type_metadata.name()))

        self.mainLayout = QVBoxLayout()
        self.tab = QTabWidget()
        self.mainLayout.addWidget(self.tab)

        self.setMinimumWidth(300)

        self.verticalLayout = QVBoxLayout()

        self.label = QLabel(self.tr('Parameter name'))
        self.verticalLayout.addWidget(self.label)
        self.nameTextBox = QLineEdit()
        self.verticalLayout.addWidget(self.nameTextBox)

        if isinstance(self.param, QgsProcessingParameterDefinition):
            self.nameTextBox.setText(self.param.description())

        if isinstance(self.param, QgsProcessingDestinationParameter):
            self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
            self.defaultWidget = QgsProcessingLayerOutputDestinationWidget(
                self.param, defaultSelection=True)
            self.verticalLayout.addWidget(self.defaultWidget)

        self.verticalLayout.addSpacing(20)
        self.requiredCheck = QCheckBox()
        self.requiredCheck.setText(self.tr('Mandatory'))
        self.requiredCheck.setChecked(True)
        if self.param is not None:
            self.requiredCheck.setChecked(
                not self.param.flags()
                & QgsProcessingParameterDefinition.FlagOptional)
        self.verticalLayout.addWidget(self.requiredCheck)

        self.advancedCheck = QCheckBox()
        self.advancedCheck.setText(self.tr('Advanced'))
        self.advancedCheck.setChecked(False)
        if self.param is not None:
            self.advancedCheck.setChecked(
                self.param.flags()
                & QgsProcessingParameterDefinition.FlagAdvanced)
        self.verticalLayout.addWidget(self.advancedCheck)

        # If child algorithm output is mandatory, disable checkbox
        if isinstance(self.param, QgsProcessingDestinationParameter):
            child = self.alg.childAlgorithms()[self.param.metadata()
                                               ['_modelChildId']]
            model_output = child.modelOutput(
                self.param.metadata()['_modelChildOutputName'])
            param_def = child.algorithm().parameterDefinition(
                model_output.childOutputName())
            if not (param_def.flags()
                    & QgsProcessingParameterDefinition.FlagOptional):
                self.requiredCheck.setEnabled(False)
                self.requiredCheck.setChecked(True)

            self.advancedCheck.setEnabled(False)
            self.advancedCheck.setChecked(False)

        self.verticalLayout.addStretch()

        w = QWidget()
        w.setLayout(self.verticalLayout)
        self.tab.addTab(w, self.tr('Properties'))

        self.commentLayout = QVBoxLayout()
        self.commentEdit = QTextEdit()
        self.commentEdit.setAcceptRichText(False)
        self.commentLayout.addWidget(self.commentEdit, 1)

        hl = QHBoxLayout()
        hl.setContentsMargins(0, 0, 0, 0)
        hl.addWidget(QLabel(self.tr('Color')))
        self.comment_color_button = QgsColorButton()
        self.comment_color_button.setAllowOpacity(True)
        self.comment_color_button.setWindowTitle(self.tr('Comment Color'))
        self.comment_color_button.setShowNull(True, self.tr('Default'))
        hl.addWidget(self.comment_color_button)
        self.commentLayout.addLayout(hl)

        w2 = QWidget()
        w2.setLayout(self.commentLayout)
        self.tab.addTab(w2, self.tr('Comments'))

        self.buttonBox = QDialogButtonBox(self)
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel
                                          | QDialogButtonBox.Ok)
        self.buttonBox.setObjectName('buttonBox')
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

        self.mainLayout.addWidget(self.buttonBox)

        self.setLayout(self.mainLayout)
class ModelerParameterDefinitionDialog(QDialog):
    @staticmethod
    def use_legacy_dialog(param=None, paramType=None):
        if isinstance(param, QgsProcessingDestinationParameter):
            return True

        # yay, use new API!
        return False

    def __init__(self, alg, paramType=None, param=None):
        self.alg = alg
        self.paramType = paramType
        self.param = param
        QDialog.__init__(self)
        self.setModal(True)
        self.setupUi()
        settings = QgsSettings()
        self.restoreGeometry(
            settings.value(
                "/Processing/modelParametersDefinitionDialogGeometry",
                QByteArray()))

    def closeEvent(self, event):
        settings = QgsSettings()
        settings.setValue(
            "/Processing/modelParametersDefinitionDialogGeometry",
            self.saveGeometry())
        super(ModelerParameterDefinitionDialog, self).closeEvent(event)

    def switchToCommentTab(self):
        self.tab.setCurrentIndex(1)
        self.commentEdit.setFocus()
        self.commentEdit.selectAll()

    def setupUi(self):
        type_metadata = QgsApplication.processingRegistry().parameterType(
            self.param.type() if self.param else self.paramType)
        self.setWindowTitle(
            self.tr('{} Parameter Definition').format(type_metadata.name()))

        self.mainLayout = QVBoxLayout()
        self.tab = QTabWidget()
        self.mainLayout.addWidget(self.tab)

        self.setMinimumWidth(300)

        self.verticalLayout = QVBoxLayout()

        self.label = QLabel(self.tr('Parameter name'))
        self.verticalLayout.addWidget(self.label)
        self.nameTextBox = QLineEdit()
        self.verticalLayout.addWidget(self.nameTextBox)

        if isinstance(self.param, QgsProcessingParameterDefinition):
            self.nameTextBox.setText(self.param.description())

        if isinstance(self.param, QgsProcessingDestinationParameter):
            self.verticalLayout.addWidget(QLabel(self.tr('Default value')))
            self.defaultWidget = QgsProcessingLayerOutputDestinationWidget(
                self.param, defaultSelection=True)
            self.verticalLayout.addWidget(self.defaultWidget)

        self.verticalLayout.addSpacing(20)
        self.requiredCheck = QCheckBox()
        self.requiredCheck.setText(self.tr('Mandatory'))
        self.requiredCheck.setChecked(True)
        if self.param is not None:
            self.requiredCheck.setChecked(
                not self.param.flags()
                & QgsProcessingParameterDefinition.FlagOptional)
        self.verticalLayout.addWidget(self.requiredCheck)

        self.advancedCheck = QCheckBox()
        self.advancedCheck.setText(self.tr('Advanced'))
        self.advancedCheck.setChecked(False)
        if self.param is not None:
            self.advancedCheck.setChecked(
                self.param.flags()
                & QgsProcessingParameterDefinition.FlagAdvanced)
        self.verticalLayout.addWidget(self.advancedCheck)

        # If child algorithm output is mandatory, disable checkbox
        if isinstance(self.param, QgsProcessingDestinationParameter):
            child = self.alg.childAlgorithms()[self.param.metadata()
                                               ['_modelChildId']]
            model_output = child.modelOutput(
                self.param.metadata()['_modelChildOutputName'])
            param_def = child.algorithm().parameterDefinition(
                model_output.childOutputName())
            if not (param_def.flags()
                    & QgsProcessingParameterDefinition.FlagOptional):
                self.requiredCheck.setEnabled(False)
                self.requiredCheck.setChecked(True)

            self.advancedCheck.setEnabled(False)
            self.advancedCheck.setChecked(False)

        self.verticalLayout.addStretch()

        w = QWidget()
        w.setLayout(self.verticalLayout)
        self.tab.addTab(w, self.tr('Properties'))

        self.commentLayout = QVBoxLayout()
        self.commentEdit = QTextEdit()
        self.commentEdit.setAcceptRichText(False)
        self.commentLayout.addWidget(self.commentEdit, 1)

        hl = QHBoxLayout()
        hl.setContentsMargins(0, 0, 0, 0)
        hl.addWidget(QLabel(self.tr('Color')))
        self.comment_color_button = QgsColorButton()
        self.comment_color_button.setAllowOpacity(True)
        self.comment_color_button.setWindowTitle(self.tr('Comment Color'))
        self.comment_color_button.setShowNull(True, self.tr('Default'))
        hl.addWidget(self.comment_color_button)
        self.commentLayout.addLayout(hl)

        w2 = QWidget()
        w2.setLayout(self.commentLayout)
        self.tab.addTab(w2, self.tr('Comments'))

        self.buttonBox = QDialogButtonBox(self)
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel
                                          | QDialogButtonBox.Ok)
        self.buttonBox.setObjectName('buttonBox')
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

        self.mainLayout.addWidget(self.buttonBox)

        self.setLayout(self.mainLayout)

    def setComments(self, text):
        self.commentEdit.setPlainText(text)

    def comments(self):
        return self.commentEdit.toPlainText()

    def setCommentColor(self, color):
        if color.isValid():
            self.comment_color_button.setColor(color)
        else:
            self.comment_color_button.setToNull()

    def commentColor(self):
        return self.comment_color_button.color(
        ) if not self.comment_color_button.isNull() else QColor()

    def accept(self):
        description = self.nameTextBox.text()
        if description.strip() == '':
            QMessageBox.warning(self, self.tr('Unable to define parameter'),
                                self.tr('Invalid parameter name'))
            return

        validChars = \
            'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
        safeName = ''.join(c for c in description if c in validChars)
        name = safeName.lower()

        # Destination parameter
        if (isinstance(self.param, QgsProcessingParameterFeatureSink)):
            self.param = QgsProcessingParameterFeatureSink(
                name=name,
                description=description,
                type=self.param.dataType(),
                defaultValue=self.defaultWidget.value())
        elif (isinstance(self.param, QgsProcessingParameterFileDestination)):
            self.param = QgsProcessingParameterFileDestination(
                name=name,
                description=description,
                fileFilter=self.param.fileFilter(),
                defaultValue=self.defaultWidget.value())
        elif (isinstance(self.param, QgsProcessingParameterFolderDestination)):
            self.param = QgsProcessingParameterFolderDestination(
                name=name,
                description=description,
                defaultValue=self.defaultWidget.value())
        elif (isinstance(self.param, QgsProcessingParameterRasterDestination)):
            self.param = QgsProcessingParameterRasterDestination(
                name=name,
                description=description,
                defaultValue=self.defaultWidget.value())
        elif (isinstance(self.param, QgsProcessingParameterVectorDestination)):
            self.param = QgsProcessingParameterVectorDestination(
                name=name,
                description=description,
                type=self.param.dataType(),
                defaultValue=self.defaultWidget.value())

        else:
            if self.paramType:
                typeId = self.paramType
            else:
                typeId = self.param.type()

            paramTypeDef = QgsApplication.instance().processingRegistry(
            ).parameterType(typeId)
            if not paramTypeDef:
                msg = self.tr(
                    'The parameter `{}` is not registered, are you missing a required plugin?'
                    .format(typeId))
                raise UndefinedParameterException(msg)
            self.param = paramTypeDef.create(name)
            self.param.setDescription(description)
            self.param.setMetadata(paramTypeDef.metadata())

        if not self.requiredCheck.isChecked():
            self.param.setFlags(
                self.param.flags()
                | QgsProcessingParameterDefinition.FlagOptional)
        else:
            self.param.setFlags(
                self.param.flags()
                & ~QgsProcessingParameterDefinition.FlagOptional)

        if self.advancedCheck.isChecked():
            self.param.setFlags(
                self.param.flags()
                | QgsProcessingParameterDefinition.FlagAdvanced)
        else:
            self.param.setFlags(
                self.param.flags()
                & ~QgsProcessingParameterDefinition.FlagAdvanced)

        settings = QgsSettings()
        settings.setValue(
            "/Processing/modelParametersDefinitionDialogGeometry",
            self.saveGeometry())

        QDialog.accept(self)

    def reject(self):
        self.param = None

        settings = QgsSettings()
        settings.setValue(
            "/Processing/modelParametersDefinitionDialogGeometry",
            self.saveGeometry())

        QDialog.reject(self)
Ejemplo n.º 34
0
class CalculatorModelerParametersDialog(ModelerParametersDialog):
    def setupUi(self):
        self.valueItems = {}
        self.dependentItems = {}
        self.resize(650, 450)
        self.buttonBox = QDialogButtonBox()
        self.buttonBox.setOrientation(Qt.Horizontal)
        self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel
                                          | QDialogButtonBox.Ok)
        self.infoText = QTextEdit()
        numbers = self.getAvailableValuesOfType(ParameterNumber, OutputNumber)
        text = self.tr(
            'You can refer to model values in your formula, using '
            'single-letter variables, as follows:\n',
            'CalculatorModelerParametersDialog')
        ichar = 97
        if numbers:
            for number in numbers:
                text += chr(ichar) + '->' + self.resolveValueDescription(
                    number) + '\n'
                ichar += 1
        else:
            text += self.tr('\n - No numerical variables are available.',
                            'CalculatorModelerParametersDialog')
        self.infoText.setText(text)
        self.infoText.setEnabled(False)
        self.formulaText = QLineEdit()
        if hasattr(self.formulaText, 'setPlaceholderText'):
            self.formulaText.setPlaceholderText(
                self.tr('[Enter your formula here]',
                        'CalculatorModelerParametersDialog'))
        if self._algName is not None:
            alg = self.model.algs[self._algName]
            self.formulaText.setText(alg.params[FORMULA])
        self.setWindowTitle(
            self.tr('Calculator', 'CalculatorModelerParametersDialog'))
        self.verticalLayout = QVBoxLayout()
        self.verticalLayout.setSpacing(2)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.addWidget(self.infoText)
        self.verticalLayout.addWidget(self.formulaText)
        self.verticalLayout.addWidget(self.buttonBox)
        self.setLayout(self.verticalLayout)
        self.buttonBox.accepted.connect(self.okPressed)
        self.buttonBox.rejected.connect(self.cancelPressed)
        QMetaObject.connectSlotsByName(self)

    def createAlgorithm(self):
        alg = Algorithm('modelertools:calculator')
        alg.setName(self.model)
        alg.description = self.tr('Calculator',
                                  'CalculatorModelerParametersDialog')

        formula = self.formulaText.text()
        alg.params[FORMULA] = formula

        for i in xrange(AVAILABLE_VARIABLES):
            paramname = NUMBER + unicode(i)
            alg.params[paramname] = None

        numbers = self.getAvailableValuesOfType(ParameterNumber, OutputNumber)
        used = []
        for i in range(len(numbers)):
            if unicode(chr(i + 97)) in formula:
                used.append(numbers[i])

        for i, variable in enumerate(used):
            paramname = NUMBER + unicode(i)
            alg.params[paramname] = variable

        # TODO check formula is correct
        return alg
Ejemplo n.º 35
0
    def construct_form_param_system(self, row, pos):

        widget = None
        for field in row[pos]['fields']:
            if field['label']:
                lbl = QLabel()
                lbl.setObjectName('lbl' + field['widgetname'])
                lbl.setText(field['label'])
                lbl.setMinimumSize(160, 0)
                lbl.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
                lbl.setToolTip(field['tooltip'])

                if field['widgettype'] == 'text' or field[
                        'widgettype'] == 'linetext':
                    widget = QLineEdit()
                    widget.setText(field['value'])
                    widget.editingFinished.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'textarea':
                    widget = QTextEdit()
                    widget.setText(field['value'])
                    widget.editingFinished.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'combo':
                    widget = QComboBox()
                    self.populate_combo(widget, field)
                    widget.currentIndexChanged.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'checkbox' or field[
                        'widgettype'] == 'check':
                    widget = QCheckBox()
                    if field['value'] in ('true', 'True', 'TRUE', True):
                        widget.setChecked(True)
                    elif field['value'] in ('false', 'False', 'FALSE', False):
                        widget.setChecked(False)
                    widget.stateChanged.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
                elif field['widgettype'] == 'datetime':
                    widget = QDateEdit()
                    widget.setCalendarPopup(True)
                    if field['value']:
                        field['value'] = field['value'].replace('/', '-')
                    date = QDate.fromString(field['value'], 'yyyy-MM-dd')
                    widget.setDate(date)
                    widget.dateChanged.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'spinbox':
                    widget = QSpinBox()
                    if 'value' in field and field['value'] is not None:
                        value = float(str(field['value']))
                        widget.setValue(value)
                    widget.valueChanged.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                else:
                    pass

                if widget:
                    widget.setObjectName(field['widgetname'])
                else:
                    pass

                # Order Widgets
                if 'layoutname' in field:
                    if field['layoutname'] == 'lyt_topology':
                        self.order_widgets_system(field, self.topology_form,
                                                  lbl, widget)
                    elif field['layoutname'] == 'lyt_builder':
                        self.order_widgets_system(field, self.builder_form,
                                                  lbl, widget)
                    elif field['layoutname'] == 'lyt_review':
                        self.order_widgets_system(field, self.review_form, lbl,
                                                  widget)
                    elif field['layoutname'] == 'lyt_analysis':
                        self.order_widgets_system(field, self.analysis_form,
                                                  lbl, widget)
                    elif field['layoutname'] == 'lyt_system':
                        self.order_widgets_system(field, self.system_form, lbl,
                                                  widget)
Ejemplo n.º 36
0
    def construct_form_param_user(self, row, pos):

        widget = None
        for field in row[pos]['fields']:
            if field['label']:
                lbl = QLabel()
                lbl.setObjectName('lbl' + field['widgetname'])
                lbl.setText(field['label'])
                lbl.setMinimumSize(160, 0)
                lbl.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
                lbl.setToolTip(field['tooltip'])

                chk = QCheckBox()
                chk.setObjectName('chk_' + field['widgetname'])

                if field['checked'] in ('true', 'True', 'TRUE', True):
                    chk.setChecked(True)
                elif field['checked'] in ('false', 'False', 'FALSE', False):
                    chk.setChecked(False)
                chk.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

                if field['widgettype'] == 'text' or field[
                        'widgettype'] == 'linetext' or field[
                            'widgettype'] == 'typeahead':
                    widget = QLineEdit()
                    widget.setText(field['value'])
                    widget.editingFinished.connect(
                        partial(self.get_values_changed_param_user, chk,
                                widget, field))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                    if field['widgettype'] == 'typeahead':
                        completer = QCompleter()
                        if 'dv_querytext' in field or 'dv_querytext_filterc' in field:
                            widget.setProperty('typeahead', True)
                            model = QStringListModel()
                            widget.textChanged.connect(
                                partial(self.populate_typeahead, completer,
                                        model, field, self.dlg_config, widget))

                elif field['widgettype'] == 'textarea':
                    widget = QTextEdit()
                    widget.setText(field['value'])
                    widget.editingFinished.connect(
                        partial(self.get_values_changed_param_user, chk,
                                widget, field))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'combo':
                    widget = QComboBox()
                    self.populate_combo(widget, field)
                    widget.currentIndexChanged.connect(
                        partial(self.get_values_changed_param_user, chk,
                                widget, field))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'check':
                    widget = chk
                    widget.stateChanged.connect(
                        partial(self.get_values_changed_param_user, chk, chk,
                                field))
                    widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
                elif field['widgettype'] == 'datetime':
                    widget = QgsDateTimeEdit()
                    widget.setAllowNull(True)
                    widget.setCalendarPopup(True)
                    widget.setDisplayFormat('dd/MM/yyyy')

                    if field['value']:
                        field['value'] = field['value'].replace('/', '-')
                    date = QDate.fromString(field['value'], 'yyyy-MM-dd')
                    if date:
                        widget.setDate(date)
                    else:
                        widget.clear()
                    widget.dateChanged.connect(
                        partial(self.get_values_changed_param_user, chk,
                                widget, field))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'spinbox':
                    widget = QDoubleSpinBox()
                    if 'value' in field and field['value'] is not None:
                        value = float(str(field['value']))
                        widget.setValue(value)
                    widget.valueChanged.connect(
                        partial(self.get_values_changed_param_user, chk,
                                widget, field))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                else:
                    pass

                widget.setObjectName(field['widgetname'])

                # Set signals
                chk.stateChanged.connect(
                    partial(self.get_values_checked_param_user, chk, widget,
                            field))

                if field['layoutname'] == 'lyt_basic':
                    self.order_widgets(field, self.basic_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_om':
                    self.order_widgets(field, self.om_form, lbl, chk, widget)
                elif field['layoutname'] == 'lyt_inventory':
                    self.order_widgets(field, self.inventory_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_mapzones':
                    self.order_widgets(field, self.mapzones_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_edit':
                    self.order_widgets(field, self.cad_form, lbl, chk, widget)
                elif field['layoutname'] == 'lyt_epa':
                    self.order_widgets(field, self.epa_form, lbl, chk, widget)
                elif field['layoutname'] == 'lyt_masterplan':
                    self.order_widgets(field, self.masterplan_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_other':
                    self.order_widgets(field, self.other_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_node_vdef':
                    self.order_widgets(field, self.node_type_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_arc_vdef':
                    self.order_widgets(field, self.cat_form, lbl, chk, widget)
                elif field['layoutname'] == 'lyt_utils_vdef':
                    self.order_widgets(field, self.utils_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_connec_vdef':
                    self.order_widgets(field, self.connec_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_gully_vdef':
                    self.order_widgets(field, self.gully_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_fluid_type':
                    self.order_widgets(field, self.fluid_type_form, lbl, chk,
                                       widget)
                elif field['layoutname'] == 'lyt_location_type':
                    self.order_widgets(field, self.location_type_form, lbl,
                                       chk, widget)
                elif field['layoutname'] == 'lyt_category_type':
                    self.order_widgets(field, self.category_type_form, lbl,
                                       chk, widget)
                elif field['layoutname'] == 'lyt_function_type':
                    self.order_widgets(field, self.function_type_form, lbl,
                                       chk, widget)
                elif field['layoutname'] == 'lyt_addfields':
                    self.order_widgets(field, self.addfields_form, lbl, chk,
                                       widget)
Ejemplo n.º 37
0
class ModelerParametersWidget(QWidget):

    def __init__(self, alg, model, algName=None, configuration=None, dialog=None, context=None):
        super().__init__()
        self._alg = alg  # The algorithm to define in this dialog. It is an instance of QgsProcessingAlgorithm
        self.model = model  # The model this algorithm is going to be added to. It is an instance of QgsProcessingModelAlgorithm
        self.childId = algName  # The name of the algorithm in the model, in case we are editing it and not defining it for the first time
        self.configuration = configuration
        self.context = context
        self.dialog = dialog

        self.widget = ModelerParametersPanelWidget(alg, model, algName, configuration, dialog, context)

        class ContextGenerator(QgsProcessingContextGenerator):

            def __init__(self, context):
                super().__init__()
                self.processing_context = context

            def processingContext(self):
                return self.processing_context

        self.context_generator = ContextGenerator(self.context)

        self.setupUi()
        self.params = None

    def algorithm(self):
        return self._alg

    def switchToCommentTab(self):
        self.tab.setCurrentIndex(1)
        self.commentEdit.setFocus()
        self.commentEdit.selectAll()

    def setupUi(self):
        self.mainLayout = QVBoxLayout()
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.tab = QTabWidget()
        self.mainLayout.addWidget(self.tab)

        self.param_widget = QgsPanelWidgetStack()
        self.widget.setDockMode(True)
        self.param_widget.setMainPanel(self.widget)

        self.tab.addTab(self.param_widget, self.tr('Properties'))

        self.commentLayout = QVBoxLayout()
        self.commentEdit = QTextEdit()
        self.commentEdit.setAcceptRichText(False)
        self.commentLayout.addWidget(self.commentEdit, 1)

        hl = QHBoxLayout()
        hl.setContentsMargins(0, 0, 0, 0)
        hl.addWidget(QLabel(self.tr('Color')))
        self.comment_color_button = QgsColorButton()
        self.comment_color_button.setAllowOpacity(True)
        self.comment_color_button.setWindowTitle(self.tr('Comment Color'))
        self.comment_color_button.setShowNull(True, self.tr('Default'))
        hl.addWidget(self.comment_color_button)
        self.commentLayout.addLayout(hl)

        w2 = QWidget()
        w2.setLayout(self.commentLayout)
        self.tab.addTab(w2, self.tr('Comments'))

        self.setLayout(self.mainLayout)

    def setComments(self, text):
        self.commentEdit.setPlainText(text)

    def comments(self):
        return self.commentEdit.toPlainText()

    def setCommentColor(self, color):
        if color.isValid():
            self.comment_color_button.setColor(color)
        else:
            self.comment_color_button.setToNull()

    def commentColor(self):
        return self.comment_color_button.color() if not self.comment_color_button.isNull() else QColor()

    def getAvailableDependencies(self):
        return self.widget.getAvailableDependencies()

    def getDependenciesPanel(self):
        return self.widget.getDependenciesPanel()

    def getAvailableValuesOfType(self, paramType, outTypes=[], dataTypes=[]):
        return self.widget.getAvailableValuesOfType(paramType, outTypes, dataTypes)

    def resolveValueDescription(self, value):
        return self.widget.resolveValueDescription(value)

    def setPreviousValues(self):
        self.widget.setPreviousValues()

    def createAlgorithm(self):
        alg = self.widget.createAlgorithm()
        if alg:
            alg.comment().setDescription(self.comments())
            alg.comment().setColor(self.commentColor())
        return alg
Ejemplo n.º 38
0
 def __init__(self, commit, tags):
     QTextEdit.__init__(self)
     self.setWordWrap(False)
     self.tags = tags
     self.commit = commit
     self.updateText()
Ejemplo n.º 39
0
    def construct_form_param_system(self, row, pos):

        widget = None
        for field in row[pos]['fields']:
            if field['label']:
                lbl = QLabel()
                lbl.setObjectName('lbl' + field['widgetname'])
                lbl.setText(field['label'])
                lbl.setMinimumSize(160, 0)
                lbl.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
                lbl.setToolTip(field['tooltip'])

                if field['widgettype'] == 'text' or field[
                        'widgettype'] == 'linetext':
                    widget = QLineEdit()
                    widget.setText(field['value'])
                    widget.editingFinished.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'textarea':
                    widget = QTextEdit()
                    widget.setText(field['value'])
                    widget.editingFinished.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'combo':
                    widget = QComboBox()
                    self.populate_combo(widget, field)
                    widget.currentIndexChanged.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'checkbox' or field[
                        'widgettype'] == 'check':
                    widget = QCheckBox()
                    if field['value'].lower() == 'true':
                        widget.setChecked(True)
                    elif field['value'].lower() == 'FALSE':
                        widget.setChecked(False)
                    widget.stateChanged.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
                elif field['widgettype'] == 'datepickertime':
                    widget = QDateEdit()
                    widget.setCalendarPopup(True)
                    date = QDate.fromString(field['value'], 'yyyy/MM/dd')
                    widget.setDate(date)
                    widget.dateChanged.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                elif field['widgettype'] == 'spinbox':
                    widget = QDoubleSpinBox()
                    if 'value' in field and field['value'] is not None:
                        value = float(str(field['value']))
                        widget.setValue(value)
                    widget.valueChanged.connect(
                        partial(self.get_values_changed_param_system, widget))
                    widget.setSizePolicy(QSizePolicy.Expanding,
                                         QSizePolicy.Fixed)
                else:
                    pass

                if widget:
                    widget.setObjectName(field['widgetname'])
                else:
                    pass

                # Order Widgets
                if field['layout_id'] == 1:
                    self.order_widgets_system(field, self.basic_form, lbl,
                                              widget)
                elif field['layout_id'] == 2:
                    self.order_widgets_system(field, self.om_form, lbl, widget)
                elif field['layout_id'] == 3:
                    self.order_widgets_system(field, self.workcat_form, lbl,
                                              widget)
                elif field['layout_id'] == 4:
                    self.order_widgets_system(field, self.mapzones_form, lbl,
                                              widget)
                elif field['layout_id'] == 5:
                    self.order_widgets_system(field, self.cad_form, lbl,
                                              widget)
                elif field['layout_id'] == 6:
                    self.order_widgets_system(field, self.epa_form, lbl,
                                              widget)
                elif field['layout_id'] == 7:
                    self.order_widgets_system(field, self.masterplan_form, lbl,
                                              widget)
                elif field['layout_id'] == 8:
                    self.order_widgets_system(field, self.other_form, lbl,
                                              widget)
                elif field['layout_id'] == 9:
                    self.order_widgets_system(field, self.node_type_form, lbl,
                                              widget)
                elif field['layout_id'] == 10:
                    self.order_widgets_system(field, self.cat_form, lbl,
                                              widget)
                elif field['layout_id'] == 11:
                    self.order_widgets_system(field, self.utils_form, lbl,
                                              widget)
                elif field['layout_id'] == 12:
                    self.order_widgets_system(field, self.connec_form, lbl,
                                              widget)
                elif field['layout_id'] == 13:
                    self.order_widgets_system(field, self.topology_form, lbl,
                                              widget)
                elif field['layout_id'] == 14:
                    self.order_widgets_system(field, self.builder_form, lbl,
                                              widget)
                elif field['layout_id'] == 15:
                    self.order_widgets_system(field, self.review_form, lbl,
                                              widget)
                elif field['layout_id'] == 16:
                    self.order_widgets_system(field, self.analysis_form, lbl,
                                              widget)
                elif field['layout_id'] == 17:
                    self.order_widgets_system(field, self.system_form, lbl,
                                              widget)
 def __init__(self, commit, tags):
     QTextEdit.__init__(self)
     self.setWordWrap(False)
     self.tags = tags
     self.commit = commit
     self.updateText()