Coverage for graphica/gui/dataset_style_icon.py: 96.5%
110 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"""データセット一覧の各項目のアイコン(見た目の小さなプレビューと、表示/非表示の目)。
3main_window と dataset_mixin の両方が使うので、循環 import を避けて別のモジュールにしている。
4"""
5from PySide6.QtCore import QPointF, Qt
6from PySide6.QtGui import QPixmap, QPainter, QPainterPath, QPen, QColor, QBrush, QIcon
8from graphica.core.dataset import COLOR_BY_COLUMN_PLOT_TYPE, linestyle_name
9from graphica.core.marker_style import MARKER_EDGE_COLOR, MARKER_FILL_COLOR, MARKER_FILL_HOLLOW, marker_is_unfilled
10from graphica.gui.icon_utils import icon as _icon_from_svg
12_STYLE_ICON_SIZE = (28, 14)
14# 列0はアイコンと名前、列1は表示/非表示の目
15DATASET_TREE_NAME_COLUMN = 0
16DATASET_TREE_VISIBILITY_COLUMN = 1
18# 線種の表示名(linestyle_name で揃えたもの)から Qt のペン。記号('--')だけ見ると 'dashed' が実線になる
19_LINESTYLE_TO_QT_PEN = {
20 'solid': Qt.PenStyle.SolidLine,
21 'dashed': Qt.PenStyle.DashLine,
22 'dashdot': Qt.PenStyle.DashDotLine,
23 'dotted': Qt.PenStyle.DotLine,
24}
27# マーカーを描く種類
28MARKER_PLOT_TYPES = ('Scatter', 'Line+Scatter', 'Density Scatter', COLOR_BY_COLUMN_PLOT_TYPE)
29# マーカーを描かない組み込みの種類(プラグインの種類はマーカーを使うかもしれない)
30NON_MARKER_PLOT_TYPES = ('Line', 'Area', 'Bar', 'Step')
31# 線を描かない組み込みの種類(線の種類・太さの欄を隠す)。プラグインの種類は使うかもしれないので含めない
32NON_LINE_PLOT_TYPES = ('Scatter', 'Bar', 'Density Scatter', COLOR_BY_COLUMN_PLOT_TYPE)
35def marker_painter_path(marker, size):
36 """matplotlib のマーカーの形を、原点を中心にした幅 size px ほどの QPainterPath にする。描かない値は None。"""
37 from matplotlib.markers import MarkerStyle
38 from matplotlib.path import Path as MplPath
40 try:
41 style = MarkerStyle(marker)
42 except ValueError:
43 return None
44 path = style.get_path().transformed(style.get_transform())
45 if len(path.vertices) == 0:
46 return None
47 # matplotlib の形はおよそ幅 1 の Y 上向き。画面は Y 下向きなので反転する
48 def point(x, y):
49 return QPointF(x * size, -y * size)
51 painter_path = QPainterPath()
52 for vertices, code in path.iter_segments(simplify=False, curves=True):
53 if code == MplPath.MOVETO:
54 painter_path.moveTo(point(*vertices))
55 elif code == MplPath.LINETO:
56 painter_path.lineTo(point(*vertices))
57 elif code == MplPath.CURVE3: 57 ↛ 58line 57 didn't jump to line 58 because the condition on line 57 was never true
58 painter_path.quadTo(point(*vertices[:2]), point(*vertices[2:]))
59 elif code == MplPath.CURVE4:
60 painter_path.cubicTo(point(*vertices[:2]), point(*vertices[2:4]), point(*vertices[4:]))
61 elif code == MplPath.CLOSEPOLY: 61 ↛ 52line 61 didn't jump to line 52 because the condition on line 61 was always true
62 painter_path.closeSubpath()
63 return painter_path
66def _draw_marker(painter, marker, center, size, brush, pen):
67 path = marker_painter_path(marker, size)
68 if path is None:
69 return
70 painter.save()
71 painter.translate(center)
72 painter.setPen(pen)
73 painter.setBrush(brush)
74 painter.drawPath(path)
75 painter.restore()
78_MARKER_CHOICE_ICON_SIZE = 16
79# 明暗どちらのテーマの選択肢の背景でも見える中間の灰色
80_MARKER_CHOICE_ICON_COLOR = '#808080'
83def marker_choice_icon(marker):
84 """マーカーの形の選択肢に付ける形の絵。描かない値('None')は None。"""
85 if marker_painter_path(marker, 1) is None:
86 return None
87 side = _MARKER_CHOICE_ICON_SIZE
88 pixmap = QPixmap(side, side)
89 pixmap.fill(Qt.GlobalColor.transparent)
90 painter = QPainter(pixmap)
91 painter.setRenderHint(QPainter.RenderHint.Antialiasing)
92 color = QColor(_MARKER_CHOICE_ICON_COLOR)
93 brush = Qt.BrushStyle.NoBrush if marker_is_unfilled(marker) else QBrush(color)
94 _draw_marker(painter, marker, QPointF(side / 2, side / 2), side * 0.7, brush, QPen(color, 1.2))
95 painter.end()
96 return QIcon(pixmap)
99def _marker_brush_and_pen(dataset, color):
100 """一覧のアイコン用。塗りと枠線の指定を映す(値で色分けする種類も系列の色で代わりに描く)。"""
101 edge = QColor(dataset.marker_edge_color) if dataset.marker_edge == MARKER_EDGE_COLOR else QColor(color)
102 edge.setAlphaF(color.alphaF())
103 pen = QPen(edge, max(0.0, min(dataset.marker_edge_width, 2.0)))
104 if dataset.marker_edge_width <= 0: 104 ↛ 105line 104 didn't jump to line 105 because the condition on line 104 was never true
105 pen = QPen(Qt.PenStyle.NoPen)
106 if marker_is_unfilled(dataset.marker):
107 return Qt.BrushStyle.NoBrush, QPen(edge, 1.2)
108 if dataset.marker_fill == MARKER_FILL_HOLLOW:
109 return Qt.BrushStyle.NoBrush, pen
110 if dataset.marker_fill == MARKER_FILL_COLOR:
111 face = QColor(dataset.marker_face_color)
112 face.setAlphaF(color.alphaF())
113 return QBrush(face), pen
114 return QBrush(color), pen
117def make_dataset_style_icon(dataset):
118 """色・線種・線幅・マーカー・透明度を映した小さな画像。選ばなくても一覧で見分けられるように。"""
119 width, height = _STYLE_ICON_SIZE
120 pixmap = QPixmap(width, height)
121 pixmap.fill(Qt.GlobalColor.transparent)
123 painter = QPainter(pixmap)
124 painter.setRenderHint(QPainter.RenderHint.Antialiasing)
126 color = QColor(dataset.color)
127 # 透明度が低くても一覧では見えるよう下限を設ける
128 color.setAlphaF(max(0.2, min(dataset.alpha, 1.0)))
130 show_line = dataset.plot_type in ('Line', 'Line+Scatter', 'Step')
131 show_marker = dataset.plot_type in MARKER_PLOT_TYPES
132 y = height // 2
134 if show_line:
135 pen = QPen(color, max(1.0, min(dataset.linewidth, 3.0)))
136 pen.setStyle(_LINESTYLE_TO_QT_PEN.get(linestyle_name(dataset.linestyle), Qt.PenStyle.SolidLine))
137 painter.setPen(pen)
138 painter.drawLine(2, y, width - 2, y)
140 if show_marker:
141 brush, pen = _marker_brush_and_pen(dataset, color)
142 _draw_marker(painter, dataset.marker, QPointF(width / 2, y), 8, brush, pen)
144 painter.end()
146 # Selected モードにも同じ画像を入れる(入れないと Fusion が選択時の画像を作り、塗りつぶした四角になる)
147 icon = QIcon()
148 icon.addPixmap(pixmap, QIcon.Mode.Normal)
149 icon.addPixmap(pixmap, QIcon.Mode.Selected)
150 return icon
153# 目の列の固定幅(DATASET_TREE_VISIBILITY_COLUMN_WIDTH)に収まる大きさ
154_VISIBILITY_ICON_SIZE = 16
157def make_dataset_visibility_icon(dataset):
158 name = "eye" if getattr(dataset, "visible", True) else "eye-off"
159 return _icon_from_svg(name, size=_VISIBILITY_ICON_SIZE)
162def apply_dataset_visibility_text_style(item, dataset, column=0):
163 """隠した系列の名前を text_muted の色にする。表示に戻すときは色を消してテーマの文字色に任せる。"""
164 if getattr(dataset, "visible", True):
165 item.setData(column, Qt.ItemDataRole.ForegroundRole, None)
166 else:
167 from graphica.gui import theme
168 item.setForeground(column, QBrush(QColor(theme.current_tokens()["text_muted"])))