Skip to content

Commit 2e701bb

Browse files
committed
fix panel management pb
1 parent 7028712 commit 2e701bb

File tree

5 files changed

+154
-89
lines changed

5 files changed

+154
-89
lines changed

nominatim/logic/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import os
33

4-
from qgis.PyQt.QtCore import Qt, QVariant, QUrl, QUrlQuery, QSettings
4+
from qgis.PyQt.QtCore import Qt, QUrl, QUrlQuery, QSettings
55
from qgis.PyQt.QtNetwork import QNetworkRequest
66
from qgis.PyQt.QtCore import QLocale, QUrl, QDir
77
from qgis.PyQt.QtGui import QDesktopServices

nominatim/metadata.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ qgisMinimumVersion=3.6
44
qgisMaximumVersion=3.98
55
description=Location search helper
66
about=Based on OSM data, Nominatim online tool (https://operations.osmfoundation.org/policies/nominatim/)
7-
version=1.4.3
7+
version=1.4.4
88

99
changelog=
10+
1.4.4 : fix panel management pb
1011
1.4.3 : fix help loading on Windows. Thanks to https://github.com/AlisterH
1112
1.4.2 : fix Help is not showing
1213
1.4.1 : The reverse search returns geometry

nominatim/nominatim.py

Lines changed: 142 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,28 @@
2020
import os
2121

2222
from qgis.PyQt.QtCore import QCoreApplication, QFileInfo, Qt, QSettings, QTranslator
23-
from qgis.PyQt.QtWidgets import QAction, QApplication
24-
#from qgis.utils import showPluginHelp
23+
from qgis.PyQt.QtWidgets import QAction
24+
from qgis.PyQt.QtGui import QIcon
2525
from .ui.nominatimdialog import NominatimDialog
2626
from .ui.nominatim_conf_dlg import nominatim_conf_dlg
27-
28-
# from .osmLocatorFilter import OsmLocatorFilter
29-
3027
from nominatim.__about__ import DIR_PLUGIN_ROOT, __title__, __version__
3128
from nominatim.logic import tools
29+
# from .osmLocatorFilter import OsmLocatorFilter
3230

31+
from qgis.core import QgsMessageLog
3332

3433
class Nominatim:
3534
def __init__(self, iface):
3635
# Save reference to the QGIS interface
3736
self.iface = iface
3837
self.path = QFileInfo(os.path.realpath(__file__)).path()
39-
self.toolBar = None
38+
self.toolbar = self.iface.pluginToolBar()
4039
self.canvas = self.iface.mapCanvas()
41-
self.dlgPosX = 100
42-
self.dlgPosY = 100
4340
self.lastSearch = ""
4441
self.localiseOnStartup = True
45-
self.defaultArea = Qt.LeftDockWidgetArea
4642
self.singleLayer = True
43+
self.mainAction = None
44+
self.defaultArea = Qt.LeftDockWidgetArea
4745

4846
self.read()
4947

@@ -69,17 +67,10 @@ def __init__(self, iface):
6967
# no translation
7068
pass
7169

72-
try:
73-
self.nominatim_dlg
74-
except:
75-
self.nominatim_dlg = NominatimDialog(self.iface.mainWindow(), self)
76-
self.nominatim_dlg.visibilityChanged.connect(self.dockVisibilityChanged)
77-
self.nominatim_dlg.dockLocationChanged.connect(self.dockLocationChanged)
70+
self.actions = []
7871

79-
try:
80-
self.nominatim_dlg.editSearch.setText(self.lastSearch)
81-
except:
82-
pass
72+
self.pluginIsActive = False
73+
self.dockwidget = None
8374

8475
# self.filter = OsmLocatorFilter(self.iface, self)
8576
# self.filter.resultProblem.connect(self.showLocatorProblem)
@@ -98,12 +89,10 @@ def store(self):
9889
s = QSettings()
9990
s.setValue("nominatim/localiseOnStartup", self.localiseOnStartup)
10091
s.setValue("nominatim/limitSearchToExtent", tools.limitSearchToExtent)
101-
s.setValue("nominatim/dlgPosX", self.dlgPosX)
102-
s.setValue("nominatim/dlgPosY", self.dlgPosY)
10392
s.setValue("nominatim/lastSearch", self.lastSearch)
10493
s.setValue("nominatim/gnOptions", tools.gnOptions)
105-
s.setValue("nominatim/defaultArea", self.defaultArea)
10694
s.setValue("nominatim/singleLayer", self.singleLayer)
95+
s.setValue("nominatim/defaultArea", self.defaultArea)
10796

10897
def read(self):
10998
s = QSettings()
@@ -114,84 +103,166 @@ def read(self):
114103
tools.limitSearchToExtent = s.value(
115104
"nominatim/limitSearchToExtent", (False), type=bool
116105
)
117-
self.dlgPosX = s.value("nominatim/dlgPosX", 100, type=int)
118-
self.dlgPosY = s.value("nominatim/dlgPosY", 100, type=int)
119106
self.lastSearch = s.value("nominatim/lastSearch", "")
120107
tools.gnOptions = s.value("nominatim/gnOptions", "")
108+
self.singleLayer = s.value("nominatim/singleLayer", (True), type=bool)
121109
self.defaultArea = s.value(
122110
"nominatim/defaultArea", Qt.LeftDockWidgetArea, type=int
123111
)
124-
self.singleLayer = s.value("nominatim/singleLayer", (True), type=bool)
112+
113+
def add_action(
114+
self,
115+
text, menu,
116+
callback,
117+
icon_path=None,
118+
enabled_flag=True,
119+
add_to_menu=True,
120+
add_to_toolbar=True,
121+
status_tip=None,
122+
whats_this=None,
123+
parent=None):
124+
"""Add a toolbar icon to the toolbar.
125+
126+
:param text: Text that should be shown in menu items for this action.
127+
:type text: str
128+
129+
:param menu: Menu entry that should be shown in menu items for this action.
130+
:type text: str
131+
132+
:param icon_path: Path to the icon for this action. Can be a resource
133+
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
134+
:type icon_path: str
135+
136+
:param callback: Function to be called when the action is triggered.
137+
:type callback: function
138+
139+
:param enabled_flag: A flag indicating if the action should be enabled
140+
by default. Defaults to True.
141+
:type enabled_flag: bool
142+
143+
:param add_to_menu: Flag indicating whether the action should also
144+
be added to the menu. Defaults to True.
145+
:type add_to_menu: bool
146+
147+
:param add_to_toolbar: Flag indicating whether the action should also
148+
be added to the toolbar. Defaults to True.
149+
:type add_to_toolbar: bool
150+
151+
:param status_tip: Optional text to show in a popup when mouse pointer
152+
hovers over the action.
153+
:type status_tip: str
154+
155+
:param parent: Parent widget for the new action. Defaults None.
156+
:type parent: QWidget
157+
158+
:param whats_this: Optional text to show in the status bar when the
159+
mouse pointer hovers over the action.
160+
161+
:returns: The action that was created. Note that the action is also
162+
added to self.actions list.
163+
:rtype: QAction
164+
"""
165+
166+
if icon_path is None:
167+
action = QAction(text, parent)
168+
else:
169+
action = QAction(QIcon(icon_path), text, parent)
170+
171+
action.triggered.connect(callback)
172+
action.setEnabled(enabled_flag)
173+
174+
if status_tip is not None:
175+
action.setStatusTip(status_tip)
176+
177+
if whats_this is not None:
178+
action.setWhatsThis(whats_this)
179+
180+
if add_to_toolbar:
181+
self.toolbar.addAction(action)
182+
183+
if add_to_menu:
184+
self.iface.addPluginToMenu(menu, action)
185+
186+
self.actions.append(action)
187+
188+
return action
125189

126190
def initGui(self):
127-
self.toolBar = self.iface.pluginToolBar()
191+
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
192+
193+
icon_path = "{}/resources/nominatim.png".format(DIR_PLUGIN_ROOT)
194+
self.mainAction = self.add_action(
195+
self.tr(__title__),
196+
self.tr(__title__),
197+
self.run,
198+
icon_path=icon_path,
199+
parent=self.iface.mainWindow())
200+
self.mainAction.setCheckable(True)
128201

129-
self.act_config = QAction(
202+
self.add_action(
130203
self.tr("Configuration") + "...",
131-
self.iface.mainWindow(),
132-
)
133-
self.act_nominatim_help = QAction(
204+
self.tr(__title__),
205+
self.do_config,
206+
add_to_toolbar=False,
207+
parent=self.iface.mainWindow())
208+
209+
self.add_action(
134210
self.tr("Help") + "...",
135-
self.iface.mainWindow(),
136-
)
211+
self.tr(__title__),
212+
self.do_help,
213+
add_to_toolbar=False,
214+
parent=self.iface.mainWindow())
137215

138-
self.iface.addPluginToMenu(
139-
"&" + self.tr(__title__),
140-
self.act_config,
141-
)
142-
self.iface.addPluginToMenu(
143-
"&" + self.tr(__title__),
144-
self.act_nominatim_help,
145-
)
216+
# Create the dockwidget
217+
self.dockwidget = NominatimDialog(self.iface.mainWindow(), self)
146218

147-
# Add actions to the toolbar
148-
self.act_config.triggered.connect(self.do_config)
149-
self.act_nominatim_help.triggered.connect(
150-
lambda: tools.showPluginHelp(filename="doc/index")
151-
)
219+
# connect events
220+
self.dockwidget.closingPlugin.connect(self.onClosePlugin)
221+
self.dockwidget.dockLocationChanged.connect(self.dockLocationChanged)
222+
self.dockwidget.visibilityChanged.connect(self.dockVisibilityChanged)
152223

153-
self.iface.addDockWidget(self.defaultArea, self.nominatim_dlg)
224+
def onClosePlugin(self):
225+
self.dockwidget.hide()
154226

155227
def unload(self):
156-
self.iface.removePluginMenu(
157-
"&" + self.tr(__title__),
158-
self.act_config,
159-
)
160-
self.iface.removePluginMenu(
161-
"&" + self.tr(__title__),
162-
self.act_nominatim_help,
163-
)
228+
"""Removes the plugin menu item and icon from QGIS GUI."""
229+
230+
for action in self.actions:
231+
self.iface.removePluginMenu(self.tr(__title__), action)
232+
self.iface.removeToolBarIcon(action)
233+
164234
self.store()
165-
self.deactivate()
166-
self.iface.removeDockWidget(self.nominatim_dlg)
167235

168-
def dockVisibilityChanged(self, visible):
169-
try:
170-
self.defaultActive = visible
171-
if visible and self.localiseOnStartup:
172-
self.nominatim_dlg.doLocalize()
173-
except:
174-
pass
236+
def run(self):
237+
"""Run method that loads and starts the plugin"""
238+
239+
if not self.pluginIsActive:
240+
# show the dockwidget
241+
self.iface.addDockWidget(self.defaultArea, self.dockwidget)
242+
self.dockwidget.show()
243+
else:
244+
# hide only
245+
self.onClosePlugin()
175246

176247
def dockLocationChanged(self, area):
177248
self.defaultArea = area
178249

179-
def activate(self):
180-
self.nominatim_dlg.show()
181-
182-
def deactivate(self):
250+
def dockVisibilityChanged(self, visible):
251+
self.pluginIsActive = visible
252+
self.mainAction.setChecked(visible)
183253
try:
184-
self.nominatim_dlg.hide()
254+
if visible and self.localiseOnStartup:
255+
self.dockwidget.doLocalize()
185256
except:
186257
pass
187258

188-
def zoom(self):
189-
pass
190-
191259
def do_config(self):
192260
dlg = nominatim_conf_dlg(self.iface.mainWindow(), self)
193261
dlg.setModal(True)
194262

195263
dlg.show()
196264
dlg.exec_()
197265
del dlg
266+
267+
def do_help(self):
268+
tools.showPluginHelp(filename="doc/index")

nominatim/resources/nominatim.png

3.7 KB
Loading

nominatim/ui/nominatimdialog.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import json
2-
3-
from qgis.PyQt import uic
4-
from qgis.PyQt.QtCore import Qt, QVariant, QUrl, QUrlQuery
1+
from qgis.PyQt import uic
2+
from qgis.PyQt.QtCore import Qt, QVariant, pyqtSignal
53
from qgis.PyQt.QtWidgets import (
64
QDockWidget,
75
QHeaderView,
86
QApplication,
97
QTableWidgetItem,
108
)
119
from qgis.PyQt.QtGui import QIcon, QColor
12-
from qgis.PyQt.QtNetwork import QNetworkRequest
13-
# from qgis.utils import showPluginHelp
1410

1511
from qgis.core import (
1612
QgsProject,
@@ -24,10 +20,8 @@
2420
QgsField,
2521
QgsFields,
2622
QgsFeature,
27-
QgsLineSymbol,
2823
QgsWkbTypes,
2924
QgsUnitTypes,
30-
QgsNetworkAccessManager,
3125
)
3226

3327
from qgis.gui import QgsRubberBand
@@ -41,6 +35,8 @@
4135

4236
class NominatimDialog(QDockWidget, FORM_CLASS):
4337

38+
closingPlugin = pyqtSignal()
39+
4440
"""
4541
Gestion de l'évènement "leave", afin d'effacer l'objet sélectionné en sortie du dock
4642
"""
@@ -144,7 +140,7 @@ def populateRow(self, item, idx):
144140
typeName = ""
145141

146142
wkt = item.get("geotext")
147-
osm_type = item.get("osm_type")
143+
#osm_type = item.get("osm_type")
148144

149145
# extratags and address_details are dictionaries with content that can
150146
# vary per feature and also per nominatim server. We expose them as
@@ -415,13 +411,6 @@ def doLayer(self, item, singleLayer=None):
415411

416412
# mise a jour etendue de la couche
417413
vl.updateExtents()
418-
"""
419-
layerTree = QgsProject.instance().layerTreeRoot().findLayer(vl)
420-
if layerTree:
421-
self.plugin.iface.layerTreeView().layerTreeModel().refreshLayerLegend(
422-
layerTree
423-
) # Refresh legend
424-
"""
425414
self.go(item, False)
426415

427416
return vl
@@ -470,3 +459,7 @@ def doMask(self, item):
470459
str(DIR_PLUGIN_ROOT / "resources" / "mask.qml")
471460
)
472461
maskLayer.triggerRepaint()
462+
463+
def closeEvent(self, event):
464+
self.closingPlugin.emit()
465+
event.accept()

0 commit comments

Comments
 (0)