Reputation: 4314
I have a hash with values that build into following structure:
string type1_name -> Hash(
string name_member -> DataStruct,
string name_member -> DataStruct,
string name_member -> DataStruct,
string name_member -> DataStruct
),
string type2_name -> Hash(
string name_member -> DataStruct,
string name_member -> DataStruct,
string name_member -> DataStruct,
string name_member -> DataStruct
),
/// etc
the problem is I have 3 views: 2 TreeViews (extended actual tree showing all columns in hierarchial way and brief as ListStore showing only type icon and name of DataStruct, those views are owned by different parents and maybe shown at the same time) and 1 ListView with icon representation of same data. ATM I have a class managing data (validations, serialization, deserialization, ...) and 3 models for each of those views, so every time I update/delete/create item I must update all 3 models and that doesn't look good :(
What I thought about is creating a class implementing Gtk.TreeModel and providing common data source that can be used as model for all 3 views, but I can't find any documentation on how to implement gtk.TreeModel. I've tried to look through GtkListStore (native C implementaion) and I see it reimplements really lots of methods. Isn't there any easier way?
Upvotes: 2
Views: 840
Reputation: 41
Simple inherit from Gtk.TreeStore / Gtk.ListStore and set the column types explicit. The tree view column is set to the cell renderer and the data callback function you like to show.
Vala sample to map MyCommon.data1 to the treeview1.column = 0
public class MyCommon : GLib.Object {
string data1 {get; set; }
string data2 {get; set; }
}
public class Store : Gtk.TreeStore {
public Store() {
set_column_types( new GLib.Type[] { typeof(MyCommon) } );
}
public set_treeview1(Gtk.TreeView treeview) {
treeview.insert_column_with_data_func(0, "data1", new Gtk.CellRendererText(), tree_cell_data1);
treeview.insert_column_with_data_func(1, "data2", new Gtk.CellRendererText(), tree_cell_data2);
treeview.model = this;
}
protected MyCommon? my_common(Gtk.TreeModel model, Gtk.TreeIter iter) {
GLib.Value data;
model.get_value(iter, 0, out data);
return (MyCommon)data;
}
public void tree_cell_data1(Gtk.TreeViewColumn column, Gtk.CellRenderer cell,
Gtk.TreeModel model, Gtk.TreeIter iter) {
MyCommon? property = my_common(this,iter);
if(property != null) (cell as Gtk.CellRendererText).text = property.data1;
}
public void tree_cell_data2(Gtk.TreeViewColumn column, Gtk.CellRenderer cell,
Gtk.TreeModel model, Gtk.TreeIter iter) {
MyCommon? property = my_common(this,iter);
if(property != null) (cell as Gtk.CellRendererText).text = property.data2;
}
.....
Upvotes: 4
Reputation: 17482
No, there isn't an easier way, but it's really not too difficult. Looking at the C code can be intimidating, but there are really only about a dozen methods which you need to implement. They are marked as abstract
in gtk+-3.0.vapi (and in valadoc.org's Gtk.TreeModel docs)., and the implementations are usually pretty trivial. If you want an example SQLHeavyGtk.Model is the only one I can think of.
The methods which are marked as virtual
don't generally require an implementation, though you can provide one if you want (often used for optimizations, and I'm guessing Gtk.ListStore provides a lot of them).
Upvotes: 6