369. How to display a tree structure using GtkTreeStore with GtkTreeModelSort on top of GtkTreeModelFilter?

Problem

This is in response to Jake Cobb's post related to using GtkTreeStore with GtkTreeModelFilter and GtkTreeModelSort.

How to display a tree structure using GtkTreeStore with GtkTreeModelSort on top of GtkTreeModelFilter?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
33   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
105   
106   
107   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
118   
119   
120   
121   
122   
123   
124   
125   
126   
127   
129   
130   
131   
132   
133   
134   
135   
136   
137   
138   
139   
140   
141   
142   
143   
144   
145   
146   
149   
150   
151   
152   
153   
154   
155   
157   
158   
160   
161   
162   
164   
165   
166   
171   
172   
173   
174   
175   
176   
177   
178   
179   
180   
181   
182   
183   
184   
185   
186   
187   
188   
189   
190   
191   
192   
193   
194   
195   
196   
197   
198   
199   
200   
201   
202   
203   
204   
205   
206   
207   
208   
209   
210   
211   
212   
213   
214   
215   
216   
217   
218   
219   
220   
221   
222   
223   
224   
225   
226   
228   
229   
230   
231   
232   
233   
234   
235   
236   
237   
238   
239   
240   
241   
242   
243   
245   
246   
247   
248   
249   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 306);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

$accel_group = new GtkAccelGroup();
$window->add_accel_group($accel_group);

// define menu definition
$menu_definition = array(
    '_File' => array('_Quit|Q'),
    '_Filter' => array('Show All Rows|1',
                'Show widgets containing the word button|2',
                'Show widgets containing the word box|3'),
    '_Sort' => array('No Sort|A',
                'Sort by widget ascending|B',
                'Sort by widget descending|C',
                'Sort by quantity ascending|D',
                'Sort by quantity descending|E')
);
setup_menu($vbox, $menu_definition);

// display title
$title = new GtkLabel("                       GtkTreeStore with \n".
"GtkTreeModelFilter on top of GtkTreeModelSort");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0);

$data = array(
    array ('id' => 'GtkButton', 'parent' => '0'),
    array ('id' => 'GtkColorButton', 'parent' => 'GtkButton'),
    array ('id' => 'GtkToggleButton', 'parent' => 'GtkButton'),
    array ('id' => 'GtkFontButton', 'parent' => 'GtkButton'),
    array ('id' => 'GtkBox', 'parent' => '0'),
    array ('id' => 'GtkButtonBox', 'parent' => 'GtkBox'),
    array ('id' => 'GtkVButtonBox', 'parent' => 'GtkButtonBox'),
    array ('id' => 'GtkHButtonBox', 'parent' => 'GtkButtonBox'),
    array ('id' => 'GtkVBox', 'parent' => 'GtkBox'),
    array ('id' => 'GtkFontSelection', 'parent' => 'GtkVBox'),
    array ('id' => 'GtkColorSelection', 'parent' => 'GtkVBox'),
    array ('id' => 'GtkHBox', 'parent' => 'GtkBox'),
    array ('id' => 'test1', 'parent' => '0'),
    array ('id' => 'button_test1', 'parent' => 'test1'),
    array ('id' => 'box_test1', 'parent' => 'test1'),
    array ('id' => 'button_test_root', 'parent' => '0'),
    array ('id' => 'button_test2', 'parent' => 'button_test_root'),
    array ('id' => 'box_test2', 'parent' => 'button_test_root'),
);

display_table($vbox, $data);

$window->show_all();
Gtk::main();

function display_table($vbox, $data) {

    // Set up a scroll window
    $scrolled_win = new GtkScrolledWindow();
    $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC,
        Gtk::POLICY_AUTOMATIC);
    $vbox->pack_start($scrolled_win);

    // Creates the list store
    global $model, $modelfilter1, $modelfilter2, $view;
    if (defined("GObject::TYPE_STRING")) {
        $model = new GtkTreeStore(GObject::TYPE_STRING, GObject::TYPE_LONG, 
            GObject::TYPE_BOOLEAN, GObject::TYPE_BOOLEAN); // note 1
    } else {
        $model = new GtkTreeStore(Gtk::TYPE_STRING, Gtk::TYPE_LONG, 
            Gtk::TYPE_BOOLEAN, Gtk::TYPE_BOOLEAN); // note 1
    }
    $field_header = array('Widget', 'Qty');

    // setup GtkTreeModelFilter1
    $modelfilter1 = new GtkTreeModelFilter($model); // note 2
    $modelfilter1->set_visible_column(2);

    // setup GtkTreeModelFilter2
    $modelfilter2 = new GtkTreeModelFilter($model); // note 2
    $modelfilter2->set_visible_column(3);

    // setup GtkTreeModelSort
    global $modelsort, $current_filter, $current_sort;
    $current_filter = 0;
    $current_sort = 0;

    $modelsort[0][0] = $model;
    $modelsort[0][1] = new GtkTreeModelSort($model); // note 3
    $modelsort[0][1]->set_sort_column_id(0, Gtk::SORT_ASCENDING);
    $modelsort[0][2] = new GtkTreeModelSort($model); // note 3
    $modelsort[0][2]->set_sort_column_id(0, Gtk::SORT_DESCENDING);
    $modelsort[0][3] = new GtkTreeModelSort($model); // note 3
    $modelsort[0][3]->set_sort_column_id(1, Gtk::SORT_ASCENDING);
    $modelsort[0][4] = new GtkTreeModelSort($model); // note 3
    $modelsort[0][4]->set_sort_column_id(1, Gtk::SORT_DESCENDING);

    $modelsort[1][0] = $modelfilter1;
    $modelsort[1][1] = new GtkTreeModelSort($modelfilter1);
    $modelsort[1][1]->set_sort_column_id(0, Gtk::SORT_ASCENDING);
    $modelsort[1][2] = new GtkTreeModelSort($modelfilter1);
    $modelsort[1][2]->set_sort_column_id(0, Gtk::SORT_DESCENDING);
    $modelsort[1][3] = new GtkTreeModelSort($modelfilter1);
    $modelsort[1][3]->set_sort_column_id(1, Gtk::SORT_ASCENDING);
    $modelsort[1][4] = new GtkTreeModelSort($modelfilter1);
    $modelsort[1][4]->set_sort_column_id(1, Gtk::SORT_DESCENDING);

    $modelsort[2][0] = $modelfilter2;
    $modelsort[2][1] = new GtkTreeModelSort($modelfilter2);
    $modelsort[2][1]->set_sort_column_id(0, Gtk::SORT_ASCENDING);
    $modelsort[2][2] = new GtkTreeModelSort($modelfilter2);
    $modelsort[2][2]->set_sort_column_id(0, Gtk::SORT_DESCENDING);
    $modelsort[2][3] = new GtkTreeModelSort($modelfilter2);
    $modelsort[2][3]->set_sort_column_id(1, Gtk::SORT_ASCENDING);
    $modelsort[2][4] = new GtkTreeModelSort($modelfilter2);
    $modelsort[2][4]->set_sort_column_id(1, Gtk::SORT_DESCENDING);

    // Creates the view to display the list store
    $view = new GtkTreeView($model);
    $scrolled_win->add($view);

    // Creates the columns
    for ($col=0; $col<count($field_header); ++$col) {
        $cell_renderer = new GtkCellRendererText();
        $column = new GtkTreeViewColumn($field_header[$col],
            $cell_renderer, 'text', $col);
        $view->append_column($column);
    }

    // pupulates the data
    $nodes = array();
    $nodes[0] = null; // root
    foreach($data as $item) {
        $id = $item['id'];
        $parent = $item['parent'];
        $qty = rand(0, 100);

        // sets the flag for filter 1
        $filter1_flag = (preg_match('/button/i', $id)) ? 1 : 0;

        // sets the flag for filter 2
        $filter2_flag = (preg_match('/box/i', $id)) ? 1 : 0;

        $nodes[$id] = $model->append($nodes[$parent],
            array($id, $qty, $filter1_flag, $filter2_flag));
    }

    $view->expand_all();
    if (method_exists($view, 'set_enable_tree_lines'))
        $view->set_enable_tree_lines(1);
    // setup GtkTreeModelSort
}

// setup menu
function setup_menu($vbox, $menus) {
    global $accel_group;
    $menubar = new GtkMenuBar();
    $vbox->pack_start($menubar, 0, 0);
    foreach($menus as $toplevel => $sublevels) {
        $menubar->append($top_menu = new GtkMenuItem($toplevel));
        $menu = new GtkMenu();
        $top_menu->set_submenu($menu);
        foreach($sublevels as $submenu) {
            if (strpos("$submenu", '|') === false) {
                $accel_key = '';
            } else {
                list($submenu, $accel_key) = explode('|', $submenu);
            }

            if (is_array($submenu)) { // set up radio menus
                $i=0;
                $radio[0] = null;
                foreach($submenu as $radio_item) {
                    $radio[$i] = new GtkRadioMenuItem($radio[0], $radio_item);
                    $radio[$i]->connect('toggled', "on_toggle");
                    $menu->append($radio[$i]);
                    ++$i;
                }
                $radio[0]->set_active(1); // select the first item
            } else {
                if ($submenu=='<hr>') {
                    $menu->append(new GtkSeparatorMenuItem());
                } else {
                    $submenu2 = str_replace('_', '', $submenu);
                    $submenu2 = str_replace(' ', '_', $submenu2);
                    $stock_image_name = 'Gtk::STOCK_'.strtoupper($submenu2);
                    if (defined($stock_image_name)) {
                        $menu_item = new GtkImageMenuItem(
                            constant($stock_image_name));
                    } else {
                        $menu_item = new GtkMenuItem($submenu);
                    }
                    if ($accel_key!='') {
                        $menu_item->add_accelerator("activate",
                            $accel_group, ord($accel_key), Gdk::CONTROL_MASK, 1);
                    }

                    $menu->append($menu_item);
                    $menu_item->connect('activate', 'on_menu_select');
                }
            }
        }
    }
}

// process menu item selection
function on_menu_select($menu_item) {
    global $modelsort, $current_filter, $current_sort, $view;
    $item = $menu_item->child->get_label();
    echo "menu selected: $item\n";
    switch($item) {
        case '_Quit': Gtk::main_quit(); break;
        case 'Show All Rows': $current_filter = 0; break;
        case 'Show widgets containing the word button':
            $current_filter = 1; break;
        case 'Show widgets containing the word box':
             $current_filter = 2; break;
        case 'No Sort': $current_sort = 0; break;
        case 'Sort by widget ascending': $current_sort = 1; break;
        case 'Sort by widget descending': $current_sort = 2; break;
        case 'Sort by quantity ascending': $current_sort = 3; break;
        case 'Sort by quantity descending': $current_sort = 4; break;
    }
    $view->set_model($modelsort[$current_filter][$current_sort]); // note 4
    $view->expand_all();
}

?>

Output

As shown above.
 

Explanation

The above example make use of the code from the following:

What's new here:

  1. This is the base model.
  2. Create the GtkTreeModelFilter from the base model.
  3. Create the GtkTreeModelSort on top of each GtkTreeModelFilter.
  4. Switch to respective model accordingly.

Related Links

Add comment


Security code
Refresh