Actualiser CenRa_METABASE/CenRa_Metabase.py

This commit is contained in:
Tom LAVEILLE 2024-12-19 11:49:40 +01:00
parent 5e71a891d0
commit 7a3f223024

View File

@ -1,218 +1,218 @@
__copyright__ = "Copyright 2021, 3Liz" __copyright__ = "Copyright 2021, 3Liz"
__license__ = "GPL version 3" __license__ = "GPL version 3"
__email__ = "info@3liz.org" __email__ = "info@3liz.org"
from qgis.core import QgsApplication from qgis.core import QgsApplication
from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator, QUrl from qgis.PyQt.QtCore import QCoreApplication, Qt, QTranslator, QUrl
from qgis.PyQt.QtGui import QDesktopServices, QIcon from qgis.PyQt.QtGui import QDesktopServices, QIcon
from qgis.PyQt.QtWidgets import QAction, QMessageBox from qgis.PyQt.QtWidgets import QAction, QMessageBox
from qgis.utils import iface from qgis.utils import iface
import qgis import qgis
#include <QSettings> #include <QSettings>
''' '''
from pg_metadata.connection_manager import ( from pg_metadata.connection_manager import (
store_connections, store_connections,
validate_connections_names, validate_connections_names,
) )
from pg_metadata.locator import LocatorFilter from pg_metadata.locator import LocatorFilter
from pg_metadata.processing.provider import PgMetadataProvider from pg_metadata.processing.provider import PgMetadataProvider
from pg_metadata.qgis_plugin_tools.tools.custom_logging import setup_logger from pg_metadata.qgis_plugin_tools.tools.custom_logging import setup_logger
''' '''
import os import os
from .tools.resources import ( from .tools.resources import (
plugin_path, plugin_path,
resources_path, resources_path,
maj_verif, maj_verif,
) )
from .dock import CenRa_Metabase from .dock import CenRa_Metabase
from .editor import Metabase_Editor from .editor import Metabase_Editor
from .about_form import MetabaseAboutDialog from .about_form import MetabaseAboutDialog
#from CenRa_Metabase.issues import CenRa_Issues #from CenRa_Metabase.issues import CenRa_Issues
from PyQt5.QtCore import * from PyQt5.QtCore import *
class PgMetadata: class PgMetadata:
def __init__(self): def __init__(self):
""" Constructor. """ """ Constructor. """
self.dock = None self.dock = None
self.editor = None self.editor = None
# self.issues = None # self.issues = None
self.provider = None self.provider = None
self.locator_filter = None self.locator_filter = None
self.dock_action = None self.dock_action = None
self.help_action = None self.help_action = None
plugin_dir = os.path.dirname(__file__) plugin_dir = os.path.dirname(__file__)
end_find = plugin_dir.rfind('\\')+1 end_find = plugin_dir.rfind('\\')+1
global NAME global NAME
NAME = plugin_dir[end_find:] NAME = plugin_dir[end_find:]
maj_verif(NAME) maj_verif(NAME)
# Display About window on first use # Display About window on first use
version = qgis.utils.pluginMetadata('CenRa_METABASE','version') version = qgis.utils.pluginMetadata('CenRa_METABASE','version')
s = QSettings() s = QSettings()
versionUse = s.value("metadata/version", 1, type=str) versionUse = s.value("metadata/version", 1, type=str)
if str(versionUse) != str(version) : if str(versionUse) != str(version) :
s.setValue("metadata/version", str(version)) s.setValue("metadata/version", str(version))
print(versionUse,version) print(versionUse,version)
self.open_about_dialog() self.open_about_dialog()
# setup_logger('pg_metadata') # setup_logger('pg_metadata')
#locale, file_path = setup_translation( #locale, file_path = setup_translation(
# folder=plugin_path("i18n"), file_pattern="CenRa_Metabase_{}.qm") # folder=plugin_path("i18n"), file_pattern="CenRa_Metabase_{}.qm")
#if file_path: #if file_path:
# self.translator = QTranslator() # self.translator = QTranslator()
# self.translator.load(file_path) # self.translator.load(file_path)
# noinspection PyCallByClass,PyArgumentList # noinspection PyCallByClass,PyArgumentList
# QCoreApplication.installTranslator(self.translator) # QCoreApplication.installTranslator(self.translator)
# noinspection PyPep8Naming # noinspection PyPep8Naming
#def initProcessing(self): #def initProcessing(self):
#""" Add the QGIS Processing provider. """ #""" Add the QGIS Processing provider. """
#if not self.provider: #if not self.provider:
#self.provider = PgMetadataProvider() #self.provider = PgMetadataProvider()
#QgsApplication.processingRegistry().addProvider(self.provider) #QgsApplication.processingRegistry().addProvider(self.provider)
# noinspection PyPep8Naming # noinspection PyPep8Naming
def initGui(self): def initGui(self):
""" Build the plugin GUI. """ """ Build the plugin GUI. """
#self.initProcessing() #self.initProcessing()
#self.check_invalid_connection_names() #self.check_invalid_connection_names()
self.toolBar = iface.addToolBar("CenRa_Metabase") self.toolBar = iface.addToolBar("CenRa_Metabase")
self.toolBar.setObjectName("CenRa_Metabase") self.toolBar.setObjectName("CenRa_Metabase")
icon = QIcon(resources_path('icons', 'icon.png')) icon = QIcon(resources_path('icons', 'icon.png'))
icon2 = QIcon(resources_path('icons', 'icon_2.png')) icon2 = QIcon(resources_path('icons', 'icon_2.png'))
# Open the online help # Open the online help
self.help_action = QAction(icon, 'CenRa_Metabase', iface.mainWindow()) self.help_action = QAction(icon, 'CenRa_Metabase', iface.mainWindow())
iface.pluginHelpMenu().addAction(self.help_action) iface.pluginHelpMenu().addAction(self.help_action)
self.help_action.triggered.connect(self.open_help) self.help_action.triggered.connect(self.open_help)
if not self.editor: if not self.editor:
self.editor = Metabase_Editor() self.editor = Metabase_Editor()
self.editor_action = QAction(icon2, 'CenRa_Metabase',None) self.editor_action = QAction(icon2, 'CenRa_Metabase',None)
self.toolBar.addAction(self.editor_action) self.toolBar.addAction(self.editor_action)
self.editor_action.triggered.connect(self.open_editor) self.editor_action.triggered.connect(self.open_editor)
if not self.dock: if not self.dock:
self.dock = CenRa_Metabase() self.dock = CenRa_Metabase()
iface.addDockWidget(Qt.RightDockWidgetArea, self.dock) iface.addDockWidget(Qt.RightDockWidgetArea, self.dock)
# Open/close the dock from plugin menu # Open/close the dock from plugin menu
self.dock_action = QAction(icon, 'CenRa_Metabase', iface.mainWindow()) self.dock_action = QAction(icon, 'CenRa_Metabase', iface.mainWindow())
iface.pluginMenu().addAction(self.dock_action) iface.pluginMenu().addAction(self.dock_action)
self.dock_action.triggered.connect(self.open_dock) self.dock_action.triggered.connect(self.open_dock)
# if not self.issues: # if not self.issues:
# self.issues = CenRa_Issues() # self.issues = CenRa_Issues()
# self.issues_action = QAction(icon, 'CenRa_Metabase',None) # self.issues_action = QAction(icon, 'CenRa_Metabase',None)
# self.toolBar.addAction(self.issues_action) # self.toolBar.addAction(self.issues_action)
# self.issues_action.triggered.connect(self.open_issues) # self.issues_action.triggered.connect(self.open_issues)
''' '''
if not self.locator_filter: if not self.locator_filter:
self.locator_filter = LocatorFilter(iface) self.locator_filter = LocatorFilter(iface)
iface.registerLocatorFilter(self.locator_filter) iface.registerLocatorFilter(self.locator_filter)
@staticmethod @staticmethod
def check_invalid_connection_names(): def check_invalid_connection_names():
""" Check for invalid connection names in the QgsSettings. """ """ Check for invalid connection names in the QgsSettings. """
valid, invalid = validate_connections_names() valid, invalid = validate_connections_names()
n_invalid = len(invalid) n_invalid = len(invalid)
if n_invalid == 0: if n_invalid == 0:
return return
invalid_text = ', '.join(invalid) invalid_text = ', '.join(invalid)
msg = QMessageBox() msg = QMessageBox()
msg.setIcon(QMessageBox.Warning) msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle(tr('PgMetadata: Database connection(s) not available')) msg.setWindowTitle(tr('PgMetadata: Database connection(s) not available'))
msg.setText(tr( msg.setText(tr(
f'{n_invalid} connection(s) listed in PgMetadatas settings are invalid or ' f'{n_invalid} connection(s) listed in PgMetadatas settings are invalid or '
f'no longer available: {invalid_text}')) f'no longer available: {invalid_text}'))
msg.setInformativeText(tr( msg.setInformativeText(tr(
'Do you want to remove these connection(s) from the PgMetadata settings? ' 'Do you want to remove these connection(s) from the PgMetadata settings? '
'(You can also do this later with the “Set Connections” tool.)')) '(You can also do this later with the “Set Connections” tool.)'))
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No) msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
clicked = msg.exec() clicked = msg.exec()
if clicked == QMessageBox.Yes: if clicked == QMessageBox.Yes:
iface.messageBar().pushSuccess('PgMetadata', tr(f'{n_invalid} invalid connection(s) removed.')) iface.messageBar().pushSuccess('PgMetadata', tr(f'{n_invalid} invalid connection(s) removed.'))
store_connections(valid) store_connections(valid)
if clicked == QMessageBox.No: if clicked == QMessageBox.No:
iface.messageBar().pushInfo('PgMetadata', tr(f'Keeping {n_invalid} invalid connections.')) iface.messageBar().pushInfo('PgMetadata', tr(f'Keeping {n_invalid} invalid connections.'))
''' '''
def open_about_dialog(self): def open_about_dialog(self):
""" """
About dialog About dialog
""" """
dialog = MetabaseAboutDialog(iface) dialog = MetabaseAboutDialog(iface)
dialog.exec_() dialog.exec_()
def open_help(): def open_help():
""" Open the online help. """ """ Open the online help. """
QDesktopServices.openUrl(QUrl('https://plateformesig.cenra-outils.org/')) QDesktopServices.openUrl(QUrl('https://plateformesig.cenra-outils.org/'))
def open_dock(self): def open_dock(self):
""" Open the dock. """ """ Open the dock. """
self.dock.show() self.dock.show()
self.dock.raise_() self.dock.raise_()
def open_editor(self): def open_editor(self):
self.editor.show() self.editor.show()
self.editor.raise_() self.editor.raise_()
# def open_issues(self): # def open_issues(self):
# self.issues.show() # self.issues.show()
# self.issues.raise_() # self.issues.raise_()
def unload(self): def unload(self):
""" Unload the plugin. """ """ Unload the plugin. """
#if self.editor: #if self.editor:
#iface.removePluginMenu('CenRa_Metabase',self.editor_action) #iface.removePluginMenu('CenRa_Metabase',self.editor_action)
#self.editor.removeToolBarIcon(self.editor_action) #self.editor.removeToolBarIcon(self.editor_action)
if self.dock: if self.dock:
iface.removeDockWidget(self.dock) iface.removeDockWidget(self.dock)
self.dock.deleteLater() self.dock.deleteLater()
if self.provider: if self.provider:
QgsApplication.processingRegistry().removeProvider(self.provider) QgsApplication.processingRegistry().removeProvider(self.provider)
del self.provider del self.provider
if self.locator_filter: if self.locator_filter:
iface.deregisterLocatorFilter(self.locator_filter) iface.deregisterLocatorFilter(self.locator_filter)
del self.locator_filter del self.locator_filter
if self.dock_action: if self.dock_action:
iface.pluginMenu().removeAction(self.dock_action) iface.pluginMenu().removeAction(self.dock_action)
del self.dock_action del self.dock_action
if self.help_action: if self.help_action:
iface.pluginHelpMenu().removeAction(self.help_action) iface.pluginHelpMenu().removeAction(self.help_action)
del self.help_action del self.help_action
@staticmethod @staticmethod
def run_tests(pattern='test_*.py', package=None): def run_tests(pattern='test_*.py', package=None):
"""Run the test inside QGIS.""" """Run the test inside QGIS."""
try: try:
from pathlib import Path from pathlib import Path
from pg_metadata.qgis_plugin_tools.infrastructure.test_runner import ( from pg_metadata.qgis_plugin_tools.infrastructure.test_runner import (
test_package, test_package,
) )
if package is None: if package is None:
package = '{}.__init__'.format(Path(__file__).parent.name) package = '{}.__init__'.format(Path(__file__).parent.name)
test_package(package, pattern) test_package(package, pattern)
except (AttributeError, ModuleNotFoundError): except (AttributeError, ModuleNotFoundError):
message = 'Could not load tests. Are you using a production package?' message = 'Could not load tests. Are you using a production package?'
print(message) # NOQA print(message) # NOQA