Coverage for graphica/gui/builders/axis_format_sections.py: 92.4%

141 statements  

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

1"""「軸」タブの「ラベル/書式」を、ラベル・目盛・グリッド・外枠・カラーバーの開閉できる節に分ける。 

2 

3行は Designer と axis_panel が formLayout_3 に作ったものを takeRow() で移す(欄の部品はそのままなので、 

4値の対応 AXIS_BINDINGS は変わらない)。見た目の部品(スライダー・セグメント)は値の持ち主の欄を取り込むか、 

5隠した持ち主と同期する。軸の設定を欄に戻した後(シグナルが止まっている)は refresh_axis_format_widgets() で合わせる。 

6""" 

7from PySide6.QtWidgets import QFormLayout, QHBoxLayout, QLabel, QPushButton, QVBoxLayout, QWidget 

8 

9from graphica.core.i18n import tr 

10from graphica.gui import ui_mode 

11from graphica.gui.builders.common import discard_widget 

12from graphica.gui.builders.property_sections import attach_header_switch, build_section 

13from graphica.gui.widgets import ComboMirror, SegmentedControl, SliderSpin 

14 

15# (キー, 見出し, 移す行(その行の名前か欄の属性の名前、並び順)) 

16AXIS_FORMAT_SECTIONS = ( 

17 ("labels", "ラベル", ("ui.title_text_label", "ui.x_label_text_label", "ui.y_label_text_label", 

18 "y2_label_text_label", "ui.axis_label_format_label")), 

19 ("ticks", "目盛", ("ui.tick_format_label", "tick_direction_label", "tick_direction_y2_label", 

20 "ui.tick_width_label", "tick_length_label")), 

21 ("grid", "グリッド", ("ui.minor_grid_visible_checkbox", "x_major_grid_style_label", "x_minor_grid_style_label", 

22 "y_major_grid_style_label", "y_minor_grid_style_label")), 

23 ("spine", "外枠", ("ui.spine_format_label",)), 

24 ("colorbar", "カラーバー", ("colorbar_position_label", "colorbar_width_label", "colorbar_label_label")), 

25) 

26# 節の見出しに置くオン/オフ(その機能を表示するかどうか) 

27AXIS_SECTION_SWITCHES = {"grid": "ui.grid_visible_checkbox", "colorbar": "colorbar_enabled_checkbox"} 

28 

29# 目盛の向き: (値, 表示, 説明)。コンボの文字がそのまま値 

30TICK_DIRECTION_ITEMS = (("out", "外", "外向き"), ("in", "内", "内向き"), ("inout", "両", "内外の両方")) 

31# 目盛の向きは主と補助を1行ずつ(2つ並べるとインスペクタの幅に入らない)。(名前の属性, 名前, 値の持ち主のコンボ) 

32TICK_DIRECTION_ROWS = ( 

33 ("tick_direction_label", "主目盛の向き", "major_tick_direction_combo"), 

34 ("minor_tick_direction_label", "補助目盛の向き", "minor_tick_direction_combo"), 

35 ("tick_direction_y2_label", "第2軸の主目盛", "major_tick_direction_y2_combo"), 

36 ("minor_tick_direction_y2_label", "第2軸の補助目盛", "minor_tick_direction_y2_combo"), 

37) 

38Y2_TICK_DIRECTION_LABELS = ("tick_direction_y2_label", "minor_tick_direction_y2_label") 

39# スライダーの範囲(よく使う範囲。数値の欄はこれより外の値も受け付ける) 

40TICK_WIDTH_SLIDER_RANGE = (0.0, 3.0) 

41SPINE_WIDTH_SLIDER_RANGE = (0.0, 3.0) 

42COLORBAR_WIDTH_SLIDER_RANGE = (0.01, 0.2) 

43 

44 

45def build_axis_format_sections(app): 

46 """formLayout_3 の行を節に移し、部品をスイッチ・スライダー・セグメントにする。凡例の行を移した後に呼ぶ。""" 

47 source = app.ui.formLayout_3 

48 container = QWidget() 

49 container.setObjectName("axis_format_sections") 

50 layout = QVBoxLayout(container) 

51 layout.setContentsMargins(0, 0, 0, 0) 

52 layout.setSpacing(0) 

53 app._axis_sections = {} 

54 for index, (key, title, rows) in enumerate(AXIS_FORMAT_SECTIONS): 

55 entry = build_section(tr(title), opened=True, first=(index == 0)) 

56 for name in rows: 

57 _move_row(source, entry['form'], ui_mode.resolve(app, name)) 

58 switch_name = AXIS_SECTION_SWITCHES.get(key) 

59 if switch_name is not None: 

60 checkbox = ui_mode.resolve(app, switch_name) 

61 row, _role = source.getWidgetPosition(checkbox) 

62 source.takeRow(row) 

63 attach_header_switch(entry, checkbox) 

64 layout.addWidget(entry['section']) 

65 app._axis_sections[key] = entry 

66 

67 # サブプロットが2つ以上あるときだけ出す(dock_layout.refresh_inspector) 

68 app.apply_format_to_all_button = QPushButton(tr("この書式を全サブプロットに適用")) 

69 app.apply_format_to_all_button.setToolTip( 

70 tr("フォント・目盛・グリッド・外枠・カラーバーの設定を、ほかのサブプロットにも写します")) 

71 app.apply_format_to_all_button.clicked.connect( 

72 lambda _checked=False: app.panels['axis_settings'].apply_format_to_all_subplots()) 

73 app.apply_format_to_all_button.setVisible(False) 

74 layout.addSpacing(10) 

75 layout.addWidget(app.apply_format_to_all_button) 

76 

77 # 隣のタブ(X軸・Y軸)のほうが高いと余りの高さが節に配られ、節の間が空くので、余りは末尾で受ける 

78 layout.addStretch(1) 

79 

80 # 行を移した後の formLayout_3 の場所に、節を入れる 

81 grid = app.ui.tab_3.layout() 

82 for index in range(grid.count()): 82 ↛ 87line 82 didn't jump to line 87 because the loop on line 82 didn't complete

83 if grid.itemAt(index).layout() is source: 83 ↛ 82line 83 didn't jump to line 82 because the condition on line 83 was always true

84 row, column, _rows, _columns = grid.getItemPosition(index) 

85 grid.addWidget(container, row + 1, column) 

86 break 

87 app.axis_format_sections_container = container 

88 

89 _build_axis_format_widgets(app) 

90 

91 

92def _move_row(source, target, anchor): 

93 row, _role = source.getWidgetPosition(anchor) 

94 if row < 0: 94 ↛ 95line 94 didn't jump to line 95 because the condition on line 94 was never true

95 raise ValueError(f"{anchor!r} は formLayout_3 にありません") 

96 taken = source.takeRow(row) 

97 items = [item for item in (taken.labelItem, taken.fieldItem) if item is not None] 

98 parts = [item.widget() if item.widget() is not None else item.layout() for item in items] 

99 target.addRow(*parts) 

100 

101 

102def _build_axis_format_widgets(app): 

103 ticks = app._axis_sections['ticks']['form'] 

104 # 主/補助を横に並べた2行を外し、1行ずつのセグメントにする(置く位置は外した行の所) 

105 position, _role = ticks.getWidgetPosition(app.tick_direction_label) 

106 for label_attr in ("tick_direction_label", "tick_direction_y2_label"): 

107 row, _role = ticks.getWidgetPosition(getattr(app, label_attr)) 

108 taken = ticks.takeRow(row) 

109 if taken.fieldItem is not None and taken.fieldItem.layout() is not None: 109 ↛ 106line 109 didn't jump to line 106 because the condition on line 109 was always true

110 holder = taken.fieldItem.layout() 

111 while holder.count(): 

112 holder.takeAt(0) 

113 holder.deleteLater() 

114 app.tick_direction_mirrors = [] 

115 for offset, (label_attr, text, combo_attr) in enumerate(TICK_DIRECTION_ROWS): 

116 label = getattr(app, label_attr, None) 

117 if label is None: 

118 label = QLabel() 

119 setattr(app, label_attr, label) 

120 label.setText(tr(text)) 

121 label.setToolTip(tr("{name}の向き").format(name=tr(text))) 

122 combo = getattr(app, combo_attr) 

123 segments = SegmentedControl() 

124 for value, item_text, tooltip in TICK_DIRECTION_ITEMS: 

125 segments.addItem(tr(item_text), value, tr(tooltip)) 

126 ticks.insertRow(position + offset, label, _hidden_owner(segments, combo)) 

127 app.tick_direction_mirrors.append(ComboMirror(segments, combo)) 

128 app.tick_width_slider = _slider_in_form(ticks, app.ui.tick_width_spinbox, *TICK_WIDTH_SLIDER_RANGE) 

129 

130 # 外枠: 太さ(スライダー)と色を別の行に 

131 spine = app._axis_sections['spine']['form'] 

132 row, _role = spine.getWidgetPosition(app.ui.spine_format_label) 

133 taken = spine.takeRow(row) 

134 owner_layout = taken.fieldItem.layout() if taken.fieldItem is not None else None 

135 if owner_layout is not None: 135 ↛ 138line 135 didn't jump to line 138 because the condition on line 135 was always true

136 owner_layout.removeWidget(app.ui.spine_width_spinbox) 

137 owner_layout.removeWidget(app.ui.spine_color_button) 

138 app.ui.spine_format_label.setText(tr("外枠の太さ")) 

139 app.spine_width_slider = SliderSpin(app.ui.spine_width_spinbox, *SPINE_WIDTH_SLIDER_RANGE) 

140 spine.addRow(app.ui.spine_format_label, app.spine_width_slider) 

141 app.spine_color_label = QLabel(tr("外枠の色")) 

142 spine.addRow(app.spine_color_label, app.ui.spine_color_button) 

143 

144 colorbar = app._axis_sections['colorbar']['form'] 

145 app.colorbar_position_segments = SegmentedControl() 

146 for index in range(app.colorbar_position_combo.count()): 

147 app.colorbar_position_segments.addItem(app.colorbar_position_combo.itemText(index), 

148 app.colorbar_position_combo.itemData(index)) 

149 owner = _hidden_owner(app.colorbar_position_segments, None) 

150 colorbar.replaceWidget(app.colorbar_position_combo, owner) 

151 _adopt_hidden(owner, app.colorbar_position_combo) 

152 app.colorbar_position_mirror = ComboMirror(app.colorbar_position_segments, app.colorbar_position_combo) 

153 app.colorbar_width_slider = _slider_in_form(colorbar, app.colorbar_width_spinbox, *COLORBAR_WIDTH_SLIDER_RANGE) 

154 

155 

156def set_y2_tick_direction_visible(app, visible): 

157 """第2軸の目盛の向きの2行を出し入れする(第2Y軸を使っているサブプロットがあるときだけ出す)。""" 

158 sections = getattr(app, '_axis_sections', None) 

159 for label_attr in Y2_TICK_DIRECTION_LABELS: 

160 label = getattr(app, label_attr, None) 

161 if label is None: 161 ↛ 162line 161 didn't jump to line 162 because the condition on line 161 was never true

162 continue 

163 if sections is None: 163 ↛ 164line 163 didn't jump to line 164 because the condition on line 163 was never true

164 label.setVisible(visible) 

165 else: 

166 sections['ticks']['form'].setRowVisible(label, visible) 

167 

168 

169def refresh_axis_format_widgets(app): 

170 """軸の設定を欄に戻した後(シグナルが止まっている)、見た目の部品を合わせる。""" 

171 if not hasattr(app, 'colorbar_width_slider'): 171 ↛ 172line 171 didn't jump to line 172 because the condition on line 171 was never true

172 return 

173 for mirror in (*app.tick_direction_mirrors, *getattr(app, 'tick_mode_mirrors', ()), 

174 *getattr(app, 'grid_linestyle_mirrors', ())): 

175 mirror.refresh() 

176 app.colorbar_position_mirror.refresh() 

177 for slider in (app.tick_width_slider, app.spine_width_slider, app.colorbar_width_slider): 

178 slider.refresh() 

179 

180 

181def _hidden_owner(view, owner): 

182 """見た目の部品と、隠した値の持ち主(コンボ)を1つの欄にまとめる。""" 

183 field = QWidget() 

184 layout = QHBoxLayout(field) 

185 layout.setContentsMargins(0, 0, 0, 0) 

186 layout.setSpacing(0) 

187 layout.addWidget(view, 1) 

188 if owner is not None: 

189 _adopt_hidden(field, owner) 

190 return field 

191 

192 

193def _adopt_hidden(field, owner): 

194 owner.setParent(field) 

195 owner.hide() 

196 

197 

198def _slider_in_form(form: QFormLayout, spinbox, slider_min, slider_max): 

199 placeholder = QWidget() 

200 if form.replaceWidget(spinbox, placeholder) is None: 200 ↛ 201line 200 didn't jump to line 201 because the condition on line 200 was never true

201 raise ValueError("欄がフォームに見つからない") 

202 slider = SliderSpin(spinbox, slider_min, slider_max) 

203 form.replaceWidget(placeholder, slider) 

204 discard_widget(placeholder) 

205 return slider