LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
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 
9 #include "flatitemsmodelbase.h"
10 
11 namespace LC::Util
12 {
13  FlatItemsModelBase::FlatItemsModelBase (QStringList headers, QObject *parent)
14  : QAbstractItemModel { parent }
15  , Headers_ { std::move (headers) }
16  {
17  }
18 
19  int FlatItemsModelBase::columnCount (const QModelIndex& index) const
20  {
21  return index.isValid () ? 0 : Headers_.size ();
22  }
23 
24  QVariant FlatItemsModelBase::data (const QModelIndex& index, int role) const
25  {
26  if (!index.isValid ())
27  return {};
28 
29  const auto& var = GetData (index.row (), index.column (), role);
30  return var.isValid () ? var : GlobalData_.value (role);
31  }
32 
33  QVariant FlatItemsModelBase::headerData (int section, Qt::Orientation orientation, int role) const
34  {
35  if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
36  return {};
37 
38  return Headers_.value (section);
39  }
40 
41  QModelIndex FlatItemsModelBase::index (int row, int col, const QModelIndex& parent) const
42  {
43  if (parent.isValid () ||
44  row >= GetItemsCount () ||
45  col >= Headers_.size ())
46  return {};
47 
48  return createIndex (row, col);
49  }
50 
51  QModelIndex FlatItemsModelBase::parent (const QModelIndex&) const
52  {
53  return {};
54  }
55 
56  int FlatItemsModelBase::rowCount (const QModelIndex& parent) const
57  {
58  return parent.isValid () ? 0 : GetItemsCount ();
59  }
60 
61  void FlatItemsModelBase::SetGlobalData (const QVariant& data, int role)
62  {
63  if (data.isValid ())
64  GlobalData_ [role] = data;
65  else
66  GlobalData_.remove (role);
67 
68  if (const auto rows = GetItemsCount ())
69  emit dataChanged (index (0, 0), index (rows - 1, Headers_.size () - 1), { role });
70  }
71 }
FlatItemsModelBase(QStringList headers, QObject *=nullptr)