Coverage for graphica/gui/shell/header.py: 96.4%

155 statements  

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

1"""上部バー: ≡(メニュー)・ロゴ・プロジェクトのタブ・コマンドを検索・モードの切り替え・元に戻す/やり直す・保存・ 

2クイックアクセス・エクスポート。高さ 48px の1段。見本は docs/dev/mockups/gui_redesign/。 

3 

4押したときの行き先(表示中のタブ)は MainAppWindow がつなぐ。ここは部品と並びだけを持つ。 

5""" 

6from PySide6.QtCore import QSize, Qt, Signal 

7from PySide6.QtGui import QIcon 

8from PySide6.QtWidgets import QCheckBox, QFrame, QHBoxLayout, QLabel, QMenu, QPushButton, QToolButton, QWidget 

9 

10from graphica.core.i18n import tr 

11from graphica.gui import ui_mode 

12from graphica.gui.shell.icons import themed_icon 

13from graphica.gui.shell.rail import RAIL_WIDTH 

14from graphica.gui.theme import make_toggle_switch 

15 

16HEADER_HEIGHT = 48 

17HEADER_BUTTON_SIZE = 30 

18LOGO_SIZE = 26 

19SEARCH_WIDTH = 300 

20COMMAND_PALETTE_SHORTCUT_TEXT = "Ctrl+Shift+P" 

21EXPORT_ICON = "file-export" 

22 

23 

24class ModeToggle(QWidget): 

25 """「アドバンスド (スイッチ)」。オンでアドバンスド、オフでシンプル。名前を押しても切り替わる。 

26 

27 mode_changed は利用者の操作でだけ出る(set_mode では出ない)。 

28 """ 

29 

30 mode_changed = Signal(str) 

31 

32 def __init__(self, parent=None): 

33 super().__init__(parent) 

34 self.setObjectName("mode_switch") 

35 self.setCursor(Qt.CursorShape.PointingHandCursor) 

36 self.setToolTip(tr("オンでアドバンスド(すべての設定)、オフでシンプル")) 

37 layout = QHBoxLayout(self) 

38 layout.setContentsMargins(4, 0, 4, 0) 

39 layout.setSpacing(8) 

40 self.label = QLabel(ui_mode.mode_label(ui_mode.ADVANCED)) 

41 self.label.setObjectName("mode_label") 

42 self.switch = QCheckBox() 

43 # 押すのはこの部品全体(スイッチだけが押しを受けると、名前を押しても切り替わらない) 

44 self.switch.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents, True) 

45 self.switch.setFocusPolicy(Qt.FocusPolicy.NoFocus) 

46 make_toggle_switch(self.switch) 

47 layout.addWidget(self.label) 

48 layout.addWidget(self.switch) 

49 

50 def mode(self): 

51 return ui_mode.ADVANCED if self.switch.isChecked() else ui_mode.LITE 

52 

53 def set_mode(self, mode): 

54 self.switch.setChecked(mode == ui_mode.ADVANCED) 

55 

56 def toggle(self): 

57 self.set_mode(ui_mode.LITE if self.mode() == ui_mode.ADVANCED else ui_mode.ADVANCED) 

58 self.mode_changed.emit(self.mode()) 

59 

60 def mouseReleaseEvent(self, event): 

61 if event.button() == Qt.MouseButton.LeftButton and self.rect().contains(event.position().toPoint()): 

62 self.toggle() 

63 super().mouseReleaseEvent(event) 

64 

65 

66def _header_button(name, tooltip): 

67 button = QToolButton() 

68 button.setObjectName("header_button") 

69 button.setToolTip(tooltip) 

70 button.setIconSize(QSize(18, 18)) 

71 button.setFixedSize(HEADER_BUTTON_SIZE, HEADER_BUTTON_SIZE) 

72 button.setFocusPolicy(Qt.FocusPolicy.NoFocus) 

73 button.setCursor(Qt.CursorShape.PointingHandCursor) 

74 button.setProperty("iconName", name) 

75 button.setIcon(themed_icon(name)) 

76 return button 

77 

78 

79def _separator(): 

80 line = QFrame() 

81 line.setObjectName("header_separator") 

82 line.setFrameShape(QFrame.Shape.VLine) 

83 line.setFixedSize(1, 20) 

84 return line 

85 

86 

87class AppHeader(QWidget): 

88 def __init__(self, tab_bar, app_icon_path, parent=None): 

89 super().__init__(parent) 

90 self.setObjectName("app_header") 

91 # 上の帯の色を QSS で塗るため(素の QWidget は背景を描かない) 

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

93 self.setFixedHeight(HEADER_HEIGHT) 

94 outer = QHBoxLayout(self) 

95 outer.setContentsMargins(0, 0, 12, 0) 

96 outer.setSpacing(0) 

97 

98 # ≡ は左の帯と同じ幅の箱の真ん中(帯のアイコンと同じ縦の列)。ロゴはその右、左パネルの白い所の左端から 

99 self.menu_button = _header_button("menu-2", tr("メニュー")) 

100 self.menu_button.setPopupMode(QToolButton.ToolButtonPopupMode.InstantPopup) 

101 # ▼の印を出さない(≡ だけ) 

102 self.menu_button.setStyleSheet("QToolButton::menu-indicator { image: none; width: 0; }") 

103 rail_column = QWidget() 

104 rail_column.setFixedWidth(RAIL_WIDTH) 

105 rail_column_layout = QHBoxLayout(rail_column) 

106 rail_column_layout.setContentsMargins(0, 0, 0, 0) 

107 rail_column_layout.addWidget(self.menu_button, 0, Qt.AlignmentFlag.AlignCenter) 

108 outer.addWidget(rail_column) 

109 

110 layout = QHBoxLayout() 

111 layout.setContentsMargins(0, 0, 0, 0) 

112 layout.setSpacing(6) 

113 outer.addLayout(layout, 1) 

114 

115 self.logo = QLabel() 

116 self.logo.setObjectName("header_logo") 

117 self.logo.setPixmap(QIcon(app_icon_path).pixmap(LOGO_SIZE, LOGO_SIZE)) 

118 layout.addWidget(self.logo) 

119 self.title_label = QLabel("Graphica") 

120 self.title_label.setObjectName("header_title") 

121 layout.addWidget(self.title_label) 

122 layout.addSpacing(8) 

123 

124 self.tab_bar = tab_bar 

125 layout.addWidget(tab_bar, 0, Qt.AlignmentFlag.AlignVCenter) 

126 self.add_tab_button = _header_button("plus", tr("新しいプロジェクトタブを開く")) 

127 self.add_tab_button.setObjectName("add_tab_button") 

128 layout.addWidget(self.add_tab_button) 

129 

130 # 検索は右側(モードの切り替えの左)に寄せる。タブが増えても動かない 

131 layout.addStretch(1) 

132 self.search_button = self._make_search_button() 

133 layout.addWidget(self.search_button) 

134 layout.addSpacing(10) 

135 

136 self.mode_switch = ModeToggle() 

137 layout.addWidget(self.mode_switch) 

138 layout.addWidget(_separator()) 

139 

140 self.undo_button = _header_button("arrow-back-up", tr("元に戻す")) 

141 self.redo_button = _header_button("arrow-forward-up", tr("やり直し")) 

142 self.save_button = _header_button("device-floppy", tr("上書き保存")) 

143 for button in (self.undo_button, self.redo_button, self.save_button): 

144 layout.addWidget(button) 

145 

146 # クイックアクセス(ピン留め)は ★ の一覧にまとめる(いくつ増やしても上部バーの幅を取らない)。 

147 # 中身は開くたびに表示中のタブのピン留めから作る(MainAppWindow) 

148 self.quick_access_button = _header_button("star", tr("クイックアクセス")) 

149 self.quick_access_button.setObjectName("header_button") 

150 self.quick_access_button.setPopupMode(QToolButton.ToolButtonPopupMode.InstantPopup) 

151 self.quick_access_button.setStyleSheet("QToolButton::menu-indicator { image: none; width: 0; }") 

152 self.quick_access_menu = QMenu(self.quick_access_button) 

153 self.quick_access_button.setMenu(self.quick_access_menu) 

154 layout.addWidget(self.quick_access_button) 

155 

156 # エクスポートはアイコンだけ(名前はツールチップ)。強調の色で塗って、ほかのボタンと見分ける 

157 self.export_button = QPushButton() 

158 self.export_button.setObjectName("header_export_button") 

159 self.export_button.setProperty("primaryButton", True) 

160 self.export_button.setToolTip(tr("エクスポート")) 

161 self.export_button.setCursor(Qt.CursorShape.PointingHandCursor) 

162 self.export_button.setFocusPolicy(Qt.FocusPolicy.NoFocus) 

163 self.export_button.setFixedSize(34, 30) 

164 self.export_button.setIconSize(QSize(17, 17)) 

165 self.export_button.setIcon(themed_icon(EXPORT_ICON, size=17, token="on_accent")) 

166 layout.addWidget(self.export_button) 

167 

168 def _make_search_button(self): 

169 button = QPushButton() 

170 button.setObjectName("command_search") 

171 button.setCursor(Qt.CursorShape.PointingHandCursor) 

172 button.setFocusPolicy(Qt.FocusPolicy.NoFocus) 

173 button.setFixedSize(SEARCH_WIDTH, 30) 

174 inner = QHBoxLayout(button) 

175 inner.setContentsMargins(10, 0, 8, 0) 

176 inner.setSpacing(6) 

177 self.search_icon = QLabel() 

178 self.search_icon.setPixmap(themed_icon("search", size=15, token="text_muted").pixmap(15, 15)) 

179 text = QLabel(tr("コマンドを検索")) 

180 text.setObjectName("command_search_text") 

181 shortcut = QLabel(COMMAND_PALETTE_SHORTCUT_TEXT) 

182 shortcut.setObjectName("kbd_hint") 

183 for label in (self.search_icon, text, shortcut): 

184 # ボタンの子のラベルが押下を取らないように(ボタン全体で押せる) 

185 label.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents, True) 

186 inner.addWidget(self.search_icon) 

187 inner.addWidget(text) 

188 inner.addStretch(1) 

189 # 検索欄の高さいっぱいに伸ばさず、文字の高さで真ん中に置く 

190 inner.addWidget(shortcut, 0, Qt.AlignmentFlag.AlignVCenter) 

191 return button 

192 

193 def set_mode(self, mode): 

194 self.mode_switch.set_mode(mode) 

195 

196 def refresh_icons(self): 

197 for button in self.findChildren(QToolButton, "header_button") + [self.add_tab_button]: 

198 name = button.property("iconName") 

199 if name: 199 ↛ 197line 199 didn't jump to line 197 because the condition on line 199 was always true

200 button.setIcon(themed_icon(name)) 

201 self.search_icon.setPixmap(themed_icon("search", size=15, token="text_muted").pixmap(15, 15)) 

202 self.export_button.setIcon(themed_icon(EXPORT_ICON, size=17, token="on_accent"))