1# -*- coding: utf-8 -*- 2 3""" 4*************************************************************************** 5 MultipleExternalInputDialog.py 6 --------------------- 7 Date : August 2012 8 Copyright : (C) 2012 by Victor Olaya 9 (C) 2013 by CS Systemes d'information (CS SI) 10 Email : volayaf at gmail dot com 11 otb at c-s dot fr (CS SI) 12 Contributors : Victor Olaya - basis from MultipleInputDialog 13 Alexia Mondot (CS SI) - new parameter 14*************************************************************************** 15* * 16* This program is free software; you can redistribute it and/or modify * 17* it under the terms of the GNU General Public License as published by * 18* the Free Software Foundation; either version 2 of the License, or * 19* (at your option) any later version. * 20* * 21*************************************************************************** 22""" 23 24__author__ = 'Victor Olaya' 25__date__ = 'August 2012' 26__copyright__ = '(C) 2012, Victor Olaya' 27 28import os 29import warnings 30 31from qgis.core import QgsSettings 32from qgis.PyQt import uic 33from qgis.PyQt.QtCore import QByteArray 34from qgis.PyQt.QtWidgets import QDialog, QAbstractItemView, QPushButton, QDialogButtonBox, QFileDialog 35from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem 36 37pluginPath = os.path.split(os.path.dirname(__file__))[0] 38 39with warnings.catch_warnings(): 40 warnings.filterwarnings("ignore", category=DeprecationWarning) 41 WIDGET, BASE = uic.loadUiType( 42 os.path.join(pluginPath, 'ui', 'DlgMultipleSelection.ui')) 43 44 45class MultipleFileInputDialog(BASE, WIDGET): 46 47 def __init__(self, options): 48 super(MultipleFileInputDialog, self).__init__(None) 49 self.setupUi(self) 50 51 self.lstLayers.setSelectionMode(QAbstractItemView.ExtendedSelection) 52 53 self.selectedoptions = options 54 55 # Additional buttons 56 self.btnAdd = QPushButton(self.tr('Add file')) 57 self.buttonBox.addButton(self.btnAdd, 58 QDialogButtonBox.ActionRole) 59 self.btnRemove = QPushButton(self.tr('Remove file(s)')) 60 self.buttonBox.addButton(self.btnRemove, 61 QDialogButtonBox.ActionRole) 62 self.btnRemoveAll = QPushButton(self.tr('Remove all')) 63 self.buttonBox.addButton(self.btnRemoveAll, 64 QDialogButtonBox.ActionRole) 65 66 self.btnAdd.clicked.connect(self.addFile) 67 self.btnRemove.clicked.connect(lambda: self.removeRows()) 68 self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True)) 69 70 self.settings = QgsSettings() 71 self.restoreGeometry(self.settings.value("/Processing/multipleFileInputDialogGeometry", QByteArray())) 72 73 self.populateList() 74 self.finished.connect(self.saveWindowGeometry) 75 76 def saveWindowGeometry(self): 77 self.settings.setValue("/Processing/multipleInputDialogGeometry", self.saveGeometry()) 78 79 def populateList(self): 80 model = QStandardItemModel() 81 for option in self.selectedoptions: 82 item = QStandardItem(option) 83 model.appendRow(item) 84 85 self.lstLayers.setModel(model) 86 87 def accept(self): 88 self.selectedoptions = [] 89 model = self.lstLayers.model() 90 for i in range(model.rowCount()): 91 item = model.item(i) 92 self.selectedoptions.append(item.text()) 93 QDialog.accept(self) 94 95 def reject(self): 96 QDialog.reject(self) 97 98 def addFile(self): 99 settings = QgsSettings() 100 if settings.contains('/Processing/LastInputPath'): 101 path = settings.value('/Processing/LastInputPath') 102 else: 103 path = '' 104 105 files, selected_filter = QFileDialog.getOpenFileNames(self, 106 self.tr('Select File(s)'), path, self.tr('All files (*.*)')) 107 108 if len(files) == 0: 109 return 110 111 model = self.lstLayers.model() 112 for filePath in files: 113 item = QStandardItem(filePath) 114 model.appendRow(item) 115 116 settings.setValue('/Processing/LastInputPath', 117 os.path.dirname(files[0])) 118 119 def removeRows(self, removeAll=False): 120 if removeAll: 121 self.lstLayers.model().clear() 122 else: 123 self.lstLayers.setUpdatesEnabled(False) 124 indexes = sorted(self.lstLayers.selectionModel().selectedIndexes()) 125 for i in reversed(indexes): 126 self.lstLayers.model().removeRow(i.row()) 127 self.lstLayers.setUpdatesEnabled(True) 128