fix: template selection - add placeholder item and improve selection

- Add '-- Select a template --' as first item in combo box
- Block signals while refreshing to prevent unwanted callbacks
- Select newly created template automatically
- Fix index check to skip placeholder (index > 0 instead of >= 0)
- Add enable/disable based on whether templates exist
This commit is contained in:
LemonNexus 2026-02-11 12:57:23 +00:00
parent f403f3edfe
commit 91a8da3910
1 changed files with 30 additions and 6 deletions

View File

@ -1100,19 +1100,32 @@ class MainWindow(QMainWindow):
def refresh_session_templates(self):
"""Refresh the session template list."""
# Block signals while updating to prevent triggering on_template_changed
self.template_combo.blockSignals(True)
self.template_combo.clear()
# Load templates from database
templates = self._load_templates_from_db()
# Add "Select a template..." as first item
self.template_combo.addItem("-- Select a template --", None)
for template in templates:
self.template_combo.addItem(
f"{template.activity_type.display_name} - {template.name}",
template
)
display_text = f"{template.activity_type.display_name} - {template.name}"
self.template_combo.addItem(display_text, template)
# Restore signals
self.template_combo.blockSignals(False)
self.log_debug("Templates", f"Loaded {len(templates)} session templates")
# If we have templates, enable the combo box
if len(templates) > 0:
self.template_combo.setEnabled(True)
else:
self.template_combo.setEnabled(False)
def _load_templates_from_db(self) -> List[SessionTemplate]:
"""Load session templates from database."""
templates = []
@ -1176,7 +1189,18 @@ class MainWindow(QMainWindow):
)
self.db.commit()
# Get the new template ID
new_id = result.lastrowid
self.refresh_session_templates()
# Select the newly created template
for i in range(self.template_combo.count()):
template = self.template_combo.itemData(i)
if template and template.id == new_id:
self.template_combo.setCurrentIndex(i)
break
self.log_info("Templates", f"Created template: {name} ({activity_type.display_name})")
self.status_bar.showMessage(f"Template '{name}' created", 3000)
except Exception as e:
@ -1184,12 +1208,12 @@ class MainWindow(QMainWindow):
def on_template_changed(self, index: int):
"""Handle template selection change."""
if index >= 0:
if index > 0: # Skip first item which is "-- Select a template --"
self.current_template = self.template_combo.currentData()
if self.current_template:
self.current_template_label.setText(self.current_template.name)
self.current_template_label.setStyleSheet(f"font-weight: bold; color: {self.current_template.activity_type.color};")
self.log_debug("Templates", f"Selected template: {self.current_template.name}")
self.log_info("Templates", f"Selected template: {self.current_template.name}")
self._update_start_button()
else:
self.current_template = None