Getting a hang of list and tree stores.
This commit is contained in:
parent
1915198282
commit
52a8c6fe5c
@ -7,4 +7,5 @@ authors = ["Jordan Petridis <jordanpetridis@protonmail.com>"]
|
||||
gtk = { version = "0.2", features = ["v3_22"]}
|
||||
glib = "0.3"
|
||||
gdk-pixbuf = "0.2"
|
||||
hammond-data = {path = "../hammond-data"}
|
||||
hammond-data = {path = "../hammond-data"}
|
||||
diesel = { version = "0.16", features = ["sqlite"] }
|
||||
@ -21,20 +21,6 @@
|
||||
<property name="position">3</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="refbutton">
|
||||
<property name="label">gtk-refresh</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="use_underline">True</property>
|
||||
<property name="use_stock">True</property>
|
||||
<property name="always_show_image">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="pack_type">end</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="addbutton">
|
||||
<property name="label">gtk-add</property>
|
||||
@ -65,6 +51,21 @@
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="refbutton">
|
||||
<property name="label">gtk-refresh</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<property name="use_underline">True</property>
|
||||
<property name="use_stock">True</property>
|
||||
<property name="always_show_image">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="pack_type">end</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<object class="GtkWindow" id="window1">
|
||||
<property name="can_focus">False</property>
|
||||
@ -131,4 +132,47 @@
|
||||
<placeholder/>
|
||||
</child>
|
||||
</object>
|
||||
<object class="GtkWindow" id="window2">
|
||||
<property name="can_focus">False</property>
|
||||
<property name="default_width">800</property>
|
||||
<property name="default_height">400</property>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<child>
|
||||
<object class="GtkScrolledWindow">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="shadow_type">in</property>
|
||||
<child>
|
||||
<object class="GtkViewport">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<child>
|
||||
<object class="GtkBox" id="box2">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<child>
|
||||
<placeholder/>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">True</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<child type="titlebar">
|
||||
<placeholder/>
|
||||
</child>
|
||||
</object>
|
||||
</interface>
|
||||
|
||||
@ -1,12 +1,15 @@
|
||||
// extern crate glib;
|
||||
extern crate diesel;
|
||||
extern crate gtk;
|
||||
// extern crate gdk_pixbuf;
|
||||
extern crate hammond_data;
|
||||
|
||||
use diesel::prelude::*;
|
||||
use gtk::prelude::*;
|
||||
use gtk::Orientation;
|
||||
use gtk::IconSize;
|
||||
// use gtk::{CellRendererText, TreeStore, TreeView, TreeViewColumn};
|
||||
use gtk::Type;
|
||||
use gtk::{CellRendererText, TreeStore, TreeView, TreeViewColumn};
|
||||
|
||||
use hammond_data::dbqueries;
|
||||
|
||||
@ -23,27 +26,77 @@ fn create_child(name: &str) -> gtk::Box {
|
||||
box_
|
||||
}
|
||||
|
||||
fn create_list_store(connection: &SqliteConnection) -> gtk::ListStore {
|
||||
let podcast_model = gtk::ListStore::new(&[Type::String, Type::String, Type::String]);
|
||||
|
||||
let podcasts = dbqueries::get_podcasts(connection).unwrap();
|
||||
|
||||
for pd in &podcasts {
|
||||
podcast_model.insert_with_values(
|
||||
None,
|
||||
&[0, 1, 2],
|
||||
&[&pd.title(), &pd.description(), &pd.link()],
|
||||
);
|
||||
}
|
||||
|
||||
podcast_model
|
||||
}
|
||||
|
||||
fn create_and_setup_view() -> TreeView {
|
||||
// Creating the tree view.
|
||||
let tree = TreeView::new();
|
||||
|
||||
tree.set_headers_visible(false);
|
||||
|
||||
// Creating the two columns inside the view.
|
||||
let column = TreeViewColumn::new();
|
||||
let cell = CellRendererText::new();
|
||||
|
||||
column.pack_start(&cell, true);
|
||||
// Association of the view's column with the model's `id` column.
|
||||
column.add_attribute(&cell, "text", 0);
|
||||
tree.append_column(&column);
|
||||
|
||||
let column = TreeViewColumn::new();
|
||||
let cell = CellRendererText::new();
|
||||
|
||||
column.pack_start(&cell, true);
|
||||
column.add_attribute(&cell, "text", 1);
|
||||
tree.append_column(&column);
|
||||
|
||||
let column = TreeViewColumn::new();
|
||||
let cell = CellRendererText::new();
|
||||
|
||||
column.pack_start(&cell, true);
|
||||
column.add_attribute(&cell, "text", 2);
|
||||
tree.append_column(&column);
|
||||
|
||||
tree
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if gtk::init().is_err() {
|
||||
println!("Failed to initialize GTK.");
|
||||
return;
|
||||
}
|
||||
hammond_data::init().unwrap();
|
||||
|
||||
// Adapted copy of the way gnome-music does albumview
|
||||
let glade_src = include_str!("../gtk/foo.ui");
|
||||
let builder = gtk::Builder::new_from_string(glade_src);
|
||||
|
||||
// Get the main window
|
||||
let window: gtk::Window = builder.get_object("window1").unwrap();
|
||||
// let window: gtk::Window = builder.get_object("window1").unwrap();
|
||||
let window: gtk::Window = builder.get_object("window2").unwrap();
|
||||
// Get the headerbar
|
||||
let header: gtk::HeaderBar = builder.get_object("headerbar1").unwrap();
|
||||
window.set_titlebar(&header);
|
||||
|
||||
let refresh_button: gtk::Button = builder.get_object("refbutton").unwrap();
|
||||
// TODO: Have a small dropdown menu
|
||||
let add_button: gtk::Button = builder.get_object("addbutton").unwrap();
|
||||
let search_button: gtk::Button = builder.get_object("searchbutton").unwrap();
|
||||
let home_button: gtk::Button = builder.get_object("homebutton").unwrap();
|
||||
let _add_button: gtk::Button = builder.get_object("addbutton").unwrap();
|
||||
let _search_button: gtk::Button = builder.get_object("searchbutton").unwrap();
|
||||
let _home_button: gtk::Button = builder.get_object("homebutton").unwrap();
|
||||
|
||||
// FIXME: This locks the ui atm.
|
||||
refresh_button.connect_clicked(|_| {
|
||||
@ -57,16 +110,21 @@ fn main() {
|
||||
Inhibit(false)
|
||||
});
|
||||
|
||||
let flowbox: gtk::FlowBox = builder.get_object("flowbox1").unwrap();
|
||||
|
||||
// TODO: This should be in a TreeStore.
|
||||
// let flowbox: gtk::FlowBox = builder.get_object("flowbox1").unwrap();
|
||||
let db = hammond_data::establish_connection();
|
||||
let podcasts = dbqueries::get_podcasts(&db).unwrap();
|
||||
let pd_model = create_list_store(&db);
|
||||
// let podcasts = dbqueries::get_podcasts(&db).unwrap();
|
||||
|
||||
for pd in &podcasts {
|
||||
let f = create_child(pd.title());
|
||||
flowbox.add(&f);
|
||||
}
|
||||
// for pd in &podcasts {
|
||||
// // TODO: This should be in a TreeStore.
|
||||
// let f = create_child(pd.title());
|
||||
// flowbox.add(&f);
|
||||
// }
|
||||
let box2: gtk::Box = builder.get_object("box2").unwrap();
|
||||
|
||||
let treeview = create_and_setup_view();
|
||||
treeview.set_model(Some(&pd_model));
|
||||
box2.add(&treeview);
|
||||
|
||||
window.show_all();
|
||||
gtk::main();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user