forked from AG_QGIS/Plugin_SN_Basis
85 lines
2.0 KiB
Python
85 lines
2.0 KiB
Python
"""
|
||
sn_basis/functions/message_wrapper.py – zentrale MessageBar-Abstraktion
|
||
"""
|
||
|
||
from typing import Any
|
||
|
||
from sn_basis.functions.qgisui_wrapper import iface
|
||
from sn_basis.functions.qgiscore_wrapper import Qgis
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# Interne Hilfsfunktion
|
||
# ---------------------------------------------------------
|
||
|
||
def _get_message_bar():
|
||
"""
|
||
Liefert eine MessageBar-Instanz (QGIS oder Mock).
|
||
"""
|
||
try:
|
||
bar = iface.messageBar()
|
||
if bar is not None:
|
||
return bar
|
||
except Exception:
|
||
pass
|
||
|
||
class _MockMessageBar:
|
||
def pushMessage(self, title, text, level=0, duration=5):
|
||
return {
|
||
"title": title,
|
||
"text": text,
|
||
"level": level,
|
||
"duration": duration,
|
||
}
|
||
|
||
return _MockMessageBar()
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# Öffentliche API
|
||
# ---------------------------------------------------------
|
||
|
||
def push_message(
|
||
level: int,
|
||
title: str,
|
||
text: str,
|
||
duration: int = 5,
|
||
parent: Any = None,
|
||
):
|
||
"""
|
||
Zeigt eine Message in der QGIS-MessageBar an.
|
||
|
||
Im Mock-Modus wird ein strukturierter Dict zurückgegeben.
|
||
"""
|
||
bar = _get_message_bar()
|
||
|
||
try:
|
||
return bar.pushMessage(
|
||
title,
|
||
text,
|
||
level=level,
|
||
duration=duration,
|
||
)
|
||
except Exception:
|
||
return None
|
||
|
||
|
||
def info(title: str, text: str, duration: int = 5):
|
||
level = Qgis.MessageLevel.Info
|
||
return push_message(level, title, text, duration)
|
||
|
||
|
||
def warning(title: str, text: str, duration: int = 5):
|
||
level = Qgis.MessageLevel.Warning
|
||
return push_message(level, title, text, duration)
|
||
|
||
|
||
def error(title: str, text: str, duration: int = 5):
|
||
level = Qgis.MessageLevel.Critical
|
||
return push_message(level, title, text, duration)
|
||
|
||
|
||
def success(title: str, text: str, duration: int = 5):
|
||
level = Qgis.MessageLevel.Success
|
||
return push_message(level, title, text, duration)
|