-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_navigation.py
More file actions
372 lines (335 loc) · 13.8 KB
/
Copy pathblock_navigation.py
File metadata and controls
372 lines (335 loc) · 13.8 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""Block navigation controller for coordinating block list interactions."""
from __future__ import annotations
from typing import Callable, Iterable, List, Optional
from PyQt6.QtWidgets import QListWidget, QListWidgetItem, QMessageBox, QScrollArea, QWidget
from block import BlockWidget
from i18n import I18N
from model import INSTRUMENTAL_SECTIONS, Project, create_instrumental_block, create_lyrics_block
class BlockNavigator:
"""Manage navigation actions for project blocks."""
def __init__(
self,
project_provider: Callable[[], Project],
block_widgets_provider: Callable[[], Iterable[BlockWidget]],
nav_list: Optional[QListWidget],
scroll_area: QScrollArea,
parent_widget: QWidget,
i18n: I18N,
set_active_block: Callable[[Optional[str]], None],
get_active_block: Callable[[], Optional[str]],
rebuild_all: Callable[[], None],
mark_modified: Callable[[bool], None],
building_ui_flag: Callable[[], bool],
navigation_updater: Optional[Callable[[List[str], int], None]] = None,
navigation_index_setter: Optional[Callable[[int], None]] = None,
) -> None:
self._project = project_provider
self._block_widgets = block_widgets_provider
self._nav = nav_list
self._scroll = scroll_area
self._parent = parent_widget
self._i18n = i18n
self._set_active_block = set_active_block
self._get_active_block = get_active_block
self._rebuild_all = rebuild_all
self._mark_modified = mark_modified
self._is_building = building_ui_flag
self._update_navigation_items = navigation_updater
self._set_navigation_index = navigation_index_setter
self._nav_items: List[str] = []
self._current_index: int = 0
def project(self) -> Project:
return self._project()
def block_widgets(self) -> Iterable[BlockWidget]:
return self._block_widgets()
def index_of_block_id(self, block_id: str) -> Optional[int]:
for index, block in enumerate(self.project().blocks):
if block.id == block_id:
return index
return None
def widget_by_id(self, block_id: str) -> Optional[BlockWidget]:
for widget in self.block_widgets():
if widget.block.id == block_id:
return widget
return None
def rebuild_nav(self) -> None:
project = self.project()
captions: List[str] = []
if self._nav is not None:
self._nav.blockSignals(True)
self._nav.clear()
for idx, block in enumerate(project.blocks):
number = idx + 1
if block.kind == "lyrics":
caption = self._format_lyrics_caption(block, number)
else:
caption = self._format_instrumental_caption(block, number)
captions.append(caption)
if self._nav is not None:
self._nav.addItem(QListWidgetItem(caption))
if self._nav is not None:
self._nav.blockSignals(False)
self._nav_items = captions
active_id = self._get_active_block()
active_index = self.index_of_block_id(active_id) if active_id else None
if active_index is None and captions:
active_index = 0
if captions:
self._current_index = max(0, min(active_index if active_index is not None else 0, len(captions) - 1))
else:
self._current_index = 0
if self._update_navigation_items is not None:
self._update_navigation_items(self._nav_items, self._current_index)
self._refresh_navigation_controls()
def _format_lyrics_caption(self, block: "Block", number: int) -> str:
block_prefix = self._i18n.t("NAV_BLOCK_PREFIX")
section_key = block.section or "None"
if section_key == "Verse":
verse_index = self._verse_index_for_block(block)
section_display = f"Verse {verse_index}"
else:
section_display = self._section_display_name(section_key)
if not section_display:
section_display = self._i18n.t("SECTION_NONE")
return f"{block_prefix}{number} | {section_display}"
def _format_instrumental_caption(self, block: "Block", number: int) -> str:
block_prefix = self._i18n.t("NAV_BLOCK_PREFIX")
section_display = self._instrumental_display_name(block.instrumental_type)
if not section_display:
section_display = self._i18n.t("NAV_PREFIX_INSTRUMENTAL")
return f"{block_prefix}{number} | {section_display}"
def _verse_index_for_block(self, block: "Block") -> int:
verse_counter = 0
for candidate in self.project().blocks:
if candidate.kind != "instrumental" and candidate.section == "Verse":
verse_counter += 1
if candidate.id == block.id:
return verse_counter
return max(1, verse_counter)
def _section_display_name(self, key: str) -> str:
if key == "None":
return "None"
return key
def _instrumental_display_name(self, key: Optional[str]) -> str:
if key is None:
return ""
return key
def on_block_active(self, block_id: str) -> None:
self._set_active_block(block_id)
index = self.index_of_block_id(block_id)
if index is None:
return
self._current_index = index
if self._nav is not None:
self._nav.blockSignals(True)
self._nav.setCurrentRow(index)
self._nav.blockSignals(False)
self._refresh_navigation_controls()
def on_blocks_rebuilt(self) -> None:
self.rebuild_nav()
active = self._get_active_block()
if active is None:
return
index = self.index_of_block_id(active)
if index is None:
return
self._current_index = index
if self._nav is not None:
self._nav.blockSignals(True)
self._nav.setCurrentRow(index)
self._nav.blockSignals(False)
self._refresh_navigation_controls()
def insert_block_after_active(self) -> None:
active = self._get_active_block()
if not active:
return
self.insert_block_after(active)
def insert_instrumental_after_active(self) -> None:
active = self._get_active_block()
if not active:
return
self.insert_instrumental_block_after(active)
def insert_block_after(self, block_id: str, line_count: int = 4) -> None:
index = self.index_of_block_id(block_id)
if index is None:
return
new_block = create_lyrics_block(line_count=line_count)
project = self.project()
project.blocks.insert(index + 1, new_block)
self._set_active_block(new_block.id)
self._rebuild_all()
widget = self.widget_by_id(new_block.id)
if widget:
widget.focus_line(0)
self._mark_modified(True)
def insert_instrumental_block_after(self, block_id: str) -> None:
index = self.index_of_block_id(block_id)
if index is None:
return
project = self.project()
base = project.blocks[index]
default_type = base.instrumental_type if base.kind == "instrumental" else INSTRUMENTAL_SECTIONS[0]
new_block = create_instrumental_block(default_type)
project.blocks.insert(index + 1, new_block)
self._set_active_block(new_block.id)
self._rebuild_all()
widget = self.widget_by_id(new_block.id)
if widget:
widget.focus_line(0)
self._mark_modified(True)
def delete_active_block(self) -> None:
active = self._get_active_block()
if not active:
return
self.delete_block(active)
def delete_block(self, block_id: str) -> None:
project = self.project()
index = self.index_of_block_id(block_id)
if index is None:
return
if len(project.blocks) == 1:
QMessageBox.information(
self._parent,
self._i18n.t("CANT_DELETE"),
self._i18n.t("AT_LEAST_ONE_BLOCK"),
)
return
response = QMessageBox.question(
self._parent,
self._i18n.t("DELETE"),
self._i18n.t("DELETE_BLOCK_Q"),
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
)
if response != QMessageBox.StandardButton.Yes:
return
project.blocks.pop(index)
new_index = min(index, len(project.blocks) - 1)
self._set_active_block(project.blocks[new_index].id if project.blocks else None)
self._rebuild_all()
self._mark_modified(True)
def move_active_block(self, delta: int) -> None:
active = self._get_active_block()
if not active:
return
self.move_block(active, delta)
def move_block(self, block_id: str, delta: int) -> None:
project = self.project()
index = self.index_of_block_id(block_id)
if index is None:
return
new_index = index + delta
if new_index < 0 or new_index >= len(project.blocks):
return
project.blocks[index], project.blocks[new_index] = project.blocks[new_index], project.blocks[index]
self._set_active_block(project.blocks[new_index].id)
self._rebuild_all()
self._mark_modified(True)
self._refresh_navigation_controls()
def on_section_changed(self) -> None:
# Update all block widgets' headers (for Verse numbering)
for idx, widget in enumerate(self.block_widgets()):
widget.set_index(idx)
widget.update_reading_view()
self.rebuild_nav()
self._mark_modified(True)
def handle_block_content_changed(self) -> None:
if not self._is_building():
self._mark_modified(True)
def on_navigation_requested(self, index: int) -> None:
if index < 0:
return
project = self.project()
if index >= len(project.blocks):
return
self._activate_index(index)
def navigation_items(self) -> List[str]:
return list(self._nav_items)
def current_navigation_index(self) -> int:
return self._current_index
def _activate_index(self, index: int) -> None:
if self._is_building():
return
project = self.project()
if index < 0 or index >= len(project.blocks):
return
block = project.blocks[index]
self._set_active_block(block.id)
self._current_index = index
widget = self.widget_by_id(block.id)
if widget:
self._scroll.ensureWidgetVisible(widget, xMargin=0, yMargin=24)
if self._nav is not None:
self._nav.blockSignals(True)
self._nav.setCurrentRow(index)
self._nav.blockSignals(False)
self._refresh_navigation_controls()
def _refresh_navigation_controls(self) -> None:
if self._set_navigation_index is not None:
self._set_navigation_index(self._current_index)
def enter_from_block_line(self, block_id: str, line_idx: int) -> None:
index = self.index_of_block_id(block_id)
if index is None:
return
widget = self.widget_by_id(block_id)
if widget is None:
return
if line_idx < len(widget.line_edits) - 1:
widget.focus_line(line_idx + 1)
return
project = self.project()
if index + 1 < len(project.blocks):
next_block = project.blocks[index + 1]
self._set_active_block(next_block.id)
next_widget = self.widget_by_id(next_block.id)
if next_widget:
self._scroll.ensureWidgetVisible(next_widget, 0, 24)
next_widget.focus_line(0)
self.on_block_active(next_block.id)
return
current_widget = widget
if current_widget.block.kind == "instrumental":
new_block = create_instrumental_block(current_widget.block.instrumental_type)
else:
new_block = create_lyrics_block()
project.blocks.append(new_block)
self._set_active_block(new_block.id)
self._rebuild_all()
new_widget = self.widget_by_id(new_block.id)
if new_widget:
new_widget.focus_line(0)
self._mark_modified(True)
def move_up_from_block_line(self, block_id: str, line_idx: int) -> None:
index = self.index_of_block_id(block_id)
if index is None:
return
widget = self.widget_by_id(block_id)
if widget is None:
return
if line_idx > 0:
widget.focus_line(line_idx - 1)
return
if index > 0:
prev_block = self.project().blocks[index - 1]
prev_widget = self.widget_by_id(prev_block.id)
if prev_widget:
self._scroll.ensureWidgetVisible(prev_widget, 0, 24)
prev_widget.focus_line(len(prev_widget.line_edits) - 1)
self.on_block_active(prev_block.id)
def move_down_from_block_line(self, block_id: str, line_idx: int) -> None:
index = self.index_of_block_id(block_id)
if index is None:
return
widget = self.widget_by_id(block_id)
if widget is None:
return
if line_idx < len(widget.line_edits) - 1:
widget.focus_line(line_idx + 1)
return
project = self.project()
if index + 1 < len(project.blocks):
next_block = project.blocks[index + 1]
next_widget = self.widget_by_id(next_block.id)
if next_widget:
self._scroll.ensureWidgetVisible(next_widget, 0, 24)
next_widget.focus_line(0)
self.on_block_active(next_block.id)