Chore/code cleanup (#43)

* čištění kódu podle flake8

* Update .gitignore

* Update .gitignore (#41)

* oprava komentářů a překlad do angličtiny

* oprava přihlašování
This commit is contained in:
2026-06-04 17:32:32 +02:00
committed by GitHub
parent b313fc6db0
commit 493696c67b
4 changed files with 732 additions and 360 deletions
+51 -30
View File
@@ -3,35 +3,37 @@ from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QMenu, QAction, QToolButton, QDialog
from qgis.core import Qgis
from qgis.utils import iface
from .amcr_tools import load_amcr_data, login_to_api
from .amcr_dialog import AmcrFilterDialog, LoginDialog
from .resources import *
import os.path
class AmcrViewer:
"""
Main plugin class that manages the GUI elements, menu entries,
Main plugin class that manages the GUI elements, menu entries,
and coordinates the flow between user input and data processing.
"""
def __init__(self, iface):
"""
Constructor initializes the connection to QGIS interface and sets up
Constructor initializes the connection to QGIS interface and sets up
internationalization (i18n).
"""
self.iface = iface
self.plugin_dir = os.path.dirname(__file__)
# Determine the user's locale to load appropriate translation files
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'AmcrViewer_{}.qm'.format(locale))
'AmcrViewer_{}.qm'.format(locale)
)
# Install the translator if a translation file for the current locale exists
# Install the translator if a translation file
# for the current locale exists
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
@@ -43,14 +45,17 @@ class AmcrViewer:
self.first_start = None
def tr(self, message):
"""Helper method for translating strings within the AmcrViewer context."""
"""
Helper method for translating strings within
the AmcrViewer context.
"""
return QCoreApplication.translate('AmcrViewer', message)
def add_action(self, icon_path, text, callback, enabled_flag=True,
add_to_menu=True, add_to_toolbar=True, status_tip=None,
whats_this=None, parent=None):
"""
Helper method to create QActions and automatically register them
Helper method to create QActions and automatically register them
into the QGIS Menu and Toolbar.
"""
icon = QIcon(icon_path)
@@ -71,15 +76,16 @@ class AmcrViewer:
if add_to_menu:
self.iface.addPluginToMenu(self.menu, action)
# Store only actions that are directly attached to the QGIS UI for later cleanup
# Store only actions that are directly attached
# to the QGIS UI for later cleanup
if add_to_toolbar or add_to_menu:
self.actions.append(action)
return action
def initGui(self):
"""
Called when the plugin is loaded. Creates the menu structure,
Called when the plugin is loaded. Creates the menu structure,
sub-actions, and the dropdown tool button in the toolbar.
"""
# Define paths for action-specific icons
@@ -90,7 +96,8 @@ class AmcrViewer:
self.plugin_menu = QMenu()
# 2. Create sub-actions (Download Projects / Download Sites)
# add_to_menu/toolbar is False because these go into our custom dropdown menu
# add_to_menu/toolbar is False because these go into our
# custom dropdown menu
self.action_download_akce = self.add_action(
icon_path=icon_akce_path,
text=self.tr(u'Stáhnout data akcí | AMČR Viewer'),
@@ -104,7 +111,7 @@ class AmcrViewer:
self.action_download_lokality = self.add_action(
icon_path=icon_lokality_path,
text=self.tr(u'Stáhnout data lokalit | AMČR Viewer'),
callback=lambda checked=False: self.run_download('lokalita'),
callback=lambda checked=False: self.run_download('lokalita'),
parent=self.iface.mainWindow(),
add_to_menu=False,
add_to_toolbar=False
@@ -114,7 +121,7 @@ class AmcrViewer:
self.action_login_dialog = self.add_action(
icon_path=icon_akce_path,
text=self.tr(u'Přihlásit se | AMČR Viewer'),
callback=lambda checked=False: self.login(),
callback=lambda checked=False: self.login(),
parent=self.iface.mainWindow(),
add_to_menu=False,
add_to_toolbar=False
@@ -123,7 +130,11 @@ class AmcrViewer:
# 3. Create the main project action and attach the menu to it
main_icon = QIcon(icon_akce_path)
self.main_action = QAction(main_icon, 'AMČR Viewer', self.iface.mainWindow())
self.main_action = QAction(
main_icon,
'AMČR Viewer',
self.iface.mainWindow()
)
self.main_action.setMenu(self.plugin_menu)
self.iface.addPluginToMenu(self.menu, self.main_action)
@@ -132,16 +143,19 @@ class AmcrViewer:
self.tool_button = QToolButton()
self.tool_button.setMenu(self.plugin_menu)
self.tool_button.setDefaultAction(self.action_download_akce)
self.tool_button.setPopupMode(QToolButton.ToolButtonPopupMode.MenuButtonPopup)
# Add the widget directly to the toolbar and store the reference for cleanup
self.tool_button.setPopupMode(
QToolButton.ToolButtonPopupMode.MenuButtonPopup
)
# Add the widget directly to the toolbar
# and store the reference for cleanup
self.toolbar_action = self.iface.addToolBarWidget(self.tool_button)
self.first_start = True
def unload(self):
"""
Called when the plugin is disabled or removed.
Called when the plugin is disabled or removed.
Ensures all GUI elements are removed from QGIS to avoid ghost icons.
"""
# 1. Remove the custom entry from the main 'Plugins' menu
@@ -157,7 +171,7 @@ class AmcrViewer:
self.iface.removePluginMenu(self.menu, action)
self.iface.removeToolBarIcon(action)
self.actions.clear()
# 4. Reset map tools if currently active
if hasattr(self, 'tool'):
self.iface.mapCanvas().unsetMapTool(self.tool)
@@ -165,20 +179,22 @@ class AmcrViewer:
# --- Data downloading ---
def run_download(self, typ_dat):
"""
Triggered by menu/toolbar actions. Opens the filter dialog and
Triggered by menu/toolbar actions. Opens the filter dialog and
hands off the parameters to the data loader.
"""
# Open the specific filter dialog (Projects vs Sites)
dlg = AmcrFilterDialog(typ_dat)
result = dlg.exec()
# If user confirmed the dialog (OK button), gather filters and load data
# If user confirmed the dialog (OK button),
# gather filters and load data
if result == QDialog.DialogCode.Accepted:
filters = dlg.get_filters()
bbox = dlg.get_bbox()
komponenty = dlg.get_komponenty()
# Access the map canvas and start the fetch/render process from amcr_tools
# Access the map canvas and start
# the fetch/render process from amcr_tools
canvas = self.iface.mapCanvas()
load_amcr_data(canvas, bbox, filters, typ_dat, komponenty)
@@ -190,9 +206,14 @@ class AmcrViewer:
session = login_to_api(username, password)
if session:
self.iface.messageBar().pushMessage(
"AMČR", "Přihlášení proběhlo úspěšně.", level=Qgis.MessageLevel.Success
"AMČR",
"Přihlášení proběhlo úspěšně.",
level=Qgis.MessageLevel.Success
)
else:
self.iface.messageBar().pushMessage(
"AMČR", "Přihlášení se nezdařilo viz záložka AMČR login v panelu Zprávy.", level=Qgis.MessageLevel.Critical
)
"AMČR",
"Přihlášení se nezdařilo viz záložka AMČR login "
"v panelu Zprávy.",
level=Qgis.MessageLevel.Critical
)