2012-10-05 23:28:47 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
***************************************************************************
|
|
|
|
|
ParametersPanel.py
|
|
|
|
|
---------------------
|
|
|
|
|
Date : August 2012
|
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
2014-01-17 19:01:08 +01:00
|
|
|
|
(C) 2013 by CS Systemes d'information (CS SI)
|
2012-10-05 23:28:47 +02:00
|
|
|
|
Email : volayaf at gmail dot com
|
2014-01-17 19:01:08 +01:00
|
|
|
|
otb at c-s dot fr (CS SI)
|
2014-04-06 12:27:51 +02:00
|
|
|
|
Contributors : Victor Olaya
|
2016-01-18 14:16:31 +01:00
|
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
|
***************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* 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__ = 'Victor Olaya'
|
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
import os
|
2016-01-18 14:16:31 +01:00
|
|
|
|
|
2017-05-15 16:02:45 +10:00
|
|
|
|
from qgis.core import (QgsProcessingParameterDefinition,
|
|
|
|
|
QgsProcessingParameterExtent,
|
|
|
|
|
QgsProcessingParameterPoint,
|
2017-06-05 11:15:17 +10:00
|
|
|
|
QgsProcessingParameterFeatureSource,
|
2017-05-15 16:16:32 +10:00
|
|
|
|
QgsProcessingOutputVectorLayer,
|
2017-06-06 14:38:24 +10:00
|
|
|
|
QgsProcessingOutputRasterLayer,
|
2017-07-08 15:25:45 +10:00
|
|
|
|
QgsProcessingParameterRasterDestination,
|
2017-06-22 18:21:33 +10:00
|
|
|
|
QgsProcessingParameterFeatureSink,
|
2017-11-29 15:09:19 +10:00
|
|
|
|
QgsProcessingParameterVectorDestination,
|
|
|
|
|
QgsProject)
|
2016-04-29 11:39:26 +02:00
|
|
|
|
from qgis.PyQt import uic
|
2017-07-03 09:13:30 +02:00
|
|
|
|
from qgis.PyQt.QtCore import QCoreApplication, Qt
|
2017-03-04 19:41:23 +01:00
|
|
|
|
from qgis.PyQt.QtWidgets import (QWidget, QHBoxLayout, QToolButton,
|
2017-12-27 10:50:52 +07:00
|
|
|
|
QLabel, QCheckBox, QSizePolicy)
|
2016-04-22 10:38:48 +02:00
|
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2013-02-01 13:09:39 +04:00
|
|
|
|
|
2017-05-15 16:16:32 +10:00
|
|
|
|
from processing.gui.DestinationSelectionPanel import DestinationSelectionPanel
|
2017-05-15 16:02:45 +10:00
|
|
|
|
from processing.gui.wrappers import WidgetWrapperFactory
|
2017-06-22 18:21:33 +10:00
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
2015-05-18 21:04:20 +03:00
|
|
|
|
WIDGET, BASE = uic.loadUiType(
|
|
|
|
|
os.path.join(pluginPath, 'ui', 'widgetParametersPanel.ui'))
|
2015-05-18 19:51:26 +03:00
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
2015-05-18 21:04:20 +03:00
|
|
|
|
class ParametersPanel(BASE, WIDGET):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
2014-10-03 21:56:24 +03:00
|
|
|
|
NOT_SELECTED = QCoreApplication.translate('ParametersPanel', '[Not selected]')
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
|
|
def __init__(self, parent, alg):
|
2015-05-18 21:04:20 +03:00
|
|
|
|
super(ParametersPanel, self).__init__(None)
|
2014-11-12 11:42:29 +02:00
|
|
|
|
self.setupUi(self)
|
|
|
|
|
|
|
|
|
|
self.grpAdvanced.hide()
|
|
|
|
|
|
2017-12-27 10:50:52 +07:00
|
|
|
|
self.scrollAreaWidgetContents.setContentsMargins(4, 4, 4, 4)
|
2014-11-12 11:42:29 +02:00
|
|
|
|
self.layoutMain = self.scrollAreaWidgetContents.layout()
|
|
|
|
|
self.layoutAdvanced = self.grpAdvanced.layout()
|
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
self.parent = parent
|
2013-10-01 20:52:22 +03:00
|
|
|
|
self.alg = alg
|
2016-09-07 14:30:20 +02:00
|
|
|
|
self.wrappers = {}
|
2016-11-17 12:06:24 +01:00
|
|
|
|
self.outputWidgets = {}
|
2012-09-15 18:25:25 +03:00
|
|
|
|
self.checkBoxes = {}
|
|
|
|
|
self.dependentItems = {}
|
|
|
|
|
self.iterateButtons = {}
|
2014-11-12 11:42:29 +02:00
|
|
|
|
|
|
|
|
|
self.initWidgets()
|
|
|
|
|
|
2017-11-29 15:09:19 +10:00
|
|
|
|
QgsProject.instance().layerWasAdded.connect(self.layerRegistryChanged)
|
|
|
|
|
QgsProject.instance().layersWillBeRemoved.connect(self.layerRegistryChanged)
|
|
|
|
|
|
2016-09-07 14:30:20 +02:00
|
|
|
|
def layerRegistryChanged(self, layers):
|
2016-09-27 19:51:06 +02:00
|
|
|
|
for wrapper in list(self.wrappers.values()):
|
2016-09-07 14:30:20 +02:00
|
|
|
|
wrapper.refresh()
|
2016-01-18 14:16:31 +01:00
|
|
|
|
|
2017-08-07 17:23:23 +10:00
|
|
|
|
def formatParameterTooltip(self, parameter):
|
2017-08-11 17:26:38 +02:00
|
|
|
|
return '<p><b>{}</b></p><p>{}</p>'.format(
|
|
|
|
|
parameter.description(),
|
2018-03-09 18:31:10 +02:00
|
|
|
|
QCoreApplication.translate('ParametersPanel', 'Python identifier: ‘{}’').format('<i>{}</i>'.format(parameter.name()))
|
2017-08-11 17:26:38 +02:00
|
|
|
|
)
|
2017-08-07 17:23:23 +10:00
|
|
|
|
|
2014-11-12 11:42:29 +02:00
|
|
|
|
def initWidgets(self):
|
|
|
|
|
# If there are advanced parameters — show corresponding groupbox
|
2017-05-15 16:02:45 +10:00
|
|
|
|
for param in self.alg.parameterDefinitions():
|
|
|
|
|
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
|
2014-11-12 11:42:29 +02:00
|
|
|
|
self.grpAdvanced.show()
|
2013-09-15 13:52:46 +02:00
|
|
|
|
break
|
2014-11-12 11:42:29 +02:00
|
|
|
|
# Create widgets and put them in layouts
|
2017-05-15 16:02:45 +10:00
|
|
|
|
for param in self.alg.parameterDefinitions():
|
|
|
|
|
if param.flags() & QgsProcessingParameterDefinition.FlagHidden:
|
2013-09-15 13:52:46 +02:00
|
|
|
|
continue
|
2014-11-12 11:42:29 +02:00
|
|
|
|
|
2017-05-15 16:02:45 +10:00
|
|
|
|
if param.isDestination():
|
2017-05-15 16:16:32 +10:00
|
|
|
|
continue
|
2017-05-15 16:02:45 +10:00
|
|
|
|
else:
|
|
|
|
|
wrapper = WidgetWrapperFactory.create_wrapper(param, self.parent)
|
|
|
|
|
self.wrappers[param.name()] = wrapper
|
|
|
|
|
widget = wrapper.widget
|
|
|
|
|
|
|
|
|
|
if widget is not None:
|
2017-06-05 11:15:17 +10:00
|
|
|
|
if isinstance(param, QgsProcessingParameterFeatureSource):
|
2017-05-15 16:02:45 +10:00
|
|
|
|
layout = QHBoxLayout()
|
2017-12-27 10:50:52 +07:00
|
|
|
|
layout.setSpacing(6)
|
2017-05-15 16:02:45 +10:00
|
|
|
|
layout.setMargin(0)
|
|
|
|
|
layout.addWidget(widget)
|
|
|
|
|
button = QToolButton()
|
|
|
|
|
icon = QIcon(os.path.join(pluginPath, 'images', 'iterate.png'))
|
|
|
|
|
button.setIcon(icon)
|
2017-12-27 10:50:52 +07:00
|
|
|
|
button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
|
2017-06-12 13:39:42 +10:00
|
|
|
|
button.setToolTip(self.tr('Iterate over this layer, creating a separate output for every feature in the layer'))
|
2017-05-15 16:02:45 +10:00
|
|
|
|
button.setCheckable(True)
|
|
|
|
|
layout.addWidget(button)
|
2017-07-03 09:13:30 +02:00
|
|
|
|
layout.setAlignment(button, Qt.AlignTop)
|
2017-05-15 16:02:45 +10:00
|
|
|
|
self.iterateButtons[param.name()] = button
|
|
|
|
|
button.toggled.connect(self.buttonToggled)
|
|
|
|
|
widget = QWidget()
|
|
|
|
|
widget.setLayout(layout)
|
|
|
|
|
|
2017-12-01 17:19:36 +10:00
|
|
|
|
widget.setToolTip(param.toolTip())
|
2017-05-15 16:02:45 +10:00
|
|
|
|
|
2018-03-16 11:11:20 +01:00
|
|
|
|
if wrapper.label is not None:
|
2017-05-15 16:02:45 +10:00
|
|
|
|
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
|
2018-03-16 11:11:20 +01:00
|
|
|
|
self.layoutAdvanced.addWidget(wrapper.label)
|
2017-05-15 16:02:45 +10:00
|
|
|
|
else:
|
|
|
|
|
self.layoutMain.insertWidget(
|
2018-03-16 11:11:20 +01:00
|
|
|
|
self.layoutMain.count() - 2, wrapper.label)
|
|
|
|
|
else:
|
|
|
|
|
desc = param.description()
|
|
|
|
|
if isinstance(param, QgsProcessingParameterExtent):
|
|
|
|
|
desc += self.tr(' (xmin, xmax, ymin, ymax)')
|
|
|
|
|
if isinstance(param, QgsProcessingParameterPoint):
|
|
|
|
|
desc += self.tr(' (x, y)')
|
|
|
|
|
if param.flags() & QgsProcessingParameterDefinition.FlagOptional:
|
|
|
|
|
desc += self.tr(' [optional]')
|
|
|
|
|
widget.setText(desc)
|
2017-05-15 16:02:45 +10:00
|
|
|
|
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
|
|
|
|
|
self.layoutAdvanced.addWidget(widget)
|
2017-01-09 16:51:01 +10:00
|
|
|
|
else:
|
|
|
|
|
self.layoutMain.insertWidget(
|
2017-05-15 16:02:45 +10:00
|
|
|
|
self.layoutMain.count() - 2, widget)
|
2013-09-15 13:52:46 +02:00
|
|
|
|
|
2017-05-15 16:16:32 +10:00
|
|
|
|
for output in self.alg.destinationParameterDefinitions():
|
|
|
|
|
if output.flags() & QgsProcessingParameterDefinition.FlagHidden:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
label = QLabel(output.description())
|
|
|
|
|
widget = DestinationSelectionPanel(output, self.alg)
|
|
|
|
|
self.layoutMain.insertWidget(self.layoutMain.count() - 1, label)
|
|
|
|
|
self.layoutMain.insertWidget(self.layoutMain.count() - 1, widget)
|
2017-07-08 15:25:45 +10:00
|
|
|
|
if isinstance(output, (QgsProcessingParameterRasterDestination, QgsProcessingParameterFeatureSink, QgsProcessingParameterVectorDestination)):
|
2017-05-15 16:16:32 +10:00
|
|
|
|
check = QCheckBox()
|
|
|
|
|
check.setText(self.tr('Open output file after running algorithm'))
|
|
|
|
|
check.setChecked(True)
|
|
|
|
|
self.layoutMain.insertWidget(self.layoutMain.count() - 1, check)
|
|
|
|
|
self.checkBoxes[output.name()] = check
|
2017-08-07 17:23:23 +10:00
|
|
|
|
|
2017-12-01 17:19:36 +10:00
|
|
|
|
widget.setToolTip(param.toolTip())
|
2017-05-15 16:16:32 +10:00
|
|
|
|
self.outputWidgets[output.name()] = widget
|
|
|
|
|
|
2016-09-27 19:51:06 +02:00
|
|
|
|
for wrapper in list(self.wrappers.values()):
|
|
|
|
|
wrapper.postInitialize(list(self.wrappers.values()))
|
2016-04-28 14:06:59 +02:00
|
|
|
|
|
2017-12-21 10:22:19 +10:00
|
|
|
|
def setParameters(self, parameters):
|
|
|
|
|
for param in self.alg.parameterDefinitions():
|
|
|
|
|
if param.flags() & QgsProcessingParameterDefinition.FlagHidden:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if not param.name() in parameters:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if not param.isDestination():
|
|
|
|
|
wrapper = self.wrappers[param.name()]
|
|
|
|
|
wrapper.setValue(parameters[param.name()])
|
|
|
|
|
else:
|
|
|
|
|
dest_widget = self.outputWidgets[param.name()]
|
|
|
|
|
dest_widget.setValue(parameters[param.name()])
|
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
def buttonToggled(self, value):
|
|
|
|
|
if value:
|
|
|
|
|
sender = self.sender()
|
2016-09-21 18:24:26 +02:00
|
|
|
|
for button in list(self.iterateButtons.values()):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
if button is not sender:
|
|
|
|
|
button.setChecked(False)
|