roster/src/widgets/variable_data_row.py

107 lines
3.6 KiB
Python

# variable_data_row.py
#
# Copyright 2025 Pavel Baksy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import Gtk, GObject
@Gtk.Template(resource_path='/cz/vesp/roster/widgets/variable-data-row.ui')
class VariableDataRow(Gtk.Box):
"""Widget for a data row with variable name and values for each environment."""
__gtype_name__ = 'VariableDataRow'
variable_cell = Gtk.Template.Child()
name_entry = Gtk.Template.Child()
delete_button = Gtk.Template.Child()
values_box = Gtk.Template.Child()
__gsignals__ = {
'variable-changed': (GObject.SIGNAL_RUN_FIRST, None, ()),
'variable-delete-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
'value-changed': (GObject.SIGNAL_RUN_FIRST, None, (str, str)), # env_id, value
}
def __init__(self, variable_name, environments, size_group):
super().__init__()
self.variable_name = variable_name
self.environments = environments
self.size_group = size_group
self.value_entries = {}
# Set variable name
self.name_entry.set_text(variable_name)
# Add variable cell to size group for alignment
if size_group:
size_group.add_widget(self.variable_cell)
self._populate_values()
def _populate_values(self):
"""Populate value entries for each environment."""
# Clear existing
while child := self.values_box.get_first_child():
self.values_box.remove(child)
self.value_entries = {}
# Create entry for each environment
for env in self.environments:
# Create a box to hold the entry (for size group alignment)
entry_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
entry_box.set_size_request(200, -1)
entry = Gtk.Entry()
entry.set_placeholder_text(env.name)
entry.set_text(env.variables.get(self.variable_name, ""))
entry.set_hexpand(True)
entry.connect('changed', self._on_value_changed, env.id)
entry_box.append(entry)
# Add entry box to size group for alignment
if self.size_group:
self.size_group.add_widget(entry_box)
self.values_box.append(entry_box)
self.value_entries[env.id] = entry
def _on_value_changed(self, entry, env_id):
"""Handle value entry changes."""
self.emit('value-changed', env_id, entry.get_text())
@Gtk.Template.Callback()
def on_name_changed(self, entry):
"""Handle variable name changes."""
self.emit('variable-changed')
@Gtk.Template.Callback()
def on_delete_clicked(self, button):
"""Handle delete button click."""
self.emit('variable-delete-requested')
def get_variable_name(self):
"""Return current variable name."""
return self.name_entry.get_text().strip()
def refresh(self, environments):
"""Refresh with updated environments list."""
self.environments = environments
self._populate_values()