aboutsummaryrefslogtreecommitdiffstats
path: root/sb_builder/ui.py
blob: 82ddc6d954ee80313b9491ebb3b384356a6d5e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# sb-builder - ribbon actions
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""Ribbon actions shared by the tabs (UI guidelines in AGENTS.md).

Each tab owns two lists of QActions: `actions` for the main ribbon (what it
does, in workflow order) and `view_actions` for the small one (selection and
view controls). The main window shows the current tab's lists.
"""

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QAction, QColor, QIcon, QPainter, QPalette, QPixmap
from PyQt6.QtWidgets import QApplication, QLabel, QLineEdit, QPushButton, QStyle


def action(parent, text, icon, fallback, slot=None, checkable=False, shortcut=None):
    """A ribbon action: freedesktop icon with a QStyle fallback."""
    a = QAction(QIcon.fromTheme(icon, parent.style().standardIcon(fallback)), text, parent,
                checkable=checkable)
    if slot:
        a.triggered.connect(lambda _checked=False: slot())
    a.setData(shortcut)  # shown in the tooltip; the tab's QShortcut does the work
    enable(a, None)
    return a


def enable(a, reason):
    """Enable a, or disable it with reason as its tooltip: disabled, never hidden."""
    a.setEnabled(not reason)
    tip = a.text() + (f" ({a.data()})" if a.data() else "")
    a.setToolTip(f"{tip}: {reason}" if reason else tip)


SP = QStyle.StandardPixmap


# ── filter bars: search field, group labels, chips ──────────────────────────

CHIP_STYLE = ("QPushButton { background: palette(button); border: 1px solid palette(mid);"
              " border-radius: 11px; padding: 3px 12px; }"
              "QPushButton:hover { border-color: palette(highlight); }"
              "QPushButton:checked { background: palette(highlight);"
              " color: palette(highlighted-text); border-color: palette(highlight); }")


def chip(text, color=None):
    """A filter toggle that reads as a button; color: a dot, the one the table uses."""
    b = QPushButton(text, checkable=True)
    b.setStyleSheet(CHIP_STYLE)
    if color:
        pix = QPixmap(10, 10)
        pix.fill(Qt.GlobalColor.transparent)
        p = QPainter(pix)
        p.setRenderHint(QPainter.RenderHint.Antialiasing)
        p.setPen(Qt.PenStyle.NoPen)
        p.setBrush(QColor(color))
        p.drawEllipse(0, 0, 10, 10)
        p.end()
        b.setIcon(QIcon(pix))
    return b


def group_label(text):
    """The muted name in front of a group of chips."""
    label = QLabel(text + ":")
    label.setForegroundRole(QPalette.ColorRole.PlaceholderText)
    label.setContentsMargins(4, 0, 6, 0)
    return label


def search_field(placeholder):
    q = QLineEdit(placeholderText=placeholder, clearButtonEnabled=True)
    icon = QIcon.fromTheme("edit-find", QApplication.style().standardIcon(SP.SP_FileDialogContentsView))
    q.addAction(icon, QLineEdit.ActionPosition.LeadingPosition)
    return q