Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to enable/disable a toolbar icon #25

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyqgis_resource_browser/gui/dlg_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def apply(self):

# misc
settings.debug_mode = self.opt_debug.isChecked()
settings.toolbar_browser_shortcut = (
self.opt_toolbar_browser_shortcut.isChecked()
)
settings.version = __version__

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

# global
self.opt_debug.setChecked(settings.debug_mode)
self.opt_toolbar_browser_shortcut.setChecked(settings.toolbar_browser_shortcut)
self.lbl_version_saved_value.setText(settings.version)

self.gb_filter_resourceprefix.setChecked(settings.filter_prefixes)
Expand Down
100 changes: 58 additions & 42 deletions pyqgis_resource_browser/gui/dlg_settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>673</width>
<height>525</height>
<width>690</width>
<height>555</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -164,8 +164,8 @@ xpn</string>
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="1">
<widget class="QLabel" name="lbl_version_saved_value">
<item row="1" column="0">
<widget class="QCheckBox" name="opt_debug">
<property name="minimumSize">
<size>
<width>0</width>
Expand All @@ -178,19 +178,25 @@ xpn</string>
<height>30</height>
</size>
</property>
<property name="toolTip">
<string>Enable debug mode.</string>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="text">
<string notr="true">X.X.x</string>
<string>Debug mode (degraded performances)</string>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="lbl_version_saved">
<item row="6" column="1">
<widget class="QLabel" name="lbl_version_saved_value">
<property name="minimumSize">
<size>
<width>0</width>
Expand All @@ -207,12 +213,15 @@ xpn</string>
<locale language="English" country="UnitedStates"/>
</property>
<property name="text">
<string>Version used to save settings:</string>
<string notr="true">X.X.x</string>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QPushButton" name="btn_reset">
<item row="2" column="0">
<widget class="QPushButton" name="btn_help">
<property name="minimumSize">
<size>
<width>200</width>
Expand All @@ -221,73 +230,64 @@ xpn</string>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<width>500</width>
<height>30</height>
</size>
</property>
<property name="autoFillBackground">
<bool>true</bool>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="text">
<string>Reset setttings to factory defaults</string>
<string>Help</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="opt_debug">
<item row="2" column="1">
<widget class="QPushButton" name="btn_report">
<property name="minimumSize">
<size>
<width>0</width>
<width>200</width>
<height>25</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<width>500</width>
<height>30</height>
</size>
</property>
<property name="toolTip">
<string>Enable debug mode.</string>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="text">
<string>Debug mode (degraded performances)</string>
</property>
<property name="tristate">
<bool>false</bool>
<string>Report an issue</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="btn_report">
<item row="6" column="0">
<widget class="QLabel" name="lbl_version_saved">
<property name="minimumSize">
<size>
<width>200</width>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>500</width>
<width>16777215</width>
<height>30</height>
</size>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
<property name="text">
<string>Report an issue</string>
<string>Version used to save settings:</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="btn_help">
<item row="7" column="0" colspan="2">
<widget class="QPushButton" name="btn_reset">
<property name="minimumSize">
<size>
<width>200</width>
Expand All @@ -296,19 +296,35 @@ xpn</string>
</property>
<property name="maximumSize">
<size>
<width>500</width>
<width>16777215</width>
<height>30</height>
</size>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="text">
<string>Help</string>
<string>Reset setttings to factory defaults</string>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="0" column="0">
<widget class="QCheckBox" name="opt_toolbar_browser_shortcut">
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
<property name="toolTip">
<string>Display a shortcut to open the resource browser in the plugin toolbar</string>
</property>
<property name="text">
<string>Shortcut icon in toolbar</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand Down
5 changes: 3 additions & 2 deletions pyqgis_resource_browser/gui/resource_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ def __init__(self, *args, **kwds):
partial(iface.showOptionsDialog, currentPage=f"mOptionsPage{__title__}")
)

self.reloadConfig()
self.slot_config_changed()

def reloadConfig(self):
def slot_config_changed(self):
"""When settings have been saved."""
settings = PlgOptionsManager.get_plg_settings()
if settings.filter_prefixes:
self.resourceProxyModel.setPrefixFilters(settings.prefix_filters)
Expand Down
60 changes: 48 additions & 12 deletions pyqgis_resource_browser/plugin_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from functools import partial
from pathlib import Path

# PyQGIS
from qgis.core import QgsApplication, QgsSettings
from qgis.gui import QgisInterface

# PyQGIS
from qgis.PyQt.QtCore import QCoreApplication, QLocale, QTranslator, QUrl
from qgis.PyQt.QtGui import QDesktopServices, QIcon
from qgis.PyQt.QtWidgets import QAction
Expand All @@ -25,7 +26,7 @@
)
from pyqgis_resource_browser.gui.dlg_settings import PlgOptionsFactory
from pyqgis_resource_browser.gui.resource_browser import ResourceBrowser
from pyqgis_resource_browser.toolbelt import PlgLogger
from pyqgis_resource_browser.toolbelt import PlgLogger, PlgOptionsManager

# ############################################################################
# ########## Classes ###############
Expand All @@ -40,15 +41,18 @@ def __init__(self, iface: QgisInterface):
provides the hook by which you can manipulate the QGIS application at run time.
:type iface: QgsInterface
"""
# set attributes
self.action_browse_resources = None
self.action_help = None
self.action_help_plugin_menu_cheatsheet = None
self.action_help_plugin_menu_documentation = None
self.action_settings = None
self.action_browse_resources = None
self.action_toolbar = None
self.browser: ResourceBrowser = None
self.options_factory: PlgOptionsFactory = None
self.action_help = None
self.iface = iface
self.log = PlgLogger().log
self.browser: ResourceBrowser = None
self.plg_settings = PlgOptionsManager()

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

def initGui(self):
"""Set up plugin UI elements."""
settings = self.plg_settings.get_plg_settings()

# settings page within the QGIS preferences menu
if not self.options_factory:
Expand Down Expand Up @@ -102,6 +107,11 @@ def initGui(self):
self.iface.addPluginToMenu(__title__, self.action_settings)
self.iface.addPluginToMenu(__title__, self.action_help)

# -- Toolbar
if settings.toolbar_browser_shortcut and not self.action_toolbar:
self.action_toolbar = self.action_browse_resources
self.iface.addToolBarIcon(self.action_toolbar)

# -- Help menu

# documentation
Expand Down Expand Up @@ -131,6 +141,26 @@ def initGui(self):

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

def slot_config_changed(self):
"""When settings have been saved."""
settings = self.plg_settings.get_plg_settings()

# toolbar icon or not
if settings.toolbar_browser_shortcut and not self.action_toolbar:
self.action_toolbar = self.action_browse_resources
self.iface.addToolBarIcon(self.action_toolbar)
self.log(
message="DEBUG - Config changed: toolbar shortcut has been enabled.",
log_level=4,
)
elif not settings.toolbar_browser_shortcut and self.action_toolbar:
self.iface.removeToolBarIcon(self.action_toolbar)
self.action_toolbar = None
self.log(
message="DEBUG - Config changed: toolbar shortcut has been removed.",
log_level=4,
)

def tr(self, message: str) -> str:
"""Get the translation for a string using Qt translation API.

Expand All @@ -157,6 +187,11 @@ def unload(self):
self.action_help_plugin_menu_cheatsheet
)

# -- Clean up toolbar
if self.action_toolbar:
self.iface.removeToolBarIcon(self.action_toolbar)
self.action_toolbar = None

# -- Clean up preferences panel in QGIS settings
self.iface.unregisterOptionsWidgetFactory(self.options_factory)

Expand All @@ -173,20 +208,21 @@ def run(self):
try:
if not isinstance(self.browser, ResourceBrowser):
self.browser = ResourceBrowser()
self.options_factory.configChanged.connect(self.browser.reloadConfig)
self.options_factory.configChanged.connect(
self.browser.slot_config_changed
)
self.options_factory.configChanged.connect(self.slot_config_changed)
self.browser.show()
self.log(
message=self.tr(
message="Everything ran OK.",
),
message="Everything ran OK.",
log_level=3,
push=False,
)
except Exception as err:
self.log(
message=self.tr(
message=f"Houston, we've got a problem: {err}",
),
message=f"Houston, we've got a problem: {err}",
log_level=2,
push=True,
duration=60,
button=True,
)
3 changes: 3 additions & 0 deletions pyqgis_resource_browser/toolbelt/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class PlgSettingsStructure:
default_factory=lambda: ["ico", "png", "svg", "xpn"]
)

# misc
toolbar_browser_shortcut: bool = True


class PlgOptionsManager:
"""Class to deal with settings: get, set."""
Expand Down