[processing] Use ExpressionWidgetWrapperMixin in StringWidgetWrapper

This commit is contained in:
arnaud.morvan@camptocamp.com 2017-02-10 10:10:32 +01:00
parent 7bffef7044
commit 337d1889b8
2 changed files with 14 additions and 87 deletions

View File

@ -1,74 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
NumberInputPanel.py
---------------------
Date : August 2016
Copyright : (C) 2016 by Victor Olaya
Email : volayaf 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. *
* *
***************************************************************************
"""
from builtins import str
__author__ = 'Victor Olaya'
__date__ = 'August 2016'
__copyright__ = '(C) 2016, Victor Olaya'
# 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.QtCore import pyqtSignal
from qgis.PyQt.QtWidgets import QDialog
from qgis.core import (QgsDataSourceUri,
QgsCredentials,
QgsExpression,
QgsRasterLayer)
from qgis.gui import QgsEncodingFileDialog, QgsExpressionBuilderDialog
from qgis.utils import iface
pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'widgetBaseSelector.ui'))
class StringInputPanel(BASE, WIDGET):
hasChanged = pyqtSignal()
def __init__(self, param):
super(StringInputPanel, self).__init__(None)
self.setupUi(self)
self.param = param
self.text = param.default
self.btnSelect.clicked.connect(self.showExpressionsBuilder)
self.leText.textChanged.connect(lambda: self.hasChanged.emit())
def showExpressionsBuilder(self):
context = self.param.expressionContext()
dlg = QgsExpressionBuilderDialog(None, self.leText.text(), self, 'generic', context)
dlg.setWindowTitle(self.tr('Expression based input'))
if dlg.exec_() == QDialog.Accepted:
exp = QgsExpression(dlg.expressionText())
if not exp.hasParserError():
self.setValue(dlg.expressionText())
def getValue(self):
return self.leText.text()
def setValue(self, value):
self.leText.setText(str(value))

View File

@ -88,7 +88,6 @@ from processing.gui.MultipleInputPanel import MultipleInputPanel
from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel
from processing.gui.FixedTablePanel import FixedTablePanel
from processing.gui.ExtentSelectionPanel import ExtentSelectionPanel
from processing.gui.StringInputPanel import StringInputPanel
DIALOG_STANDARD = 'standard'
@ -837,22 +836,19 @@ class VectorWidgetWrapper(WidgetWrapper):
return self.comboValue(validator, combobox=self.combo)
class StringWidgetWrapper(WidgetWrapper):
class StringWidgetWrapper(WidgetWrapper, ExpressionWidgetWrapperMixin):
def createWidget(self):
if self.dialogType == DIALOG_STANDARD:
if self.param.multiline:
widget = QPlainTextEdit()
if self.param.default:
widget.setPlainText(self.param.default)
else:
widget = StringInputPanel(self.param)
if self.param.default:
widget.setValue(self.param.default)
self._lineedit = QLineEdit()
return self.wrapWithExpressionButton(self._lineedit)
elif self.dialogType == DIALOG_BATCH:
widget = QLineEdit()
if self.param.default:
widget.setText(self.param.default)
else:
# strings, numbers, files and table fields are all allowed input types
strings = self.dialog.getAvailableValuesOfType([ParameterString, ParameterNumber, ParameterFile,
@ -860,20 +856,23 @@ class StringWidgetWrapper(WidgetWrapper):
options = [(self.dialog.resolveValueDescription(s), s) for s in strings]
if self.param.multiline:
widget = MultilineTextPanel(options)
widget.setText(self.param.default or "")
else:
widget = QComboBox()
widget.setEditable(True)
for desc, val in options:
widget.addItem(desc, val)
widget.setEditText(self.param.default or "")
return widget
def setValue(self, value):
if self.dialogType == DIALOG_STANDARD:
pass # TODO
if self.param.multiline:
self.widget.setPlainText(value)
else:
self._lineedit.setText(value)
elif self.dialogType == DIALOG_BATCH:
self.widget.setText(value)
else:
if self.param.multiline:
self.widget.setValue(value)
@ -885,10 +884,12 @@ class StringWidgetWrapper(WidgetWrapper):
if self.param.multiline:
text = self.widget.toPlainText()
else:
text = self.widget.getValue()
text = self._lineedit.text()
return text
elif self.dialogType == DIALOG_BATCH:
return self.widget.text()
else:
if self.param.multiline:
value = self.widget.getValue()