Skip to content
Permalink
Browse files

[processing] add modeler GUI for enum parameter

  • Loading branch information
alexbruy authored and nyalldawson committed May 4, 2018
1 parent 213fc55 commit ffa4b04e2988581d0cb3573b32232aee3c8558fe
@@ -0,0 +1,114 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
EnumModelerWidget.py
---------------------
Date : May 2018
Copyright : (C) 2018 by Alexander Bruy
Email : alexander dot bruy 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__ = 'Alexander Bruy'
__date__ = 'May 2018'
__copyright__ = '(C) 2018, Alexander Bruy'

# 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 Qt
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem

from qgis.core import QgsApplication

pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'enummodelerwidgetbase.ui'))


class EnumModelerWidget(BASE, WIDGET):

def __init__(self, parent=None):
super(EnumModelerWidget, self).__init__(parent)
self.setupUi(self)

self.btnAdd.setIcon(QgsApplication.getThemeIcon('/symbologyAdd.svg'))
self.btnRemove.setIcon(QgsApplication.getThemeIcon('/symbologyRemove.svg'))
self.btnClear.setIcon(QgsApplication.getThemeIcon('/mIconClearText.svg'))

self.btnAdd.clicked.connect(self.addItem)
self.btnRemove.clicked.connect(lambda: self.removeItems())
self.btnClear.clicked.connect(lambda: self.removeItems(True))

self.lstItems.setModel(QStandardItemModel())

def addItem(self):
model = self.lstItems.model()

item = QStandardItem('new item')
item.setCheckable(True)
item.setCheckState(Qt.Unchecked)
item.setDropEnabled(False)

model.appendRow(item)

def removeItems(self, removeAll=False):
if removeAll:
self.lstItems.model().clear()
else:
self.lstItems.setUpdatesEnabled(False)
indexes = sorted(self.lstItems.selectionModel().selectedIndexes())
for i in reversed(indexes):
self.lstItems.model().removeRow(i.row())
self.lstItems.setUpdatesEnabled(True)

def options(self):
items = []
model = self.lstItems.model()
for i in range(model.rowCount()):
item = model.item(i)
items.append(item.text())

return items

def defaultOption(self):
model = self.lstItems.model()
for i in range(model.rowCount()):
item = model.item(i)
if item.checkState() == Qt.Checked:
return i
return None

def allowMultiple(self):
return self.chkAllowMultiple.isChecked()

def setOptions(self, options):
model = self.lstItems.model()
for i in options:
item = QStandardItem()
item.setCheckable(True)
item.setCheckState(Qt.Unchecked)
item.setDropEnabled(False)

model.appendRow(item)

def setDefault(self, index):
model = self.lstItems.model()
item = model.item(index, 0)
if item:
item.setCheckState(Qt.Checked)

def setAllowMultiple(self):
self.chkAllowMultiple.setChecked(True)
@@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from processing.modeler.exceptions import UndefinedParameterException

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -28,6 +27,18 @@

import math

from qgis.PyQt.QtCore import (Qt,
QByteArray,
QCoreApplication)
from qgis.PyQt.QtWidgets import (QDialog,
QVBoxLayout,
QLabel,
QLineEdit,
QComboBox,
QCheckBox,
QDialogButtonBox,
QMessageBox)

from qgis.gui import QgsExpressionLineEdit, QgsProjectionSelectionWidget
from qgis.core import (QgsApplication,
QgsSettings,
@@ -54,19 +65,10 @@
QgsProcessingParameterFeatureSource,
QgsProcessingParameterBand
)
from qgis.PyQt.QtCore import (Qt,
QByteArray,
QCoreApplication)
from qgis.PyQt.QtWidgets import (QDialog,
QVBoxLayout,
QLabel,
QLineEdit,
QComboBox,
QCheckBox,
QDialogButtonBox,
QMessageBox)

from processing.gui.enummodelerwidget import EnumModelerWidget
from processing.core import parameters
from processing.modeler.exceptions import UndefinedParameterException


class ModelerParameterDefinitionDialog(QDialog):
@@ -273,6 +275,14 @@ def setupUi(self):
else:
self.selector.setCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
self.verticalLayout.addWidget(self.selector)
elif self.paramType == parameters.PARAMETER_ENUM or \
isinstance(self.param, QgsProcessingParameterEnum):
self.widget = EnumModelerWidget(self)
if self.param is not None:
self.widget.setOptions(self.param.options())
self.widget.setDefault(int(self.param.defaultValue()))
self.widget.setAllowMultiple(bool(self.param.allowMultiple()))
self.verticalLayout.addWidget(self.widget)

self.verticalLayout.addSpacing(20)
self.requiredCheck = QCheckBox()
@@ -400,6 +410,9 @@ def accept(self):
elif (self.paramType == parameters.PARAMETER_CRS or
isinstance(self.param, QgsProcessingParameterCrs)):
self.param = QgsProcessingParameterCrs(name, description, self.selector.crs().authid())
if (self.paramType == parameters.PARAMETER_ENUM or
isinstance(self.param, QgsProcessingParameterEnum)):
self.param = QgsProcessingParameterEnum(name, description, self.widget.options(), self.widget.allowMultiple(), self.widget.defaultOption())
else:
if self.paramType:
typeId = self.paramType
@@ -0,0 +1,96 @@
<?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">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="1" column="1">
<widget class="QToolButton" name="btnRemove">
<property name="toolTip">
<string>Remove item</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="3" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>190</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="btnAdd">
<property name="toolTip">
<string>Add item</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="btnClear">
<property name="toolTip">
<string>Clear all</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="0" rowspan="4">
<widget class="QListView" name="lstItems">
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="chkAllowMultiple">
<property name="text">
<string>Allow multiple selection</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit ffa4b04

Please sign in to comment.
You can’t perform that action at this time.