Search code examples
gtkvala

Gtk+ and Vala: Inheriting gtk.TreeModel


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?


Solution

  • 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).