Coverage for graphica/gui/shell/inspector.py: 97.7%

120 statements  

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

1"""右のインスペクタ: 下線のタブ(プロパティ・軸・凡例・エクスポート)と、選んでいるデータセットの見出し。 

2 

3見本(docs/dev/mockups/gui_redesign/)では幅 340px。中身は今の部品をそのまま並べ直す(値の対応は変わらない)。 

4""" 

5from PySide6.QtCore import QSize, Qt 

6from PySide6.QtWidgets import (QFormLayout, QHBoxLayout, QLabel, QScrollArea, QStackedWidget, QTabBar, QVBoxLayout, 

7 QWidget) 

8 

9from graphica.core.i18n import tr 

10from graphica.gui import motion 

11from graphica.gui.dataset_bindings import MARKER_CHOICES, NO_MARKER 

12from graphica.gui.dataset_style_icon import NON_MARKER_PLOT_TYPES, make_dataset_style_icon 

13from graphica.gui.widgets import ElidedLabel 

14 

15INSPECTOR_WIDTH = 372 

16# (キー, タブの名前)。並びは見本どおり 

17INSPECTOR_PAGES = (("properties", "プロパティ"), ("axes", "軸"), ("legend", "凡例"), ("export", "エクスポート")) 

18 

19 

20def scroll_page(widget, object_name): 

21 """縦だけスクロールする入れ物(横に出ると欄が切れるので、横のスクロールバーは出さない)。""" 

22 scroll = QScrollArea() 

23 scroll.setObjectName(object_name) 

24 scroll.setWidgetResizable(True) 

25 scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) 

26 # 出し入れすると中の幅が変わって欄が並び直すので、縦のスクロールバーの幅はいつも取っておき、 

27 # スクロールするものが無いときはつまみを見せない(QSS の scrollIdle) 

28 scroll.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn) 

29 bar = scroll.verticalScrollBar() 

30 bar.rangeChanged.connect(lambda minimum, maximum: _set_scroll_idle(bar, maximum <= minimum)) 

31 _set_scroll_idle(bar, True) 

32 scroll.setWidget(widget) 

33 return scroll 

34 

35 

36def _set_scroll_idle(bar, idle): 

37 if bar.property("scrollIdle") == idle: 

38 return 

39 bar.setProperty("scrollIdle", idle) 

40 bar.style().unpolish(bar) 

41 bar.style().polish(bar) 

42 

43 

44def wrap_long_rows(widget): 

45 """中の QFormLayout を、入らない行は名前を上・欄を下に折り返す形にする(幅 340px に収めるため)。""" 

46 for form in widget.findChildren(QFormLayout): 

47 form.setRowWrapPolicy(QFormLayout.RowWrapPolicy.WrapLongRows) 

48 

49 

50class TargetHeader(QWidget): 

51 """選んでいるデータセットの見出し: 線の見本・名前・「時間_s → 温度_degC · Line」。""" 

52 

53 def __init__(self, parent=None): 

54 super().__init__(parent) 

55 self.setObjectName("inspector_target") 

56 self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) 

57 layout = QHBoxLayout(self) 

58 layout.setContentsMargins(16, 10, 16, 10) 

59 layout.setSpacing(10) 

60 self.swatch = QLabel() 

61 self.swatch.setFixedWidth(24) 

62 texts = QVBoxLayout() 

63 texts.setSpacing(0) 

64 self.name_label = ElidedLabel("-") 

65 self.name_label.setObjectName("inspector_target_name") 

66 self.sub_label = ElidedLabel("") 

67 self.sub_label.setObjectName("inspector_target_sub") 

68 texts.addWidget(self.name_label) 

69 texts.addWidget(self.sub_label) 

70 layout.addWidget(self.swatch) 

71 layout.addLayout(texts, 1) 

72 

73 def set_dataset(self, dataset): 

74 if dataset is None: 

75 self.swatch.clear() 

76 self.name_label.setText(tr("データセットを選んでください")) 

77 self.sub_label.setText("") 

78 return 

79 self.swatch.setPixmap(make_dataset_style_icon(dataset).pixmap(QSize(24, 10))) 

80 self.name_label.setText(dataset.name) 

81 x_name = getattr(dataset, 'x_col_name', '') or '' 

82 y_name = getattr(dataset, 'y_col_name', '') or '' 

83 self.sub_label.setText(f"{x_name} → {y_name} · {dataset.plot_type}") 

84 

85 

86def section_state_texts(dataset): 

87 """閉じていても分かるよう、節の見出しの右に出す今の状態(節のキー -> 文字)。""" 

88 if dataset is None: 

89 return {} 

90 marker_names = dict(MARKER_CHOICES) 

91 if dataset.plot_type in NON_MARKER_PLOT_TYPES or dataset.marker in (NO_MARKER, None, ''): 

92 marker = tr("なし") 

93 else: 

94 marker = tr(marker_names.get(dataset.marker, dataset.marker)) 

95 on, off = tr("オン"), tr("オフ") 

96 return { 

97 "marker": marker, 

98 "gradient": on if dataset.gradient_enabled else off, 

99 "waterfall": on if dataset.waterfall_enabled else off, 

100 "map": "2D" if getattr(dataset, 'data_kind', '1d') == '2d_grid' else off, 

101 } 

102 

103 

104# タブとタブの間(QSS の QTabBar#inspector_tabs::tab の margin-right と同じ値) 

105INSPECTOR_TAB_SPACING = 16 

106 

107 

108class Inspector(QWidget): 

109 """下線のタブと、タブごとのページ。""" 

110 

111 def __init__(self, parent=None): 

112 super().__init__(parent) 

113 self.setObjectName("inspector") 

114 self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) 

115 layout = QVBoxLayout(self) 

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

117 layout.setSpacing(0) 

118 self.tab_bar = QTabBar() 

119 self.tab_bar.setObjectName("inspector_tabs") 

120 self.tab_bar.setDrawBase(False) 

121 self.tab_bar.setExpanding(False) 

122 self.tab_bar.setFocusPolicy(Qt.FocusPolicy.NoFocus) 

123 tab_row = QHBoxLayout() 

124 tab_row.setContentsMargins(16, 0, 16, 0) 

125 tab_row.addWidget(self.tab_bar) 

126 tab_row.addStretch(1) 

127 tab_box = QWidget() 

128 tab_box.setObjectName("inspector_tab_row") 

129 tab_box.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) 

130 tab_box.setLayout(tab_row) 

131 layout.addWidget(tab_box) 

132 self.stack = QStackedWidget() 

133 layout.addWidget(self.stack, 1) 

134 self._keys = [] 

135 self.tab_bar.currentChanged.connect(self._on_tab_changed) 

136 self.tab_indicator = motion.TabIndicator(self.tab_bar, trailing_margin=INSPECTOR_TAB_SPACING) 

137 

138 def _on_tab_changed(self, index): 

139 before = motion.grab(self.stack) 

140 self.stack.setCurrentIndex(index) 

141 motion.crossfade(self.stack, before) 

142 

143 def add_page(self, key, title, widget): 

144 self._keys.append(key) 

145 self.stack.addWidget(widget) 

146 self.tab_bar.addTab(tr(title)) 

147 

148 def page_index(self, key): 

149 return self._keys.index(key) 

150 

151 def show_page(self, key): 

152 self.tab_bar.setCurrentIndex(self.page_index(key)) 

153 

154 def current_page(self): 

155 index = self.tab_bar.currentIndex() 

156 return self._keys[index] if 0 <= index < len(self._keys) else None 

157 

158 def set_page_visible(self, key, visible): 

159 """タブを見せる・隠す(アドバンスドだけの「凡例」)。隠したタブを開いていたら最初のタブへ。""" 

160 index = self.page_index(key) 

161 self.tab_bar.setTabVisible(index, visible) 

162 # タブを出し直しても QTabBar は自分の幅の目安を知らせないので、並びの幅が前のまま残り、 

163 # 「エクスポート」が切れて ‹ › が出る(シンプル → アドバンスドで凡例のタブを出したとき) 

164 self.tab_bar.updateGeometry() 

165 if not visible and self.tab_bar.currentIndex() == index: 165 ↛ 166line 165 didn't jump to line 166 because the condition on line 165 was never true

166 self.tab_bar.setCurrentIndex(0) 

167 

168 def is_page_visible(self, key): 

169 return self.tab_bar.isTabVisible(self.page_index(key))