Skip to content

Commit

Permalink
[processing] Add useCheckBoxes option to SelectionWidgetWrapper
Browse files Browse the repository at this point in the history
This allow to show checkboxes or radioboxes intead of line edit with button.
This is configurable for each algorithm thought ParameterSelection metadata parameter.
It is also possible to choose the number of columns.
This is not applied in case of the BatchDialog.
  • Loading branch information
arnaud-morvan authored and nyalldawson committed Sep 15, 2017
1 parent 7a1b9f9 commit 534fe21
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/core/parameters.py
Expand Up @@ -371,8 +371,8 @@ def __init__(self, name='', description='', optional=False, showSublayersDialog=
class ParameterSelection(Parameter):

def __init__(self, name='', description='', options=[], default=None, isSource=False,
multiple=False, optional=False):
Parameter.__init__(self, name, description, default, optional)
multiple=False, optional=False, metadata={}):
Parameter.__init__(self, name, description, default, optional, metadata)
self.multiple = multiple
isSource = parseBool(isSource)
self.options = options
Expand Down
89 changes: 89 additions & 0 deletions python/plugins/processing/gui/CheckboxesPanel.py
@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
CheckBoxesPanel.py
---------------------
Date : January 2015
Copyright : (C) 2015 by Arnaud Morvan
Email : arnaud dot morvan at camptocamp dot com
Contributors : Arnaud Morvan
***************************************************************************
* *
* 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 range

__author__ = 'Arnaud Morvan'
__date__ = 'January 2015'
__copyright__ = '(C) 2015, Arnaud Morvan'

# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


from qgis.PyQt.QtWidgets import (
QCheckBox,
QRadioButton,
QGridLayout,
QButtonGroup,
QSizePolicy,
QSpacerItem,
QWidget,
)


class CheckboxesPanel(QWidget):

def __init__(self, options, multiple, columns=2, parent=None):
super(CheckboxesPanel, self).__init__(parent)

self._options = []
for i, option in enumerate(options):
if isinstance(option, str):
self._options.append((i, option))
else:
self.options.append(option)
self._multiple = multiple
self._buttons = []
rows = len(options) / columns

self._buttonGroup = QButtonGroup()
self._buttonGroup.setExclusive(not multiple)
layout = QGridLayout()
for i, (v, t) in enumerate(self._options):
if multiple:
button = QCheckBox(t)
else:
button = QRadioButton(t)
self._buttons.append((v, button))
self._buttonGroup.addButton(button, i)
layout.addWidget(button, i % rows, i / rows)
layout.addItem(QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Minimum),
0, columns)
self.setLayout(layout)

def value(self):
if self._multiple:
value = []
for (v, checkbox) in self._buttons:
if checkbox.isChecked():
value.append(v)
return value
else:
return self._options[self._buttonGroup.checkedId()][0]

def setValue(self, value):
if self._multiple:
for (v, button) in self._buttons:
if v in value:
button.setChecked(True)
else:
for v, button in self._buttons:
if v == value:
button.setChecked(True)
13 changes: 12 additions & 1 deletion python/plugins/processing/gui/wrappers.py
Expand Up @@ -118,6 +118,7 @@
from processing.core.outputs import (OutputFile, OutputRaster, OutputVector,
OutputString, OutputTable, OutputExtent)
from processing.tools import dataobjects
from processing.gui.CheckboxesPanel import CheckboxesPanel
from processing.gui.MultipleInputPanel import MultipleInputPanel
from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel
from processing.gui.FixedTablePanel import FixedTablePanel
Expand Down Expand Up @@ -803,7 +804,12 @@ def selectFile(self):

class EnumWidgetWrapper(WidgetWrapper):

def createWidget(self):
def createWidget(self, useCheckBoxes=False, columns=1):
self._useCheckBoxes = useCheckBoxes
if self._useCheckBoxes and not self.dialogType == DIALOG_BATCH:
return CheckboxesPanel(options=self.param.options(),
multiple=self.param.allowMultiple(),
columns=columns)
if self.param.allowMultiple():
return MultipleInputPanel(options=self.param.options())
else:
Expand All @@ -815,12 +821,17 @@ def createWidget(self):
return widget

def setValue(self, value):
if self._useCheckBoxes and not self.dialogType == DIALOG_BATCH:
self.widget.setValue(value)
return
if self.param.allowMultiple():
self.widget.setSelectedItems(value)
else:
self.widget.setCurrentIndex(self.widget.findData(value))

def value(self):
if self._useCheckBoxes and not self.dialogType == DIALOG_BATCH:
return self.widget.value()
if self.param.allowMultiple():
return self.widget.selectedoptions
else:
Expand Down

0 comments on commit 534fe21

Please sign in to comment.