from __future__ import annotations from typing import Optional from qgis.PyQt.QtCore import QSize, Qt from qgis.PyQt.QtGui import QColor, QFont, QFontMetrics, QPainter, QPixmap from qgis.PyQt.QtWidgets import ( QComboBox, QFrame, QLabel, QSizePolicy, QStackedWidget, QVBoxLayout, ) from ..config import BRAND PAGE_TOP = 10 GAP = 7 TIGHT = 3 DARK_THEMES = {"night mapping", "nightmapping"} def night() -> bool: themed: Optional[bool] = None try: from qgis.core import QgsApplication themed = str(QgsApplication.themeName() or "").strip().lower() in DARK_THEMES except Exception: themed = None if themed: return True try: from qgis.PyQt.QtWidgets import QApplication palette = QApplication.palette() if palette is None: return False return palette.window().color().lightness() < 128 except Exception: return False def colours() -> dict: if night(): return { "surface": BRAND["tint_night"], "surface_line": BRAND["tint_line_night"], "title": "#ffffff", "claim": BRAND["muted_night"], "note": BRAND["muted_night"], "warn": BRAND["warn_night"], "error": BRAND["error_night"], "good": BRAND["good_night"], "line": BRAND["tint_line_night"], "off_bg": BRAND["tint_line_night"], "off_fg": "#8d9cb5", "chart_bg": "#101a2e", "chart_grid": BRAND["tint_line_night"], "chart_text": "#e8eefb", "chart_muted": BRAND["muted_night"], "chart_terrain": BRAND["green"], "link": BRAND["green"], } return { "surface": BRAND["tint"], "surface_line": BRAND["tint_line"], "title": BRAND["navy"], "claim": BRAND["muted"], "note": BRAND["muted"], "warn": BRAND["warn"], "error": BRAND["error"], "good": BRAND["good"], "line": BRAND["line"], "off_bg": BRAND["tint_line"], "off_fg": "#8a99b3", "chart_bg": BRAND["paper"], "chart_grid": BRAND["line"], "chart_text": BRAND["ink"], "chart_muted": BRAND["muted"], "chart_terrain": BRAND["green_deep"], "link": BRAND["navy"], } def header_style() -> str: values = dict(BRAND, **colours()) return """ QFrame#gdHeader { background-color: %(surface)s; border: 1px solid %(surface_line)s; border-left: 4px solid %(green)s; border-radius: 8px; } QLabel#gdHeaderTitle { color: %(title)s; font-weight: 700; font-size: 13pt; } QLabel#gdHeaderClaim { color: %(claim)s; font-size: 9pt; } """ % values def primary_button_style() -> str: values = dict(BRAND, **colours()) return """ QPushButton { background-color: %(green)s; color: %(navy)s; border: 1px solid %(green_dark)s; border-radius: 4px; padding: 6px 14px; font-weight: 600; } QPushButton:hover { background-color: %(green_dark)s; border-color: %(green_dark)s; } QPushButton:pressed { background-color: %(green_deep)s; border-color: %(green_deep)s; } QPushButton:disabled { background-color: %(off_bg)s; border-color: %(off_bg)s; color: %(off_fg)s; } """ % values def note_style() -> str: return "color: %(note)s;" % colours() def link_style() -> str: return "color: %(link)s;" % colours() def page_title_style() -> str: return "color: %(title)s; font-weight: 700; font-size: 11pt;" % colours() def section_style() -> str: return "color: %(title)s; font-weight: 600;" % colours() def page_title(text: str) -> QLabel: label = QLabel(text) label.setStyleSheet(page_title_style()) return label def section(text: str) -> QLabel: label = QLabel(text) label.setWordWrap(True) label.setStyleSheet(section_style()) return label def rule() -> QFrame: line = QFrame() line.setFrameShape(QFrame.Shape.HLine) line.setFrameShadow(QFrame.Shadow.Plain) line.setFixedHeight(1) line.setStyleSheet("background-color: %(line)s; border: none;" % colours()) return line class LayerBox(QComboBox): def __init__(self, refill=None, parent=None) -> None: super().__init__(parent) self.refill = refill def showPopup(self): # noqa: N802 if self.refill is not None: self.refill() super().showPopup() class ElidedLabel(QLabel): def __init__(self, text: str = "", parent=None) -> None: super().__init__(text, parent) self._full = text self.setToolTip(text) self.setMinimumWidth(1) self.setSizePolicy(QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Preferred) def full_text(self) -> str: return self._full def setText(self, text: str) -> None: # noqa: N802 self._full = text or "" self.setToolTip(self._full) self._shorten() def resizeEvent(self, event): # noqa: N802 super().resizeEvent(event) self._shorten() def _shorten(self) -> None: metrics = QFontMetrics(self.font()) QLabel.setText(self, metrics.elidedText( self._full, Qt.TextElideMode.ElideRight, max(1, self.width()))) class FittedStack(QStackedWidget): def __init__(self, parent=None) -> None: super().__init__(parent) self.currentChanged.connect(lambda _index: self.updateGeometry()) def sizeHint(self) -> QSize: # noqa: N802 page = self.currentWidget() return page.sizeHint() if page is not None else super().sizeHint() def minimumSizeHint(self) -> QSize: # noqa: N802 page = self.currentWidget() if page is None: return super().minimumSizeHint() return page.minimumSizeHint().expandedTo(page.minimumSize()) def hasHeightForWidth(self) -> bool: # noqa: N802 page = self.currentWidget() return (page.hasHeightForWidth() if page is not None else super().hasHeightForWidth()) def heightForWidth(self, width: int) -> int: # noqa: N802 page = self.currentWidget() return (page.heightForWidth(width) if page is not None else super().heightForWidth(width)) def page_box(widget) -> QVBoxLayout: box = QVBoxLayout(widget) box.setContentsMargins(0, PAGE_TOP, 0, 0) box.setSpacing(GAP) return box def warn_style() -> str: return "color: %(warn)s; font-weight: 600;" % colours() def error_style() -> str: return "color: %(error)s; font-weight: 600;" % colours() def good_style() -> str: return "color: %(good)s; font-weight: 600;" % colours() def swatch(colors, size: int = 16) -> QPixmap: values = [c for c in (colors or []) if isinstance(c, str) and c.startswith("#")] if not values: values = ["#cccccc"] pixmap = QPixmap(QSize(size, size)) pixmap.fill(Qt.GlobalColor.transparent) painter = QPainter(pixmap) try: width = size / float(len(values)) for index, value in enumerate(values): painter.fillRect(int(index * width), 0, int(width + 1), size, QColor(value)) painter.setPen(QColor(colours()["line"])) painter.drawRect(0, 0, size - 1, size - 1) finally: painter.end() return pixmap def small(widget, points: int = 8): font = QFont(widget.font()) font.setPointSize(max(6, widget.font().pointSize() - 1 if points is None else points)) widget.setFont(font) return widget