Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
filtered-store.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#ifndef INKSCAPE_PATTERN_EDITOR_H
4#define INKSCAPE_PATTERN_EDITOR_H
5
6#include <algorithm>
7#include <functional>
8#include <iterator>
9#include <vector>
10#include <giomm/liststore.h>
11
12// Simplistic filtered list store implementation
13// It's a GIO ListStore plus custom filter function to control visibility of items
14
15namespace Inkscape {
16
17template<class T> class FilteredStore {
18public:
22
23 typedef Glib::RefPtr<T> TPtr;
24
25 bool assign(const std::vector<TPtr>& items) {
26 if (items == _items) return false; // not changed
27
28 _items = items;
30 return true; // store updated
31 }
32
33 void refresh() {
34 apply_filter(true);
35 }
36
37 const std::vector<TPtr>& get_items() const {
38 return _items;
39 }
40
41 void set_filter(const std::function<bool (const TPtr&)>& filter_callback) {
42 _filter_callback = filter_callback;
43 }
44
45 void apply_filter(bool force_refresh = false) {
46 // if there's a filter, run it
47 std::vector<TPtr> visible;
48 if (_filter_callback) {
49 std::copy_if(_items.cbegin(), _items.cend(),
50 std::back_inserter(visible), std::ref(_filter_callback));
51 }
52 auto const &items = _filter_callback ? visible : _items;
53
54 if (force_refresh) {
56 return;
57 }
58
59 // compare store content with visible list items to avoid needless updates
60 const auto n = _store->get_n_items();
61 bool same = false;
62 if (n == items.size()) {
63 same = true;
64 // compare each item
65 for (int i = 0; i < n; ++i) {
66 if (items[i] != _store->get_item(i)) {
67 same = false;
68 break;
69 }
70 }
71 }
72
73 if (same) return; // nothing to do
74
76 }
77
78 Glib::RefPtr<Gio::ListStore<T>> get_store() {
79 return _store;
80 }
81
82private:
83 void update_store(const std::vector<TPtr>& items) {
84 _store->freeze_notify();
85 _store->remove_all();
86 for (auto&& t : items) {
87 _store->append(t);
88 }
89 _store->thaw_notify();
90 }
91
92 Glib::RefPtr<Gio::ListStore<T>> _store;
93 std::function<bool (const TPtr&)> _filter_callback;
94 std::vector<TPtr> _items;
95 std::vector<TPtr> _visible;
96};
97
98} // namespace Inkscape
99
100#endif // INKSCAPE_PATTERN_EDITOR_H
101
102/*
103 Local Variables:
104 mode:c++
105 c-file-style:"stroustrup"
106 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
107 indent-tabs-mode:nil
108 fill-column:99
109 End:
110*/
111// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99:
Geom::IntRect visible
Definition canvas.cpp:154
std::vector< TPtr > _items
std::function< bool(const TPtr &)> _filter_callback
const std::vector< TPtr > & get_items() const
Glib::RefPtr< Gio::ListStore< T > > _store
Glib::RefPtr< T > TPtr
void update_store(const std::vector< TPtr > &items)
std::vector< TPtr > _visible
bool assign(const std::vector< TPtr > &items)
Glib::RefPtr< Gio::ListStore< T > > get_store()
void apply_filter(bool force_refresh=false)
void set_filter(const std::function< bool(const TPtr &)> &filter_callback)
Helper class to stream background task notifications as a series of messages.
GList * items