Coverage for graphica/gui/builders/property_sections.py: 94.0%

190 statements  

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

1"""データセットのプロパティ欄の節(折りたたみ・開閉の保存・見出しの出し入れ・ラベル列の幅)。""" 

2import json 

3from PySide6.QtCore import Qt, QTimer 

4from PySide6.QtWidgets import QFormLayout, QHBoxLayout, QLabel, QSizePolicy, QVBoxLayout, QWidget 

5from graphica.core.i18n import tr 

6from graphica.gui import app_settings, motion, theme, ui_mode 

7from graphica.gui.builders.common import DATASET_PROPERTY_SECTIONS, _svg_icon, cap_combo_width 

8from graphica.gui.widgets import SectionHeader 

9 

10 

11def build_dataset_property_sections(app): 

12 """データセットのプロパティ欄を DATASET_PROPERTY_SECTIONS の節に分け、節ごとに QFormLayout を持たせる。 

13 

14 列幅の広がりが節の中に閉じる。Designer の行は takeRow() で移す(removeRow() はウィジェットごと破棄する)。 

15 平滑化のチェックだけは「平滑化の手法」の直前に置くので、ここでは移さない。 

16 """ 

17 app._prop_sections = {} 

18 

19 container = QWidget() 

20 container_layout = QVBoxLayout(container) 

21 container_layout.setContentsMargins(0, 0, 0, 0) 

22 container_layout.setSpacing(0) 

23 

24 collapsed = app._load_collapsed_property_sections() 

25 

26 for index, (key, title) in enumerate(DATASET_PROPERTY_SECTIONS): 

27 entry = build_section(tr(title), opened=key not in collapsed, first=(index == 0)) 

28 entry['toggle'].toggled.connect( 

29 lambda checked, k=key: app._on_property_section_toggled(k, checked)) 

30 container_layout.addWidget(entry['section']) 

31 app._prop_sections[key] = entry 

32 

33 # 空の formLayout_4 は生成物の構造なので残し、余白だけ潰す 

34 for row in range(app.ui.formLayout_4.rowCount() - 1, -1, -1): 

35 app.ui.formLayout_4.takeRow(row) 

36 app.ui.formLayout_4.setContentsMargins(0, 0, 0, 0) 

37 app.ui.formLayout_4.setSpacing(0) 

38 app.ui.gridLayout_4.setContentsMargins(0, 0, 0, 0) 

39 app.ui.gridLayout_4.addWidget(container, 1, 0, 1, 1) 

40 app._dataset_property_sections_container = container 

41 

42 # 無効な親の下の子は有効にできないので、グループボックスは常に有効にして中身だけ無効にする 

43 # (でないと、データセットが無い間は節を開閉できない) 

44 app.ui.properties_groupbox.setEnabled(True) 

45 app._set_dataset_property_fields_enabled(False) 

46 

47 style_form = app._prop_form('style') 

48 # シンプルの種類のタイル(中身は property_widgets.build_property_widgets)。節の先頭の行 

49 app._plot_type_tiles_row = QWidget() 

50 tiles_layout = QVBoxLayout(app._plot_type_tiles_row) 

51 tiles_layout.setContentsMargins(0, 0, 0, 2) 

52 style_form.addRow(app._plot_type_tiles_row) 

53 style_form.addRow(app.ui.legend_name_label, app.ui.legend_name_edit) 

54 style_form.addRow(app.ui.plot_type_label, app.ui.plot_type_combo) 

55 style_form.addRow(app.ui.color_label, app.color_picker_widget) 

56 style_form.addRow(app.ui.linestyle_label, app.ui.linestyle_combo) 

57 style_form.addRow(app.ui.linewidth_label, app.ui.linewidth_spinbox) 

58 marker_form = app._prop_form('marker') 

59 marker_form.addRow(app.ui.marker_label, app.ui.marker_combo) 

60 marker_form.addRow(app.ui.makersize_label, app.ui.markersize_spinbox) 

61 

62 cap_combo_width(app.ui.plot_type_combo) 

63 

64 

65# 見出しの文字の真ん中に合わせる、スイッチの上の余白。QSS の見出しの余白から決めた値 

66# (見出しの余白は開閉で変えない。変えると押した瞬間に文字とスイッチが跳ねる) 

67SECTION_SWITCH_TOP_OFFSET = 9 

68 

69 

70def build_section(title, opened=True, first=False): 

71 """開閉できる節(見出しと、QFormLayout の中身)。{'form', 'body', 'toggle', 'section'} を返す。 

72 

73 プロパティの節と「軸」タブの節が同じ見た目になるよう、ここで作る。 

74 """ 

75 body = QWidget() 

76 body.setObjectName("property_section_body") 

77 form = QFormLayout(body) 

78 # 見本どおり、見出しより 20px 字下げし、名前と欄の間 10px、行の間 4px 

79 form.setContentsMargins(20, 0, 0, 6) 

80 form.setHorizontalSpacing(10) 

81 form.setVerticalSpacing(4) 

82 form.setRowWrapPolicy(QFormLayout.RowWrapPolicy.WrapLongRows) 

83 

84 # 閉じていても今の状態(「オフ」など)が右に出る見出し(GUI 刷新) 

85 toggle_button = SectionHeader(title) 

86 toggle_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon) 

87 # 上の2つの開閉ボタンとは別の名前(theme.py で控えめに描く) 

88 toggle_button.setObjectName("property_subsection_toggle") 

89 toggle_button.setCursor(Qt.CursorShape.PointingHandCursor) 

90 toggle_button.setFocusPolicy(Qt.FocusPolicy.NoFocus) 

91 # QToolButton は文字幅なので、区切りの罫線が見出しの下にしか引かれない 

92 toggle_button.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed) 

93 toggle_button.setChecked(opened) 

94 # 1本目は親の見出しのすぐ下なので、罫線が二重に見える 

95 if first: 

96 toggle_button.setProperty("firstSection", True) 

97 

98 section = QWidget() 

99 section_layout = QVBoxLayout(section) 

100 section_layout.setContentsMargins(0, 0, 0, 0) 

101 section_layout.setSpacing(0) 

102 section_layout.addWidget(toggle_button) 

103 clip = motion.SectionClip(body, section) 

104 section_layout.addWidget(clip) 

105 

106 entry = {'form': form, 'body': body, 'toggle': toggle_button, 'section': section, 'clip': clip} 

107 

108 def refresh_body(_checked=None): 

109 # 見出しのスイッチがある節は、オフの間は中身を出さない(オフでは中の欄を使わない) 

110 switch = entry.get('switch') 

111 shown = toggle_button.isChecked() and (switch is None or switch.isChecked()) 

112 toggle_button.setIcon(_svg_icon("chevron-down" if shown else "chevron-right", size=13)) 

113 # 見えている節を押したときだけ高さを伸び縮みさせる(組み立て・復元・隠れたタブではすぐ切り替える) 

114 if not (motion.is_enabled() and section.isVisible()): 

115 motion.stop(clip, "_motion_expand") 

116 for widget in (body, clip): 

117 widget.setMinimumHeight(0) 

118 widget.setMaximumHeight(motion.UNBOUNDED_HEIGHT) 

119 widget.setVisible(shown) 

120 return 

121 # スイッチでオンにしたとき(グラデーションなど)は、同じ知らせで中の行が出し入れされる。 

122 # 開くときは行が出そろってから高さを測るよう、動き出すのは次の回にする。 

123 # 閉じるときは行が消える前に、今の中身のまま止めておく 

124 if not shown and not clip.isHidden(): 

125 # 中の行が消えるのはスイッチをオフにしたときだけ。そのときだけ絵を撮っておく 

126 motion.freeze_for_close(clip, keep_picture=switch is not None and not switch.isChecked()) 

127 if not entry.get('_motion_pending'): 

128 entry['_motion_pending'] = True 

129 QTimer.singleShot(0, start_motion) 

130 

131 def start_motion(): 

132 entry['_motion_pending'] = False 

133 switch = entry.get('switch') 

134 shown = toggle_button.isChecked() and (switch is None or switch.isChecked()) 

135 moving = motion.running(clip, "_motion_expand") 

136 # 動いている途中なら、その行き先(開くか閉じるか)と比べる 

137 heading_open = entry.get('_opening', False) if moving else not clip.isHidden() 

138 if shown != heading_open: 138 ↛ exitline 138 didn't return from function 'start_motion' because the condition on line 138 was always true

139 entry['_opening'] = shown 

140 motion.animate_section(clip, shown) 

141 

142 entry['refresh_body'] = refresh_body 

143 toggle_button.toggled.connect(refresh_body) 

144 refresh_body() 

145 return entry 

146 

147 

148def put_switch_in_section_header(app, section_key, checkbox): 

149 """プロパティの節の見出しに、その機能のオン/オフのスイッチを置く(attach_header_switch)。""" 

150 attach_header_switch(app._prop_sections[section_key], checkbox) 

151 

152 

153def attach_header_switch(entry, checkbox): 

154 """節の見出しの右端に、その機能のオン/オフのスイッチを置く(節を開かなくてもオンにできる)。 

155 

156 スイッチは元のチェックボックスそのもの(バインディングと変更の処理はそのまま)。オンにしたら節を開く。 

157 見出しに置くので、その節は中の行が全部隠れていてもスイッチが出ていれば見出しを出す。 

158 """ 

159 header = entry['toggle'] 

160 checkbox.setToolTip(checkbox.text()) 

161 checkbox.setText("") 

162 checkbox.setFocusPolicy(Qt.FocusPolicy.NoFocus) 

163 checkbox.setCursor(Qt.CursorShape.PointingHandCursor) 

164 theme.make_toggle_switch(checkbox) 

165 row = QHBoxLayout(header) 

166 row.setContentsMargins(0, SECTION_SWITCH_TOP_OFFSET, 2, 0) 

167 row.addStretch(1) 

168 row.addWidget(checkbox, 0, Qt.AlignmentFlag.AlignVCenter) 

169 checkbox.clicked.connect(lambda checked: header.setChecked(True) if checked else None) 

170 entry['switch'] = checkbox 

171 checkbox.toggled.connect(entry['refresh_body']) 

172 entry['refresh_body']() 

173 

174 

175def prop_form(app, section_key): 

176 return app._prop_sections[section_key]['form'] 

177 

178 

179def align_form_label_columns(app, forms, width=None): 

180 """ラベルの列幅を複数の QFormLayout で揃え、入力欄の左端を合わせる。決めた幅(px)を返す。 

181 

182 隠れているラベルも数える(QFormLayout は隠れたものを外すので、出た瞬間に列幅が変わって欄がずれる)。 

183 width を渡すとその幅に固定し、入らない名前は折り返す(インスペクタのプロパティは見本どおり 92px)。 

184 """ 

185 labels = [] 

186 widest = 0 

187 for form in forms: 

188 for row in range(form.rowCount()): 

189 item = form.itemAt(row, QFormLayout.ItemRole.LabelRole) 

190 if item is None or not isinstance(item.widget(), QLabel): 

191 continue 

192 label = item.widget() 

193 labels.append(label) 

194 widest = max(widest, label.sizeHint().width()) 

195 

196 if width is not None: 196 ↛ 201line 196 didn't jump to line 201 because the condition on line 196 was always true

197 for label in labels: 

198 label.setFixedWidth(width) 

199 label.setWordWrap(True) 

200 return width 

201 for label in labels: 

202 label.setMinimumWidth(widest) 

203 return widest 

204 

205 

206def set_dataset_property_fields_enabled(app, enabled): 

207 """選択が無いときは無効にする。無効にするのは節の中身だけ(見出しまで無効にすると開閉できない)。""" 

208 for entry in getattr(app, '_prop_sections', {}).values(): 

209 entry['body'].setEnabled(enabled) 

210 

211 

212def load_collapsed_property_sections(app): 

213 raw = app_settings.DATASET_PROPERTY_COLLAPSED_SECTIONS.read(app.settings) 

214 try: 

215 keys = json.loads(raw) if isinstance(raw, str) else list(raw) 

216 except (ValueError, TypeError): 

217 return set() 

218 valid = {key for key, _ in DATASET_PROPERTY_SECTIONS} 

219 return {k for k in keys if k in valid} 

220 

221 

222def on_property_section_toggled(app, section_key, checked): 

223 """開閉し、閉じている節を QSettings に書き戻す(一度閉じたものは次の起動でも閉じたまま)。""" 

224 entry = app._prop_sections.get(section_key) 

225 if entry is None: 225 ↛ 226line 225 didn't jump to line 226 because the condition on line 225 was never true

226 return 

227 entry['refresh_body']() 

228 

229 collapsed = sorted( 

230 key for key, _ in DATASET_PROPERTY_SECTIONS 

231 if not app._prop_sections[key]['toggle'].isChecked() 

232 ) 

233 app_settings.DATASET_PROPERTY_COLLAPSED_SECTIONS.write(app.settings, json.dumps(collapsed)) 

234 

235 

236def lite_hidden_property_widgets(app): 

237 """シンプルで隠す行の欄と名前。""" 

238 widgets = [] 

239 for dotted_name in ui_mode.LITE_HIDDEN_PROPERTY_FIELDS: 

240 field = ui_mode.resolve(app, dotted_name) 

241 if field is None: 241 ↛ 242line 241 didn't jump to line 242 because the condition on line 241 was never true

242 continue 

243 widgets.append(field) 

244 for entry in app._prop_sections.values(): 

245 label = entry['form'].labelForField(field) 

246 if label is not None: 

247 widgets.append(label) 

248 break 

249 return widgets 

250 

251 

252def update_property_section_visibility(app): 

253 """シンプルで隠す行を隠し、中の行が全部隠れた節は見出しごと隠す(シンプルで出さない節も)。 

254 

255 行の出し入れ(property_panel の update_*_visibility)の最後に呼ばれるので、モードの条件はここで当てる。 

256 """ 

257 sections = getattr(app, '_prop_sections', None) 

258 if not sections: 258 ↛ 259line 258 didn't jump to line 259 because the condition on line 258 was never true

259 return 

260 lite = getattr(app, 'ui_mode', None) == ui_mode.LITE 

261 if lite: 

262 for widget in lite_hidden_property_widgets(app): 

263 widget.setVisible(False) 

264 for key, _ in DATASET_PROPERTY_SECTIONS: 

265 entry = sections.get(key) 

266 if entry is None: 266 ↛ 267line 266 didn't jump to line 267 because the condition on line 266 was never true

267 continue 

268 form = entry['form'] 

269 switch = entry.get('switch') 

270 has_visible = switch is not None and not switch.isHidden() 

271 for row in range(form.rowCount()): 

272 for role in (QFormLayout.ItemRole.LabelRole, 

273 QFormLayout.ItemRole.FieldRole, 

274 QFormLayout.ItemRole.SpanningRole): 

275 item = form.itemAt(row, role) 

276 if item is not None and item.widget() is not None \ 

277 and not item.widget().isHidden(): 

278 has_visible = True 

279 break 

280 if has_visible: 

281 break 

282 entry['section'].setVisible(has_visible and (not lite or key in ui_mode.LITE_PROPERTY_SECTIONS))