-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoppanel.py
More file actions
300 lines (247 loc) · 10.9 KB
/
Copy pathtoppanel.py
File metadata and controls
300 lines (247 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Top control panel widget with syllable settings, navigation, and reading controls."""
from __future__ import annotations
import logging
from typing import Iterable, Optional, TYPE_CHECKING
from PyQt6.QtCore import Qt, QSize, pyqtSignal
from PyQt6.QtGui import (
QFontMetrics,
QPixmap,
)
from PyQt6.QtWidgets import (
QAbstractSpinBox,
QComboBox,
QFrame,
QHBoxLayout,
QLabel,
QSizePolicy,
QSpinBox,
QToolButton,
QVBoxLayout,
QWidget,
)
from combo_popup import SquarePopupComboBox
from gui_utils import resolve_resource, colored_icon
from styles import (
TOP_PANEL_SEPARATOR_STYLESHEET,
C_TOP_TEXT,
build_top_panel_styles,
)
from constants import CENTER_MAX_WIDTH
if TYPE_CHECKING:
from i18n import I18N
LOGGER = logging.getLogger(__name__)
TOP_CONTROL_HEIGHT = 34
class BlockNavigationCombo(SquarePopupComboBox):
"""Combo box that shows its popup upwards."""
def __init__(self, parent: Optional[QWidget] = None) -> None:
super().__init__(parent)
self.setSizeAdjustPolicy(QComboBox.SizeAdjustPolicy.AdjustToContents)
self.currentTextChanged.connect(self.update_width)
self.setMinimumContentsLength(1)
self.update_width()
def update_width(self) -> None:
metrics = QFontMetrics(self.font())
text = self.currentText() or self.placeholderText()
fallback_width = 160
base_width = max(fallback_width, self.sizeHint().width(), metrics.horizontalAdvance(text) + 32)
self.setMinimumWidth(base_width)
class TopPanel(QFrame):
"""Panel providing syllable settings, navigation, and reading controls."""
targetChanged = pyqtSignal(int)
toleranceChanged = pyqtSignal(int)
readingModeToggled = pyqtSignal(bool)
navigationRequested = pyqtSignal(int)
def __init__(self, parent: Optional[QWidget] = None) -> None:
super().__init__(parent)
self.setObjectName("TopPanel")
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self._block_updates: bool = False
root_layout = QVBoxLayout(self)
root_layout.setContentsMargins(0, 0, 0, 0)
root_layout.setSpacing(0)
self._top_separator = self._make_separator()
root_layout.addWidget(self._top_separator)
self._body_widget = QWidget(self)
self._body_widget.setMinimumWidth(CENTER_MAX_WIDTH)
self._body_widget.setMaximumWidth(CENTER_MAX_WIDTH)
self._body_layout = QHBoxLayout(self._body_widget)
self._body_layout.setContentsMargins(0, 12, 0, 12)
self._body_layout.setSpacing(24)
self._logo_label = self._init_logo_widget()
self._syllable_container = self._init_syllable_controls()
self._left_container = QWidget(self)
left_layout = QHBoxLayout(self._left_container)
left_layout.setContentsMargins(0, 0, 0, 0)
left_layout.setSpacing(24)
left_layout.addWidget(self._logo_label, 0, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
self._body_layout.addWidget(self._left_container)
self.informer_container = self._init_informer_controls()
self.nav_container = self._init_navigation_controls()
self._right_container = QWidget(self)
right_layout = QHBoxLayout(self._right_container)
right_layout.setContentsMargins(0, 0, 0, 0)
right_layout.setSpacing(18)
right_layout.addWidget(self.nav_container)
right_layout.addWidget(self.informer_container)
self._body_layout.addWidget(self._right_container)
# Обернём в контейнер для центрирования
self._center_container = QWidget(self)
center_layout = QHBoxLayout(self._center_container)
center_layout.setContentsMargins(0, 0, 0, 0)
center_layout.setSpacing(0)
center_layout.addStretch(1)
center_layout.addWidget(self._body_widget)
center_layout.addStretch(1)
root_layout.addWidget(self._center_container)
self._body_layout.setStretch(0, 1)
self._body_layout.setStretch(1, 1)
self._lower_separator = self._make_separator()
root_layout.addWidget(self._lower_separator)
self._apply_styles()
def _init_logo_widget(self) -> QLabel:
label = QLabel(self)
label.setObjectName("TopPanelLogo")
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
label.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
self._set_logo_pixmap(label)
return label
def _set_logo_pixmap(self, label: QLabel) -> None:
pixmap = self._load_logo_pixmap()
if pixmap is None:
label.clear()
label.setVisible(False)
label.setFixedSize(0, 0)
return
label.setPixmap(pixmap)
label.setVisible(True)
label.setFixedSize(pixmap.size())
@staticmethod
def _load_logo_pixmap() -> Optional[QPixmap]:
path = resolve_resource("logo.png")
if not path:
return None
pixmap = QPixmap(path)
if pixmap.isNull():
LOGGER.warning("Failed to load logo image: %s", path)
return None
target_height = 60
scaled = pixmap.scaledToHeight(target_height, Qt.TransformationMode.SmoothTransformation)
return scaled
def _init_syllable_controls(self) -> QWidget:
self._syll_label = QLabel(self)
self._syll_label.setObjectName("TopPanelLabel")
self.target_spin = QSpinBox(self)
self.target_spin.setRange(4, 40)
self.target_spin.setValue(16)
self.target_spin.setButtonSymbols(QAbstractSpinBox.ButtonSymbols.NoButtons)
self.target_spin.setFixedWidth(60)
self.target_spin.setFixedHeight(TOP_CONTROL_HEIGHT)
self.target_spin.valueChanged.connect(self._on_target_changed)
self._tol_label = QLabel("+_", self)
self._tol_label.setObjectName("TopPanelSubLabel")
self.tol_spin = QSpinBox(self)
self.tol_spin.setRange(0, 10)
self.tol_spin.setValue(0)
self.tol_spin.setButtonSymbols(QAbstractSpinBox.ButtonSymbols.NoButtons)
self.tol_spin.setFixedWidth(60)
self.tol_spin.setFixedHeight(TOP_CONTROL_HEIGHT)
self.tol_spin.valueChanged.connect(self._on_tol_changed)
syll_layout = QHBoxLayout()
syll_layout.setContentsMargins(0, 0, 0, 0)
syll_layout.setSpacing(12)
syll_layout.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
syll_layout.addWidget(self._syll_label)
syll_layout.addWidget(self.target_spin)
syll_layout.addWidget(self._tol_label)
syll_layout.addWidget(self.tol_spin)
syll_container = QWidget(self)
syll_container.setLayout(syll_layout)
return syll_container
def _init_informer_controls(self):
informer_layout = QHBoxLayout()
informer_layout.setContentsMargins(0, 0, 0, 0)
informer_layout.setSpacing(12)
informer_container = QWidget(self)
informer_container.setLayout(informer_layout)
return informer_container
def _init_navigation_controls(self) -> QWidget:
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(8)
self.nav_combo = BlockNavigationCombo(self)
self.nav_combo.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
layout.addWidget(self.nav_combo)
self.nav_combo.currentIndexChanged.connect(self._on_nav_combo_changed)
layout.addWidget(self._syllable_container)
self.reading_btn = QToolButton(self)
self.reading_btn.setObjectName("ReadingModeButton")
focus_icon = colored_icon(resolve_resource("focus.svg"), C_TOP_TEXT, QSize(TOP_CONTROL_HEIGHT, TOP_CONTROL_HEIGHT))
self.reading_btn.setIcon(focus_icon)
self.reading_btn.setIconSize(QSize(TOP_CONTROL_HEIGHT, TOP_CONTROL_HEIGHT))
self.reading_btn.setFixedSize(TOP_CONTROL_HEIGHT, TOP_CONTROL_HEIGHT)
self.reading_btn.setCheckable(True)
self.reading_btn.setAutoRaise(True)
self.reading_btn.toggled.connect(self._on_reading_toggled)
layout.addWidget(self.reading_btn)
container = QWidget(self)
container.setLayout(layout)
return container
def set_navigation_items(self, items: Iterable[str], current_index: int = 0) -> None:
self.nav_combo.blockSignals(True)
self.nav_combo.clear()
for text in items:
self.nav_combo.addItem(text)
if self.nav_combo.count() == 0:
self.nav_combo.setCurrentIndex(-1)
else:
index = max(0, min(current_index, self.nav_combo.count() - 1))
self.nav_combo.setCurrentIndex(index)
self.nav_combo.blockSignals(False)
self.nav_combo.update_width()
def set_navigation_index(self, index: int) -> None:
if index < 0 or index >= self.nav_combo.count():
return
self.nav_combo.blockSignals(True)
self.nav_combo.setCurrentIndex(index)
self.nav_combo.blockSignals(False)
self.nav_combo.update_width()
def _on_nav_combo_changed(self, index: int) -> None:
if index >= 0:
self.navigationRequested.emit(index)
@staticmethod
def _make_separator() -> QFrame:
separator = QFrame()
separator.setFixedHeight(1)
separator.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
separator.setStyleSheet(TOP_PANEL_SEPARATOR_STYLESHEET)
return separator
def _apply_styles(self) -> None:
self.setStyleSheet(build_top_panel_styles())
def retranslate(self, i18n: "I18N") -> None:
self._syll_label.setText(i18n.t("SYLLABLES"))
self.nav_combo.setPlaceholderText(i18n.t("NAVIGATION_PLACEHOLDER"))
self.nav_combo.setToolTip(i18n.t("NAVIGATION_TIP"))
self._syll_label.setToolTip(i18n.t("TARGET_SYLLABLES_TIP"))
self.target_spin.setToolTip(i18n.t("TARGET_SYLLABLES_TIP"))
self._tol_label.setToolTip(i18n.t("SYLLABLE_TOLERANCE_TIP"))
self.tol_spin.setToolTip(i18n.t("SYLLABLE_TOLERANCE_TIP"))
self.reading_btn.setToolTip(i18n.t("READING_MODE_TIP"))
# --- Стандартные методы ---
def set_state(self, *, target: int, tol: int, reading_mode: bool) -> None:
previous = self._block_updates
self._block_updates = True
try:
self.target_spin.setValue(target)
self.tol_spin.setValue(tol)
self.reading_btn.setChecked(reading_mode)
finally:
self._block_updates = previous
def _on_target_changed(self, value: int) -> None:
if not self._block_updates:
self.targetChanged.emit(int(value))
def _on_tol_changed(self, value: int) -> None:
if not self._block_updates:
self.toleranceChanged.emit(int(value))
def _on_reading_toggled(self, checked: bool) -> None:
if not self._block_updates:
self.readingModeToggled.emit(bool(checked))