LeechCraft 0.6.70-17609-g3dde4097dd
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
47 void AddItem (const T& item)
48 {
49 beginInsertRows ({}, Items_.size (), Items_.size ());
50 Items_.push_back (item);
51 endInsertRows ();
52 }
53
54 void AddItems (const QList<T>& items)
55 {
56 if (items.isEmpty ())
57 return;
58
59 beginInsertRows ({}, Items_.size (), Items_.size () + items.size () - 1);
60 Items_ += items;
61 endInsertRows ();
62 }
63
64 void SetItem (int idx, const T& item)
65 {
66 Items_ [idx] = item;
67 emit dataChanged (index (idx, 0),
68 index (idx, columnCount ({}) - 1));
69 }
70
71 template<typename F>
72 void EditItem (int idx, F&& editor)
73 {
74 std::invoke (std::forward<F> (editor), Items_ [idx]);
75 emit dataChanged (index (idx, 0),
76 index (idx, columnCount ({}) - 1));
77 }
78
79 void RemoveItem (int idx)
80 {
81 beginRemoveRows ({}, idx, idx);
82 Items_.removeAt (idx);
83 endRemoveRows ();
84 }
85 protected:
86 int GetItemsCount () const override
87 {
88 return Items_.size ();
89 }
90 };
91}
QModelIndex index(int row, int col, const QModelIndex &parent={}) const override
int columnCount(const QModelIndex &index={}) const override
FlatItemsModelBase(QStringList headers, QObject *=nullptr)
void AddItems(const QList< T > &items)
void SetItem(int idx, const T &item)
FlatItemsModelBase(QStringList headers, QObject *=nullptr)