143. How to sort treeview using self defined sort function?

Problem

If you have read the example in How to sort treeview using GtkTreeModelSort?, you will notice that when we sort by item description, "item 36" comes before "item 4":

How to sort treeview using GtkTreeModelSort?

Suppose you would like "item 4" to come before "item 36", we can do so by writing our self-defined sort function as shown below:

How to sort treeview using self defined sort function?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
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   
69   
70   
71   
72   
73   
75   
76   
77   
78   
79   
80   
81   
82   
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   
121   
122   
123   
124   
125   
126   
127   
128   
129   
130   
131   
132   
133   
134   
135   
136   
137   
138   
139   
140   
141   
142   
143   
144   
145   
146   
147   
148   
150   
151   
152   
153   
154   
156   
157   
158   
159   
160   
161   
162   
163   
164   
165   
166   
167   
168   
169   
170   
171   
172   
173   
175   
176   
177   
178   
179   
180   
186   
187   
188   
189   
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   
229   
230   
231   
232   
234   
235   
236   
237   
238   
239   
240   
241   
242   
243   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 312);
$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'),
    '_Sort By' => array('Row#|0', 'Description|1', 'Qty|2', 'Price|3')
);
setup_menu($vbox, $menu_definition);

// display title
$title = new GtkLabel("Sort treeview using self-defined sort function");
$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, 0);

$vbox->pack_start(new GtkLabel("Ctrl-0: sort by row #\n".
"Ctrl-1: sort by Description\n".
"Ctrl-2: sort by Qty\n".
"Ctrl-3: sort by Price"), 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0); // add a small gap

// the 2D table
$data = array(
array('row0', 'item 42', 2, 3.1, 1),
array('row1', 'item 36', 20, 6.21, 1),
array('row2', 'item 21', 8, 9.36, 1),
array('row3', 'item 10', 11, 12.4, 1),
array('row4', 'item 7', 5, 15.5, 1),
array('row5', 'item 4', 17, 18.6, 1),
array('row6', 'item 3', 20, 21.73, 1));

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, $view;
    if (defined("GObject::TYPE_STRING")) {
        $model = new GtkListStore(GObject::TYPE_STRING, GObject::TYPE_STRING,
                GObject::TYPE_LONG, GObject::TYPE_DOUBLE, GObject::TYPE_BOOLEAN);
    } else {
        $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING,
                    Gtk::TYPE_LONG, Gtk::TYPE_DOUBLE, Gtk::TYPE_BOOLEAN);
    }
    $field_header = array('Row #', 'Description', 'Qty', 'Price');
    $field_justification = array(0.0, 0.0, 0.5, 1.0);

    // setup GtkTreeModelSort
    $modelsort = new GtkTreeModelSort($model);
    $modelsort->set_sort_column_id(0, Gtk::SORT_ASCENDING);
    $modelsort->set_sort_func(1, 'sort_item'); // note 1
    
    // Creates the view to display the list store
    $view = new GtkTreeView($modelsort);
    $scrolled_win->add($view);

    // Creates the columns
    for ($col=0; $col<count($field_header); ++$col) {
        $cell_renderer = new GtkCellRendererText();
        $cell_renderer->set_property("xalign", $field_justification[$col]);
        $column = new GtkTreeViewColumn($field_header[$col],
            $cell_renderer, 'text', $col);
        $column->set_alignment($field_justification[$col]);
        $column->set_sort_column_id($col);

        // set the header font and color
        $label = new GtkLabel($field_header[$col]);
        $label->modify_font(new PangoFontDescription("Arial Bold"));
        $label->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000FF"));
        $column->set_widget($label);
        $label->show();

        // setup self-defined function to display alternate row color
        $column->set_cell_data_func($cell_renderer, "format_col", $col);
        $view->append_column($column);
    }

    // pupulates the data
    for ($row=0; $row<count($data); ++$row) {
        $values = array();
        for ($col=0; $col<count($data[$row]); ++$col) {
            $values[] = $data[$row][$col];
        }
        $model->append($values);
    }

    // setup selection
    $selection = $view->get_selection();
    $selection->connect('changed', 'on_selection');

}

// self-defined function to format the price column
function format_col($column, $cell, $model, $iter, $col_num) {
    $path = $model->get_path($iter); // get the current path
    $row_num = $path[0]; // get the row number
    if ($col_num==3) {
        $amt = $model->get_value($iter, 3);
        $cell->set_property('text', '$'.number_format($amt,2));
    }
    $row_color = ($row_num%2==1) ? '#dddddd' : '#ffffff';
    $cell->set_property('cell-background', $row_color);
}

// the function that is called when user selects a row
function on_selection($selection) {
    list($model, $iter) = $selection->get_selected();
    if ($iter==NULL) return;
    $desc = $model->get_value($iter, 1);
    $qty = $model->get_value($iter, 2);
    $price = $model->get_value($iter, 3);
    print "You have selected $desc: $qty ($price)\n";
}

// 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 $view;
    $model_sort = $view->get_model();
    $item = $menu_item->child->get_label();
    echo "menu selected: $item\n";
    if ($item=='_Quit') {
        print "bp0\n";
        Gtk::main_quit();
        print "bp1\n";
        return false;
    } elseif ($item=='Row#') {
        $model_sort->set_sort_column_id(0,     Gtk::SORT_ASCENDING);
    } elseif ($item=='Description') {
        $model_sort->set_sort_column_id(1,     Gtk::SORT_ASCENDING);
    } elseif ($item=='Qty') {
        $model_sort->set_sort_column_id(2,     Gtk::SORT_ASCENDING);
    } elseif ($item=='Price') {
        $model_sort->set_sort_column_id(3,     Gtk::SORT_ASCENDING);
    }
}

// self-defined sort function for sorting column 1 (item description)
function sort_item($model, $iter1, $iter2) {
    $item1 = $model->get_value($iter1, 1);
    $item2 = $model->get_value($iter2, 1);
    $val1 = intval(substr($item1, 5)); // note 2
    $val2 = intval(substr($item2, 5)); // note 2
    if ($val1<$val2) {
        return -1; // note 3
    } elseif ($val1==$val2) {
        return 0; // note 3
    } else {
        return 1; // note 3
    }
}

?>

Output

As shown above.
 

Explanation

The above code is based on How to sort treeview using GtkTreeModelSort?.

What's new here:

  1. Set the self-defined sort function.
  2. Get the integer value of the item description.
  3. Compare the two values and return accordingly.

Related Links

Add comment


Security code
Refresh