roster/src/widgets/environment_row.py

87 lines
3.0 KiB
Python

# environment_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/environment-row.ui')
class EnvironmentRow(Gtk.Box):
"""Widget for displaying and editing an environment with its variables."""
__gtype_name__ = 'EnvironmentRow'
name_label = Gtk.Template.Child()
values_box = Gtk.Template.Child()
edit_button = Gtk.Template.Child()
delete_button = Gtk.Template.Child()
__gsignals__ = {
'edit-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
'delete-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
'value-changed': (GObject.SIGNAL_RUN_FIRST, None, (str, str)) # variable_name, value
}
def __init__(self, environment, variable_names):
super().__init__()
self.environment = environment
self.variable_names = variable_names
self.value_entries = {}
self._populate()
def _populate(self):
"""Populate with environment data."""
self.name_label.set_text(self.environment.name)
# Clear values box
while child := self.values_box.get_first_child():
self.values_box.remove(child)
# Create entry for each variable
for var_name in self.variable_names:
entry = Gtk.Entry()
entry.set_placeholder_text(var_name)
entry.set_text(self.environment.variables.get(var_name, ""))
entry.set_hexpand(True)
entry.connect('changed', self._on_value_changed, var_name)
self.values_box.append(entry)
self.value_entries[var_name] = entry
def _on_value_changed(self, entry, var_name):
"""Handle value entry changes."""
self.emit('value-changed', var_name, entry.get_text())
@Gtk.Template.Callback()
def on_edit_clicked(self, button):
"""Handle edit button click."""
self.emit('edit-requested')
@Gtk.Template.Callback()
def on_delete_clicked(self, button):
"""Handle delete button click."""
self.emit('delete-requested')
def refresh(self, variable_names):
"""Refresh widget with updated variable names."""
self.variable_names = variable_names
self._populate()
def get_values(self):
"""Get all variable values."""
return {var_name: entry.get_text() for var_name, entry in self.value_entries.items()}