-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultilayerselect.py
484 lines (388 loc) · 20.1 KB
/
multilayerselect.py
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Multilayer Select
Multilayer selection tools
-------------------
begin : 2020-09-01
git sha : $Format:%H$
copyright : (C) 2020 Yoann Quenach de Quivillic
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
import configparser
import os
from functools import partial
from qgis.core import Qgis, QgsProject, QgsProjectVersion, QgsVectorLayer
from qgis.gui import QgsGui
from qgis.PyQt.QtCore import QCoreApplication, QObject, QSettings, QTranslator
from qgis.PyQt.QtGui import QColor, QIcon
from qgis.PyQt.QtWidgets import QAction, QMessageBox, QToolBar, QToolButton, QWidget
from .icon_utils import create_icon, expression_select_icon, invert_selection_icon, select_all_icon
from .maptools import (
MultiSelectionAreaTool,
MultiSelectionFreehandTool,
MultiSelectionPolygonTool,
MultiSelectionRadiusTool,
update_status_message,
)
from .multiselectionexpressionbuilder import MultiLayerSelectionExpressionBuilder
from .resources import * # noqa
from .settingsdialogimpl import SettingsDialog
from .utils import vector_layers
class MultiLayerSelect:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value("locale/userLocale")[0:2]
locale_path = os.path.join(self.plugin_dir, "i18n", "MultiLayerSelect_{}.qm".format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
# Init settings
self.settings = QSettings()
self.settings.beginGroup("plugins/multilayerselect")
self.action_container = QObject()
self.action_container.setObjectName("MultiLayerSelect")
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
return QCoreApplication.translate("MultiLayerSelect", message)
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
# Create settings dialog
self.settings_dialog = SettingsDialog(self.settings, self.iface.mainWindow())
self.expression_dialog = None
try:
QgsProject.instance().selectionColorChanged.connect(self.on_color_changed)
QgsProject.instance().selectionColorChanged.connect(self.settings_dialog.on_project_color_changed)
except AttributeError: # QGIS < 3.10
self.settings_dialog.colorChanged.connect(self.on_color_changed)
QgsProject.instance().readProject.connect(self.settings_dialog.on_project_color_changed)
self.settings_dialog.settingsChanged.connect(self.on_settings_changed)
self.toolbar = QToolBar("Multilayer Select", self.iface.mainWindow())
self.toolbar.setObjectName("MultiSelectToolbar")
self.about_action = QAction(
QIcon(":/plugins/multilayerselect/icons/about.svg"),
self.tr("About Multilayer Select"),
parent=self.action_container,
)
self.about_action.triggered.connect(self.show_about)
self.settings_action = QAction(
QIcon(":/images/themes/default/console/iconSettingsConsole.svg"),
self.tr("Multilayer Select Settings"),
parent=self.action_container,
)
self.settings_action.setObjectName("actionMultiLayerSelectSettings")
self.settings_action.triggered.connect(self.show_settings)
self.plugin_menu = self.iface.pluginMenu().addMenu(
QIcon(":/plugins/multilayerselect/icons/icon.svg"), "Multilayer Select"
)
self.plugin_menu.addAction(self.about_action)
self.plugin_menu.addAction(self.settings_action)
self.selection_tool_button = QToolButton(self.toolbar)
self.selection_tool_button.setPopupMode(QToolButton.MenuButtonPopup)
self.selection_tool_button.setObjectName("selectionToolButton")
self.advanced_selection_tool_button = QToolButton(self.toolbar)
self.advanced_selection_tool_button.setPopupMode(QToolButton.MenuButtonPopup)
self.advanced_selection_tool_button.setObjectName("advancedSelectionToolButton")
self.select_rect_tool = MultiSelectionAreaTool(self.iface.mapCanvas())
self.select_polygon_tool = MultiSelectionPolygonTool(self.iface.mapCanvas())
self.select_freehand_tool = MultiSelectionFreehandTool(self.iface.mapCanvas())
self.select_radius_tool = MultiSelectionRadiusTool(self.iface.mapCanvas())
self.actions_settings = [
SelectAction(
text=self.tr("Select Features"),
tooltip=self.tr("<b>Select Features by area or single click</b>"),
icon=":/plugins/multilayerselect/icons/selectRectangle.svg",
objectname="actionMultiSelectByRectangle",
tool=self.select_rect_tool,
),
SelectAction(
text=self.tr("Select Features by Polygon"),
icon=":/plugins/multilayerselect/icons/selectPolygon.svg",
objectname="actionMultiSelectByPolygon",
tool=self.select_polygon_tool,
),
SelectAction(
text=self.tr("Select Features by Freehand"),
icon=":/plugins/multilayerselect/icons/selectFreehand.svg",
objectname="actionMultiSelectByFreehand",
tool=self.select_freehand_tool,
),
SelectAction(
text=self.tr("Select Features by Radius"),
icon=":/plugins/multilayerselect/icons/selectRadius.svg",
objectname="actionMultiSelectByRadius",
tool=self.select_radius_tool,
),
]
def on_select_tool(tool, action):
self.selection_tool_button.setDefaultAction(action)
if self.embedded_selection_tool_button:
self.embedded_selection_tool_button.setDefaultAction(action)
self.iface.mapCanvas().setMapTool(tool)
self.select_actions = []
# Hack to make the shortcut work before QGIS 3.32
# Pior to 3.31, if two QAction share the same text, the Shortcut is applied to both.
# Consequently, when the shortcut is triggered, neither are called (Ambiguous trigger)
# See https://github.com/qgis/QGIS/pull/51593
disambiguator = ""
if not QgsProjectVersion(Qgis.version()) >= QgsProjectVersion("3.31"):
disambiguator = " "
for select_action in self.actions_settings:
action = QAction(select_action.text + disambiguator, parent=self.action_container)
action.setToolTip(select_action.tooltip)
action.setObjectName(select_action.objectname)
action.setCheckable(True)
select_action.tool.setAction(action)
action.triggered.connect(partial(on_select_tool, select_action.tool, action))
self.selection_tool_button.addAction(action)
if not self.selection_tool_button.defaultAction():
self.selection_tool_button.setDefaultAction(action)
self.select_actions.append(action)
self.toolbar.addWidget(self.selection_tool_button)
self.select_all_action = QAction(self.tr("Select All Features from All Layers"), parent=self.action_container)
self.select_all_action.setToolTip("<b>{}</b>".format(self.select_all_action.text()))
self.select_all_action.setObjectName("actionMultiSelectAll")
self.select_all_action.triggered.connect(self.select_all)
self.advanced_selection_tool_button.addAction(self.select_all_action)
self.advanced_selection_tool_button.setDefaultAction(self.select_all_action)
self.invert_all_action = QAction(self.tr("Invert Selection for All Layers"), parent=self.action_container)
self.invert_all_action.setToolTip("<b>{}</b>".format(self.invert_all_action.text()))
self.invert_all_action.setObjectName("actionMultiSelectInvert")
self.invert_all_action.triggered.connect(self.invert_all)
self.advanced_selection_tool_button.addAction(self.invert_all_action)
self.select_by_expr_action = QAction(
QIcon(":/images/themes/default/mIconExpressionSelect.svg"),
self.tr("Select Features by Expression...") + disambiguator,
parent=self.action_container,
)
self.select_by_expr_action.setToolTip("<b>{}</b>".format(self.select_by_expr_action.text()))
self.select_by_expr_action.setObjectName("actionMultiSelectExpr")
self.select_by_expr_action.triggered.connect(self.select_by_expression)
self.advanced_selection_tool_button.addAction(self.select_by_expr_action)
self.toolbar.addWidget(self.advanced_selection_tool_button)
self.deselect_all_action = QAction(
self.tr("Deselect Features from All Layers") + disambiguator, parent=self.action_container
)
self.deselect_all_action.setToolTip("<b>{}</b>".format(self.deselect_all_action.text()))
self.deselect_all_action.setObjectName("actionDeselectAll")
self.deselect_all_action.triggered.connect(self.deselect_all)
self.toolbar.addAction(self.deselect_all_action)
self.toolbar.addAction(self.settings_action)
self.iface.mainWindow().addToolBar(self.toolbar)
# Embedded actions
self.embedded_selection_tool_button_action = None
self.embedded_selection_tool_button = None
self.embedded_advanced_tool_button_action = None
self.embedded_advanced_tool_button = None
self.on_color_changed()
self.on_settings_changed()
QgsGui.shortcutsManager().registerAllChildren(self.action_container)
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
# Delete Settings dialog
self.settings_dialog.deleteLater()
# Remove menu from plugins menu
self.iface.pluginMenu().removeAction(self.plugin_menu.menuAction())
self.select_freehand_tool.deleteLater()
self.select_polygon_tool.deleteLater()
self.select_radius_tool.deleteLater()
self.select_rect_tool.deleteLater()
self.iface.mainWindow().removeToolBar(self.toolbar)
self.toolbar.deleteLater()
self.replace_default_action(False)
try:
QgsProject.instance().selectionColorChanged.disconnect(self.on_color_changed)
QgsProject.instance().selectionColorChanged.disconnect(self.settings_dialog.on_project_color_changed)
except AttributeError: # QGIS < 3.10
pass
for action in self.action_container.children():
action.deleteLater()
def show_about(self):
"""Show the about dialog"""
# Used to display plugin icon in the about message box
bogus = QWidget(self.iface.mainWindow())
bogus.setWindowIcon(QIcon(":/plugins/multilayerselect/icons/icon.svg"))
cfg = configparser.ConfigParser()
cfg.read(os.path.join(os.path.dirname(__file__), "metadata.txt"))
version = cfg.get("general", "version")
homepage = cfg.get("general", "homepage")
tracker = cfg.get("general", "tracker")
repository = cfg.get("general", "repository")
QMessageBox.about(
bogus,
self.tr("About Multilayer Select"),
"<b>Version</b> {3}<br><br>"
"<b>{4}</b> : <a href={0}>GitHub</a><br>"
"<b>{5}</b> : <a href={1}>GitHub</a><br>"
"<b>{6}</b> : <a href={2}>GitHub Pages</a>".format(
repository,
tracker,
homepage,
version,
self.tr("Source code"),
self.tr("Report issues"),
self.tr("Documentation"),
),
)
bogus.deleteLater()
def show_settings(self):
"""Show the settings dialog"""
geometry = self.settings_dialog.geometry()
# The first time the dialog is shown (y=0), explicitely set its geometry
# which allow to restore the geometry on subsequent calls
if geometry.y() == 0:
self.settings_dialog.show()
self.settings_dialog.raise_()
self.settings_dialog.setGeometry(self.settings_dialog.geometry())
return
self.settings_dialog.show()
self.settings_dialog.raise_()
def on_color_changed(self):
"""Called when the selection color has changed. Replace every icon"""
color = self.iface.mapCanvas().selectionColor()
color = QColor.fromHsv(
int(color.hue()),
int(color.saturation() * 0.9),
int(color.value() * 0.95),
int(color.alpha()),
)
for i in range(len(self.select_actions)):
path = self.actions_settings[i].icon
icon = create_icon(path, color)
self.select_actions[i].setIcon(icon)
icon = create_icon(":/plugins/multilayerselect/icons/deselectAll.svg", color)
self.deselect_all_action.setIcon(icon)
icon = select_all_icon(color)
self.select_all_action.setIcon(icon)
icon = invert_selection_icon(color)
self.invert_all_action.setIcon(icon)
icon = expression_select_icon(color)
self.select_by_expr_action.setIcon(icon)
def on_settings_changed(self):
"""Called when any setting has changed"""
if self.settings.value("show_settings", True, bool):
self.toolbar.addAction(self.settings_action)
else:
self.toolbar.removeAction(self.settings_action)
self.replace_default_action(self.settings.value("replace_actions", False, bool))
def deselect_all(self):
"""Deselect every feature"""
for layer in QgsProject.instance().mapLayers().values():
if isinstance(layer, QgsVectorLayer):
layer.removeSelection()
update_status_message()
def select_all(self):
"""Select all the features from every vector layer"""
for layer in vector_layers():
layer.selectAll()
self.advanced_selection_tool_button.setDefaultAction(self.select_all_action)
if self.embedded_advanced_tool_button:
self.embedded_advanced_tool_button.setDefaultAction(self.select_all_action)
update_status_message()
def invert_all(self):
"""Invert the selection of every vector layer"""
for layer in vector_layers():
layer.invertSelection()
self.advanced_selection_tool_button.setDefaultAction(self.invert_all_action)
if self.embedded_advanced_tool_button:
self.embedded_advanced_tool_button.setDefaultAction(self.invert_all_action)
update_status_message()
def select_by_expression(self):
"""Create and open the Expression builder dialog"""
if self.expression_dialog:
self.expression_dialog.deleteLater()
self.expression_dialog = MultiLayerSelectionExpressionBuilder()
self.expression_dialog.show()
self.advanced_selection_tool_button.setDefaultAction(self.select_by_expr_action)
if self.embedded_advanced_tool_button:
self.embedded_advanced_tool_button.setDefaultAction(self.select_by_expr_action)
update_status_message()
def replace_default_action(self, value):
"""Replace the default QGIS selection action with the multilayer ones
Args:
value (bool): If true, replace the actions, else put the multi actions
inside their own toolbar
"""
toolbar = self.iface.attributesToolBar()
main_window = self.iface.mainWindow()
main_window.findChild(QAction, "ActionSelect").setVisible(not value)
main_window.findChild(QAction, "ActionSelection").setVisible(not value)
main_window.findChild(QAction, "mActionDeselectAll").setVisible(not value)
actiontable = main_window.findChild(QAction, "mActionOpenTable")
actionform = main_window.findChild(QAction, "mActionSelectByForm")
# Remove the multi layer tool buttons from the QGIS attribute toolbar
toolbar.removeAction(self.embedded_selection_tool_button_action)
toolbar.removeAction(self.embedded_advanced_tool_button_action)
if value:
# Create the QToolButtons that will be added to the default toolbar
self.embedded_selection_tool_button = QToolButton()
self.embedded_selection_tool_button.setPopupMode(QToolButton.MenuButtonPopup)
# Add selection tools action to the button (Rect, Polygon, Radius, Freehand)
self.embedded_selection_tool_button.addActions(self.select_actions)
self.embedded_selection_tool_button.setDefaultAction(self.select_actions[0])
self.embedded_advanced_tool_button = QToolButton()
self.embedded_advanced_tool_button.setPopupMode(QToolButton.MenuButtonPopup)
# Add Invert, Select All, Select from value and Select from expressions
self.embedded_advanced_tool_button.addAction(self.select_all_action)
self.embedded_advanced_tool_button.setDefaultAction(self.select_all_action)
self.embedded_advanced_tool_button.addAction(self.invert_all_action)
self.embedded_advanced_tool_button.addAction(self.select_by_expr_action)
self.embedded_advanced_tool_button.addAction(actionform)
self.embedded_selection_tool_button_action = toolbar.insertWidget(
actiontable, self.embedded_selection_tool_button
)
self.embedded_advanced_tool_button_action = toolbar.insertWidget(
actiontable, self.embedded_advanced_tool_button
)
# Add the deselect all action
toolbar.insertAction(actiontable, self.deselect_all_action)
# If the settigns is enabled add the show settings action
if self.settings.value("show_settings", True, bool):
toolbar.insertAction(actiontable, self.settings_action)
else:
toolbar.removeAction(self.settings_action)
self.toolbar.hide()
else:
# Remove the multi actions from the default toolbar, and show
# the custom toolbar
self.embedded_selection_tool_button = None
self.embedded_advanced_tool_button = None
toolbar.removeAction(self.deselect_all_action)
toolbar.removeAction(self.settings_action)
self.toolbar.show()
class SelectAction:
"""Structure used to define a selection action"""
def __init__(self, text, **kwargs):
self.text = text
self.tooltip = "<b>{}</b>".format(text)
self.icon = ":/plugins/multilayerselect/icons/selectRectangle.svg"
self.objectname = ""
self.tool = None
self.__dict__.update(kwargs)