|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + DirectorySelectorDialog.py |
| 6 | + --------------------- |
| 7 | + Date : May 2016 |
| 8 | + Copyright : (C) 2016 by Alexander Bruy |
| 9 | + Email : alexander dot bruy at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Alexander Bruy' |
| 21 | +__date__ = 'May 2016' |
| 22 | +__copyright__ = '(C) 2016, Victor Olaya' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +import os |
| 29 | + |
| 30 | +from qgis.PyQt import uic |
| 31 | +from qgis.PyQt.QtCore import QSettings |
| 32 | +from qgis.PyQt.QtWidgets import QDialog, QAbstractItemView, QPushButton, QDialogButtonBox, QFileDialog |
| 33 | +from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem |
| 34 | + |
| 35 | +pluginPath = os.path.split(os.path.dirname(__file__))[0] |
| 36 | +WIDGET, BASE = uic.loadUiType( |
| 37 | + os.path.join(pluginPath, 'ui', 'DlgMultipleSelection.ui')) |
| 38 | + |
| 39 | + |
| 40 | +class DirectorySelectorDialog(BASE, WIDGET): |
| 41 | + |
| 42 | + def __init__(self, parent, options): |
| 43 | + super(DirectorySelectorDialog, self).__init__(None) |
| 44 | + self.setupUi(self) |
| 45 | + |
| 46 | + self.lstLayers.setSelectionMode(QAbstractItemView.ExtendedSelection) |
| 47 | + |
| 48 | + self.options = options |
| 49 | + |
| 50 | + # Additional buttons |
| 51 | + self.btnAdd = QPushButton(self.tr('Add')) |
| 52 | + self.buttonBox.addButton(self.btnAdd, |
| 53 | + QDialogButtonBox.ActionRole) |
| 54 | + self.btnRemove = QPushButton(self.tr('Remove')) |
| 55 | + self.buttonBox.addButton(self.btnRemove, |
| 56 | + QDialogButtonBox.ActionRole) |
| 57 | + self.btnRemoveAll = QPushButton(self.tr('Remove all')) |
| 58 | + self.buttonBox.addButton(self.btnRemoveAll, |
| 59 | + QDialogButtonBox.ActionRole) |
| 60 | + |
| 61 | + self.btnAdd.clicked.connect(self.addDirectory) |
| 62 | + self.btnRemove.clicked.connect(lambda: self.removeRows()) |
| 63 | + self.btnRemoveAll.clicked.connect(lambda: self.removeRows(True)) |
| 64 | + |
| 65 | + self.populateList() |
| 66 | + |
| 67 | + def populateList(self): |
| 68 | + model = QStandardItemModel() |
| 69 | + for option in self.options: |
| 70 | + item = QStandardItem(option) |
| 71 | + model.appendRow(item) |
| 72 | + |
| 73 | + self.lstLayers.setModel(model) |
| 74 | + |
| 75 | + def accept(self): |
| 76 | + self.selectedoptions = [] |
| 77 | + model = self.lstLayers.model() |
| 78 | + for i in xrange(model.rowCount()): |
| 79 | + item = model.item(i) |
| 80 | + self.selectedoptions.append(item.text()) |
| 81 | + QDialog.accept(self) |
| 82 | + |
| 83 | + def reject(self): |
| 84 | + QDialog.reject(self) |
| 85 | + |
| 86 | + def addDirectory(self): |
| 87 | + settings = QSettings() |
| 88 | + if settings.contains('/Processing/lastDirectory'): |
| 89 | + path = settings.value('/Processing/lastDirectory') |
| 90 | + else: |
| 91 | + path = '' |
| 92 | + |
| 93 | + folder = QFileDialog.getExistingDirectory(self, |
| 94 | + self.tr('Select directory'), |
| 95 | + path, |
| 96 | + QFileDialog.ShowDirsOnly) |
| 97 | + |
| 98 | + if folder == '': |
| 99 | + return |
| 100 | + |
| 101 | + model = self.lstLayers.model() |
| 102 | + item = QStandardItem(folder) |
| 103 | + model.appendRow(item) |
| 104 | + |
| 105 | + settings.setValue('/Processing/lastDirectory', |
| 106 | + os.path.dirname(folder)) |
| 107 | + |
| 108 | + def removeRows(self, removeAll=False): |
| 109 | + if removeAll: |
| 110 | + self.lstLayers.model().clear() |
| 111 | + else: |
| 112 | + self.lstLayers.setUpdatesEnabled(False) |
| 113 | + indexes = sorted(self.lstLayers.selectionModel().selectedIndexes()) |
| 114 | + for i in reversed(indexes): |
| 115 | + self.lstLayers.model().removeRow(i.row()) |
| 116 | + self.lstLayers.setUpdatesEnabled(True) |
| 117 | + |
| 118 | + def value(self): |
| 119 | + folders = [] |
| 120 | + model = self.lstLayers.model() |
| 121 | + for i in xrange(model.rowCount()): |
| 122 | + folders.append(model.item(i).text()) |
| 123 | + |
| 124 | + return ';'.join(folders) |
0 commit comments