LeechCraft 0.6.70-18450-gabe19ee3b0
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 "flatitemsmodelbase.h"
12
13namespace LC::Util
14{
15 template<typename T>
17 {
18 protected:
20 public:
22
23 void SetItems (QList<T> items)
24 {
25 beginResetModel ();
26 Items_ = std::move (items);
27 endResetModel ();
28 }
29
30 template<typename U>
31 requires std::is_constructible_v<T, U&&>
32 void SetItems (QList<U> items)
33 {
34 beginResetModel ();
35 Items_.clear ();
36 Items_.reserve (items.size ());
37 for (auto&& item : items)
38 Items_ << T { std::move (item) };
39 endResetModel ();
40 }
41
42 const QList<T>& GetItems () const
43 {
44 return Items_;
45 }
46
48 {
49 return Items_;
50 }
51
52 qsizetype AddItem (const T& item)
53 {
54 const auto pos = Items_.size ();
55
56 beginInsertRows ({}, Items_.size (), Items_.size ());
57 Items_.push_back (item);
58 endInsertRows ();
59
60 return pos;
61 }
62
63 void AddItems (const QList<T>& items)
64 {
65 if (items.isEmpty ())
66 return;
67
68 beginInsertRows ({}, Items_.size (), Items_.size () + items.size () - 1);
69 Items_ += items;
70 endInsertRows ();
71 }
72
73 void SetItem (int idx, const T& item)
74 {
75 Items_ [idx] = item;
76 emit dataChanged (index (idx, 0),
77 index (idx, columnCount ({}) - 1));
78 }
79
80 template<typename F>
81 void EditItem (int idx, F&& editor)
82 {
83 std::invoke (std::forward<F> (editor), Items_ [idx]);
84 emit dataChanged (index (idx, 0),
85 index (idx, columnCount ({}) - 1));
86 }
87
88 void RemoveItem (int idx)
89 {
90 beginRemoveRows ({}, idx, idx);
91 Items_.removeAt (idx);
92 endRemoveRows ();
93 }
94
96 {
97 RemoveItem (it - Items_.begin ());
98 }
99 protected:
100 int GetItemsCount () const override
101 {
102 return Items_.size ();
103 }
104 };
105}
QModelIndex index(int row, int col, const QModelIndex &parent={}) const override
int columnCount(const QModelIndex &index={}) const override
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)
FlatItemsModelBase(QStringList headers, QObject *=nullptr)