File: sec-listmodel-sorting.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (355 lines) | stat: -rw-r--r-- 13,567 bytes parent folder | download
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
32
33
34
35
36
37
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
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
118
119
120
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
      hljs.configure({languages: ['cpp']});
      hljs.highlightAll();
    </script><title>Sorting</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-listmodel.html" title="Chapter 10. ListView, GridView, ColumnView">
<link rel="prev" href="sec-listmodel-view.html" title="The View">
<link rel="next" href="sec-listmodel-filtering.html" title="Filtering">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Sorting</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-listmodel-view.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 10. ListView, GridView, ColumnView</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-listmodel-filtering.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-listmodel-sorting"></a>Sorting</h2></div></div></div>


<p>
The list can be sorted by wrapping it in a <code class="classname">SortListModel</code>.
There are two ways to do this.
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>In a <code class="classname">ColumnView</code>, get the
  <code class="classname">ColumnViewSorter</code> from the <code class="classname">ColumnView</code>
  and set it to the <code class="classname">SortListModel</code>. Set a <code class="classname">Sorter</code>
  to each <code class="classname">ColumnViewColumn</code>. Then the user of your app can
  sort the items by clicking on a column heading.</p></li>
<li class="listitem"><p>In any view, set a <code class="classname">Sorter</code> such as a
  <code class="classname">StringSorter</code> or a <code class="classname">NumericSorter</code>
  to the <code class="classname">SortListModel</code>.</p></li>
</ul></div>

<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1SortListModel.html" target="_top">SortListModel Reference</a></p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1StringSorter.html" target="_top">StringSorter Reference</a></p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1NumericSorter.html" target="_top">NumericSorter Reference</a></p>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="listmodel-sorting-example"></a>Example</h3></div></div></div>


<div class="figure">
<a name="figure-listmodel-sorting"></a><p class="title"><b>Figure 10.4. SortListModel</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/listmodel_sort.png" alt="SortListModel"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/listmodelviews/sort" target="_top">Source Code</a></p>

<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  ~ExampleWindow() override;

protected:
  // Signal handlers:
  void on_button_nosort();
  void on_button_sortheadings();
  void on_button_sortlength();
  void on_button_quit();
  void on_setup_label(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item, Gtk::Align halign);
  void on_bind_id(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);
  void on_bind_name(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item);

  // A Gio::ListStore item.
  class ModelColumns : public Glib::Object
  {
  public:
    unsigned int m_col_id;
    Glib::ustring m_col_name;

    static Glib::RefPtr&lt;ModelColumns&gt; create(int col_id, const Glib::ustring&amp; col_name)
    {
      return Glib::make_refptr_for_instance&lt;ModelColumns&gt;(
        new ModelColumns(col_id, col_name));
    }

  protected:
    ModelColumns(int col_id, const Glib::ustring&amp; col_name)
    : m_col_id(col_id), m_col_name(col_name)
    {}
  }; // ModelColumns

  // Child widgets:
  Gtk::Box m_VBox;
  Gtk::ScrolledWindow m_ScrolledWindow;
  Gtk::ColumnView m_ColumnView;
  Gtk::Box m_ButtonBox;
  Gtk::ToggleButton m_Button_NoSort;
  Gtk::ToggleButton m_Button_SortHeadings;
  Gtk::ToggleButton m_Button_SortLength;
  Gtk::Button m_Button_Quit;

  Glib::RefPtr&lt;Gtk::SingleSelection&gt; m_SelectionModel; 
  Glib::RefPtr&lt;Gio::ListStore&lt;ModelColumns&gt;&gt; m_ListStore;
  Glib::RefPtr&lt;Gtk::SortListModel&gt; m_SortListModel;
  Glib::RefPtr&lt;Gtk::ColumnViewColumn&gt; m_IdColumn;
  Glib::RefPtr&lt;Gtk::ColumnViewColumn&gt; m_NameColumn;
  Glib::RefPtr&lt;Gtk::NumericSorter&lt;Glib::ustring::size_type&gt;&gt; m_LengthSorter;
  Glib::RefPtr&lt;Gtk::NumericSorter&lt;unsigned int&gt;&gt; m_IdSorter;
  Glib::RefPtr&lt;Gtk::StringSorter&gt; m_NameSorter;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"

ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
  m_Button_NoSort("Don't Sort"),
  m_Button_SortHeadings("Sort Headings"),
  m_Button_SortLength("Sort Length"),
  m_Button_Quit("Quit")
{
  set_title("Gtk::ColumnView (Gtk::SortListModel) example");
  set_default_size(400, 350);

  m_VBox.set_margin(5);
  set_child(m_VBox);

  // Add the ColumnView, inside a ScrolledWindow, with the buttons underneath:
  m_ScrolledWindow.set_child(m_ColumnView);

  // Only show the scrollbars when they are necessary:
  m_ScrolledWindow.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
  m_ScrolledWindow.set_expand();

  m_VBox.append(m_ScrolledWindow);
  m_VBox.append(m_ButtonBox);

  m_ButtonBox.set_margin(5);
  m_ButtonBox.set_spacing(10);
  m_ButtonBox.append(m_Button_NoSort);
  m_ButtonBox.append(m_Button_SortHeadings);
  m_ButtonBox.append(m_Button_SortLength);
  m_ButtonBox.append(m_Button_Quit);
  m_Button_NoSort.signal_toggled().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_button_nosort));
  m_Button_SortHeadings.signal_toggled().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_button_sortheadings));
  m_Button_SortLength.signal_toggled().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_button_sortlength));
  m_Button_Quit.set_hexpand(true);
  m_Button_Quit.set_halign(Gtk::Align::END);
  m_Button_Quit.signal_clicked().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_button_quit));

  // Gtk::CheckButton and Gtk::ToggleButton have set_group() methods.
  // They act as radio buttons, if they are included in a group.
  m_Button_SortHeadings.set_group(m_Button_NoSort);
  m_Button_SortLength.set_group(m_Button_NoSort);

  // Create the List model.
  m_ListStore = Gio::ListStore&lt;ModelColumns&gt;::create();
  m_ListStore-&gt;append(ModelColumns::create(1, "Billy Bob"));
  m_ListStore-&gt;append(ModelColumns::create(11, "Billy Bob Junior"));
  m_ListStore-&gt;append(ModelColumns::create(12, "Sue Bob"));
  m_ListStore-&gt;append(ModelColumns::create(2, "Joey Jojo"));
  m_ListStore-&gt;append(ModelColumns::create(3, "Rob McRoberts"));
  m_ListStore-&gt;append(ModelColumns::create(31, "Xavier McRoberts"));

  // Create the sort model.
  auto size_expression = Gtk::ClosureExpression&lt;Glib::ustring::size_type&gt;::create(
    [](const Glib::RefPtr&lt;Glib::ObjectBase&gt;&amp; item)-&gt;Glib::ustring::size_type
    {
      const auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(item);
      return col ? col-&gt;m_col_name.size() : 0;
    });
  m_LengthSorter = Gtk::NumericSorter&lt;Glib::ustring::size_type&gt;::create(size_expression);
  m_LengthSorter-&gt;set_sort_order(Gtk::SortType::ASCENDING);
  m_SortListModel = Gtk::SortListModel::create(m_ListStore, m_LengthSorter);

  // Set list model and selection model.
  m_SelectionModel = Gtk::SingleSelection::create(m_ListStore);
  m_SelectionModel-&gt;set_autoselect(false);
  m_SelectionModel-&gt;set_can_unselect(true);
  m_ColumnView.set_model(m_SelectionModel);
  m_ColumnView.add_css_class("data-table"); // high density table

  // Make the columns reorderable.
  // This is not necessary, but it's nice to show the feature.
  m_ColumnView.set_reorderable(true);

  // Add the ColumnView's columns:

  // Id column
  auto factory = Gtk::SignalListItemFactory::create();
  factory-&gt;signal_setup().connect(sigc::bind(sigc::mem_fun(*this,
    &amp;ExampleWindow::on_setup_label), Gtk::Align::END));
  factory-&gt;signal_bind().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_bind_id));
  m_IdColumn = Gtk::ColumnViewColumn::create("ID", factory);
  m_ColumnView.append_column(m_IdColumn);
  auto uint_expression = Gtk::ClosureExpression&lt;unsigned int&gt;::create(
    [](const Glib::RefPtr&lt;Glib::ObjectBase&gt;&amp; item)-&gt;unsigned int
    {
      const auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(item);
      return col ? col-&gt;m_col_id : 0;
    });
  m_IdSorter = Gtk::NumericSorter&lt;unsigned int&gt;::create(uint_expression);

  // Name column
  factory = Gtk::SignalListItemFactory::create();
  factory-&gt;signal_setup().connect(sigc::bind(sigc::mem_fun(*this,
    &amp;ExampleWindow::on_setup_label), Gtk::Align::START));
  factory-&gt;signal_bind().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_bind_name));
  m_NameColumn = Gtk::ColumnViewColumn::create("Name", factory);
  m_ColumnView.append_column(m_NameColumn);
  auto ustring_expression = Gtk::ClosureExpression&lt;Glib::ustring&gt;::create(
    [](const Glib::RefPtr&lt;Glib::ObjectBase&gt;&amp; item)-&gt;Glib::ustring
    {
      const auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(item);
      return col ? col-&gt;m_col_name : "";
    });
  m_NameSorter = Gtk::StringSorter::create(ustring_expression);

  // Show the list model without sorting.
  m_Button_NoSort.set_active(true);
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_button_nosort()
{
  if (m_Button_NoSort.get_active())
  {
    m_SelectionModel-&gt;set_model(m_ListStore);
    m_IdColumn-&gt;set_sorter({});
    m_NameColumn-&gt;set_sorter({});
  }
}

void ExampleWindow::on_button_sortheadings()
{
  if (m_Button_SortHeadings.get_active())
  {
    m_SortListModel-&gt;set_sorter(m_ColumnView.get_sorter());
    m_SelectionModel-&gt;set_model(m_SortListModel);
    m_IdColumn-&gt;set_sorter(m_IdSorter);
    m_NameColumn-&gt;set_sorter(m_NameSorter);
  }
}

void ExampleWindow::on_button_sortlength()
{
  if (m_Button_SortLength.get_active())
  {
    m_SortListModel-&gt;set_sorter(m_LengthSorter);
    m_SelectionModel-&gt;set_model(m_SortListModel);
    m_IdColumn-&gt;set_sorter({});
    m_NameColumn-&gt;set_sorter({});
  }
}

void ExampleWindow::on_button_quit()
{
  set_visible(false);
}

void ExampleWindow::on_setup_label(
  const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item, Gtk::Align halign)
{
  list_item-&gt;set_child(*Gtk::make_managed&lt;Gtk::Label&gt;("", halign));
}

void ExampleWindow::on_bind_id(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(list_item-&gt;get_item());
  if (!col)
    return;
  auto label = dynamic_cast&lt;Gtk::Label*&gt;(list_item-&gt;get_child());
  if (!label)
    return;
  label-&gt;set_text(Glib::ustring::format(col-&gt;m_col_id));
}

void ExampleWindow::on_bind_name(const Glib::RefPtr&lt;Gtk::ListItem&gt;&amp; list_item)
{
  auto col = std::dynamic_pointer_cast&lt;ModelColumns&gt;(list_item-&gt;get_item());
  if (!col)
    return;
  auto label = dynamic_cast&lt;Gtk::Label*&gt;(list_item-&gt;get_child());
  if (!label)
    return;
  label-&gt;set_text(col-&gt;m_col_name);
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;gtkmm/application.h&gt;

int main(int argc, char* argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  // Shows the window and returns when it is closed.
  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv);
}
</code></pre>


</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-listmodel-view.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-listmodel.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-listmodel-filtering.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">The View </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Filtering</td>
</tr>
</table>
</div>
</body>
</html>