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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
|
|
|
from builtins import range
|
2012-10-05 23:28:47 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
2013-05-24 00:38:29 +02:00
|
|
|
|
2013-03-15 09:32:15 +01:00
|
|
|
import os
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-03-03 20:39:17 +01:00
|
|
|
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
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-04-05 14:37:06 +10:00
|
|
|
from qgis.core import (QgsMapLayer,
|
|
|
|
QgsSettings,
|
|
|
|
QgsProject,
|
|
|
|
QgsProcessingUtils)
|
2016-09-18 16:09:30 +02:00
|
|
|
|
2014-04-14 08:56:00 +02:00
|
|
|
from processing.gui.MultipleInputDialog import MultipleInputDialog
|
2014-11-12 19:36:12 +02:00
|
|
|
|
|
|
|
from processing.core.parameters import ParameterMultipleInput
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterRaster
|
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTable
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
from processing.tools import dataobjects
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
class BatchInputSelectionPanel(QWidget):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2016-09-18 16:09:30 +02:00
|
|
|
valueChanged = pyqtSignal()
|
|
|
|
|
2016-09-07 14:30:20 +02:00
|
|
|
def __init__(self, param, row, col, dialog):
|
2014-11-12 19:36:12 +02:00
|
|
|
super(BatchInputSelectionPanel, self).__init__(None)
|
2012-09-15 18:25:25 +03:00
|
|
|
self.param = param
|
2016-09-07 14:30:20 +02:00
|
|
|
self.dialog = dialog
|
2012-09-15 18:25:25 +03:00
|
|
|
self.row = row
|
|
|
|
self.col = col
|
2014-11-12 19:36:12 +02:00
|
|
|
self.horizontalLayout = QHBoxLayout(self)
|
2014-06-18 11:55:18 +02:00
|
|
|
self.horizontalLayout.setSpacing(0)
|
2012-09-15 18:25:25 +03:00
|
|
|
self.horizontalLayout.setMargin(0)
|
2014-11-12 19:36:12 +02:00
|
|
|
self.text = QLineEdit()
|
2016-09-18 16:09:30 +02:00
|
|
|
self.text.setObjectName('text')
|
2015-06-22 12:56:51 +02:00
|
|
|
self.text.setMinimumWidth(300)
|
2016-09-18 16:09:30 +02:00
|
|
|
self.setValue('')
|
2016-09-19 13:33:06 +02:00
|
|
|
self.text.editingFinished.connect(self.textEditingFinished)
|
2014-11-12 19:36:12 +02:00
|
|
|
self.text.setSizePolicy(QSizePolicy.Expanding,
|
|
|
|
QSizePolicy.Expanding)
|
2012-09-15 18:25:25 +03:00
|
|
|
self.horizontalLayout.addWidget(self.text)
|
2014-11-12 19:36:12 +02:00
|
|
|
self.pushButton = QPushButton()
|
2013-10-01 20:52:22 +03:00
|
|
|
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
|
|
|
|
2016-09-07 14:30:20 +02:00
|
|
|
def _panel(self):
|
2016-09-18 16:09:30 +02:00
|
|
|
return self.dialog.mainWidget
|
2016-09-07 14:30:20 +02:00
|
|
|
|
|
|
|
def _table(self):
|
|
|
|
return self._panel().tblParameters
|
|
|
|
|
2014-04-14 08:56:00 +02:00
|
|
|
def showPopupMenu(self):
|
2014-11-12 19:36:12 +02:00
|
|
|
popupmenu = QMenu()
|
|
|
|
|
|
|
|
if not (isinstance(self.param, ParameterMultipleInput) and
|
2016-08-23 19:33:42 +03:00
|
|
|
self.param.datatype == dataobjects.TYPE_FILE):
|
2014-11-12 19:36:12 +02:00
|
|
|
selectLayerAction = QAction(
|
|
|
|
self.tr('Select from open layers'), self.pushButton)
|
2014-04-14 08:56:00 +02:00
|
|
|
selectLayerAction.triggered.connect(self.showLayerSelectionDialog)
|
|
|
|
popupmenu.addAction(selectLayerAction)
|
2014-11-12 19:36:12 +02:00
|
|
|
|
|
|
|
selectFileAction = QAction(
|
|
|
|
self.tr('Select from filesystem'), self.pushButton)
|
2014-04-14 08:56:00 +02:00
|
|
|
selectFileAction.triggered.connect(self.showFileSelectionDialog)
|
|
|
|
popupmenu.addAction(selectFileAction)
|
2014-11-12 19:36:12 +02:00
|
|
|
|
|
|
|
popupmenu.exec_(QCursor.pos())
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-04-14 08:56:00 +02:00
|
|
|
def showLayerSelectionDialog(self):
|
2014-11-12 19:36:12 +02:00
|
|
|
if (isinstance(self.param, ParameterRaster) or
|
|
|
|
(isinstance(self.param, ParameterMultipleInput) and
|
2016-08-23 19:33:42 +03:00
|
|
|
self.param.datatype == dataobjects.TYPE_RASTER)):
|
2017-04-05 14:37:06 +10:00
|
|
|
layers = QgsProcessingUtils.compatibleRasterLayers(QgsProject.instance())
|
2014-04-14 08:56:00 +02:00
|
|
|
elif isinstance(self.param, ParameterTable):
|
2017-04-05 14:37:06 +10:00
|
|
|
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
|
2014-04-14 08:56:00 +02:00
|
|
|
else:
|
|
|
|
if isinstance(self.param, ParameterVector):
|
2016-08-08 15:37:33 +03:00
|
|
|
datatype = self.param.datatype
|
2014-04-14 08:56:00 +02:00
|
|
|
else:
|
|
|
|
datatype = [self.param.datatype]
|
2017-04-05 14:37:06 +10:00
|
|
|
if datatype != dataobjects.TYPE_VECTOR_ANY:
|
|
|
|
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance(), [datatype])
|
|
|
|
else:
|
|
|
|
layers = QgsProcessingUtils.compatibleVectorLayers(QgsProject.instance())
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2014-04-14 08:56:00 +02:00
|
|
|
dlg = MultipleInputDialog([layer.name() for layer in layers])
|
|
|
|
dlg.exec_()
|
2014-04-19 22:04:24 +02:00
|
|
|
if dlg.selectedoptions is not None:
|
|
|
|
selected = dlg.selectedoptions
|
2014-04-14 08:56:00 +02:00
|
|
|
if len(selected) == 1:
|
2016-09-20 13:43:10 +02:00
|
|
|
self.setValue(layers[selected[0]])
|
2014-04-19 22:04:24 +02:00
|
|
|
else:
|
2014-04-14 08:56:00 +02:00
|
|
|
if isinstance(self.param, ParameterMultipleInput):
|
|
|
|
self.text.setText(';'.join(layers[idx].name() for idx in selected))
|
|
|
|
else:
|
2016-09-07 14:30:20 +02:00
|
|
|
rowdif = len(selected) - (self._table().rowCount() - self.row)
|
2014-04-14 08:56:00 +02:00
|
|
|
for i in range(rowdif):
|
2016-09-07 14:30:20 +02:00
|
|
|
self._panel().addRow()
|
2014-04-14 08:56:00 +02:00
|
|
|
for i, layeridx in enumerate(selected):
|
2016-09-07 14:30:20 +02:00
|
|
|
self._table().cellWidget(i + self.row,
|
2016-09-19 11:52:31 +03:00
|
|
|
self.col).setValue(layers[layeridx])
|
2014-04-19 22:04:24 +02:00
|
|
|
|
2014-04-14 08:56:00 +02:00
|
|
|
def showFileSelectionDialog(self):
|
2017-03-03 20:39:17 +01:00
|
|
|
settings = QgsSettings()
|
2016-09-21 18:24:26 +02:00
|
|
|
text = str(self.text.text())
|
2013-03-15 09:32:15 +01:00
|
|
|
if os.path.isdir(text):
|
|
|
|
path = text
|
|
|
|
elif os.path.isdir(os.path.dirname(text)):
|
|
|
|
path = os.path.dirname(text)
|
2013-10-01 20:52:22 +03:00
|
|
|
elif settings.contains('/Processing/LastInputPath'):
|
2016-09-21 18:24:26 +02:00
|
|
|
path = str(settings.value('/Processing/LastInputPath'))
|
2013-03-15 09:32:15 +01:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
path = ''
|
2013-03-26 14:15:12 +01:00
|
|
|
|
2016-09-05 11:39:43 +10:00
|
|
|
ret, selected_filter = QFileDialog.getOpenFileNames(self, self.tr('Open file'), path,
|
2017-03-04 16:23:36 +01:00
|
|
|
self.tr('All files (*.*);;') + self.param.getFileFilter())
|
2013-03-26 14:15:12 +01:00
|
|
|
if ret:
|
|
|
|
files = list(ret)
|
2015-01-20 12:36:46 +01:00
|
|
|
settings.setValue('/Processing/LastInputPath',
|
2016-09-21 18:24:26 +02:00
|
|
|
os.path.dirname(str(files[0])))
|
2015-01-20 12:36:46 +01:00
|
|
|
for i, filename in enumerate(files):
|
2015-01-22 20:53:45 +01:00
|
|
|
files[i] = dataobjects.getRasterSublayer(filename, self.param)
|
2015-01-20 12:36:46 +01:00
|
|
|
if len(files) == 1:
|
2014-04-14 08:56:00 +02:00
|
|
|
self.text.setText(files[0])
|
2016-09-20 13:43:10 +02:00
|
|
|
self.textEditingFinished()
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
|
|
|
if isinstance(self.param, ParameterMultipleInput):
|
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:
|
2016-09-07 14:30:20 +02:00
|
|
|
rowdif = len(files) - (self._table().rowCount() - self.row)
|
2012-09-15 18:25:25 +03:00
|
|
|
for i in range(rowdif):
|
2016-09-07 14:30:20 +02:00
|
|
|
self._panel().addRow()
|
2014-04-14 08:56:00 +02:00
|
|
|
for i, f in enumerate(files):
|
2016-09-07 14:30:20 +02:00
|
|
|
self._table().cellWidget(i + self.row,
|
2016-09-19 11:52:31 +03:00
|
|
|
self.col).setValue(f)
|
2016-09-18 16:09:30 +02:00
|
|
|
|
2016-09-19 13:33:06 +02:00
|
|
|
def textEditingFinished(self):
|
2016-09-18 16:09:30 +02:00
|
|
|
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())
|
2016-09-20 13:43:10 +02:00
|
|
|
else: # should be basestring
|
2016-09-18 16:09:30 +02:00
|
|
|
self.text.setText(value)
|
|
|
|
self.valueChanged.emit()
|