Skip to content

Commit

Permalink
feature(logs): customize parent location for QgsMessageBar
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Apr 29, 2024
1 parent 94ce764 commit cc151b0
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions qtribu/toolbelt/log_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import logging
from functools import partial
from logging.handlers import RotatingFileHandler
from typing import Callable
from typing import Callable, Optional

# PyQGIS
from qgis.core import QgsMessageLog, QgsMessageOutput
from qgis.PyQt.QtWidgets import QPushButton
from qgis.gui import QgsMessageBar
from qgis.PyQt.QtWidgets import QPushButton, QWidget
from qgis.utils import iface

# project package
Expand All @@ -29,12 +30,14 @@ def log(
application: str = __title__,
log_level: int = 0,
push: bool = False,
duration: int = None,
duration: Optional[int] = None,
# widget
button: bool = False,
button_label: str = None,
button_more_text: str = None,
button_connect: Callable = None,
button_label: Optional[str] = None,
button_more_text: Optional[str] = None,
button_connect: Optional[Callable] = None,
# parent
parent_location: Optional[QWidget] = None,
):
"""Send messages to QGIS messages windows and to the user as a message bar. \
Plugin name is used as title. If debug mode is disabled, only warnings (1) and \
Expand All @@ -56,8 +59,7 @@ def log(
If set to 0, then the message must be manually dismissed by the user. \
Defaults to None.
:type duration: int, optional
:param button: display a button in the message bar to open a QgsMessageOutput. \
Defaults to False.
:param button: display a button in the message bar. Defaults to False.
:type button: bool, optional
:param button_label: text label of the button. Defaults to None.
:type button_label: str, optional
Expand All @@ -67,6 +69,11 @@ def log(
If not set, a simple dialog (QgsMessageOutput) is used to dislay the message. \
Defaults to None.
:type button_connect: Callable, optional
:param parent_location: parent location widget. \
If not set, QGIS canvas message bar is used to push message, \
otherwise if a QgsMessageBar is available in parent_location it is used instead. \
Defaults to None.
:type parent_location: Widget, optional
:Example:
Expand Down Expand Up @@ -115,7 +122,18 @@ def log(
)

# optionally, display message on QGIS Message bar (above the map canvas)
if push:
if push and iface is not None:
msg_bar = None

# QGIS or custom dialog
if parent_location and isinstance(parent_location, QWidget):
msg_bar = parent_location.findChild(QgsMessageBar)
print(msg_bar)

if not msg_bar:
print("use QGIS message bar as fallback")
msg_bar = iface.messageBar()

# calc duration
if duration is None:
duration = (log_level + 1) * 3
Expand All @@ -138,13 +156,13 @@ def log(
widget_button.clicked.connect(partial(mini_dlg.showMessage, False))

notification.layout().addWidget(widget_button)
iface.messageBar().pushWidget(
msg_bar.pushWidget(
widget=notification, level=log_level, duration=duration
)

else:
# send simple message
iface.messageBar().pushMessage(
msg_bar.pushMessage(
title=application,
text=message,
level=log_level,
Expand Down

0 comments on commit cc151b0

Please sign in to comment.