Coverage for graphica/gui/shell/settings_popover.py: 89.4%

198 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-25 19:22 +0000

1"""左端の帯の ⚙ で開くポップアップ: テーマカラー(プリセット・カスタム・既定に戻す)と、環境設定への入口。 

2 

3色を選ぶとすぐ app.set_theme_color() で全部のタブに当てる。読みにくい色は theme_color.derive_accent() が 

4明るさを動かして使うので、ライト・ダークで実際に使う色と、調整したかどうかを並べて見せる。 

5""" 

6from PySide6.QtCore import QPoint, QRectF, QSize, Qt 

7from PySide6.QtGui import QColor, QPainter, QPen 

8from PySide6.QtWidgets import ( 

9 QAbstractButton, QColorDialog, QFrame, QGridLayout, QHBoxLayout, QLabel, QPushButton, QToolButton, QVBoxLayout) 

10 

11from graphica.core.i18n import tr 

12from graphica.gui import motion 

13from graphica.gui import theme 

14from graphica.gui.builders.common import _svg_icon 

15from graphica.gui.theme_color import DEFAULT_THEME_COLOR, THEME_COLOR_PRESETS, derive_accent, normalize_hex 

16 

17POPOVER_WIDTH = 318 

18SWATCH_SIZE = 34 

19 

20 

21def theme_color_preview(color): 

22 """(ライトで使う色, ダークで使う色, 調整の説明 or "")。""" 

23 light = derive_accent(color, theme.LIGHT_TOKENS["surface"], dark=False) 

24 dark = derive_accent(color, theme.DARK_TOKENS["surface"], dark=True) 

25 notes = [] 

26 if light["adjusted"]: 

27 notes.append(tr("ライトでは白い背景で見えるよう濃く")) 

28 if dark["adjusted"]: 

29 notes.append(tr("ダークでは暗い背景で見えるよう明るく")) 

30 note = tr("、").join(notes) + tr("しています(読みやすさの自動調整)。") if notes else "" 

31 return light["accent"], dark["accent"], note 

32 

33 

34class _Swatch(QAbstractButton): 

35 """角丸の色の見本と、下に名前。選んでいる色は輪で囲む。""" 

36 

37 def __init__(self, name, color, parent=None): 

38 super().__init__(parent) 

39 self.color = normalize_hex(color) 

40 self.name = name 

41 self.setCheckable(True) 

42 self.setCursor(Qt.CursorShape.PointingHandCursor) 

43 self.setToolTip(f"{name} {self.color}") 

44 self.setFixedSize(QSize(62, SWATCH_SIZE + 26)) 

45 

46 def paintEvent(self, event): 

47 tokens = theme.current_tokens() 

48 painter = QPainter(self) 

49 painter.setRenderHint(QPainter.RenderHint.Antialiasing, True) 

50 left = (self.width() - SWATCH_SIZE) / 2 

51 rect = QRectF(left, 4, SWATCH_SIZE, SWATCH_SIZE) 

52 if self.isChecked(): 

53 painter.setPen(QPen(QColor(tokens["text_primary"]), 2)) 

54 painter.setBrush(Qt.BrushStyle.NoBrush) 

55 painter.drawRoundedRect(rect.adjusted(-3, -3, 3, 3), 11, 11) 

56 painter.setPen(Qt.PenStyle.NoPen) 

57 painter.setBrush(QColor(self.color)) 

58 painter.drawRoundedRect(rect, 9, 9) 

59 painter.setPen(QColor(tokens["text_secondary"])) 

60 font = painter.font() 

61 font.setPointSizeF(max(1.0, font.pointSizeF() - 1)) 

62 painter.setFont(font) 

63 painter.drawText(QRectF(0, SWATCH_SIZE + 8, self.width(), 18), 

64 int(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignTop), self.name) 

65 painter.end() 

66 

67 

68class _ColorChip(QLabel): 

69 """小さな色の四角(プレビュー用)。""" 

70 

71 def __init__(self, parent=None): 

72 super().__init__(parent) 

73 self.setFixedSize(14, 14) 

74 

75 def set_color(self, color): 

76 self.setStyleSheet(f"background: {color}; border-radius: 4px;") 

77 

78 

79class SettingsPopover(QFrame): 

80 def __init__(self, app, parent=None): 

81 super().__init__(parent, Qt.WindowType.Popup | Qt.WindowType.FramelessWindowHint) 

82 self._app = app 

83 # 窓の四隅は透かし、中の枠を角丸にする(窓のままだと角が四角く残る) 

84 self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True) 

85 self.setFixedWidth(POPOVER_WIDTH) 

86 outer = QVBoxLayout(self) 

87 outer.setContentsMargins(0, 0, 0, 0) 

88 body = QFrame() 

89 body.setObjectName("settings_popover") 

90 outer.addWidget(body) 

91 layout = QVBoxLayout(body) 

92 layout.setContentsMargins(16, 14, 16, 14) 

93 layout.setSpacing(0) 

94 

95 title_row = QHBoxLayout() 

96 title = QLabel(tr("テーマカラー")) 

97 title.setObjectName("settings_popover_title") 

98 self.close_button = QToolButton() 

99 self.close_button.setObjectName("card_button") 

100 self.close_button.setIcon(_svg_icon("x", size=14)) 

101 self.close_button.setFixedSize(24, 24) 

102 self.close_button.clicked.connect(self.close) 

103 title_row.addWidget(title) 

104 title_row.addStretch(1) 

105 title_row.addWidget(self.close_button) 

106 layout.addLayout(title_row) 

107 subtitle = QLabel(tr("ボタン・選択・枠などに使う色です。グラフの線の色は変わりません。")) 

108 subtitle.setObjectName("settings_popover_sub") 

109 subtitle.setWordWrap(True) 

110 layout.addWidget(subtitle) 

111 layout.addSpacing(10) 

112 

113 grid = QGridLayout() 

114 grid.setHorizontalSpacing(8) 

115 grid.setVerticalSpacing(6) 

116 self.swatches = [] 

117 for index, (name, color) in enumerate(THEME_COLOR_PRESETS): 

118 swatch = _Swatch(tr(name), color) 

119 swatch.clicked.connect(lambda _checked=False, c=swatch.color: self.choose(c)) 

120 grid.addWidget(swatch, index // 4, index % 4, Qt.AlignmentFlag.AlignHCenter) 

121 self.swatches.append(swatch) 

122 layout.addLayout(grid) 

123 

124 custom_box = QFrame() 

125 custom_box.setObjectName("settings_popover_row") 

126 custom_row = QHBoxLayout(custom_box) 

127 custom_row.setContentsMargins(0, 12, 0, 0) 

128 custom_row.setSpacing(8) 

129 self.custom_button = QPushButton() 

130 self.custom_button.setObjectName("settings_custom_color") 

131 self.custom_button.setFixedSize(34, 30) 

132 self.custom_button.setCursor(Qt.CursorShape.PointingHandCursor) 

133 self.custom_button.clicked.connect(self._on_custom) 

134 custom_text = QVBoxLayout() 

135 custom_text.setSpacing(0) 

136 custom_title = QLabel(tr("カスタム")) 

137 custom_title.setObjectName("settings_popover_strong") 

138 custom_sub = QLabel(tr("好きな色を選ぶ")) 

139 custom_sub.setObjectName("settings_popover_sub") 

140 custom_text.addWidget(custom_title) 

141 custom_text.addWidget(custom_sub) 

142 self.hex_label = QLabel() 

143 self.hex_label.setObjectName("settings_popover_mono") 

144 custom_row.addWidget(self.custom_button) 

145 custom_row.addLayout(custom_text) 

146 custom_row.addStretch(1) 

147 custom_row.addWidget(self.hex_label) 

148 layout.addSpacing(12) 

149 layout.addWidget(custom_box) 

150 

151 preview = QGridLayout() 

152 preview.setContentsMargins(0, 10, 0, 0) 

153 preview.setHorizontalSpacing(10) 

154 preview.setVerticalSpacing(6) 

155 self.light_chip, self.dark_chip = _ColorChip(), _ColorChip() 

156 self.light_hex, self.dark_hex = QLabel(), QLabel() 

157 for row, (text, chip, hex_label) in enumerate(((tr("ライトで使う色"), self.light_chip, self.light_hex), 

158 (tr("ダークで使う色"), self.dark_chip, self.dark_hex))): 

159 label = QLabel(text) 

160 label.setObjectName("settings_popover_label") 

161 hex_label.setObjectName("settings_popover_mono") 

162 value = QHBoxLayout() 

163 value.setSpacing(6) 

164 value.addWidget(chip) 

165 value.addWidget(hex_label) 

166 value.addStretch(1) 

167 preview.addWidget(label, row, 0) 

168 preview.addLayout(value, row, 1) 

169 layout.addLayout(preview) 

170 

171 self.note_label = QLabel() 

172 self.note_label.setObjectName("settings_popover_note") 

173 self.note_label.setWordWrap(True) 

174 layout.addSpacing(10) 

175 layout.addWidget(self.note_label) 

176 

177 buttons = QHBoxLayout() 

178 buttons.setContentsMargins(0, 12, 0, 0) 

179 buttons.setSpacing(8) 

180 self.reset_button = QPushButton(tr("既定に戻す(アイコンの色)")) 

181 self.reset_button.clicked.connect(lambda: self.choose(DEFAULT_THEME_COLOR)) 

182 self.preferences_button = QPushButton(tr("環境設定...")) 

183 self.preferences_button.clicked.connect(self._on_preferences) 

184 buttons.addWidget(self.reset_button) 

185 buttons.addStretch(1) 

186 buttons.addWidget(self.preferences_button) 

187 layout.addLayout(buttons) 

188 self.refresh() 

189 

190 def current_color(self): 

191 return normalize_hex(theme.current_theme_color()) 

192 

193 def choose(self, color): 

194 self._app.set_theme_color(color) 

195 self.refresh() 

196 

197 def refresh(self): 

198 color = self.current_color() 

199 for swatch in self.swatches: 

200 swatch.setChecked(swatch.color == color) 

201 swatch.update() 

202 self.custom_button.setStyleSheet( 

203 f"background: {color}; border: 1px solid {theme.current_tokens()['border_strong']}; border-radius: 7px;") 

204 self.hex_label.setText(color) 

205 light, dark, note = theme_color_preview(color) 

206 self.light_chip.set_color(light) 

207 self.dark_chip.set_color(dark) 

208 self.light_hex.setText(light) 

209 self.dark_hex.setText(dark) 

210 self.note_label.setText(note) 

211 self.note_label.setVisible(bool(note)) 

212 self.close_button.setIcon(_svg_icon("x", size=14)) 

213 self.adjustSize() 

214 

215 def _on_custom(self): 

216 # 色の画面を開くとポップアップは閉じる(フォーカスが外れる)ので、選び終えてから当てる 

217 self.hide() 

218 chosen = QColorDialog.getColor(QColor(self.current_color()), self._app, tr("テーマカラー")) 

219 if chosen.isValid(): 219 ↛ exitline 219 didn't return from function '_on_custom' because the condition on line 219 was always true

220 self._app.set_theme_color(chosen.name()) 

221 

222 def _on_preferences(self): 

223 self.hide() 

224 self._app._on_show_preferences() 

225 

226 def open_beside(self, anchor): 

227 """anchor(帯の ⚙)の右、下端をそろえて開く。""" 

228 self.refresh() 

229 bottom_right = anchor.mapToGlobal(QPoint(anchor.width() + 8, anchor.height())) 

230 self.move(bottom_right.x(), bottom_right.y() - self.sizeHint().height()) 

231 self.show() 

232 motion.fade_in_window(self) 

233 

234 

235def toggle_settings_popover(app, anchor): 

236 popover = getattr(app, 'settings_popover', None) 

237 if popover is None: 

238 popover = app.settings_popover = SettingsPopover(app, app) 

239 if popover.isVisible(): 

240 popover.hide() 

241 else: 

242 popover.open_beside(anchor) 

243