LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
flatitemsmodeltypedbase.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <source_location>
12#include "flatitemsmodelbase.h"
13#include "modelsanitizer.h"
14
15namespace LC::Util
16{
17 template<typename T>
19 {
20 protected:
22 public:
23 explicit FlatItemsModelTypedBase (QStringList headers, QObject *parent = nullptr)
24 : FlatItemsModelBase { std::move (headers), parent }
25 {
27 }
28
29 void SetItems (QList<T> items)
30 {
31 beginResetModel ();
32 Items_ = std::move (items);
33 endResetModel ();
34 }
35
36 template<typename U>
37 requires std::is_constructible_v<T, U&&>
38 void SetItems (QList<U> items)
39 {
40 beginResetModel ();
41 Items_.clear ();
42 Items_.reserve (items.size ());
43 for (auto&& item : items)
44 Items_ << T { std::move (item) };
45 endResetModel ();
46 }
47
48 const QList<T>& GetItems () const
49 {
50 return Items_;
51 }
52
54 {
55 return Items_;
56 }
57
58 qsizetype AddItem (const T& item)
59 {
60 const auto pos = Items_.size ();
61
62 beginInsertRows ({}, Items_.size (), Items_.size ());
63 Items_.push_back (item);
64 endInsertRows ();
65
66 return pos;
67 }
68
69 void AddItems (const QList<T>& items)
70 {
71 if (items.isEmpty ())
72 return;
73
74 beginInsertRows ({}, Items_.size (), Items_.size () + items.size () - 1);
75 Items_ += items;
76 endInsertRows ();
77 }
78
79 void SetItem (int idx, const T& item)
80 {
81 ItemAt (idx) = item;
82 emit dataChanged (index (idx, 0),
83 index (idx, columnCount ({}) - 1));
84 }
85
86 template<typename F>
87 void EditItem (int idx, F&& editor)
88 {
89 std::invoke (std::forward<F> (editor), ItemAt (idx));
90 emit dataChanged (index (idx, 0),
91 index (idx, columnCount ({}) - 1));
92 }
93
94 void RemoveItem (int idx)
95 {
96 CheckRow (idx, std::source_location::current ());
97 beginRemoveRows ({}, idx, idx);
98 Items_.removeAt (idx);
99 endRemoveRows ();
100 }
101
103 {
104 RemoveItem (it - Items_.begin ());
105 }
106 protected:
107 int GetItemsCount () const override
108 {
109 return Items_.size ();
110 }
111
112 auto& ItemAt (this auto&& self, int row, const std::source_location& loc = std::source_location::current ())
113 {
114 self.CheckRow (row, loc);
115 return self.Items_ [row];
116 }
117 private:
118 void CheckRow (int row, const std::source_location& loc) const
119 {
120 if (row < 0 || row >= Items_.size ())
121 NotifyRowOutOfRange (*this, row, Items_.size (), loc);
122 }
123 };
124}
QModelIndex index(int row, int col, const QModelIndex &parent={}) const override
int columnCount(const QModelIndex &index={}) const override
QModelIndex parent(const QModelIndex &) const override
static void NotifyRowOutOfRange(const FlatItemsModelBase &model, int row, int count, const std::source_location &loc)
FlatItemsModelBase(QStringList headers, QObject *=nullptr)
void RemoveItem(QList< T >::const_iterator it)
void AddItems(const QList< T > &items)
void SetItem(int idx, const T &item)
auto & ItemAt(this auto &&self, int row, const std::source_location &loc=std::source_location::current())
FlatItemsModelTypedBase(QStringList headers, QObject *parent=nullptr)
void InstallModelSanitizer(QAbstractItemModel &model)