Coverage for graphica/plugin/ui.py: 96.2%
22 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 19:22 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 19:22 +0000
1"""
2プラグインのパネル(register_panel)を本体と同じ形にする部品(プラグイン API 2.2)。
4パネルを作るところでだけ import すること(Qt の画面の部品を読み込むので、計算だけのテストでは読まない)。
5見た目は本体のテーマに従い、明暗・テーマカラーを替えると一緒に替わる。本体の画面の作りが変わっても、
6ここの名前と引数はプラグイン API の版で守る。
7"""
8from typing import Any
10from PySide6.QtWidgets import QCheckBox, QFormLayout, QWidget
12from graphica.gui import motion as _motion
13from graphica.gui import theme as _theme
14from graphica.gui.builders.property_sections import attach_header_switch as _attach_header_switch
15from graphica.gui.builders.property_sections import build_section as _build_section
16from graphica.gui.widgets.segmented import SegmentedControl
18__all__ = ["SegmentedControl", "make_switch", "section", "show_toast", "theme_color"]
20# theme_color() で読める色の名前
21THEME_COLOR_NAMES = (
22 "bg", "surface", "surface_2", "border", "border_strong", "text_primary", "text_secondary", "text_muted",
23 "accent", "accent_fg", "on_accent", "success", "danger",
24)
27def section(title: str, opened: bool = True, switch: QCheckBox | None = None) -> tuple[QWidget, QFormLayout]:
28 """見出しを押して開閉できる節。(節の部品, 中身のフォーム) を返す。
30 Args:
31 title: 見出しの文字。
32 opened: 初めに開いておくか。
33 switch: 見出しの右端に置く QCheckBox(機能のオン/オフ)。オフの間は中身を畳み、オンにすると開く。
35 Returns:
36 (節の部品, 中身の QFormLayout)。部品をパネルのレイアウトに足し、行はフォームに addRow する。
37 """
38 entry = _build_section(title, opened=opened)
39 if switch is not None: 39 ↛ 41line 39 didn't jump to line 41 because the condition on line 39 was always true
40 _attach_header_switch(entry, switch)
41 return entry['section'], entry['form']
44def make_switch(checkbox: QCheckBox) -> QCheckBox:
45 """QCheckBox をオン/オフのスイッチの見た目にする(値と toggled はそのまま)。渡したものを返す。"""
46 return _theme.make_toggle_switch(checkbox)
49def theme_color(name: str) -> str:
50 """今のテーマの色(#RRGGBB か rgba(...))。自前で描く部品は、描くたびにこれで色を読む。
52 Args:
53 name: THEME_COLOR_NAMES のどれか(例: "accent"、"text_muted"、"surface")。
55 Returns:
56 色の文字列。QColor() にそのまま渡せる(rgba の色は QColor では読めないので、使うのは不透明の色だけにする)。
57 """
58 if name not in THEME_COLOR_NAMES:
59 raise ValueError(f"知らない色の名前: {name!r}(使える名前: {', '.join(THEME_COLOR_NAMES)})")
60 return str(_theme.current_tokens()[name])
63def show_toast(widget: Any, text: str) -> None:
64 """widget のウィンドウの右下に、短い知らせ(「書き出しました」など)を出して消す。"""
65 _motion.toast(widget, text)