Skip to content

Commit 7c03cbb

Browse files
committed
Add option to enable/disable a toolbar icon
1 parent bdbdb67 commit 7c03cbb

File tree

5 files changed

+116
-56
lines changed

5 files changed

+116
-56
lines changed

pyqgis_resource_browser/gui/dlg_settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def apply(self):
8383

8484
# misc
8585
settings.debug_mode = self.opt_debug.isChecked()
86+
settings.toolbar_browser_shortcut = (
87+
self.opt_toolbar_browser_shortcut.isChecked()
88+
)
8689
settings.version = __version__
8790

8891
prefix_filters = self.te_resource_prefixes.toPlainText().strip().split("\n")
@@ -108,6 +111,7 @@ def load_settings(self):
108111

109112
# global
110113
self.opt_debug.setChecked(settings.debug_mode)
114+
self.opt_toolbar_browser_shortcut.setChecked(settings.toolbar_browser_shortcut)
111115
self.lbl_version_saved_value.setText(settings.version)
112116

113117
self.gb_filter_resourceprefix.setChecked(settings.filter_prefixes)

pyqgis_resource_browser/gui/dlg_settings.ui

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>673</width>
10-
<height>525</height>
9+
<width>690</width>
10+
<height>555</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -164,8 +164,8 @@ xpn</string>
164164
<bool>false</bool>
165165
</property>
166166
<layout class="QGridLayout" name="gridLayout">
167-
<item row="3" column="1">
168-
<widget class="QLabel" name="lbl_version_saved_value">
167+
<item row="1" column="0">
168+
<widget class="QCheckBox" name="opt_debug">
169169
<property name="minimumSize">
170170
<size>
171171
<width>0</width>
@@ -178,19 +178,25 @@ xpn</string>
178178
<height>30</height>
179179
</size>
180180
</property>
181+
<property name="toolTip">
182+
<string>Enable debug mode.</string>
183+
</property>
184+
<property name="autoFillBackground">
185+
<bool>true</bool>
186+
</property>
181187
<property name="locale">
182188
<locale language="English" country="UnitedStates"/>
183189
</property>
184190
<property name="text">
185-
<string notr="true">X.X.x</string>
191+
<string>Debug mode (degraded performances)</string>
186192
</property>
187-
<property name="textInteractionFlags">
188-
<set>Qt::NoTextInteraction</set>
193+
<property name="tristate">
194+
<bool>false</bool>
189195
</property>
190196
</widget>
191197
</item>
192-
<item row="3" column="0">
193-
<widget class="QLabel" name="lbl_version_saved">
198+
<item row="6" column="1">
199+
<widget class="QLabel" name="lbl_version_saved_value">
194200
<property name="minimumSize">
195201
<size>
196202
<width>0</width>
@@ -207,12 +213,15 @@ xpn</string>
207213
<locale language="English" country="UnitedStates"/>
208214
</property>
209215
<property name="text">
210-
<string>Version used to save settings:</string>
216+
<string notr="true">X.X.x</string>
217+
</property>
218+
<property name="textInteractionFlags">
219+
<set>Qt::NoTextInteraction</set>
211220
</property>
212221
</widget>
213222
</item>
214-
<item row="4" column="0" colspan="2">
215-
<widget class="QPushButton" name="btn_reset">
223+
<item row="2" column="0">
224+
<widget class="QPushButton" name="btn_help">
216225
<property name="minimumSize">
217226
<size>
218227
<width>200</width>
@@ -221,73 +230,64 @@ xpn</string>
221230
</property>
222231
<property name="maximumSize">
223232
<size>
224-
<width>16777215</width>
233+
<width>500</width>
225234
<height>30</height>
226235
</size>
227236
</property>
228-
<property name="autoFillBackground">
229-
<bool>true</bool>
237+
<property name="locale">
238+
<locale language="English" country="UnitedStates"/>
230239
</property>
231240
<property name="text">
232-
<string>Reset setttings to factory defaults</string>
241+
<string>Help</string>
233242
</property>
234243
</widget>
235244
</item>
236-
<item row="2" column="0" colspan="2">
237-
<widget class="QCheckBox" name="opt_debug">
245+
<item row="2" column="1">
246+
<widget class="QPushButton" name="btn_report">
238247
<property name="minimumSize">
239248
<size>
240-
<width>0</width>
249+
<width>200</width>
241250
<height>25</height>
242251
</size>
243252
</property>
244253
<property name="maximumSize">
245254
<size>
246-
<width>16777215</width>
255+
<width>500</width>
247256
<height>30</height>
248257
</size>
249258
</property>
250-
<property name="toolTip">
251-
<string>Enable debug mode.</string>
252-
</property>
253-
<property name="autoFillBackground">
254-
<bool>true</bool>
255-
</property>
256259
<property name="locale">
257260
<locale language="English" country="UnitedStates"/>
258261
</property>
259262
<property name="text">
260-
<string>Debug mode (degraded performances)</string>
261-
</property>
262-
<property name="tristate">
263-
<bool>false</bool>
263+
<string>Report an issue</string>
264264
</property>
265265
</widget>
266266
</item>
267-
<item row="0" column="1">
268-
<widget class="QPushButton" name="btn_report">
267+
<item row="6" column="0">
268+
<widget class="QLabel" name="lbl_version_saved">
269269
<property name="minimumSize">
270270
<size>
271-
<width>200</width>
271+
<width>0</width>
272272
<height>25</height>
273273
</size>
274274
</property>
275275
<property name="maximumSize">
276276
<size>
277-
<width>500</width>
277+
<width>16777215</width>
278278
<height>30</height>
279279
</size>
280280
</property>
281281
<property name="locale">
282282
<locale language="English" country="UnitedStates"/>
283283
</property>
284284
<property name="text">
285-
<string>Report an issue</string>
285+
<string>Version used to save settings:</string>
286286
</property>
287287
</widget>
288288
</item>
289-
<item row="0" column="0">
290-
<widget class="QPushButton" name="btn_help">
289+
<item row="7" column="0" colspan="2">
290+
<widget class="QPushButton" name="btn_reset">
291291
<property name="minimumSize">
292292
<size>
293293
<width>200</width>
@@ -296,19 +296,35 @@ xpn</string>
296296
</property>
297297
<property name="maximumSize">
298298
<size>
299-
<width>500</width>
299+
<width>16777215</width>
300300
<height>30</height>
301301
</size>
302302
</property>
303-
<property name="locale">
304-
<locale language="English" country="UnitedStates"/>
303+
<property name="autoFillBackground">
304+
<bool>true</bool>
305305
</property>
306306
<property name="text">
307-
<string>Help</string>
307+
<string>Reset setttings to factory defaults</string>
308308
</property>
309309
</widget>
310310
</item>
311-
<item row="1" column="0">
311+
<item row="0" column="0">
312+
<widget class="QCheckBox" name="opt_toolbar_browser_shortcut">
313+
<property name="contextMenuPolicy">
314+
<enum>Qt::NoContextMenu</enum>
315+
</property>
316+
<property name="toolTip">
317+
<string>Display a shortcut to open the resource browser in the plugin toolbar</string>
318+
</property>
319+
<property name="text">
320+
<string>Shortcut icon in toolbar</string>
321+
</property>
322+
<property name="tristate">
323+
<bool>false</bool>
324+
</property>
325+
</widget>
326+
</item>
327+
<item row="3" column="0" colspan="2">
312328
<spacer name="verticalSpacer_2">
313329
<property name="orientation">
314330
<enum>Qt::Vertical</enum>

pyqgis_resource_browser/gui/resource_browser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ def __init__(self, *args, **kwds):
112112
partial(iface.showOptionsDialog, currentPage=f"mOptionsPage{__title__}")
113113
)
114114

115-
self.reloadConfig()
115+
self.slot_config_changed()
116116

117-
def reloadConfig(self):
117+
def slot_config_changed(self):
118+
"""When settings have been saved."""
118119
settings = PlgOptionsManager.get_plg_settings()
119120
if settings.filter_prefixes:
120121
self.resourceProxyModel.setPrefixFilters(settings.prefix_filters)

pyqgis_resource_browser/plugin_main.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
from functools import partial
99
from pathlib import Path
1010

11-
# PyQGIS
1211
from qgis.core import QgsApplication, QgsSettings
1312
from qgis.gui import QgisInterface
13+
14+
# PyQGIS
1415
from qgis.PyQt.QtCore import QCoreApplication, QLocale, QTranslator, QUrl
1516
from qgis.PyQt.QtGui import QDesktopServices, QIcon
1617
from qgis.PyQt.QtWidgets import QAction
@@ -25,7 +26,7 @@
2526
)
2627
from pyqgis_resource_browser.gui.dlg_settings import PlgOptionsFactory
2728
from pyqgis_resource_browser.gui.resource_browser import ResourceBrowser
28-
from pyqgis_resource_browser.toolbelt import PlgLogger
29+
from pyqgis_resource_browser.toolbelt import PlgLogger, PlgOptionsManager
2930

3031
# ############################################################################
3132
# ########## Classes ###############
@@ -40,15 +41,18 @@ def __init__(self, iface: QgisInterface):
4041
provides the hook by which you can manipulate the QGIS application at run time.
4142
:type iface: QgsInterface
4243
"""
44+
# set attributes
45+
self.action_browse_resources = None
46+
self.action_help = None
4347
self.action_help_plugin_menu_cheatsheet = None
4448
self.action_help_plugin_menu_documentation = None
4549
self.action_settings = None
46-
self.action_browse_resources = None
50+
self.action_toolbar = None
51+
self.browser: ResourceBrowser = None
4752
self.options_factory: PlgOptionsFactory = None
48-
self.action_help = None
4953
self.iface = iface
5054
self.log = PlgLogger().log
51-
self.browser: ResourceBrowser = None
55+
self.plg_settings = PlgOptionsManager()
5256

5357
# initialize the locale
5458
self.locale: str = QgsSettings().value("locale/userLocale", QLocale().name())[
@@ -65,6 +69,7 @@ def __init__(self, iface: QgisInterface):
6569

6670
def initGui(self):
6771
"""Set up plugin UI elements."""
72+
settings = self.plg_settings.get_plg_settings()
6873

6974
# settings page within the QGIS preferences menu
7075
if not self.options_factory:
@@ -102,6 +107,11 @@ def initGui(self):
102107
self.iface.addPluginToMenu(__title__, self.action_settings)
103108
self.iface.addPluginToMenu(__title__, self.action_help)
104109

110+
# -- Toolbar
111+
if settings.toolbar_browser_shortcut and not self.action_toolbar:
112+
self.action_toolbar = self.action_browse_resources
113+
self.iface.addToolBarIcon(self.action_toolbar)
114+
105115
# -- Help menu
106116

107117
# documentation
@@ -131,6 +141,26 @@ def initGui(self):
131141

132142
self.iface.pluginHelpMenu().addAction(self.action_help_plugin_menu_cheatsheet)
133143

144+
def slot_config_changed(self):
145+
"""When settings have been saved."""
146+
settings = self.plg_settings.get_plg_settings()
147+
148+
# toolbar icon or not
149+
if settings.toolbar_browser_shortcut and not self.action_toolbar:
150+
self.action_toolbar = self.action_browse_resources
151+
self.iface.addToolBarIcon(self.action_toolbar)
152+
self.log(
153+
message="DEBUG - Config changed: toolbar shortcut has been enabled.",
154+
log_level=4,
155+
)
156+
elif not settings.toolbar_browser_shortcut and self.action_toolbar:
157+
self.iface.removeToolBarIcon(self.action_toolbar)
158+
self.action_toolbar = None
159+
self.log(
160+
message="DEBUG - Config changed: toolbar shortcut has been removed.",
161+
log_level=4,
162+
)
163+
134164
def tr(self, message: str) -> str:
135165
"""Get the translation for a string using Qt translation API.
136166
@@ -157,6 +187,11 @@ def unload(self):
157187
self.action_help_plugin_menu_cheatsheet
158188
)
159189

190+
# -- Clean up toolbar
191+
if self.action_toolbar:
192+
self.iface.removeToolBarIcon(self.action_toolbar)
193+
self.action_toolbar = None
194+
160195
# -- Clean up preferences panel in QGIS settings
161196
self.iface.unregisterOptionsWidgetFactory(self.options_factory)
162197

@@ -173,20 +208,21 @@ def run(self):
173208
try:
174209
if not isinstance(self.browser, ResourceBrowser):
175210
self.browser = ResourceBrowser()
176-
self.options_factory.configChanged.connect(self.browser.reloadConfig)
211+
self.options_factory.configChanged.connect(
212+
self.browser.slot_config_changed
213+
)
214+
self.options_factory.configChanged.connect(self.slot_config_changed)
177215
self.browser.show()
178216
self.log(
179-
message=self.tr(
180-
message="Everything ran OK.",
181-
),
217+
message="Everything ran OK.",
182218
log_level=3,
183219
push=False,
184220
)
185221
except Exception as err:
186222
self.log(
187-
message=self.tr(
188-
message=f"Houston, we've got a problem: {err}",
189-
),
223+
message=f"Houston, we've got a problem: {err}",
190224
log_level=2,
191225
push=True,
226+
duration=60,
227+
button=True,
192228
)

pyqgis_resource_browser/toolbelt/preferences.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class PlgSettingsStructure:
4545
default_factory=lambda: ["ico", "png", "svg", "xpn"]
4646
)
4747

48+
# misc
49+
toolbar_browser_shortcut: bool = True
50+
4851

4952
class PlgOptionsManager:
5053
"""Class to deal with settings: get, set."""

0 commit comments

Comments
 (0)