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
|
// This file Copyright © Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include "ListModelAdapter.h"
#include "Utils.h"
namespace
{
template<typename T>
int iter_get_stamp(T const& iter)
{
return iter.gobj()->stamp;
}
template<typename T>
void iter_set_stamp(T& iter, int value)
{
iter.gobj()->stamp = value;
}
template<typename T>
int iter_get_item_id(T const& iter)
{
return GPOINTER_TO_INT(iter.gobj()->user_data);
}
template<typename T>
void iter_set_item_id(T& iter, int value)
{
iter.gobj()->user_data = GINT_TO_POINTER(value);
}
template<typename T>
void iter_clear(T& iter)
{
iter_set_stamp(iter, 0);
iter_set_item_id(iter, 0);
}
} // namespace
ListModelAdapter::ListModelAdapter(
Glib::RefPtr<Gio::ListModel> const& adaptee,
Gtk::TreeModelColumnRecord const& columns,
IdGetter id_getter,
ValueGetter value_getter)
: Glib::ObjectBase(typeid(ListModelAdapter))
, adaptee_(adaptee)
, columns_(columns)
, id_getter_(std::move(id_getter))
, value_getter_(std::move(value_getter))
{
adaptee_->signal_items_changed().connect(sigc::mem_fun(*this, &ListModelAdapter::on_adaptee_items_changed));
on_adaptee_items_changed(0, 0, adaptee_->get_n_items());
}
ListModelAdapter::TrTreeModelFlags ListModelAdapter::get_flags_vfunc() const
{
return TR_GTK_TREE_MODEL_FLAGS(ITERS_PERSIST) | TR_GTK_TREE_MODEL_FLAGS(LIST_ONLY);
}
int ListModelAdapter::get_n_columns_vfunc() const
{
return columns_.size();
}
GType ListModelAdapter::get_column_type_vfunc(int index) const
{
g_return_val_if_fail(index >= 0, G_TYPE_INVALID);
g_return_val_if_fail(index < get_n_columns_vfunc(), G_TYPE_INVALID);
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
return columns_.types()[index];
}
bool ListModelAdapter::iter_next_vfunc(iterator const& iter, iterator& iter_next) const
{
iter_clear(iter_next);
if (iter)
{
g_return_val_if_fail(iter_get_stamp(iter) == stamp_, false);
if (auto const position = find_item_position_by_id(iter_get_item_id(iter)); position.has_value())
{
if (auto const next_position = position.value() + 1; next_position < items_.size())
{
iter_set_stamp(iter_next, stamp_);
iter_set_item_id(iter_next, items_.at(next_position).id);
return true;
}
}
}
return false;
}
bool ListModelAdapter::get_iter_vfunc(Path const& path, iterator& iter) const
{
iter_clear(iter);
g_return_val_if_fail(path.size() == 1, false);
return iter_nth_root_child_vfunc(path.front(), iter);
}
bool ListModelAdapter::iter_children_vfunc(iterator const& parent, iterator& iter) const
{
iter_clear(iter);
if (parent || items_.empty())
{
return false;
}
iter_set_stamp(iter, stamp_);
iter_set_item_id(iter, items_.front().id);
return true;
}
bool ListModelAdapter::iter_parent_vfunc(iterator const& /*child*/, iterator& iter) const
{
iter_clear(iter);
return false;
}
bool ListModelAdapter::iter_nth_root_child_vfunc(int position, iterator& iter) const
{
iter_clear(iter);
g_return_val_if_fail(position >= 0, false);
if (position >= iter_n_root_children_vfunc())
{
return false;
}
iter_set_stamp(iter, stamp_);
iter_set_item_id(iter, items_.at(position).id);
return true;
}
bool ListModelAdapter::iter_has_child_vfunc(const_iterator const& /*iter*/) const
{
return false;
}
int ListModelAdapter::iter_n_root_children_vfunc() const
{
return items_.size();
}
Gtk::TreeModel::Path ListModelAdapter::get_path_vfunc(const_iterator const& iter) const
{
auto path = Path();
if (iter)
{
g_return_val_if_fail(iter_get_stamp(iter) == stamp_, path);
if (auto const position = find_item_position_by_id(iter_get_item_id(iter)); position.has_value())
{
path.push_back(position.value());
}
}
return path;
}
void ListModelAdapter::get_value_vfunc(const_iterator const& iter, int column, Glib::ValueBase& value) const
{
g_return_if_fail(column >= 0);
g_return_if_fail(column < get_n_columns_vfunc());
value.init(get_column_type_vfunc(column));
if (!iter)
{
return;
}
auto const position = find_item_position_by_id(iter_get_item_id(iter));
if (!position.has_value())
{
return;
}
auto const item = adaptee_->get_object(position.value());
if (item == nullptr)
{
return;
}
value_getter_(item, column, value);
}
std::optional<guint> ListModelAdapter::find_item_position_by_id(int item_id) const
{
auto const item_position_it = item_positions_.find(item_id);
return item_position_it != item_positions_.end() ? std::make_optional(item_position_it->second) : std::nullopt;
}
void ListModelAdapter::adjust_item_positions(guint min_position, PositionAdjustment adjustment)
{
for (auto item_it = std::next(items_.begin(), min_position); item_it != items_.end(); ++item_it)
{
if (auto const item_position_it = item_positions_.find(item_it->id); item_position_it != item_positions_.end())
{
item_position_it->second += static_cast<int>(adjustment);
}
}
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
void ListModelAdapter::on_adaptee_items_changed(guint position, guint removed, guint added)
{
g_assert(position + removed <= items_.size());
g_assert(position + added <= adaptee_->get_n_items());
for (auto i = 0U; i < removed; ++i)
{
auto const removed_position = position + removed - i - 1;
auto info = items_.at(removed_position);
items_.erase(std::next(items_.begin(), removed_position));
info.notify_tag.disconnect();
item_positions_.erase(info.id);
adjust_item_positions(removed_position, PositionAdjustment::DECREMENT);
auto path = Path();
path.push_back(removed_position);
row_deleted(path);
}
for (auto i = 0U; i < added; ++i)
{
auto const added_position = position + i;
auto const item = adaptee_->get_object(added_position);
auto const info = ItemInfo{
.id = id_getter_(item),
.notify_tag = gtr_object_signal_notify(*item.get())
.connect(sigc::mem_fun(*this, &ListModelAdapter::on_adaptee_item_changed)),
};
items_.insert(std::next(items_.begin(), added_position), info);
adjust_item_positions(added_position, PositionAdjustment::INCREMENT);
item_positions_.emplace(info.id, added_position);
auto path = Path();
path.push_back(added_position);
auto iter = iterator(this);
iter_set_stamp(iter, stamp_);
iter_set_item_id(iter, info.id);
row_inserted(path, iter);
}
}
void ListModelAdapter::on_adaptee_item_changed(Glib::RefPtr<Glib::ObjectBase const> const& item)
{
g_return_if_fail(item != nullptr);
auto const item_id = id_getter_(item);
if (auto const position = find_item_position_by_id(item_id); position.has_value())
{
auto path = Path();
path.push_back(position.value());
auto iter = iterator(this);
iter_set_stamp(iter, stamp_);
iter_set_item_id(iter, item_id);
row_changed(path, iter);
}
}
|