Coverage for graphica/gui/builders/axis_range_sections.py: 93.4%

92 statements  

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

1"""「軸」タブの X軸・Y軸を、範囲・目盛・目盛の数値(と単位・第2Y軸)の開閉できる節に分ける。 

2 

3ラベル/書式の節(axis_format_sections)と同じく、行は Designer と axis_panel が作った QFormLayout から 

4takeRow() で移すだけなので、欄の部品と値の対応 AXIS_BINDINGS は変わらない。 

5""" 

6from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QWidget 

7 

8from graphica.core.i18n import tr 

9from graphica.gui import ui_mode 

10from graphica.gui.builders.property_sections import build_section 

11from graphica.gui.widgets import ComboMirror, SegmentedControl 

12 

13# 節: (キー, 見出し, 移す行(その行の名前か欄の属性の名前、並び順))。{a} は軸の接頭辞(x / y) 

14_AXIS_SECTIONS = ( 

15 ("range", "範囲", ("ui.{a}_autoscale_checkbox", "ui.{a}_min_label", "ui.{a}_max_label", 

16 "{a}_view_range_button", "ui.{a}_log_checkbox", "{a}_log_note", "ui.{a}_invert_checkbox")), 

17 ("ticks", "目盛", ("{a}_ticks_visible_checkbox", "ui.{a}_major_tick_label", "ui.{a}_major_tick_interval_label", 

18 "ui.{a}_minor_ticks_visible_checkbox", "ui.{a}_minor_tick_interval_label", 

19 "{a}_log_minor_subs_label", "{a}_log_minor_labels_checkbox")), 

20 ("tick_labels", "目盛の数値", ("{a}_tick_labels_visible_checkbox", "{a}_tick_format_label", 

21 "{a}_tick_decimals_spinbox")), 

22) 

23X_UNIT_ROWS = ("x_secondary_axis_source_unit_label", "x_secondary_axis_target_unit_label") 

24# 第2Y軸の行(名前の無い行は欄の属性で指す)。並びは主の Y軸の「範囲」「目盛」と同じ 

25# 主目盛の決め方(選んだ番号で値を持つ index の束ね) 

26TICK_MODE_ITEMS = ("自動", "固定間隔") 

27TICK_MODE_COMBOS = (("x_ticks", "ui.x_major_tick_mode_combo"), ("y_ticks", "ui.y_major_tick_mode_combo"), 

28 ("y2", "y2_major_tick_mode_combo")) 

29Y2_ROWS = ("y2_autoscale_checkbox", "y2_min_spinbox", "y2_max_spinbox", "y2_view_range_button", 

30 "y2_log_checkbox", "y2_log_note", "y2_invert_checkbox", "y2_ticks_visible_checkbox", 

31 "y2_major_tick_mode_combo", "y2_major_tick_interval_spinbox", "y2_minor_ticks_visible_checkbox", 

32 "y2_minor_tick_interval_spinbox", "y2_tick_labels_visible_checkbox") 

33 

34 

35def build_axis_range_sections(app): 

36 """X軸・Y軸のタブの行を節に移す。第2Y軸のグループボックスも節にする。""" 

37 app._axis_tab_sections = {} 

38 x_layout = _sections_layout(app.ui.tab, app.ui.formLayout) 

39 y_layout = _sections_layout(app.ui.tab_2, app.ui.formLayout_2) 

40 for axis, source, layout in (("x", app.ui.formLayout, x_layout), ("y", app.ui.formLayout_2, y_layout)): 

41 for index, (key, title, rows) in enumerate(_AXIS_SECTIONS): 

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

43 for name in rows: 

44 _move_row(source, entry['form'], ui_mode.resolve(app, name.format(a=axis))) 

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

46 app._axis_tab_sections[f"{axis}_{key}"] = entry 

47 

48 units = build_section(tr("単位"), opened=True) 

49 for name in X_UNIT_ROWS: 

50 _move_row(app.ui.formLayout, units['form'], ui_mode.resolve(app, name)) 

51 x_layout.addWidget(units['section']) 

52 app._axis_tab_sections["x_units"] = units 

53 app.x_unit_section = units['section'] 

54 

55 y2 = build_section(tr("第2Y軸"), opened=False) 

56 group = app.y2_axis_group 

57 for name in Y2_ROWS: 

58 _move_row(group.layout(), y2['form'], ui_mode.resolve(app, name)) 

59 group.parentWidget().layout().removeWidget(group) 

60 group.hide() 

61 group.deleteLater() 

62 del app.y2_axis_group 

63 y_layout.addWidget(y2['section']) 

64 app._axis_tab_sections["y2"] = y2 

65 app.y2_axis_section = y2['section'] 

66 app._y2_in_use = None 

67 set_y2_in_use(app, False) 

68 

69 # 主目盛の 自動/固定間隔 は2択なのでセグメントにする。値の持ち主はコンボのまま(番号をデータにして突き合わせる) 

70 app.tick_mode_mirrors = [] 

71 for section_key, combo_name in TICK_MODE_COMBOS: 

72 combo = ui_mode.resolve(app, combo_name) 

73 segments = SegmentedControl() 

74 for index, text in enumerate(TICK_MODE_ITEMS): 

75 combo.setItemData(index, index) 

76 segments.addItem(tr(text), index) 

77 form = app._axis_tab_sections[section_key]['form'] 

78 field = QWidget() 

79 field_layout = QHBoxLayout(field) 

80 field_layout.setContentsMargins(0, 0, 0, 0) 

81 field_layout.addWidget(segments, 1) 

82 if form.replaceWidget(combo, field) is None: 82 ↛ 83line 82 didn't jump to line 83 because the condition on line 82 was never true

83 raise ValueError(f"{combo_name} が節のフォームにありません") 

84 combo.setParent(field) 

85 combo.hide() 

86 app.tick_mode_mirrors.append(ComboMirror(segments, combo)) 

87 

88 # 隣のタブのほうが高いと余りの高さが節に配られ、節の間が空くので、余りは末尾で受ける 

89 for layout in (x_layout, y_layout): 

90 layout.addStretch(1) 

91 # タブの中の左右の余白をなくし、入力欄の左端をほかのタブ(プロパティ・凡例・エクスポート)とそろえる 

92 for tab in (app.ui.tab, app.ui.tab_2, app.ui.tab_3): 

93 tab.layout().setContentsMargins(0, 10, 0, 0) 

94 

95 

96def set_y2_in_use(app, in_use): 

97 """第2Y軸を使うデータセットが出たら節を開き、いなくなったら畳んで「使っていません」と出す。 

98 

99 変わったときだけ開閉するので、使っていないときに手で開いたものは閉じない。 

100 """ 

101 sections = getattr(app, '_axis_tab_sections', None) 

102 if sections is None or app._y2_in_use == in_use: 

103 return 

104 app._y2_in_use = in_use 

105 toggle = sections["y2"]['toggle'] 

106 toggle.setChecked(in_use) 

107 if hasattr(toggle, 'set_state_text'): 107 ↛ exitline 107 didn't return from function 'set_y2_in_use' because the condition on line 107 was always true

108 toggle.set_state_text("" if in_use else tr("使っていません")) 

109 

110 

111def _sections_layout(tab, source): 

112 """タブの中で source(Designer のフォーム)があった位置の下に、節を縦に並べる入れ物を置く。""" 

113 container = QWidget() 

114 container.setObjectName("axis_tab_sections") 

115 layout = QVBoxLayout(container) 

116 layout.setContentsMargins(0, 0, 0, 0) 

117 layout.setSpacing(0) 

118 grid = tab.layout() 

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

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

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

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

123 break 

124 else: 

125 raise ValueError("タブの中にフォームが見つからない") 

126 return layout 

127 

128 

129def _move_row(source, target, anchor): 

130 row, _role = source.getWidgetPosition(anchor) 

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

132 raise ValueError(f"{anchor!r} は元のフォームにありません") 

133 taken = source.takeRow(row) 

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

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

136 target.addRow(*parts)