Skip to content

Commit

Permalink
[FEATURE][processing] Add checkbox for each vector input layer
Browse files Browse the repository at this point in the history
to control whether selected features only are to be used

Replaces the old global setting and makes it immediately obvious
to users when the selection will be respected
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 46596c5 commit 7f68734
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/Boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def processAlgorithm(self, parameters, context, feedback):
if QgsWkbTypes.hasM(input_wkb):
output_wkb = QgsWkbTypes.addM(output_wkb)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context, '',
source.fields(), output_wkb, source.crs())
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context,
source.fields(), output_wkb, source.sourceCrs())

features = source.getFeatures()
total = 100.0 / source.featureCount()
Expand Down
37 changes: 30 additions & 7 deletions python/plugins/processing/gui/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@
QgsProcessingParameterExpression,
QgsProcessingParameterTable,
QgsProcessingParameterTableField,
QgsProcessingParameterFeatureSource)
QgsProcessingParameterFeatureSource,
QgsProcessingFeatureSourceDefinition)

from qgis.PyQt.QtWidgets import (
QCheckBox,
QComboBox,
QDialog,
QFileDialog,
QHBoxLayout,
QVBoxLayout,
QLineEdit,
QPlainTextEdit,
QToolButton,
Expand Down Expand Up @@ -760,7 +762,18 @@ def createWidget(self):
btn.clicked.connect(self.selectFile)
layout.addWidget(btn)

widget.setLayout(layout)
vl = QVBoxLayout()
vl.setMargin(0)
vl.setContentsMargins(0, 0, 0, 0)
vl.setSpacing(2)
vl.addLayout(layout)

self.use_selection_checkbox = QCheckBox(self.tr('Selected features only'))
self.use_selection_checkbox.setChecked(False)
self.use_selection_checkbox.setEnabled(False)
vl.addWidget(self.use_selection_checkbox)

widget.setLayout(vl)

filters = QgsMapLayerProxyModel.Filters()
if QgsProcessingParameterDefinition.TypeVectorAny in self.param.dataTypes():
Expand All @@ -775,6 +788,8 @@ def createWidget(self):
try:
if iface.activeLayer().type() == QgsMapLayer.VectorLayer:
self.combo.setLayer(iface.activeLayer())
self.use_selection_checkbox.setEnabled(iface.activeLayer().selectedFeatureCount() > 0)

except:
pass

Expand All @@ -787,8 +802,7 @@ def createWidget(self):
self.combo.setFilters(filters)
self.combo.setExcludedProviders(['grass'])

self.combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
self.combo.currentTextChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
self.combo.layerChanged.connect(self.layerChanged)
return widget

elif self.dialogType == DIALOG_BATCH:
Expand Down Expand Up @@ -818,6 +832,14 @@ def createWidget(self):
widget.setLayout(layout)
return widget

def layerChanged(self, layer):
if not layer or layer.type() != QgsMapLayer.VectorLayer or layer.selectedFeatureCount() == 0:
self.use_selection_checkbox.setChecked(False)
self.use_selection_checkbox.setEnabled(False)
else:
self.use_selection_checkbox.setEnabled(True)
self.widgetValueHasChanged.emit(self)

def selectFile(self):
filename, selected_filter = self.getFileName(self.combo.currentText())
if filename:
Expand All @@ -840,14 +862,15 @@ def setValue(self, value):

def value(self):
if self.dialogType == DIALOG_STANDARD:
use_selected_features = self.use_selection_checkbox.isChecked()
try:
layer = self.combo.currentLayer()
if layer:
return layer.id()
return QgsProcessingFeatureSourceDefinition(layer.id(), use_selected_features)
else:
return self.combo.currentText()
return QgsProcessingFeatureSourceDefinition(self.combo.currentText(), use_selected_features)
except:
return self.combo.currentText()
return QgsProcessingFeatureSourceDefinition(self.combo.currentText(), use_selected_features)
elif self.dialogType == DIALOG_BATCH:
return self.widget.value()
else:
Expand Down

0 comments on commit 7f68734

Please sign in to comment.