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

195 lines
8.0 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
BatchInputSelectionPanel.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf 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__ = '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$'
import os
from qgis.PyQt.QtCore import pyqtSignal
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtWidgets import QWidget, QHBoxLayout, QMenu, QPushButton, QLineEdit, QSizePolicy, QAction, QFileDialog
from qgis.PyQt.QtGui import QCursor
from qgis.core import (QgsMapLayer,
QgsSettings,
QgsProject,
QgsProcessing,
QgsProcessingUtils,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterDefinition,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource)
from processing.gui.MultipleInputDialog import MultipleInputDialog
from processing.gui.ParameterGuiUtils import getFileFilter
from processing.tools import dataobjects
2012-09-15 18:25:25 +03:00
class BatchInputSelectionPanel(QWidget):
2012-09-15 18:25:25 +03:00
valueChanged = pyqtSignal()
def __init__(self, param, row, col, dialog):
super(BatchInputSelectionPanel, self).__init__(None)
2012-09-15 18:25:25 +03:00
self.param = param
self.dialog = dialog
2012-09-15 18:25:25 +03:00
self.row = row
self.col = col
self.horizontalLayout = QHBoxLayout(self)
self.horizontalLayout.setSpacing(0)
2012-09-15 18:25:25 +03:00
self.horizontalLayout.setMargin(0)
self.text = QLineEdit()
self.text.setObjectName('text')
self.text.setMinimumWidth(300)
self.setValue('')
self.text.editingFinished.connect(self.textEditingFinished)
self.text.setSizePolicy(QSizePolicy.Expanding,
QSizePolicy.Expanding)
2012-09-15 18:25:25 +03:00
self.horizontalLayout.addWidget(self.text)
self.pushButton = QPushButton()
self.pushButton.setText('')
2014-04-19 22:04:24 +02:00
self.pushButton.clicked.connect(self.showPopupMenu)
2012-09-15 18:25:25 +03:00
self.horizontalLayout.addWidget(self.pushButton)
self.setLayout(self.horizontalLayout)
2014-04-19 22:04:24 +02:00
def _panel(self):
return self.dialog.mainWidget()
def _table(self):
return self._panel().tblParameters
def showPopupMenu(self):
popupmenu = QMenu()
if not (isinstance(self.param, QgsProcessingParameterMultipleLayers) and
self.param.layerType == dataobjects.TYPE_FILE):
selectLayerAction = QAction(
2018-02-21 18:44:04 +10:00
self.tr('Select from Open Layers…'), self.pushButton)
selectLayerAction.triggered.connect(self.showLayerSelectionDialog)
popupmenu.addAction(selectLayerAction)
selectFileAction = QAction(
2018-02-21 18:44:04 +10:00
self.tr('Select from File System…'), self.pushButton)
selectFileAction.triggered.connect(self.showFileSelectionDialog)
popupmenu.addAction(selectFileAction)
popupmenu.exec_(QCursor.pos())
2012-09-15 18:25:25 +03:00
def showLayerSelectionDialog(self):
layers = []
if (isinstance(self.param, QgsProcessingParameterRasterLayer) or
(isinstance(self.param, QgsProcessingParameterMultipleLayers) and
self.param.layerType() == QgsProcessing.TypeRaster)):
layers = QgsProcessingUtils.compatibleRasterLayers(QgsProject.instance())
elif isinstance(self.param, QgsProcessingParameterVectorLayer):
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
else:
2017-08-19 02:46:22 +10:00
datatypes = [QgsProcessing.TypeVectorAnyGeometry]
if isinstance(self.param, QgsProcessingParameterFeatureSource):
datatypes = self.param.dataTypes()
elif isinstance(self.param, QgsProcessingParameterMultipleLayers):
datatypes = [self.param.layerType()]
2017-08-19 02:46:22 +10:00
if QgsProcessing.TypeVectorAnyGeometry not in datatypes:
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance(), datatypes)
else:
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
dlg = MultipleInputDialog([layer.name() for layer in layers])
dlg.exec_()
def generate_layer_id(layer):
# prefer layer name if unique
if len([l for l in layers if l.name().lower() == layer.name().lower()]) == 1:
return layer.name()
else:
# otherwise fall back to layer id
return layer.id()
2014-04-19 22:04:24 +02:00
if dlg.selectedoptions is not None:
selected = dlg.selectedoptions
if len(selected) == 1:
self.setValue(generate_layer_id(layers[selected[0]]))
2014-04-19 22:04:24 +02:00
else:
if isinstance(self.param, QgsProcessingParameterMultipleLayers):
self.text.setText(';'.join(layers[idx].id() for idx in selected))
else:
rowdif = len(selected) - (self._table().rowCount() - self.row)
for i in range(rowdif):
self._panel().addRow()
for i, layeridx in enumerate(selected):
self._table().cellWidget(i + self.row,
self.col).setValue(generate_layer_id(layers[layeridx]))
2014-04-19 22:04:24 +02:00
def showFileSelectionDialog(self):
settings = QgsSettings()
2016-09-21 18:24:26 +02:00
text = str(self.text.text())
if os.path.isdir(text):
path = text
elif os.path.isdir(os.path.dirname(text)):
path = os.path.dirname(text)
elif settings.contains('/Processing/LastInputPath'):
2016-09-21 18:24:26 +02:00
path = str(settings.value('/Processing/LastInputPath'))
else:
path = ''
2013-03-26 14:15:12 +01:00
2018-02-21 18:44:04 +10:00
ret, selected_filter = QFileDialog.getOpenFileNames(self, self.tr('Select Files'), path,
getFileFilter(self.param))
2013-03-26 14:15:12 +01:00
if ret:
files = list(ret)
settings.setValue('/Processing/LastInputPath',
2016-09-21 18:24:26 +02:00
os.path.dirname(str(files[0])))
for i, filename in enumerate(files):
2015-01-22 20:53:45 +01:00
files[i] = dataobjects.getRasterSublayer(filename, self.param)
if len(files) == 1:
self.text.setText(files[0])
self.textEditingFinished()
2012-09-15 18:25:25 +03:00
else:
if isinstance(self.param, QgsProcessingParameterMultipleLayers):
2016-09-21 18:24:26 +02:00
self.text.setText(';'.join(str(f) for f in files))
2012-09-15 18:25:25 +03:00
else:
rowdif = len(files) - (self._table().rowCount() - self.row)
2012-09-15 18:25:25 +03:00
for i in range(rowdif):
self._panel().addRow()
for i, f in enumerate(files):
self._table().cellWidget(i + self.row,
self.col).setValue(f)
def textEditingFinished(self):
self._value = self.text.text()
self.valueChanged.emit()
def value(self):
return self._value
def setValue(self, value):
self._value = value
if isinstance(value, QgsMapLayer):
self.text.setText(value.name())
else: # should be basestring
self.text.setText(value)
self.valueChanged.emit()