257 lines
9.1 KiB
Python
257 lines
9.1 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
from qgis.PyQt.QtCore import QCoreApplication, QLocale, QSettings, Qt, QTranslator
|
|
|
|
from .compat import PLUGIN_DIR, QAction, icon
|
|
from .i18n import later, tr
|
|
|
|
MENU_NAME = "GeoDonkey"
|
|
|
|
TAB_OSM = 0
|
|
TAB_BASEMAPS = 1
|
|
TAB_TOOLS = 2
|
|
TAB_UPLOAD = 3
|
|
TAB_ACCOUNT = 4
|
|
|
|
TOOLS = (
|
|
("isochrone.svg", "geodonkey:isochrone", later("Isochrone"),
|
|
later("Reachable area from a point, by foot, bike or car")),
|
|
("route.svg", "geodonkey:route", later("Route"),
|
|
later("Route through several points, with distance, time and directions")),
|
|
("profile.svg", "geodonkey:elevationprofile", later("Elevation profile"),
|
|
later("Terrain profile along the straight line between two points")),
|
|
("elevation.svg", "geodonkey:elevationpoints", later("Point elevation"),
|
|
later("Add the elevation above sea level to every point of a layer")),
|
|
("lineofsight.svg", "geodonkey:lineofsight", later("Line of sight"),
|
|
later("Is there a clear line of sight between two points?")),
|
|
)
|
|
|
|
PANEL_TOOLS = (
|
|
"geodonkey:isochrone",
|
|
"geodonkey:route",
|
|
"geodonkey:elevationprofile",
|
|
"geodonkey:elevationpoints",
|
|
"geodonkey:lineofsight",
|
|
)
|
|
|
|
|
|
class GeoDonkeyPlugin(object):
|
|
def __init__(self, iface) -> None:
|
|
self.iface = iface
|
|
self.actions = []
|
|
self.panel = None
|
|
self.provider = None
|
|
self.translator = None
|
|
self.toolbar = None
|
|
self._install_translator()
|
|
|
|
def _install_translator(self) -> None:
|
|
locale = QSettings().value("locale/userLocale")
|
|
if not locale:
|
|
locale = QLocale.system().name()
|
|
code = str(locale)[:2]
|
|
path = os.path.join(PLUGIN_DIR, "i18n", "geodonkey_%s.qm" % code)
|
|
if not os.path.exists(path):
|
|
return
|
|
translator = QTranslator()
|
|
if translator.load(path):
|
|
QCoreApplication.installTranslator(translator)
|
|
self.translator = translator
|
|
|
|
def initGui(self) -> None: # noqa: N802
|
|
self.initProcessing()
|
|
|
|
self.toolbar = self.iface.addToolBar(MENU_NAME)
|
|
self.toolbar.setObjectName("GeoDonkeyToolbar")
|
|
|
|
self._add_action(
|
|
"osm.svg", tr("DonkeyOSM query"),
|
|
lambda: self.show_panel(TAB_OSM),
|
|
tr("Query OpenStreetMap data and load it as vector layers"))
|
|
self._add_action(
|
|
"basemap.svg", tr("GeoDonkey basemaps"),
|
|
lambda: self.show_panel(TAB_BASEMAPS),
|
|
tr("Add a GeoDonkey vector tile basemap"))
|
|
|
|
self.toolbar.addSeparator()
|
|
for icon_name, algorithm, label, tip in TOOLS:
|
|
action = (self._panel_tool(algorithm)
|
|
if algorithm in PANEL_TOOLS else self._tool(algorithm))
|
|
self._add_action(icon_name, tr(label), action, tr(tip))
|
|
|
|
for icon_name, algorithm, label, _tip in TOOLS:
|
|
if algorithm not in PANEL_TOOLS:
|
|
continue
|
|
self._add_action(
|
|
icon_name, tr("{tool} (dialog)…").format(tool=tr(label)),
|
|
self._tool(algorithm),
|
|
tr("The full Processing dialog, with all options"),
|
|
toolbar=False)
|
|
|
|
self.toolbar.addSeparator()
|
|
self._add_action(
|
|
"upload.svg", tr("Upload data"),
|
|
lambda: self.show_panel(TAB_UPLOAD),
|
|
tr("Upload a vector layer to GeoDonkey and manage your datasets"))
|
|
|
|
self.toolbar.addSeparator()
|
|
self._add_action(
|
|
"geodonkey.svg", tr("GeoDonkey settings…"),
|
|
self.open_settings,
|
|
tr("API key, API address, account and defaults"))
|
|
|
|
self._add_action(
|
|
"basemap.svg", tr("Add last basemap"),
|
|
self.add_last_basemap,
|
|
tr("Add the last used GeoDonkey map style as a basemap"),
|
|
toolbar=False)
|
|
self._add_action(
|
|
"account.svg", tr("GeoDonkey account"),
|
|
lambda: self.show_panel(TAB_ACCOUNT),
|
|
tr("Balance, plan and hourly limit of the stored API key"),
|
|
toolbar=False)
|
|
|
|
def initProcessing(self) -> None: # noqa: N802
|
|
if self.provider is not None:
|
|
return
|
|
|
|
from qgis.core import QgsApplication
|
|
|
|
from .processing.provider import GeoDonkeyProvider
|
|
|
|
self.provider = GeoDonkeyProvider()
|
|
QgsApplication.processingRegistry().addProvider(self.provider)
|
|
|
|
def _add_action(self, icon_name: str, text: str, slot, tip: str = "",
|
|
checkable: bool = False, toolbar: bool = True) -> QAction:
|
|
action = QAction(icon(icon_name), text, self.iface.mainWindow())
|
|
action.triggered.connect(slot)
|
|
action.setCheckable(checkable)
|
|
if tip:
|
|
action.setStatusTip(tip)
|
|
action.setToolTip(tip)
|
|
if toolbar:
|
|
self.toolbar.addAction(action)
|
|
self.iface.addPluginToWebMenu(MENU_NAME, action)
|
|
self.actions.append(action)
|
|
return action
|
|
|
|
def unload(self) -> None:
|
|
for action in self.actions:
|
|
self.iface.removePluginWebMenu(MENU_NAME, action)
|
|
self.actions = []
|
|
|
|
if self.toolbar is not None:
|
|
self.iface.mainWindow().removeToolBar(self.toolbar)
|
|
self.toolbar.deleteLater()
|
|
self.toolbar = None
|
|
|
|
if self.panel is not None:
|
|
self.iface.removeDockWidget(self.panel)
|
|
self.panel.deleteLater()
|
|
self.panel = None
|
|
|
|
if self.provider is not None:
|
|
from qgis.core import QgsApplication
|
|
|
|
QgsApplication.processingRegistry().removeProvider(self.provider)
|
|
self.provider = None
|
|
|
|
def ensure_panel(self):
|
|
if self.panel is None:
|
|
from .gui.panel import GeoDonkeyPanel
|
|
|
|
self.panel = GeoDonkeyPanel(self.iface, self.iface.mainWindow())
|
|
self.iface.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, self.panel)
|
|
return self.panel
|
|
|
|
def show_panel(self, tab: int = TAB_OSM, algorithm: str = "") -> None:
|
|
panel = self.ensure_panel()
|
|
panel.setVisible(True)
|
|
panel.raise_()
|
|
tabs = getattr(panel, "tabs", None)
|
|
if tabs is not None:
|
|
tabs.setCurrentIndex(tab)
|
|
if not algorithm:
|
|
return
|
|
tools = getattr(panel, "tools", None)
|
|
if tools is None:
|
|
return
|
|
page = tools.page_for(algorithm)
|
|
if page is not None:
|
|
tools.show_page(page)
|
|
|
|
def _panel_tool(self, algorithm: str):
|
|
return lambda: self.show_panel(TAB_TOOLS, algorithm)
|
|
|
|
def _tool(self, algorithm: str):
|
|
def open_dialog() -> None:
|
|
from qgis.core import Qgis, QgsApplication
|
|
|
|
try:
|
|
import processing
|
|
except ImportError:
|
|
self._complain(tr("The Processing plugin is not active."))
|
|
return
|
|
|
|
self.initProcessing()
|
|
if QgsApplication.processingRegistry().algorithmById(algorithm) is None:
|
|
self._complain(tr(
|
|
"The tool '{name}' is not registered. Reload the GeoDonkey "
|
|
"plugin, or restart QGIS.").format(name=algorithm),
|
|
level=Qgis.MessageLevel.Critical)
|
|
return
|
|
|
|
try:
|
|
processing.execAlgorithmDialog(algorithm, {})
|
|
except Exception as exc: # noqa: BLE001
|
|
from qgis.core import QgsMessageLog
|
|
|
|
QgsMessageLog.logMessage(
|
|
"%s: %s" % (algorithm, exc), "GeoDonkey",
|
|
Qgis.MessageLevel.Critical)
|
|
self._complain(tr("The tool could not be opened. See the "
|
|
"GeoDonkey log for details."),
|
|
level=Qgis.MessageLevel.Critical)
|
|
|
|
return open_dialog
|
|
|
|
def _complain(self, text: str, level=None) -> None:
|
|
from qgis.core import Qgis
|
|
|
|
if level is None:
|
|
level = Qgis.MessageLevel.Warning
|
|
self.iface.messageBar().pushMessage(
|
|
"GeoDonkey", text, level=level, duration=10)
|
|
|
|
def open_settings(self) -> None:
|
|
from .gui.settings_dialog import SettingsDialog
|
|
|
|
dialog = SettingsDialog(self.iface.mainWindow())
|
|
accepted = dialog.exec() if hasattr(dialog, "exec") else dialog.exec_()
|
|
if accepted and self.panel is not None:
|
|
self.panel.reload_client()
|
|
|
|
def add_last_basemap(self) -> None:
|
|
from qgis.core import Qgis
|
|
|
|
from . import basemaps, config, settings
|
|
from .errors import GeoDonkeyError
|
|
|
|
style_id = str(settings.get("style"))
|
|
style = next((dict(s) for s in config.FALLBACK_STYLES if s["id"] == style_id), None)
|
|
if style is None:
|
|
style = dict(config.FALLBACK_STYLES[0])
|
|
try:
|
|
basemaps.add_basemap(settings.api_url(), style)
|
|
except GeoDonkeyError as exc:
|
|
self.iface.messageBar().pushMessage(
|
|
"GeoDonkey", exc.message, level=Qgis.MessageLevel.Critical, duration=10)
|
|
return
|
|
self.iface.messageBar().pushMessage(
|
|
"GeoDonkey",
|
|
tr("Basemap “{name}” added.").format(name=style["name"]),
|
|
level=Qgis.MessageLevel.Success, duration=5)
|