Coverage for graphica/gui/shell/subplot_target_bar.py: 94.2%
131 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"""「軸」タブの先頭の、編集するサブプロットを選ぶ行。グラフの並びを小さな升目で見せ、押して選ぶ。
3値の持ち主は今までどおり app.active_axis_combo(隠して中に置く)。自由配置や升目が多すぎるときは、そのコンボを出す。
4"""
5from PySide6.QtCore import QRectF, QSize, Qt, Signal
6from PySide6.QtGui import QColor, QFont, QPainter, QPen
7from PySide6.QtWidgets import QHBoxLayout, QLabel, QSizePolicy, QVBoxLayout, QWidget
9from graphica.core.i18n import tr
10from graphica.gui import motion, theme
11from graphica.gui.widgets import ElidedLabel
13# これより多い升目は小さすぎて押せないので、プルダウンにする
14MAX_GRID_CELLS = 9
15CELL_WIDTH = 26
16CELL_HEIGHT = 18
17CELL_GAP = 3
20class SubplotGrid(QWidget):
21 """rows × cols の升目。押した升目の番号(左上から行ごと)を cellClicked で出す。"""
23 cellClicked = Signal(int)
25 def __init__(self, parent=None):
26 super().__init__(parent)
27 self._rows, self._cols, self._current = 1, 1, 0
28 # 選んだ升目の枠が滑っている間の位置(止まっていれば None)
29 self._sliding = None
30 self.setCursor(Qt.CursorShape.PointingHandCursor)
31 self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
33 def set_grid(self, rows, cols, current):
34 if (rows, cols, current) == (self._rows, self._cols, self._current):
35 return
36 same_shape = (rows, cols) == (self._rows, self._cols)
37 start = self._cell_rect(self._current) if same_shape and self.isVisible() else None
38 self._rows, self._cols, self._current = rows, cols, current
39 self.updateGeometry()
40 if start is not None and motion.is_enabled():
41 self._sliding = start
43 def step(rect):
44 self._sliding = rect
45 self.update()
47 def finish():
48 self._sliding = None
49 self.update()
51 motion.animate_value(self, "_motion_cell", start, self._cell_rect(current), step, finish, after_paint=True)
52 self.update()
54 def sizeHint(self):
55 return QSize(self._cols * CELL_WIDTH + (self._cols - 1) * CELL_GAP,
56 self._rows * CELL_HEIGHT + (self._rows - 1) * CELL_GAP)
58 def _cell_rect(self, index):
59 row, col = divmod(index, self._cols)
60 return QRectF(col * (CELL_WIDTH + CELL_GAP) + 0.5, row * (CELL_HEIGHT + CELL_GAP) + 0.5,
61 CELL_WIDTH - 1, CELL_HEIGHT - 1)
63 def cell_at(self, pos):
64 for index in range(self._rows * self._cols): 64 ↛ 67line 64 didn't jump to line 67 because the loop on line 64 didn't complete
65 if self._cell_rect(index).contains(pos):
66 return index
67 return -1
69 def mousePressEvent(self, event):
70 index = self.cell_at(event.position())
71 if event.button() == Qt.MouseButton.LeftButton and index >= 0:
72 self.cellClicked.emit(index)
73 super().mousePressEvent(event)
75 def paintEvent(self, event):
76 tokens = theme.current_tokens()
77 painter = QPainter(self)
78 painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
79 font = QFont(self.font())
80 font.setPointSizeF(max(7.0, font.pointSizeF() - 2))
81 for index in range(self._rows * self._cols):
82 painter.setPen(QPen(QColor(tokens["border_strong"]), 1))
83 painter.setBrush(Qt.BrushStyle.NoBrush)
84 painter.drawRoundedRect(self._cell_rect(index), theme.RADIUS_SM, theme.RADIUS_SM)
85 # 選んだ升目の枠(選び直したときは前の升目から滑ってくる)
86 painter.setPen(QPen(QColor(tokens["accent"]), 1))
87 painter.setBrush(theme.current_accent_soft_qcolor())
88 selected_rect = self._sliding if self._sliding is not None else self._cell_rect(self._current)
89 painter.drawRoundedRect(selected_rect, theme.RADIUS_SM, theme.RADIUS_SM)
90 for index in range(self._rows * self._cols):
91 rect = self._cell_rect(index)
92 selected = index == self._current
93 font.setBold(selected)
94 painter.setFont(font)
95 painter.setPen(QColor(tokens["accent_fg"] if selected else tokens["text_secondary"]))
96 painter.drawText(rect, int(Qt.AlignmentFlag.AlignCenter), str(index + 1))
97 painter.end()
100class SubplotTargetBar(QWidget):
101 """「対象」+ 升目(かプルダウン)+ 選んでいるプロットの名前と系列の数。サブプロットが1つなら出さない。"""
103 def __init__(self, app, parent=None):
104 super().__init__(parent)
105 self.setObjectName("subplot_target_bar")
106 self._app = app
107 self.combo = app.active_axis_combo
108 layout = QHBoxLayout(self)
109 layout.setContentsMargins(0, 2, 0, 10)
110 layout.setSpacing(10)
111 caption = QLabel(tr("対象"))
112 caption.setObjectName("subplot_target_caption")
113 layout.addWidget(caption, 0, Qt.AlignmentFlag.AlignVCenter)
114 self.grid = SubplotGrid(self)
115 self.grid.setToolTip(tr("編集するサブプロットを選ぶ"))
116 self.grid.cellClicked.connect(self._on_cell_clicked)
117 layout.addWidget(self.grid, 0, Qt.AlignmentFlag.AlignVCenter)
118 self.combo.setParent(self)
119 layout.addWidget(self.combo, 1)
120 names = QVBoxLayout()
121 names.setContentsMargins(0, 0, 0, 0)
122 names.setSpacing(0)
123 self.name_label = ElidedLabel()
124 self.name_label.setObjectName("subplot_target_name")
125 self.detail_label = ElidedLabel()
126 self.detail_label.setObjectName("subplot_target_detail")
127 names.addWidget(self.name_label)
128 names.addWidget(self.detail_label)
129 layout.addLayout(names, 1)
130 self.combo.currentIndexChanged.connect(lambda _index: self.refresh())
132 def _grid_shape(self):
133 """(行, 列)。升目で見せられないとき(自由配置・多すぎる)は None。"""
134 app = self._app
135 if getattr(app.project, 'layout_mode', 'grid') == 'free':
136 return None
137 rows, cols = app.subplot_rows_spinbox.value(), app.subplot_cols_spinbox.value()
138 if rows * cols > MAX_GRID_CELLS or rows * cols != self.combo.count():
139 return None
140 return rows, cols
142 def _on_cell_clicked(self, index):
143 if 0 <= index < self.combo.count(): 143 ↛ exitline 143 didn't return from function '_on_cell_clicked' because the condition on line 143 was always true
144 self.combo.setCurrentIndex(index)
146 def refresh(self):
147 count = self.combo.count()
148 self.setVisible(count > 1)
149 if count <= 1:
150 return
151 current = max(0, self.combo.currentIndex())
152 shape = self._grid_shape()
153 self.grid.setVisible(shape is not None)
154 self.name_label.setVisible(shape is not None)
155 self.detail_label.setVisible(shape is not None)
156 self.combo.setVisible(shape is None)
157 if shape is None:
158 return
159 rows, cols = shape
160 self.grid.set_grid(rows, cols, current)
161 self.name_label.setText(self.combo.itemText(current))
162 series = sum(1 for ds in self._app.project.datasets if ds.subplot_target == current)
163 row, col = divmod(current, cols)
164 self.detail_label.setText(tr("{series} 系列 · {row} 行 {col} 列").format(series=series, row=row + 1, col=col + 1))