Skip to content

Commit

Permalink
[processing] Use native QGIS map layer combobox
Browse files Browse the repository at this point in the history
Switches the custom map layer combo box used in the run algorithm
dialog across to the standard QGIS map layer combo box
  • Loading branch information
nyalldawson committed Nov 16, 2016
1 parent 5c3aea3 commit f78f2a0
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 141 deletions.
90 changes: 0 additions & 90 deletions python/plugins/processing/gui/InputLayerSelectorPanel.py

This file was deleted.

204 changes: 153 additions & 51 deletions python/plugins/processing/gui/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@
import os
from functools import cmp_to_key

from qgis.core import QgsCoordinateReferenceSystem, QgsVectorLayer, QgsApplication
from qgis.PyQt.QtWidgets import QCheckBox, QComboBox, QLineEdit, QPlainTextEdit, QWidget, QHBoxLayout, QToolButton
from qgis.core import QgsCoordinateReferenceSystem, QgsVectorLayer, QgsApplication, QgsWkbTypes, QgsMapLayerProxyModel
from qgis.PyQt.QtWidgets import QCheckBox, QComboBox, QLineEdit, QPlainTextEdit, QWidget, QHBoxLayout, QToolButton, QFileDialog
from qgis.gui import (QgsFieldExpressionWidget,
QgsExpressionLineEdit,
QgsProjectionSelectionWidget,
QgsGenericProjectionSelector,
QgsFieldComboBox,
QgsFieldProxyModel)
from qgis.PyQt.QtCore import pyqtSignal, QObject, QVariant
QgsFieldProxyModel,
QgsMapLayerComboBox
)
from qgis.PyQt.QtCore import pyqtSignal, QObject, QVariant, QSettings

from processing.gui.NumberInputPanel import NumberInputPanel, ModellerNumberInputPanel
from processing.gui.InputLayerSelectorPanel import InputLayerSelectorPanel
Expand Down Expand Up @@ -156,6 +158,26 @@ def postInitialize(self, wrappers):
def refresh(self):
pass

def getFileName(self, initial_value=''):
"""Shows a file open dialog"""
settings = QSettings()
if os.path.isdir(initial_value):
path = initial_value
elif os.path.isdir(os.path.dirname(initial_value)):
path = os.path.dirname(initial_value)
elif settings.contains('/Processing/LastInputPath'):
path = str(settings.value('/Processing/LastInputPath'))
else:
path = ''

filename, selected_filter = QFileDialog.getOpenFileName(self.widget, self.tr('Select file'),
path, self.tr(
'All files (*.*);;') + self.param.getFileFilter())
if filename:
settings.setValue('/Processing/LastInputPath',
os.path.dirname(str(filename)))
return filename, selected_filter


class BasicWidgetWrapper(WidgetWrapper):

Expand Down Expand Up @@ -534,13 +556,31 @@ class RasterWidgetWrapper(WidgetWrapper):

def createWidget(self):
if self.dialogType == DIALOG_STANDARD:
layers = dataobjects.getRasterLayers()
items = []
widget = QWidget()
layout = QHBoxLayout()
layout.setMargin(0)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(2)
self.combo = QgsMapLayerComboBox()
layout.addWidget(self.combo)
btn = QToolButton()
btn.setText('...')
btn.setToolTip(self.tr("Select file"))
btn.clicked.connect(self.selectFile)
layout.addWidget(btn)

widget.setLayout(layout)
if self.param.optional:
items.append((self.NOT_SELECTED, None))
for layer in layers:
items.append((getExtendedLayerName(layer), layer))
return InputLayerSelectorPanel(items, self.param)
self.combo.setAllowEmptyLayer(True)
if ProcessingConfig.getSetting(ProcessingConfig.SHOW_CRS_DEF):
self.combo.setShowCrs(True)

self.combo.setFilters(QgsMapLayerProxyModel.RasterLayer)
self.combo.setExcludedProviders(['grass'])

self.combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
self.combo.currentTextChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
return widget
elif self.dialogType == DIALOG_BATCH:
return BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
else:
Expand All @@ -553,14 +593,14 @@ def createWidget(self):
widget.setEditText("")
return widget

def refresh(self):
items = []
layers = dataobjects.getRasterLayers()
if self.param.optional:
items.append((self.NOT_SELECTED, None))
for layer in layers:
items.append((getExtendedLayerName(layer), layer))
self.widget.update(items)
def selectFile(self):
filename, selected_filter = self.getFileName(self.combo.currentText())
if filename:
filename = dataobjects.getRasterSublayer(filename, self.param)
items = self.combo.additionalItems()
items.append(filename)
self.combo.setAdditionalItems(items)
self.combo.setCurrentIndex(self.combo.findText(filename))

def setValue(self, value):
if self.dialogType == DIALOG_STANDARD:
Expand All @@ -572,7 +612,14 @@ def setValue(self, value):

def value(self):
if self.dialogType == DIALOG_STANDARD:
return self.widget.getValue()
try:
layer = self.combo.currentLayer()
if layer:
return layer
else:
return self.combo.currentText()
except:
return self.combo.currentText()
elif self.dialogType == DIALOG_BATCH:
return self.widget.getText()
else:
Expand Down Expand Up @@ -615,16 +662,43 @@ class VectorWidgetWrapper(WidgetWrapper):

def createWidget(self):
if self.dialogType == DIALOG_STANDARD:
layers = dataobjects.getVectorLayers(self.param.datatype)
items = []
widget = QWidget()
layout = QHBoxLayout()
layout.setMargin(0)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(2)
self.combo = QgsMapLayerComboBox()
layout.addWidget(self.combo)
btn = QToolButton()
btn.setText('...')
btn.setToolTip(self.tr("Select file"))
btn.clicked.connect(self.selectFile)
layout.addWidget(btn)

widget.setLayout(layout)

filters = QgsMapLayerProxyModel.Filters()
if self.param.datatype == [-1] or -1 in self.param.datatype:
filters = QgsMapLayerProxyModel.HasGeometry
if QgsWkbTypes.PointGeometry in self.param.datatype:
filters |= QgsMapLayerProxyModel.PointLayer
if QgsWkbTypes.LineGeometry in self.param.datatype:
filters |= QgsMapLayerProxyModel.LineLayer
if QgsWkbTypes.PolygonGeometry in self.param.datatype:
filters |= QgsMapLayerProxyModel.PolygonLayer

if self.param.optional:
items.append((self.NOT_SELECTED, None))
for layer in layers:
items.append((getExtendedLayerName(layer), layer))
widget = InputLayerSelectorPanel(items, self.param)
widget.name = self.param.name
widget.valueChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
self.combo.setAllowEmptyLayer(True)
if ProcessingConfig.getSetting(ProcessingConfig.SHOW_CRS_DEF):
self.combo.setShowCrs(True)

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))
return widget

elif self.dialogType == DIALOG_BATCH:
widget = BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
widget.valueChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
Expand All @@ -639,18 +713,14 @@ def createWidget(self):
widget.setEditText("")
return widget

def _populate(self, widget):
items = []
layers = dataobjects.getVectorLayers(self.param.datatype)
layers.sort(key=lambda lay: lay.name())
if self.param.optional:
items.append((self.NOT_SELECTED, None))
for layer in layers:
items.append((getExtendedLayerName(layer), layer))
self.widget.update(items)

def refresh(self):
self._populate(self.widget)
def selectFile(self):
filename, selected_filter = self.getFileName(self.combo.currentText())
if filename:
filename = dataobjects.getRasterSublayer(filename, self.param)
items = self.combo.additionalItems()
items.append(filename)
self.combo.setAdditionalItems(items)
self.combo.setCurrentIndex(self.combo.findText(filename))

def setValue(self, value):
if self.dialogType == DIALOG_STANDARD:
Expand All @@ -663,9 +733,13 @@ def setValue(self, value):
def value(self):
if self.dialogType == DIALOG_STANDARD:
try:
return self.widget.itemData(self.widget.currentIndex())
layer = self.combo.currentLayer()
if layer:
return layer
else:
return self.combo.currentText()
except:
return self.widget.getValue()
return self.combo.currentText()
elif self.dialogType == DIALOG_BATCH:
return self.widget.value()
else:
Expand Down Expand Up @@ -809,16 +883,31 @@ class TableWidgetWrapper(WidgetWrapper):

def createWidget(self):
if self.dialogType == DIALOG_STANDARD:
widget = QComboBox()
layers = dataobjects.getTables()
layers.sort(key=lambda lay: lay.name())
widget = QWidget()
layout = QHBoxLayout()
layout.setMargin(0)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(2)
self.combo = QgsMapLayerComboBox()
layout.addWidget(self.combo)
btn = QToolButton()
btn.setText('...')
btn.setToolTip(self.tr("Select file"))
btn.clicked.connect(self.selectFile)
layout.addWidget(btn)

widget.setLayout(layout)

if self.param.optional:
widget.addItem(self.NOT_SELECTED, None)
for layer in layers:
widget.addItem(layer.name(), layer)
widget.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
widget.name = self.param.name
self.combo.setAllowEmptyLayer(True)

self.combo.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.combo.setExcludedProviders(['grass'])

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

elif self.dialogType == DIALOG_BATCH:
return BatchInputSelectionPanel(self.param, self.row, self.col, self.dialog)
else:
Expand All @@ -833,6 +922,15 @@ def createWidget(self):
widget.addItem(self.dialog.resolveValueDescription(layer), layer)
return widget

def selectFile(self):
filename, selected_filter = self.getFileName(self.combo.currentText())
if filename:
filename = dataobjects.getRasterSublayer(filename, self.param)
items = self.combo.additionalItems()
items.append(filename)
self.combo.setAdditionalItems(items)
self.combo.setCurrentIndex(self.combo.findText(filename))

def setValue(self, value):
if self.dialogType == DIALOG_STANDARD:
pass # TODO
Expand All @@ -844,9 +942,13 @@ def setValue(self, value):
def value(self):
if self.dialogType == DIALOG_STANDARD:
try:
return self.widget.itemData(self.widget.currentIndex())
layer = self.combo.currentLayer()
if layer:
return layer
else:
return self.combo.currentText()
except:
return self.widget.getValue()
return self.combo.currentText()
elif self.dialogType == DIALOG_BATCH:
return self.widget.value()
else:
Expand Down

0 comments on commit f78f2a0

Please sign in to comment.