Skip to content

Commit

Permalink
translated list view to python
Browse files Browse the repository at this point in the history
  • Loading branch information
Nokse22 committed Oct 7, 2024
1 parent 13af95a commit b780e52
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/List View/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import gi

gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Gtk
import workbench

list_view = workbench.builder.get_object("list_view")
add = workbench.builder.get_object("add")
remove = workbench.builder.get_object("remove")

string_model = Gtk.StringList.new(
["Default Item 1", "Default Item 2", "Default Item 3"]
)

item = 4

model = Gtk.SingleSelection(model=string_model)


def on_item_changed(_list, position, removed, added):
print(f"position: {position}, Item removed? {removed != 0}, Item added? {added != 0}")


def on_selection_changed(*_):
selected_item = model.get_selected()
print(f"Model item selected from view: {string_model.get_string(selected_item)}")


string_model.connect("items-changed", on_item_changed)
model.connect("selection-changed", on_selection_changed)

list_view.set_model(model)


def on_add_clicked(*_):
global item
new_item = f"New item {item}"
string_model.append(new_item)
item += 1


def on_remove_clicked(*_):
selected_item = model.get_selected()
string_model.remove(selected_item)


add.connect("clicked", on_add_clicked)
remove.connect("clicked", on_remove_clicked)

0 comments on commit b780e52

Please sign in to comment.