Coverage for graphica/gui/dock_layout.py: 96.3%
106 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 19:22 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 19:22 +0000
1"""ドックの配置: 右のインスペクタの組み立てと、起動時の復元(パネルは固定で、動かせない)。"""
2import logging
3from PySide6.QtWidgets import QDockWidget, QVBoxLayout, QWidget
4from graphica.core.i18n import tr
5from graphica.gui import app_settings, theme
6from graphica.gui.builders.common import DOCK_LAYOUT_VERSION
7from graphica.gui.export_preview_panel import ExportPreviewPanel
8from graphica.gui.builders.axis_format_sections import build_axis_format_sections
9from graphica.gui.builders.axis_range_sections import build_axis_range_sections
10from graphica.gui.builders.property_sections import attach_header_switch, build_section
11from graphica.gui.shell.inspector import (INSPECTOR_PAGES, Inspector, TargetHeader, scroll_page,
12 section_state_texts, wrap_long_rows)
14logger = logging.getLogger(__name__)
17def arrange_property_docks(app):
18 """右のインスペクタ(GUI 刷新): プロパティ・軸・凡例・エクスポートのタブを、プロパティのドックの中に並べる。
20 ドックは動かさない(決定 5)。見出しの帯はタブが兼ねるので出さない。properties_dock_widget は
21 control_dock_widget の別名(表示メニューなどがこの名前で使う)。
22 """
23 dock = app.ui.control_dock_widget
24 app.properties_dock_widget = dock
25 dock.setWindowTitle(tr("プロパティ"))
26 dock.setTitleBarWidget(QWidget())
27 dock.setFeatures(QDockWidget.DockWidgetFeature.NoDockWidgetFeatures)
29 plot_properties = dock.widget()
30 if plot_properties is None:
31 logger.warning("control_dock_widget の中身が見つかりません。")
32 plot_properties = QWidget()
33 legend_page = _take_legend_rows(app)
34 build_axis_format_sections(app)
35 build_axis_range_sections(app)
36 app.ui.properties_groupbox.setTitle("")
37 # theme.py はグループボックスのタイトル用に上の余白を取る。タイトルが無いので、それを 0 にする目印
38 app.ui.properties_groupbox.setProperty("collapsibleBody", True)
40 app.inspector = Inspector()
41 app.inspector_target = TargetHeader()
42 properties_page = QWidget()
43 properties_page.setObjectName("inspector_page")
44 properties_layout = QVBoxLayout(properties_page)
45 properties_layout.setContentsMargins(0, 0, 0, 0)
46 properties_layout.setSpacing(0)
47 properties_layout.addWidget(app.inspector_target)
48 properties_layout.addWidget(app.ui.properties_groupbox)
49 properties_layout.addStretch(1)
51 plot_properties.setObjectName("inspector_axes_content")
52 wrap_long_rows(plot_properties)
53 # 見本どおり左右 16px の余白(欄が欄の端に付かないように)
54 app.ui.gridLayout_4.setContentsMargins(16, 0, INSPECTOR_RIGHT_MARGIN, 8)
55 if plot_properties.layout() is not None:
56 plot_properties.layout().setContentsMargins(16, 8, INSPECTOR_RIGHT_MARGIN, 8)
58 # エクスポートのプレビュー。見えている間だけ描く(ExportPreviewPanel.refresh_preview)ので、裏のタブにあれば重くならない
59 app.export_preview_panel = ExportPreviewPanel(app)
61 for key, title in INSPECTOR_PAGES:
62 widget = {"properties": properties_page, "axes": plot_properties, "legend": legend_page,
63 "export": app.export_preview_panel}[key]
64 app.inspector.add_page(key, title, scroll_page(widget, f"inspector_scroll_{key}"))
65 app.inspector.tab_bar.currentChanged.connect(lambda _index: _on_inspector_page_changed(app))
66 dock.setWidget(app.inspector)
67 # 機能のオン/オフ(1行を占めるチェックボックス)はスイッチにする
68 theme.make_option_rows_switches(app.inspector)
70 # 選んでいるデータセットの見出しと節の状態は、描き直すたびに合わせる(選択・名前・種類の変更をまとめて拾える)
71 app.canvas.mpl_connect('draw_event', lambda _event: refresh_inspector(app))
74# インスペクタの中身の右の余白。縦のスクロールバーの手前に少し空ける(左の 16px より広め)
75INSPECTOR_RIGHT_MARGIN = 22
78# 凡例のタブに移す行(軸の設定の formLayout_3 から)。表示のチェックは格子の表示と同じ行の中なので、それも取り出す
79_LEGEND_FIELDS = ("legend_loc_combo", "legend_columns_spinbox", "legend_frame_checkbox", "legend_font_button",
80 "legend_color_button", "legend_order_button")
83def _take_legend_rows(app):
84 """凡例のタブ: ほかのタブと同じ節にし、「凡例を表示」は見出しのスイッチにする(オフなら中身を畳む)。"""
85 page = QWidget()
86 page.setObjectName("inspector_legend_content")
87 page_layout = QVBoxLayout(page)
88 page_layout.setContentsMargins(16, 8, INSPECTOR_RIGHT_MARGIN, 8)
89 page_layout.setSpacing(0)
90 entry = build_section(tr("凡例"), opened=True, first=True)
91 form = entry['form']
92 visible_checkbox = app.ui.legend_visible_checkbox
93 visible_checkbox.parentWidget().layout().removeWidget(visible_checkbox)
94 attach_header_switch(entry, visible_checkbox)
95 page_layout.addWidget(entry['section'])
96 page_layout.addStretch(1)
97 app.legend_section = entry
98 source = app.ui.formLayout_3
99 for attr in _LEGEND_FIELDS:
100 row, _role = source.getWidgetPosition(getattr(app, attr))
101 taken = source.takeRow(row)
102 label = taken.labelItem.widget() if taken.labelItem is not None else None
103 field = taken.fieldItem.widget() if taken.fieldItem is not None else None
104 if label is not None and field is not None:
105 form.addRow(label, field)
106 else:
107 form.addRow(field or label)
108 return page
111def inspector_forms(app):
112 """インスペクタの名前と欄の並ぶフォームすべて(名前の列の幅をそろえるため)。"""
113 entries = []
114 for attr in ('_prop_sections', '_axis_tab_sections', '_axis_sections'):
115 entries += list(getattr(app, attr, {}).values())
116 if hasattr(app, 'legend_section'): 116 ↛ 118line 116 didn't jump to line 118 because the condition on line 116 was always true
117 entries.append(app.legend_section)
118 forms = [entry['form'] for entry in entries]
119 if hasattr(app, 'export_preview_panel'): 119 ↛ 121line 119 didn't jump to line 121 because the condition on line 119 was always true
120 forms += app.export_preview_panel.section_forms()
121 return forms
124def _on_inspector_page_changed(app):
125 if app.inspector.current_page() == "export":
126 app.export_preview_panel.refresh_preview()
129def refresh_inspector(app):
130 """右の見出しと、節の見出しの右の状態の文字を、表示しているデータセットに合わせる。"""
131 dataset = app.property_panel.shown_dataset()
132 app.inspector_target.set_dataset(dataset)
133 # 系列の数と並び(行数・列数・自由配置)が変わったのも、描き直しで拾う
134 if hasattr(app, 'subplot_target_bar'): 134 ↛ 136line 134 didn't jump to line 136 because the condition on line 134 was always true
135 app.subplot_target_bar.refresh()
136 if hasattr(app, 'apply_format_to_all_button'): 136 ↛ 138line 136 didn't jump to line 138 because the condition on line 136 was always true
137 app.apply_format_to_all_button.setVisible(len(app.project.all_plot_settings) > 1)
138 states = section_state_texts(dataset)
139 for key, entry in getattr(app, '_prop_sections', {}).items():
140 toggle = entry['toggle']
141 if hasattr(toggle, 'set_state_text'): 141 ↛ 139line 141 didn't jump to line 139 because the condition on line 141 was always true
142 # スイッチのある節は、スイッチがオン/オフを見せる
143 toggle.set_state_text("" if 'switch' in entry else states.get(key, ""))
146def restore_dock_layout(app):
147 """前回のドック配置を戻す。タブが最終の大きさになってから呼ぶ(でないとスプリッターの位置がずれる)。"""
148 saved_layout_version = (app_settings.DOCK_LAYOUT_VERSION.read(app.settings) if app._run_startup_checks
149 else DOCK_LAYOUT_VERSION)
150 saved_state = app_settings.WINDOW_STATE.read(app.settings) if app._run_startup_checks else None
151 if saved_state is not None and saved_layout_version == DOCK_LAYOUT_VERSION and app.restoreState(saved_state):
152 return
153 # 戻せなければ(初回・追加したタブ)組み立てたときの配置のまま。タブで並べたドックは後から足した方が前に出るので
154 # プロパティを前にする(表示される前の raise_() は効かないので、表示された後のここで行う)
155 app.ui.control_dock_widget.raise_()