-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6513 from pblottiere/executesql_params
[FEATURE][needs-docs] Add parameters to 'Execute SQL' algorithm
- Loading branch information
Showing
13 changed files
with
397 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
python/plugins/processing/algs/qgis/ui/ExecuteSQLWidget.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
ExecuteSQLWidget.py | ||
--------------------- | ||
Date : November 2017 | ||
Copyright : (C) 2017 by Paul Blottiere | ||
Email : blottiere dot paul at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
*************************************************************************** | ||
""" | ||
|
||
__author__ = 'Paul Blottiere' | ||
__date__ = 'November 2018' | ||
__copyright__ = '(C) 2018, Paul Blottiere' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
import os | ||
|
||
from qgis.PyQt import uic | ||
from qgis.PyQt.QtWidgets import QTreeWidgetItem | ||
from qgis.PyQt.QtCore import Qt | ||
|
||
from qgis.core import (QgsApplication, | ||
QgsExpressionContextScope, | ||
QgsProcessingParameterString, | ||
QgsProcessingParameterNumber, | ||
QgsExpression, | ||
QgsProcessingModelChildParameterSource, | ||
QgsProcessingParameterFile, | ||
QgsProcessingParameterField, | ||
QgsProcessingOutputString, | ||
QgsProcessingParameterExpression, | ||
QgsProcessingOutputFile) | ||
|
||
from qgis.gui import QgsFieldExpressionWidget | ||
|
||
from processing.gui.wrappers import (WidgetWrapper, | ||
dialogTypes, | ||
DIALOG_MODELER) | ||
|
||
pluginPath = os.path.dirname(__file__) | ||
WIDGET, BASE = uic.loadUiType(os.path.join(pluginPath, 'ExecuteSQLWidgetBase.ui')) | ||
|
||
|
||
class ExecuteSQLWidget(BASE, WIDGET): | ||
|
||
def __init__(self, dialog): | ||
super(ExecuteSQLWidget, self).__init__(None) | ||
self.setupUi(self) | ||
self.dialog = dialog | ||
self.dialogType = dialogTypes[dialog.__class__.__name__] | ||
|
||
self.mExpressionWidget = QgsFieldExpressionWidget() | ||
|
||
# add model parameters in context scope if called from modeler | ||
if self.dialogType == DIALOG_MODELER: | ||
strings = dialog.getAvailableValuesOfType( | ||
[QgsProcessingParameterString, QgsProcessingParameterNumber], []) | ||
model_params = [dialog.resolveValueDescription(s) for s in strings] | ||
|
||
scope = QgsExpressionContextScope() | ||
for param in model_params: | ||
var = QgsExpressionContextScope.StaticVariable(param) | ||
scope.addVariable(var) | ||
|
||
self.mExpressionWidget.appendScope(scope) | ||
|
||
self.mHLayout.insertWidget(0, self.mExpressionWidget) | ||
|
||
self.mInsert.clicked.connect(self.insert) | ||
|
||
def insert(self): | ||
if self.mExpressionWidget.currentText(): | ||
exp = '[% {} %]'.format(self.mExpressionWidget.currentText()) | ||
self.mText.insertPlainText(exp) | ||
|
||
def setValue(self, value): | ||
text = value | ||
|
||
if self.dialogType == DIALOG_MODELER: | ||
if isinstance(value, list): | ||
for v in value: | ||
if isinstance(v, QgsProcessingModelChildParameterSource) \ | ||
and v.source() == QgsProcessingModelChildParameterSource.ExpressionText: | ||
text = v.expressionText() | ||
|
||
self.mText.setPlainText(text) | ||
|
||
def value(self): | ||
value = self.mText.toPlainText() | ||
|
||
if self.dialogType == DIALOG_MODELER: | ||
expression_values = self._expressionValues(value) | ||
if len(expression_values) > 1: | ||
value = expression_values | ||
|
||
return value | ||
|
||
def _expressionValues(self, text): | ||
strings = self.dialog.getAvailableValuesOfType( | ||
[QgsProcessingParameterString, QgsProcessingParameterNumber], []) | ||
model_params = [(self.dialog.resolveValueDescription(s), s) for s in strings] | ||
|
||
variables = QgsExpression.referencedVariables(text) | ||
expression_values = [] | ||
expression_values.append(QgsProcessingModelChildParameterSource.fromExpressionText(text)) | ||
|
||
for k, v in model_params: | ||
if k in variables: | ||
expression_values.append(v) | ||
|
||
return expression_values | ||
|
||
|
||
class ExecuteSQLWidgetWrapper(WidgetWrapper): | ||
|
||
def createWidget(self): | ||
return ExecuteSQLWidget(self.dialog) | ||
|
||
def setValue(self, value): | ||
self.widget.setValue(value) | ||
|
||
def value(self): | ||
return self.widget.value() |
48 changes: 48 additions & 0 deletions
48
python/plugins/processing/algs/qgis/ui/ExecuteSQLWidgetBase.ui
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>Form</class> | ||
<widget class="QWidget" name="Form"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Form</string> | ||
</property> | ||
<layout class="QGridLayout" name="gridLayout"> | ||
<item row="3" column="0" rowspan="2" colspan="2"> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<property name="topMargin"> | ||
<number>10</number> | ||
</property> | ||
<item> | ||
<widget class="QPlainTextEdit" name="mText"/> | ||
</item> | ||
<item> | ||
<layout class="QHBoxLayout" name="mHLayout"> | ||
<property name="spacing"> | ||
<number>6</number> | ||
</property> | ||
<property name="topMargin"> | ||
<number>0</number> | ||
</property> | ||
<item> | ||
<widget class="QPushButton" name="mInsert"> | ||
<property name="text"> | ||
<string>Insert</string> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</item> | ||
</layout> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |
Oops, something went wrong.