LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
flatitemsmodelbase.cpp
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
10#include <util/sll/logging.h>
11
12namespace LC::Util
13{
14 FlatItemsModelBase::FlatItemsModelBase (QStringList headers, QObject *parent)
15 : QAbstractItemModel { parent }
16 , Headers_ { std::move (headers) }
17 {
18 }
19
20 int FlatItemsModelBase::columnCount (const QModelIndex& index) const
21 {
22 return index.isValid () ? 0 : Headers_.size ();
23 }
24
25 QVariant FlatItemsModelBase::data (const QModelIndex& index, int role) const
26 {
27 if (!index.isValid ())
28 return {};
29
30 const auto& var = GetData (index.row (), index.column (), role);
31 return var.isValid () ? var : GlobalData_.value (role);
32 }
33
34 QVariant FlatItemsModelBase::headerData (int section, Qt::Orientation orientation, int role) const
35 {
36 if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
37 return {};
38
39 return Headers_.value (section);
40 }
41
42 QModelIndex FlatItemsModelBase::index (int row, int col, const QModelIndex& parent) const
43 {
44 return hasIndex (row, col, parent) ? createIndex (row, col) : QModelIndex {};
45 }
46
47 QModelIndex FlatItemsModelBase::parent (const QModelIndex&) const
48 {
49 return {};
50 }
51
52 int FlatItemsModelBase::rowCount (const QModelIndex& parent) const
53 {
54 return parent.isValid () ? 0 : GetItemsCount ();
55 }
56
57 void FlatItemsModelBase::SetGlobalData (const QVariant& data, int role)
58 {
59 if (data.isValid ())
60 GlobalData_ [role] = data;
61 else
62 GlobalData_.remove (role);
63
64 if (const auto rows = GetItemsCount ())
65 emit dataChanged (index (0, 0), index (rows - 1, Headers_.size () - 1), { role });
66 }
67
68 [[noreturn, gnu::cold, gnu::noinline]]
69 void FlatItemsModelBase::NotifyRowOutOfRange (const FlatItemsModelBase& model, int row, int count, const std::source_location& loc)
70 {
71 LogAt (loc).fatal ("row %d is out of range for %d items of %s", row, count, model.metaObject ()->className ());
72 }
73}
virtual int GetItemsCount() const =0
QVariant data(const QModelIndex &index, int role) const override
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)
int rowCount(const QModelIndex &parent={}) const override
virtual QVariant GetData(int row, int col, int role) const =0
void SetGlobalData(const QVariant &data, int role)
FlatItemsModelBase(QStringList headers, QObject *=nullptr)
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QMessageLogger LogAt(std::source_location loc)
Definition logging.cpp:14