scrolling to first enabled voice in the formula gui

This commit is contained in:
Juraj Borza
2025-04-29 08:27:48 +02:00
parent 06ad35fd74
commit 3497c01053
+18 -6
View File
@@ -11,7 +11,10 @@ from PyQt5.QtWidgets import (
QPushButton, QPushButton,
QSizePolicy QSizePolicy
) )
from PyQt5.QtCore import Qt from PyQt5.QtCore import (
Qt,
QTimer
)
from constants import ( from constants import (
VOICES_INTERNAL, VOICES_INTERNAL,
FLAGS FLAGS
@@ -152,22 +155,31 @@ class VoiceFormulaDialog(QDialog):
self.add_voices(initial_state) self.add_voices(initial_state)
def add_voices(self, initial_state): def add_voices(self, initial_state):
"""Add voice mixers to the dialog based on the initial state and scroll to first enabled one."""
first_enabled_voice = None
for voice in VOICES_INTERNAL: for voice in VOICES_INTERNAL:
flag = FLAGS.get(voice[0], "") flag = FLAGS.get(voice[0], "")
matching_voice = next((item for item in initial_state if item[0] == voice), None) matching_voice = next((item for item in initial_state if item[0] == voice), None)
initial_status = matching_voice is not None initial_status = matching_voice is not None
initial_weight = matching_voice[1] if matching_voice else 1.0 initial_weight = matching_voice[1] if matching_voice else 1.0
self.add_voice(voice, flag, initial_status, initial_weight) voice_mixer = self.add_voice(voice, flag, initial_status, initial_weight)
# remember the first enabled voice
if initial_status and first_enabled_voice is None:
first_enabled_voice = voice_mixer
if first_enabled_voice:
self.scroll_to_voice(first_enabled_voice)
def add_voice(self, voice_name, language_icon, initial_status=False, initial_weight=1.0): def add_voice(self, voice_name, language_icon, initial_status=False, initial_weight=1.0):
voice_mixer = VoiceMixer(voice_name, language_icon, initial_status, initial_weight) voice_mixer = VoiceMixer(voice_name, language_icon, initial_status, initial_weight)
self.voice_mixers.append(voice_mixer) self.voice_mixers.append(voice_mixer)
self.voice_list_layout.addWidget(voice_mixer) self.voice_list_layout.addWidget(voice_mixer)
return voice_mixer
def update_scroll_area_width(self): def scroll_to_voice(self, voice_mixer):
# Calculate the width of the scrollable area based on the number of VoiceMixers """Scroll the QScrollArea to ensure the given VoiceMixer is visible."""
width = len(self.voice_mixers) * VOICE_MIXER_WIDTH QTimer.singleShot(0, lambda: self.scroll_area.ensureWidgetVisible(voice_mixer))
self.voice_list_widget.setFixedWidth(width)
def get_selected_voices(self): def get_selected_voices(self):
"""Return the list of selected voices and their weights.""" """Return the list of selected voices and their weights."""