File: MultipleInputDialog.py

package info (click to toggle)
qgis 3.40.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,181,336 kB
  • sloc: cpp: 1,593,302; python: 370,494; xml: 23,474; perl: 3,664; sh: 3,482; ansic: 2,257; sql: 2,133; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 157
file content (259 lines) | stat: -rw-r--r-- 9,644 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
***************************************************************************
    MultipleInputDialog.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"

import os
import warnings
from pathlib import Path

from qgis.core import (
    QgsSettings,
    QgsProcessing,
    QgsVectorFileWriter,
    QgsProviderRegistry,
    QgsProcessingModelChildParameterSource,
)
from qgis.PyQt import uic
from qgis.PyQt.QtCore import Qt, QByteArray, QCoreApplication
from qgis.PyQt.QtWidgets import (
    QDialog,
    QAbstractItemView,
    QPushButton,
    QDialogButtonBox,
    QFileDialog,
)
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem

pluginPath = os.path.split(os.path.dirname(__file__))[0]
with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    WIDGET, BASE = uic.loadUiType(
        os.path.join(pluginPath, "ui", "DlgMultipleSelection.ui")
    )


class MultipleInputDialog(BASE, WIDGET):

    def __init__(self, options, selectedoptions=None, datatype=None):
        super().__init__(None)
        self.setupUi(self)
        self.datatype = datatype
        self.model = None

        self.options = []
        for i, option in enumerate(options):
            if option is None or isinstance(option, str):
                self.options.append((i, option))
            else:
                self.options.append((option[0], option[1]))

        self.selectedoptions = selectedoptions or []

        # Additional buttons
        self.btnSelectAll = QPushButton(self.tr("Select All"))
        self.buttonBox.addButton(
            self.btnSelectAll, QDialogButtonBox.ButtonRole.ActionRole
        )
        self.btnClearSelection = QPushButton(self.tr("Clear Selection"))
        self.buttonBox.addButton(
            self.btnClearSelection, QDialogButtonBox.ButtonRole.ActionRole
        )
        self.btnToggleSelection = QPushButton(self.tr("Toggle Selection"))
        self.buttonBox.addButton(
            self.btnToggleSelection, QDialogButtonBox.ButtonRole.ActionRole
        )
        if self.datatype is not None:
            btnAddFile = QPushButton(
                QCoreApplication.translate("MultipleInputDialog", "Add File(s)…")
            )
            btnAddFile.clicked.connect(self.addFiles)
            self.buttonBox.addButton(btnAddFile, QDialogButtonBox.ButtonRole.ActionRole)

            btnAddDir = QPushButton(
                QCoreApplication.translate("MultipleInputDialog", "Add Directory…")
            )
            btnAddDir.clicked.connect(self.addDirectory)
            self.buttonBox.addButton(btnAddDir, QDialogButtonBox.ButtonRole.ActionRole)

        self.btnSelectAll.clicked.connect(lambda: self.selectAll(True))
        self.btnClearSelection.clicked.connect(lambda: self.selectAll(False))
        self.btnToggleSelection.clicked.connect(self.toggleSelection)

        self.settings = QgsSettings()
        self.restoreGeometry(
            self.settings.value("/Processing/multipleInputDialogGeometry", QByteArray())
        )

        self.lstLayers.setSelectionMode(
            QAbstractItemView.SelectionMode.ExtendedSelection
        )
        self.lstLayers.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove)

        self.populateList()
        self.finished.connect(self.saveWindowGeometry)

    def saveWindowGeometry(self):
        self.settings.setValue(
            "/Processing/multipleInputDialogGeometry", self.saveGeometry()
        )

    def populateList(self):
        self.model = QStandardItemModel()
        for value, text in self.options:
            item = QStandardItem(text)
            item.setData(value, Qt.ItemDataRole.UserRole)
            item.setCheckState(
                Qt.CheckState.Checked
                if value in self.selectedoptions
                else Qt.CheckState.Unchecked
            )
            item.setCheckable(True)
            item.setDropEnabled(False)
            self.model.appendRow(item)

        # add extra options (e.g. manually added layers)
        for t in [o for o in self.selectedoptions if not isinstance(o, int)]:
            if isinstance(t, QgsProcessingModelChildParameterSource):
                item = QStandardItem(t.staticValue())
            else:
                item = QStandardItem(t)
            item.setData(item.text(), Qt.ItemDataRole.UserRole)
            item.setCheckState(Qt.CheckState.Checked)
            item.setCheckable(True)
            item.setDropEnabled(False)
            self.model.appendRow(item)

        self.lstLayers.setModel(self.model)

    def accept(self):
        self.selectedoptions = []
        model = self.lstLayers.model()
        for i in range(model.rowCount()):
            item = model.item(i)
            if item.checkState() == Qt.CheckState.Checked:
                self.selectedoptions.append(item.data(Qt.ItemDataRole.UserRole))
        QDialog.accept(self)

    def reject(self):
        self.selectedoptions = None
        QDialog.reject(self)

    def getItemsToModify(self):
        items = []
        if len(self.lstLayers.selectedIndexes()) > 1:
            for i in self.lstLayers.selectedIndexes():
                items.append(self.model.itemFromIndex(i))
        else:
            for i in range(self.model.rowCount()):
                items.append(self.model.item(i))
        return items

    def selectAll(self, value):
        for item in self.getItemsToModify():
            item.setCheckState(
                Qt.CheckState.Checked if value else Qt.CheckState.Unchecked
            )

    def toggleSelection(self):
        for item in self.getItemsToModify():
            checked = item.checkState() == Qt.CheckState.Checked
            item.setCheckState(
                Qt.CheckState.Unchecked if checked else Qt.CheckState.Checked
            )

    def getFileFilter(self, datatype):
        """
        Returns a suitable file filter pattern for the specified parameter definition
        :param param:
        :return:
        """
        if datatype == QgsProcessing.SourceType.TypeRaster:
            return QgsProviderRegistry.instance().fileRasterFilters()
        elif datatype == QgsProcessing.SourceType.TypeFile:
            return self.tr("All files (*.*)")
        else:
            exts = QgsVectorFileWriter.supportedFormatExtensions()
            for i in range(len(exts)):
                exts[i] = self.tr("{0} files (*.{1})").format(
                    exts[i].upper(), exts[i].lower()
                )
            return self.tr("All files (*.*)") + ";;" + ";;".join(exts)

    def addFiles(self):
        filter = self.getFileFilter(self.datatype)

        settings = QgsSettings()
        path = str(settings.value("/Processing/LastInputPath"))

        ret, selected_filter = QFileDialog.getOpenFileNames(
            self, self.tr("Select File(s)"), path, filter
        )
        if ret:
            files = list(ret)
            settings.setValue(
                "/Processing/LastInputPath", os.path.dirname(str(files[0]))
            )
            for filename in files:
                item = QStandardItem(filename)
                item.setData(filename, Qt.ItemDataRole.UserRole)
                item.setCheckState(Qt.CheckState.Checked)
                item.setCheckable(True)
                item.setDropEnabled(False)
                self.model.appendRow(item)

    def addDirectory(self):
        settings = QgsSettings()
        path = str(settings.value("/Processing/LastInputPath"))

        ret = QFileDialog.getExistingDirectory(self, self.tr("Select File(s)"), path)
        if ret:
            exts = []

            if self.datatype == QgsProcessing.SourceType.TypeVector:
                exts = QgsVectorFileWriter.supportedFormatExtensions()
            elif self.datatype == QgsProcessing.SourceType.TypeRaster:
                for t in (
                    QgsProviderRegistry.instance().fileRasterFilters().split(";;")[1:]
                ):
                    for e in t[t.index("(") + 1 : -1].split(" "):
                        if e != "*.*" and e.startswith("*."):
                            exts.append(e[2:])

            files = []
            for pp in Path(ret).rglob("*"):
                if not pp.is_file():
                    continue

                if exts and pp.suffix[1:] not in exts:
                    continue

                p = pp.as_posix()

                files.append(p)

            settings.setValue("/Processing/LastInputPath", ret)

            for filename in files:
                item = QStandardItem(filename)
                item.setData(filename, Qt.ItemDataRole.UserRole)
                item.setCheckState(Qt.CheckState.Checked)
                item.setCheckable(True)
                item.setDropEnabled(False)
                self.model.appendRow(item)