Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ListBox-Sorted-StringList-model #1879

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ name = "gtk_builder"
path = "gtk_builder/main.rs"
required-features = ["v4_10"]

[[bin]]
name = "listbox_sort_stringlist"
path = "listbox_sort_stringlist/main.rs"
required-features = ["v4_14"]

[[bin]]
name = "list_box_model"
path = "list_box_model/main.rs"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ cargo run --bin basics
- [Glium GL-Area](./glium_gl_area/)
- [Grid Packing](./grid_packing)
- [GtkBuilder example](./gtk_builder/)
- [ListModel: StringList with Sorter](./listbox_sort_stringlist/)
- [ListView: Applications Launcher](./list_view_apps_launcher/)
- [Rotation Bin](./rotation_bin/)
- [Scale](./scale/)
Expand Down
15 changes: 15 additions & 0 deletions examples/listbox_sort_stringlist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ListBox and StringListModel with Sorter example

This example demonstrates how to use `gtk::ListBox` in combination with
a StringList model and StringSorter with a PropertyExpression.

It sets up a `gtk::ListBox` containing an Inscription on each row.
The rows are sorted alphabetically.

In addition, it is possible to delete rows.

Run it by executing:

```bash
cargo run --bin list_box_sort_stringlist
``
147 changes: 147 additions & 0 deletions examples/listbox_sort_stringlist/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Variation on ListBox model example to show use of StringList and StringSorter.

// TODO: add a dialog window to allow adding rows.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I mentioned below, just drop it

// use plain gtk::Window for this per current practice.

use gtk::{
glib::{self, clone},
prelude::*,
};

fn main() -> glib::ExitCode {
let application = gtk::Application::builder()
.application_id("com.github.gtk-rs.examples.listbox-sorted-StringList")
jimbrankelly marked this conversation as resolved.
Show resolved Hide resolved
.build();

application.connect_activate(build_ui);

application.run()
}

fn build_ui(application: &gtk::Application) {
let window = gtk::ApplicationWindow::builder()
.default_width(320)
.default_height(480)
.application(application)
.title("Sorted StringList")
.build();

let vbox = gtk::Box::new(gtk::Orientation::Vertical, 5);

// Create a StringSorter with a property expression to sort
// StringObjects in a StringList. StringObject has a "string" property.
let expression = gtk::PropertyExpression::new(
gtk::StringObject::static_type(),
None::<gtk::Expression>,
"string",
);
let sorter = gtk::StringSorter::new(Some(expression));
sorter.set_ignore_case(true);

// Create our list store as a StringList and populate with some strings.
let model = gtk::StringList::new(&["zoo", "abba", "donkey", "sunrise", "river", "phoenix"]);

// Create a sort model and bind it to the ListStore and the sorter.
let sort_model = gtk::SortListModel::new(Some(model.clone()), Some(sorter.clone()));
jimbrankelly marked this conversation as resolved.
Show resolved Hide resolved

// And then create the UI part, the listbox and bind the sort
// model to it. Whenever the UI needs to show a new row, e.g. because
// it was notified that the model changed, it will call the callback
// with the corresponding item from the model and will ask for a new
// gtk::ListBoxRow that should be displayed.
//
// The gtk::ListBoxRow can contain any possible widgets.
// Here we use an Inscription.

let listbox = gtk::ListBox::new();
listbox.bind_model(
Some(&sort_model),
clone!(
#[weak(rename_to = _panel)]
window,
#[upgrade_or_panic]
move |obj| {
let list_object = obj
.downcast_ref::<gtk::StringObject>()
.expect("The object should be of type `StringObject`.");
let row = gtk::Inscription::new(Some(&list_object.string()));
row.set_xalign(0.0);
row.upcast()
}
),
);

let scrolled_window = gtk::ScrolledWindow::builder()
.hscrollbar_policy(gtk::PolicyType::Never) // Disable horizontal scrolling
.min_content_height(480)
.min_content_width(360)
.build();

scrolled_window.set_child(Some(&listbox));

let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 5);

// The add button opens a new dialog which is basically the same as the edit
// dialog, except that we don't have a corresponding item yet at that point
// and only create it once the Ok button in the dialog is clicked, and only
// then add it to the model. Once added to the model, it will immediately
// appear in the listbox UI
let add_button = gtk::Button::with_label("Add");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does nothing, i would also remove it and keep things pretty simple

add_button.set_sensitive(false);

hbox.append(&add_button);

// Via the delete button we delete the item from the model that
// is at the index of the selected row. Also deleting from the
// model is immediately reflected in the listbox.
let delete_button = gtk::Button::with_label("Delete");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this adds more complexity and it is not really the best way to achieve that. So I would just keep it for implementing a sort with stringlist

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestions. I've removed the "add" and "delete" buttons to simplify the example.

delete_button.connect_clicked(clone!(
#[weak]
model,
#[weak]
listbox,
move |_| {
let selected = listbox.selected_row();
//let sorted_model = listbox.model();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused, remove it


if let Some(selected) = selected {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if let Some(selected) = selected {
if let Some(row) = listbox.selected_row() {

// Find the selected text.
let selected = selected.child().expect("Listrow child should be a widget");
let selected = selected
.downcast_ref::<gtk::Inscription>()
.expect("The object should be of type `Inscription`.");
if let Some(selected) = selected.text() {
let mut selected_index = None;
// Find the position in the StringList model of the selected string
for ind in 0..model.n_items() {
if let Some(item) = model.item(ind) {
let item = item
.downcast_ref::<gtk::StringObject>()
.expect("Object should be a stringobject");
if item.string() == selected {
selected_index = Some(ind);
break;
}
}
}
// If the selected string is found in the stringlist model, delete it
if let Some(index) = selected_index {
model.remove(index);
}
}
}
}
));
hbox.append(&delete_button);

vbox.append(&hbox);
vbox.append(&scrolled_window);

window.set_child(Some(&vbox));

for i in 0..10 {
model.append(&format!("Name {i}"));
}

window.present();
}
Loading