2014-01-17 19:01:08 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
MultipleExternalInputDialog.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
(C) 2013 by CS Systemes d'information (CS SI)
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
otb at c-s dot fr (CS SI)
|
|
|
|
Contributors : Victor Olaya - basis from MultipleInputDialog
|
|
|
|
Alexia Mondot (CS SI) - new parameter
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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'
|
|
|
|
|
|
|
|
import os
|
2018-05-21 14:23:26 +10:00
|
|
|
import warnings
|
2014-01-17 19:01:08 +01:00
|
|
|
|
2017-03-03 20:39:17 +01:00
|
|
|
from qgis.core import QgsSettings
|
2016-04-29 11:39:26 +02:00
|
|
|
from qgis.PyQt import uic
|
2017-08-05 05:06:46 +10:00
|
|
|
from qgis.PyQt.QtCore import QByteArray
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QDialog, QAbstractItemView, QPushButton, QDialogButtonBox, QFileDialog
|
|
|
|
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem
|
2014-11-06 19:45:48 +02:00
|
|
|
|
2015-05-18 21:04:20 +03:00
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
2018-05-21 14:23:26 +10:00
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
WIDGET, BASE = uic.loadUiType(
|
|
|
|
os.path.join(pluginPath, 'ui', 'DlgMultipleSelection.ui'))
|
2014-11-06 19:45:48 +02:00
|
|
|
|
|
|
|
|
2015-05-18 21:04:20 +03:00
|
|
|
class MultipleFileInputDialog(BASE, WIDGET):
|
2014-11-06 19:45:48 +02:00
|
|
|
|
|
|
|
def __init__(self, options):
|
2015-05-18 21:04:20 +03:00
|
|
|
super(MultipleFileInputDialog, self).__init__(None)
|
2014-11-06 19:45:48 +02:00
|
|
|
self.setupUi(self)
|
|
|
|
|
|
|
|
self.lstLayers.setSelectionMode(QAbstractItemView.ExtendedSelection)
|
|
|
|
|
2014-11-08 15:55:38 +02:00
|
|
|
self.selectedoptions = options
|
|
|
|
|
2014-11-06 19:45:48 +02:00
|
|
|
# Additional buttons
|
|
|
|
self.btnAdd = QPushButton(self.tr('Add file'))
|
|
|
|
self.buttonBox.addButton(self.btnAdd,
|
|
|
|
QDialogButtonBox.ActionRole)
|
|
|
|
self.btnRemove = QPushButton(self.tr('Remove file(s)'))
|
|
|
|
self.buttonBox.addButton(self.btnRemove,
|
|
|
|
QDialogButtonBox.ActionRole)
|
|
|
|
self.btnRemoveAll = QPushButton(self.tr('Remove all'))
|
|
|
|
self.buttonBox.addButton(self.btnRemoveAll,
|
|
|
|
QDialogButtonBox.ActionRole)
|
|
|
|
|
|
|
|
self.btnAdd.clicked.connect(self.addFile)
|
2014-11-09 12:37:32 +02:00
|
|
|
self.btnRemove.clicked.connect(lambda: self.removeRows())
|
|
|
|
self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True))
|
2014-11-06 19:45:48 +02:00
|
|
|
|
2017-08-05 05:06:46 +10:00
|
|
|
self.settings = QgsSettings()
|
|
|
|
self.restoreGeometry(self.settings.value("/Processing/multipleFileInputDialogGeometry", QByteArray()))
|
|
|
|
|
2014-11-06 19:45:48 +02:00
|
|
|
self.populateList()
|
2017-08-05 05:06:46 +10:00
|
|
|
self.finished.connect(self.saveWindowGeometry)
|
|
|
|
|
|
|
|
def saveWindowGeometry(self):
|
|
|
|
self.settings.setValue("/Processing/multipleInputDialogGeometry", self.saveGeometry())
|
2014-11-06 19:45:48 +02:00
|
|
|
|
|
|
|
def populateList(self):
|
|
|
|
model = QStandardItemModel()
|
|
|
|
for option in self.selectedoptions:
|
|
|
|
item = QStandardItem(option)
|
|
|
|
model.appendRow(item)
|
|
|
|
|
|
|
|
self.lstLayers.setModel(model)
|
|
|
|
|
|
|
|
def accept(self):
|
2014-01-17 19:01:08 +01:00
|
|
|
self.selectedoptions = []
|
2014-11-06 19:45:48 +02:00
|
|
|
model = self.lstLayers.model()
|
2016-09-21 18:24:26 +02:00
|
|
|
for i in range(model.rowCount()):
|
2014-11-06 19:45:48 +02:00
|
|
|
item = model.item(i)
|
|
|
|
self.selectedoptions.append(item.text())
|
|
|
|
QDialog.accept(self)
|
2014-01-17 19:01:08 +01:00
|
|
|
|
2014-11-06 19:45:48 +02:00
|
|
|
def reject(self):
|
|
|
|
QDialog.reject(self)
|
2014-01-17 19:01:08 +01:00
|
|
|
|
|
|
|
def addFile(self):
|
2017-03-03 20:39:17 +01:00
|
|
|
settings = QgsSettings()
|
2014-11-06 19:45:48 +02:00
|
|
|
if settings.contains('/Processing/LastInputPath'):
|
|
|
|
path = settings.value('/Processing/LastInputPath')
|
|
|
|
else:
|
|
|
|
path = ''
|
|
|
|
|
2016-09-05 11:39:43 +10:00
|
|
|
files, selected_filter = QFileDialog.getOpenFileNames(self,
|
2018-02-21 18:44:04 +10:00
|
|
|
self.tr('Select File(s)'), path, self.tr('All files (*.*)'))
|
2014-11-06 19:45:48 +02:00
|
|
|
|
2015-03-04 13:01:32 +02:00
|
|
|
if len(files) == 0:
|
|
|
|
return
|
|
|
|
|
2014-11-06 19:45:48 +02:00
|
|
|
model = self.lstLayers.model()
|
|
|
|
for filePath in files:
|
|
|
|
item = QStandardItem(filePath)
|
|
|
|
model.appendRow(item)
|
|
|
|
|
|
|
|
settings.setValue('/Processing/LastInputPath',
|
|
|
|
os.path.dirname(files[0]))
|
|
|
|
|
2014-11-09 12:37:32 +02:00
|
|
|
def removeRows(self, removeAll=False):
|
2014-11-06 19:45:48 +02:00
|
|
|
if removeAll:
|
|
|
|
self.lstLayers.model().clear()
|
|
|
|
else:
|
|
|
|
self.lstLayers.setUpdatesEnabled(False)
|
2015-08-22 14:29:41 +02:00
|
|
|
indexes = sorted(self.lstLayers.selectionModel().selectedIndexes())
|
2014-11-06 19:45:48 +02:00
|
|
|
for i in reversed(indexes):
|
|
|
|
self.lstLayers.model().removeRow(i.row())
|
|
|
|
self.lstLayers.setUpdatesEnabled(True)
|