LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
flattenfiltermodel.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 "flattenfiltermodel.h"
10 
11 namespace LC::Util
12 {
13  QModelIndex FlattenFilterModel::index (int row, int column, const QModelIndex& parent) const
14  {
15  if (parent.isValid ())
16  return QModelIndex ();
17 
18  return createIndex (row, column);
19  }
20 
21  QModelIndex FlattenFilterModel::parent (const QModelIndex&) const
22  {
23  return QModelIndex ();
24  }
25 
26  int FlattenFilterModel::rowCount (const QModelIndex& parent) const
27  {
28  return parent.isValid () ? 0 : SourceIndexes_.size ();
29  }
30 
31  int FlattenFilterModel::columnCount (const QModelIndex& parent) const
32  {
33  return parent.isValid () ? 0 : 1;
34  }
35 
36  QVariant FlattenFilterModel::data (const QModelIndex& index, int role) const
37  {
38  return SourceIndexes_.value (index.row ()).data (role);
39  }
40 
41  void FlattenFilterModel::SetSource (QAbstractItemModel *model)
42  {
43  if (Source_)
44  disconnect (Source_,
45  nullptr,
46  this,
47  nullptr);
48 
49  beginResetModel ();
50  SourceIndexes_.clear ();
51  Source_ = model;
52  endResetModel ();
53  connect (Source_,
54  &QAbstractItemModel::rowsInserted,
55  this,
56  &FlattenFilterModel::HandleRowsInserted);
57  connect (Source_,
58  &QAbstractItemModel::rowsAboutToBeRemoved,
59  this,
60  &FlattenFilterModel::HandleRowsAboutRemoved);
61  connect (Source_,
62  &QAbstractItemModel::dataChanged,
63  this,
64  &FlattenFilterModel::HandleDataChanged);
65  }
66 
67  bool FlattenFilterModel::IsIndexAccepted (const QModelIndex&) const
68  {
69  return true;
70  }
71 
72  void FlattenFilterModel::HandleDataChanged (const QModelIndex& top, const QModelIndex& bottom)
73  {
74  const auto& parent = top.parent ();
75  for (int i = top.row (); i <= bottom.row (); ++i)
76  {
77  const auto& child = Source_->index (i, 0, parent);
78  const int pos = SourceIndexes_.indexOf (child);
79  if (pos < 0)
80  continue;
81 
82  const auto& ourIdx = index (pos, 0);
83  emit dataChanged (ourIdx, ourIdx);
84  }
85  }
86 
87  void FlattenFilterModel::HandleRowsInserted (const QModelIndex& parent, int start, int end)
88  {
89  for (int i = start; i <= end; ++i)
90  {
91  const auto& child = Source_->index (i, 0, parent);
92  if (IsIndexAccepted (child))
93  {
94  beginInsertRows (QModelIndex (), SourceIndexes_.size (), SourceIndexes_.size ());
95  SourceIndexes_ << child;
96  endInsertRows ();
97  }
98 
99  if (int rc = Source_->rowCount (child))
100  HandleRowsInserted (child, 0, rc - 1);
101  }
102  }
103 
104  void FlattenFilterModel::HandleRowsAboutRemoved (const QModelIndex& parent, int start, int end)
105  {
106  for (int i = start; i <= end; ++i)
107  {
108  const auto& child = Source_->index (i, 0, parent);
109  const int pos = SourceIndexes_.indexOf (child);
110 
111  if (pos >= 0)
112  {
113  beginRemoveRows (QModelIndex (), pos, pos);
114  SourceIndexes_.removeAt (pos);
115  endRemoveRows ();
116  }
117 
118  if (int rc = Source_->rowCount (child))
119  HandleRowsAboutRemoved (child, 0, rc - 1);
120  }
121  }
122 }
LC::Util::FlattenFilterModel::Source_
QAbstractItemModel * Source_
Definition: flattenfiltermodel.h:42
LC::Util
Definition: icoreproxy.h:33
flattenfiltermodel.h
LC::Util::FlattenFilterModel::rowCount
int rowCount(const QModelIndex &parent={}) const override
Reimplemented from QAbstractItemModel.
Definition: flattenfiltermodel.cpp:32
LC::Util::FlattenFilterModel::parent
QModelIndex parent(const QModelIndex &) const override
Reimplemented from QAbstractItemModel.
Definition: flattenfiltermodel.cpp:27
LC::Util::FlattenFilterModel::IsIndexAccepted
virtual bool IsIndexAccepted(const QModelIndex &index) const
Checks whether the given index should be included in the model.
Definition: flattenfiltermodel.cpp:73
LC::Util::FlattenFilterModel::SetSource
void SetSource(QAbstractItemModel *model)
Sets the source model to model.
Definition: flattenfiltermodel.cpp:47
LC::Util::FlattenFilterModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Reimplemented from QAbstractItemModel.
Definition: flattenfiltermodel.cpp:42
LC::Util::FlattenFilterModel::columnCount
int columnCount(const QModelIndex &parent={}) const override
Reimplemented from QAbstractItemModel.
Definition: flattenfiltermodel.cpp:37
LC::Util::FlattenFilterModel::index
QModelIndex index(int, int, const QModelIndex &={}) const override
Reimplemented from QAbstractItemModel.
Definition: flattenfiltermodel.cpp:19
LC::Util::FlattenFilterModel::SourceIndexes_
QList< QPersistentModelIndex > SourceIndexes_
Definition: flattenfiltermodel.h:43