The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Gtk2::Ex::ListModelConcat -- concatenated list models

SYNOPSIS

 use Gtk2::Ex::ListModelConcat;
 my $model = Gtk2::Ex::ListModelConcat->new (models => [$m1,$m2]);

OBJECT HIERARCHY

Gtk2::Ex::ListModelConcat is a subclass of Glib::Object.

    Glib::Object
      Gtk2::Ex::ListModelConcat

and implements the interfaces

    Gtk2::TreeModel
    Gtk2::TreeDragSource
    Gtk2::TreeDragDest

DESCRIPTION

Gtk2::Ex::ListModelConcat presents a set of list type TreeModels concatenated together as a single list. A Concat doesn't hold any data itself, it just presents the sub-models' content. Gtk2::ListStore objects are suitable as the sub-models, but any similar list-type model can be used.

                    +--------+
            / row 0 | apple  | row 0 \
           /        +--------+        \
           |  row 1 | orange | row 1  | first child
           |        +--------+        /
           |  row 2 | lemon  | row 2 /
           |        +--------+
    Concat |  row 3 | potato | row 0 \
           |        +--------+        \
           |  row 4 | carrot | row 1  |
           |        +--------+        | second child
           |  row 5 | squash | row 2  |
           \        +--------+        /
            \ row 6 | onion  | row 3 /
                    +--------+

Changes in the sub-models are reported up through the Concat with the usual row-changed etc signals. Conversely change methods are implemented by the Concat in the style of Gtk2::ListStore and if the sub-models have those functions too (eg. if they're ListStores) then changes on the Concat are applied down to the sub-models.

The sub-models should have the same number of columns and the same column types (or compatible types), though currently ListModelConcat doesn't try to enforce that. It works to put one Concat inside another, except of course it cannot be inside itself (directly or indirectly).

DRAG AND DROP

ListModelConcat implements TreeDragSource and TreeDragDest, allowing rows to be moved by dragging in Gtk2::TreeView or similar. The actual operations are delegated to the submodels, so a row can be dragged if the originating submodel is a TreeDragSource and a location is a drop point if the submodel there is a TreeDragDest.

It's worth noting the standard Gtk2::ListStore models only accept drops of their own rows, ie. a re-ordering, not movement of rows between different models. This is reasonable since different models might have different natures, but you might want to subclass or use a wrapper to make a more liberal policy between compatible models.

PROPERTIES

models (array reference, default empty [])

Arrayref of sub-models to present. The sub-models can be any object implementing the Gtk2::TreeModel interface. They should be list-only type, but currently ListModelConcat doesn't enforce that.

Currently when the models property is changed there's no row-inserted / row-deleted etc signals emitted by the Concat to announce the new or altered data presented. Perhaps this will change. The disadvantage would be that adding or removing a big model could generate thousands of fairly pointless signals. The suggestion is to treat models as if it were "construct-only" and make a new Concat for a new set of models.

FUNCTIONS

Creation

$concat = Gtk2::Ex::ListModelConcat->new (key=>value,...)

Create and return a new Concat object. Optional key/value pairs set initial properties per Glib::Object->new. Eg.

 my $concat = Gtk2::Ex::ListModelConcat->new (models => [$m1,$m2]);

ListStore Methods

The following functions follow the style of Gtk2::ListStore and they call down to corresponding functions in the sub-models. Those sub-models don't have to be Gtk2::ListStore objects, they can be some other class implementing the same methods.

$concat->clear
$concat->set_column_types

These are applied to all sub-models, so clear clears all the models or set_column_types sets the types in all the models.

In the current implementation Concat doesn't keep track of column types itself, but asks the sub-models when required (using the first sub-model, currently).

$iter = $concat->append
$iter = $concat->insert ($pos)
$iter = $concat->insert_with_values ($pos, $col,$val, ...)
$iter = $concat->insert_after ($iter)
$iter = $concat->insert_before ($iter)
bool = $concat->iter_is_valid ($iter)
$concat->move_after ($iter, $iter_from, $iter_to)
$concat->move_before ($iter, $iter_from, $iter_to)
$iter = $concat->prepend
bool = $concat->remove ($iter)
$concat->reorder (@neworder)
$concat->swap ($iter_a, $iter_b)
$concat->set ($iter, $col,$val, ...)
$concat->set_value ($iter, $col, $val)

These are per the Gtk2::ListStore methods.

Note set overrides the set from Glib::Object which normally sets object properties. You can use its set_property name instead.

    $model->set_property ('propname' => $value);

As of Gtk2-Perl 1.200 set_value in Gtk2::ListStore is actually an alias for set and so accepts multiple $col,$val pairs. ListModelConcat passes all set_value arguments through to the sub-model set_value (after converting the $iter), so it's up to it what should work.

Iter Conversions

The following functions convert Concat iters to iters on the child model, or vice versa. They're similar to what TreeModelFilter offers (see Gtk2::TreeModelFilter), except that a particular child model is returned and must be specified since a Concat can have multiple children.

($childmodel, $childiter, $childnum) = $concat->convert_iter_to_child_iter ($iter)

Convert a ListModelConcat iter to an iter on the child model corresponding to that row.

The return includes the $childnum which is an index into the models property arrayref. If a child model appears more than once in the models then this identifies which occurrence the $iter refers to. Often this is of no interest and can be ignored.

    my ($childmodel, $childiter)
      = $concat->convert_iter_to_child_iter ($iter);
    $childmodel->something($childiter) ...
$iter = $concat->convert_child_iter_to_iter ($childmodel, $childiter)
$iter = $concat->convert_childnum_iter_to_iter ($childnum, $childiter)

Convert an iter on one of the child models to an iter on the ListModelConcat.

The childnum func takes an index into the models list, counting from 0 for the first model. If you've got a child model appearing more than once then this lets you identify which one you mean. The plain $childmodel func gives the first occurrence of that model.

SIGNALS

The TreeModel interface implemented by ListModelConcat provides the following usual signals

    row-changed    ($concat, $path, $iter, $userdata)
    row-inserted   ($concat, $path, $iter, $userdata)
    row-deleted    ($concat, $path, $userdata)
    rows-reordered ($concat, $path, $iter, $arrayref, $userdata)

Because ListModelConcat is list-only, the path to row-changed, row-inserted and row-deleted is always depth 1, and the path to rows-reordered is always depth 0 and the iter there always undef.

When a change occurs in a sub-model the corresponding signal is reported up through Concat. The path and iter are of course reported by the Concat in its "concatenated" coordinates and iters, not the sub-model's.

BUGS

ref_node and unref_node are no-ops. The intention is to apply them down on the sub-models, but hopefully without needing lots of bookkeeping in the Concat as to what's currently reffed.

It mostly works to have a sub-model appear more than once in a Concat. The only real problem is with the row-deleted and row-inserted signals. They're emitted on the Concat the right number of times, but the multiple inserts/deletes are all present in the data as of the first emit, which could confuse handler code. (Perhaps some sort of temporary index mapping could make the changes seem one-at-a-time, except the deleted row contents have already gone ...)

What does work fine though is to have multiple TreeModelFilters (or similar) selecting different parts of a single underlying model. As long as a given row only appears once it doesn't matter where its ultimate storage location is.

SEE ALSO

Gtk2::TreeModel, Gtk2::TreeDragSource, Gtk2::TreeDragDest, Gtk2::ListStore, Glib::Object

HOME PAGE

http://user42.tuxfamily.org/gtk2-ex-listmodelconcat/

COPYRIGHT

Copyright 2008, 2009, 2010 Kevin Ryde

Gtk2-Ex-ListModelConcat is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Gtk2-Ex-ListModelConcat is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Gtk2-Ex-ListModelConcat. If not, see http://www.gnu.org/licenses/.