/
identifyplus.py
216 lines (178 loc) · 7.67 KB
/
identifyplus.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
# -*- coding: utf-8 -*-
# *****************************************************************************
#
# IdentifyPlus
# ---------------------------------------------------------
# Extended identify tool. Supports displaying and modifying photos.
#
# Copyright (C) 2012-2015 NextGIS (info@nextgis.com)
#
# This source 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.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/licenses/>. You can also obtain it by writing
# to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
# MA 02110-1335 USA.
#
# *****************************************************************************
import os
from typing import List
from qgis.PyQt.QtCore import Qt, QTranslator
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QMessageBox
from qgis.core import Qgis, QgsApplication
from qgis.gui import QgisInterface
from .qgis_plugin_base import Plugin
from .identifytools import allTools
from .identifytool import IdentifyTool
from .identifyplusmaptool import IdentifyPlusMapTool
from .identifyplusresults import IdentifyPlusResultsDock, IdentifyPlusResults
from .aboutdialog import AboutDialog
class IdentifyPlus(Plugin):
iface: QgisInterface
__activate_tool_action: QAction
__open_about_dialog_action: QAction
__identify_tool: IdentifyPlusMapTool
__identify_results_dock: IdentifyPlusResultsDock
__identify_results: IdentifyPlusResults
def __init__(self, iface: QgisInterface) -> None:
super().__init__(iface, "IdentifyPlus")
self.iface = iface
self.__init_translator()
def initGui(self) -> None:
if Qgis.QGIS_VERSION_INT < 30000:
version = Qgis.QGIS_VERSION[:Qgis.version().find('-')]
QMessageBox.warning(
parent=self.iface.mainWindow(),
title=self.tr("Error"),
text=self.tr("QGIS {} detected.").format(version) + "\n"
+ self.tr("This version of IdentifyPlus requires at least QGIS version 3.0.") # noqa: E501
+ "\n" + self.tr("Plugin will not be enabled.")
)
return
self.__init_menus()
self.__init_dock()
self.__init_tool()
def unload(self) -> None:
self.__unload_tool()
self.__unload_dock()
self.__unload_menus()
def tr(self, source_text: str) -> str:
return QgsApplication.translate('IdentifyPlus', source_text)
def getTargetIdentTools(self) -> List[IdentifyTool]:
return allTools()
def activateTool(self) -> None:
self.iface.mapCanvas().setMapTool(self.__identify_tool)
self.__activate_tool_action.setChecked(True)
def __on_map_tool_changed(self, tool):
if tool != self.__identify_tool:
self.__activate_tool_action.setChecked(False)
def about(self):
dlg = AboutDialog()
dlg.exec_()
def __init_translator(self):
plugin_dir = os.path.dirname(__file__)
locale = QgsApplication.instance().locale()
locale_path = os.path.join(
plugin_dir, 'i18n', f'identifyplus_{locale}.qm'
)
if not os.path.exists(locale_path):
return
self.translator = QTranslator()
self.translator.load(locale_path)
QgsApplication.installTranslator(self.translator)
def __init_menus(self):
self.__activate_tool_action = QAction(
self.tr("IdentifyPlus"), self.iface.mainWindow()
)
self.__activate_tool_action.setIcon(
QIcon(":/plugins/identifyplus/icons/identifyplus.svg")
)
self.__activate_tool_action.setWhatsThis(self.tr("Extended identify tool"))
self.__activate_tool_action.setCheckable(True)
self.__activate_tool_action.triggered.connect(self.activateTool)
self.iface.addPluginToMenu(
self.tr("IdentifyPlus"), self.__activate_tool_action
)
self.iface.attributesToolBar().addAction(self.__activate_tool_action)
self.__open_about_dialog_action = QAction(
self.tr("About IdentifyPlus…"), self.iface.mainWindow()
)
self.__open_about_dialog_action.setIcon(
QIcon(":/plugins/identifyplus/icons/about.png")
)
self.__open_about_dialog_action.setWhatsThis(self.tr("About IdentifyPlus"))
self.__open_about_dialog_action.triggered.connect(self.about)
self.iface.addPluginToMenu(
self.tr("IdentifyPlus"), self.__open_about_dialog_action
)
def __unload_menus(self):
self.iface.attributesToolBar().removeAction(
self.__activate_tool_action
)
self.iface.removePluginMenu(
self.tr("IdentifyPlus"), self.__activate_tool_action
)
self.__activate_tool_action.deleteLater()
self.iface.removePluginMenu(
self.tr("IdentifyPlus"), self.__open_about_dialog_action
)
self.__open_about_dialog_action.deleteLater()
def __init_dock(self):
self.__identify_results_dock = IdentifyPlusResultsDock()
self.__identify_results = IdentifyPlusResults(
self.__identify_results_dock
)
self.__identify_results_dock.setWidget(self.__identify_results)
self.iface.addDockWidget(
Qt.DockWidgetArea.RightDockWidgetArea, self.__identify_results_dock
)
self.__identify_results_dock.hide()
def __unload_dock(self):
self.__identify_results_dock.setVisible(False)
self.iface.removeDockWidget(self.__identify_results_dock)
self.__identify_results_dock.deleteLater()
def __init_tool(self):
# prepare map tool
self.__identify_tool = IdentifyPlusMapTool(self.iface.mapCanvas())
Plugin().plPrint(">>> IdentifyPlusMapTool initialized")
self.__identify_tool.avalableChanged.connect(
self.__activate_tool_action.setEnabled
)
self.__activate_tool_action.setEnabled(
self.__identify_tool.isAvalable()
)
self.iface.mapCanvas().mapToolSet.connect(self.__on_map_tool_changed)
self.__identify_tool.identified.connect(
self.__identify_results.appendIdentifyRes
)
self.__identify_tool.progressChanged.connect(
self.__identify_results.progressShow
)
self.__identify_tool.identifyStarted.connect(
self.__identify_results.identifyProcessStart
)
self.__identify_tool.identifyFinished.connect(
self.__identify_results.identifyProcessFinish
)
self.__identify_tool.identifyStarted.connect(
self.__identify_results_dock.show
)
self.__identify_tool.progressChanged.connect(
self.__identify_results_dock.raise_
)
def __unload_tool(self):
if self.iface.mapCanvas().mapTool() == self.__identify_tool:
self.iface.mapCanvas().unsetMapTool(self.__identify_tool)
self.iface.mapCanvas().mapToolSet.disconnect(
self.__on_map_tool_changed
)
self.__identify_tool.deleteLater()