Re: [gtkmm] TreeView Design - insert_column_with_data_func()



On Thu, 2002-07-18 at 11:10, Murray Cumming wrote:

> If I really had to, then I'd do one of these:
> 1. Keep them in sync, like you say.

Not very useful :(

> 2. Derive a new TreeModel. Very difficult. No examples exists.

I'd be nice to have a model written in C++ using STL containers. GTK+
models are very ineffective, they use very slow algorithms (especially
GtkTreeStore's efficiency is terrible).

> 3. Investigate TreeView::insert_column_with_data_func() to 
>    see what it is and whether it's useful.

No need to investigate, I did it some time ago :)
This method is useful when you keep custom types in your model that
can't be directly handled by a cell renderer. For example you have a
custom struct/class in the model column, and want to use
Gtk::CellRendererText to display some data from that object or even for
a simple type that is to be displayed in non-default way.

Here's a copy/pasted sample from my app. I have among others a model
column:

Gtk::TreeModelColumn<uintmax_t> size;

I use this method to add Gtk::TreeViewColumn (VList is a class derived
from Gtk::TreeView):

void VList::append_vlist_column(ColumnType col_no,
  const Gtk::TreeModelColumn<uintmax_t> &col)
{
  Gtk::CellRenderer *renderer = manage(new Gtk::CellRendererText());
  renderer->property_xalign() = 1;
  insert_column_with_data_func(-1, column_defs[col_no].title, *renderer,
    SigC::bind(SigC::slot(*this, &VList::render_size), col_no));
}

void VList::render_size(Gtk::CellRenderer *cr,
  const Gtk::TreeModel::iterator &it, ColumnType col_no)
{
  assert(col_no == VLCOL_SIZE || col_no == VLCOL_DISKSIZE);
  uintmax_t size = (*it)[col_no == VLCOL_SIZE
    ? m_columns.size : m_columns.disksize];
  static_cast<Gtk::CellRendererText*>(cr)->property_text() =
    readable_size(size);
}

This simple fragment of code shows how insert_column_with_data_func() is
used to setup a customized view column. Method readable_size() returns
Glib::ustring that contains huge uintmax_t numbers translated to a
easily readable string. Each time TreeView needs to display a cell of
this column VList::render_size() is called to setup the renderer.
In the same way you can use insert_column_with_data_func() to display
data from complex data structures or classes stored in the model.

There's a serious question: how much wasted CPU cycles C++ adds here.
This display mechanism is pretty slow when model contains many columns
and a few thousands rows, but I don't know if it is a problem caused by
C++ wrappers or perhaps Gtk+ itself is so damned slow (actually I think
its mostly Pango problem, Pango is very slow).

-- 
struct Sig {
  string name("     J a r e k   D u k a t     ");
  string mail(" madmaxer (at) poczta (dot) fm ");
};


----------------------------------------------------------------------
Czy arogancja wladzy ma jakiekolwiek granice? 
>>> http://link.interia.pl/f162c





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]