QGIS/python/plugins/processing/gui/ParametersPanel.py

169 lines
6.3 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
ParametersPanel.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
(C) 2013 by CS Systemes d'information (CS SI)
2012-10-05 23:28:47 +02:00
Email : volayaf at gmail dot com
otb at c-s dot fr (CS SI)
2014-04-06 12:27:51 +02:00
Contributors : Victor Olaya
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. *
* *
***************************************************************************
"""
2016-09-21 18:24:26 +02:00
from builtins import str
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-05 23:28:47 +02:00
__revision__ = '$Format:%H$'
2012-09-15 18:25:25 +03:00
import os
import locale
from qgis.core import QgsMapLayerRegistry, QgsMapLayer
2016-04-29 11:39:26 +02:00
from qgis.PyQt import uic
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.PyQt.QtWidgets import (QWidget, QLayout, QVBoxLayout, QHBoxLayout, QToolButton,
QLabel, QCheckBox, QComboBox, QLineEdit, QPlainTextEdit)
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtGui import QIcon
2013-08-12 20:44:27 +02:00
from processing.gui.OutputSelectionPanel import OutputSelectionPanel
from processing.core.parameters import ParameterVector, ParameterExtent, ParameterPoint
from processing.core.outputs import OutputRaster
from processing.core.outputs import OutputTable
from processing.core.outputs import OutputVector
2012-09-15 18:25:25 +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 21:04:20 +03:00
class ParametersPanel(BASE, WIDGET):
2012-09-15 18:25:25 +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)
self.setupUi(self)
self.grpAdvanced.hide()
self.layoutMain = self.scrollAreaWidgetContents.layout()
self.layoutAdvanced = self.grpAdvanced.layout()
2012-09-15 18:25:25 +03:00
self.parent = parent
self.alg = alg
2012-09-15 18:25:25 +03:00
self.valueItems = {}
self.wrappers = {}
2012-09-15 18:25:25 +03:00
self.labels = {}
self.widgets = {}
self.checkBoxes = {}
self.dependentItems = {}
self.iterateButtons = {}
self.initWidgets()
def layerRegistryChanged(self, layers):
for wrapper in self.wrappers.values():
wrapper.refresh()
def initWidgets(self):
# If there are advanced parameters — show corresponding groupbox
for param in self.alg.parameters:
if param.isAdvanced:
self.grpAdvanced.show()
break
# Create widgets and put them in layouts
for param in self.alg.parameters:
if param.hidden:
continue
desc = param.description
if isinstance(param, ParameterExtent):
desc += self.tr(' (xmin, xmax, ymin, ymax)')
if isinstance(param, ParameterPoint):
desc += self.tr(' (x, y)')
if param.optional:
desc += self.tr(' [optional]')
wrapper = self.getWidgetWrapperFromParameter(param)
self.wrappers[param.name] = wrapper
self.valueItems[param.name] = wrapper.widget
widget = wrapper.widget
if isinstance(param, ParameterVector):
layout = QHBoxLayout()
layout.setSpacing(2)
layout.setMargin(0)
layout.addWidget(widget)
button = QToolButton()
icon = QIcon(os.path.join(pluginPath, 'images', 'iterate.png'))
button.setIcon(icon)
button.setToolTip(self.tr('Iterate over this layer'))
button.setCheckable(True)
layout.addWidget(button)
self.iterateButtons[param.name] = button
button.toggled.connect(self.buttonToggled)
widget = QWidget()
widget.setLayout(layout)
tooltips = self.alg.getParameterDescriptions()
widget.setToolTip(tooltips.get(param.name, param.description))
label = QLabel(desc)
# label.setToolTip(tooltip)
self.labels[param.name] = label
if param.isAdvanced:
self.layoutAdvanced.addWidget(label)
self.layoutAdvanced.addWidget(widget)
else:
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, label)
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, widget)
self.widgets[param.name] = widget
for output in self.alg.outputs:
if output.hidden:
continue
label = QLabel(output.description)
widget = OutputSelectionPanel(output, self.alg)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, label)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, widget)
if isinstance(output, (OutputRaster, OutputVector, OutputTable)):
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
self.valueItems[output.name] = widget
for wrapper in self.wrappers.values():
wrapper.postInitialize(self.wrappers.values())
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)
def getWidgetWrapperFromParameter(self, param):
return param.wrapper(self.parent)