2020import os
2121
2222from 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
2525from .ui .nominatimdialog import NominatimDialog
2626from .ui .nominatim_conf_dlg import nominatim_conf_dlg
27-
28- # from .osmLocatorFilter import OsmLocatorFilter
29-
3027from nominatim .__about__ import DIR_PLUGIN_ROOT , __title__ , __version__
3128from nominatim .logic import tools
29+ # from .osmLocatorFilter import OsmLocatorFilter
3230
31+ from qgis .core import QgsMessageLog
3332
3433class 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" )
0 commit comments