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 <gtkmm.h>
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<Gtk::ListItem>& list_item, Gtk::Align halign);
void on_bind_id(const Glib::RefPtr<Gtk::ListItem>& list_item);
void on_bind_name(const Glib::RefPtr<Gtk::ListItem>& 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<ModelColumns> create(int col_id, const Glib::ustring& col_name)
{
return Glib::make_refptr_for_instance<ModelColumns>(
new ModelColumns(col_id, col_name));
}
protected:
ModelColumns(int col_id, const Glib::ustring& 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<Gtk::SingleSelection> m_SelectionModel;
Glib::RefPtr<Gio::ListStore<ModelColumns>> m_ListStore;
Glib::RefPtr<Gtk::SortListModel> m_SortListModel;
Glib::RefPtr<Gtk::ColumnViewColumn> m_IdColumn;
Glib::RefPtr<Gtk::ColumnViewColumn> m_NameColumn;
Glib::RefPtr<Gtk::NumericSorter<Glib::ustring::size_type>> m_LengthSorter;
Glib::RefPtr<Gtk::NumericSorter<unsigned int>> m_IdSorter;
Glib::RefPtr<Gtk::StringSorter> 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, &ExampleWindow::on_button_nosort));
m_Button_SortHeadings.signal_toggled().connect(
sigc::mem_fun(*this, &ExampleWindow::on_button_sortheadings));
m_Button_SortLength.signal_toggled().connect(
sigc::mem_fun(*this, &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, &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<ModelColumns>::create();
m_ListStore->append(ModelColumns::create(1, "Billy Bob"));
m_ListStore->append(ModelColumns::create(11, "Billy Bob Junior"));
m_ListStore->append(ModelColumns::create(12, "Sue Bob"));
m_ListStore->append(ModelColumns::create(2, "Joey Jojo"));
m_ListStore->append(ModelColumns::create(3, "Rob McRoberts"));
m_ListStore->append(ModelColumns::create(31, "Xavier McRoberts"));
// Create the sort model.
auto size_expression = Gtk::ClosureExpression<Glib::ustring::size_type>::create(
[](const Glib::RefPtr<Glib::ObjectBase>& item)->Glib::ustring::size_type
{
const auto col = std::dynamic_pointer_cast<ModelColumns>(item);
return col ? col->m_col_name.size() : 0;
});
m_LengthSorter = Gtk::NumericSorter<Glib::ustring::size_type>::create(size_expression);
m_LengthSorter->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->set_autoselect(false);
m_SelectionModel->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->signal_setup().connect(sigc::bind(sigc::mem_fun(*this,
&ExampleWindow::on_setup_label), Gtk::Align::END));
factory->signal_bind().connect(
sigc::mem_fun(*this, &ExampleWindow::on_bind_id));
m_IdColumn = Gtk::ColumnViewColumn::create("ID", factory);
m_ColumnView.append_column(m_IdColumn);
auto uint_expression = Gtk::ClosureExpression<unsigned int>::create(
[](const Glib::RefPtr<Glib::ObjectBase>& item)->unsigned int
{
const auto col = std::dynamic_pointer_cast<ModelColumns>(item);
return col ? col->m_col_id : 0;
});
m_IdSorter = Gtk::NumericSorter<unsigned int>::create(uint_expression);
// Name column
factory = Gtk::SignalListItemFactory::create();
factory->signal_setup().connect(sigc::bind(sigc::mem_fun(*this,
&ExampleWindow::on_setup_label), Gtk::Align::START));
factory->signal_bind().connect(
sigc::mem_fun(*this, &ExampleWindow::on_bind_name));
m_NameColumn = Gtk::ColumnViewColumn::create("Name", factory);
m_ColumnView.append_column(m_NameColumn);
auto ustring_expression = Gtk::ClosureExpression<Glib::ustring>::create(
[](const Glib::RefPtr<Glib::ObjectBase>& item)->Glib::ustring
{
const auto col = std::dynamic_pointer_cast<ModelColumns>(item);
return col ? col->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->set_model(m_ListStore);
m_IdColumn->set_sorter({});
m_NameColumn->set_sorter({});
}
}
void ExampleWindow::on_button_sortheadings()
{
if (m_Button_SortHeadings.get_active())
{
m_SortListModel->set_sorter(m_ColumnView.get_sorter());
m_SelectionModel->set_model(m_SortListModel);
m_IdColumn->set_sorter(m_IdSorter);
m_NameColumn->set_sorter(m_NameSorter);
}
}
void ExampleWindow::on_button_sortlength()
{
if (m_Button_SortLength.get_active())
{
m_SortListModel->set_sorter(m_LengthSorter);
m_SelectionModel->set_model(m_SortListModel);
m_IdColumn->set_sorter({});
m_NameColumn->set_sorter({});
}
}
void ExampleWindow::on_button_quit()
{
set_visible(false);
}
void ExampleWindow::on_setup_label(
const Glib::RefPtr<Gtk::ListItem>& list_item, Gtk::Align halign)
{
list_item->set_child(*Gtk::make_managed<Gtk::Label>("", halign));
}
void ExampleWindow::on_bind_id(const Glib::RefPtr<Gtk::ListItem>& list_item)
{
auto col = std::dynamic_pointer_cast<ModelColumns>(list_item->get_item());
if (!col)
return;
auto label = dynamic_cast<Gtk::Label*>(list_item->get_child());
if (!label)
return;
label->set_text(Glib::ustring::format(col->m_col_id));
}
void ExampleWindow::on_bind_name(const Glib::RefPtr<Gtk::ListItem>& list_item)
{
auto col = std::dynamic_pointer_cast<ModelColumns>(list_item->get_item());
if (!col)
return;
auto label = dynamic_cast<Gtk::Label*>(list_item->get_child());
if (!label)
return;
label->set_text(col->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 <gtkmm/application.h>
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->make_window_and_run<ExampleWindow>(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>
|