LeechCraft  0.6.70-13729-g7046a9d2a7
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "flattenfiltermodel.h"
31 
32 namespace LC
33 {
34 namespace Util
35 {
37  : QAbstractItemModel (parent)
38  , Source_ (0)
39  {
40  }
41 
42  QModelIndex FlattenFilterModel::index (int row, int column, const QModelIndex& parent) const
43  {
44  if (parent.isValid ())
45  return QModelIndex ();
46 
47  return createIndex (row, column);
48  }
49 
50  QModelIndex FlattenFilterModel::parent (const QModelIndex&) const
51  {
52  return QModelIndex ();
53  }
54 
55  int FlattenFilterModel::rowCount (const QModelIndex& parent) const
56  {
57  return parent.isValid () ? 0 : SourceIndexes_.size ();
58  }
59 
60  int FlattenFilterModel::columnCount (const QModelIndex& parent) const
61  {
62  return parent.isValid () ? 0 : 1;
63  }
64 
65  QVariant FlattenFilterModel::data (const QModelIndex& index, int role) const
66  {
67  return SourceIndexes_.value (index.row ()).data (role);
68  }
69 
70  void FlattenFilterModel::SetSource (QAbstractItemModel *model)
71  {
72  if (Source_)
73  {
74  disconnect (Source_,
75  SIGNAL (rowsInserted (QModelIndex, int, int)),
76  this,
77  SLOT (handleRowsInserted (QModelIndex, int, int)));
78  disconnect (Source_,
79  SIGNAL (rowsAboutToBeRemoved (QModelIndex, int, int)),
80  this,
81  SLOT (handleRowsAboutRemoved (QModelIndex, int, int)));
82  disconnect (Source_,
83  SIGNAL (dataChanged (QModelIndex, QModelIndex)),
84  this,
85  SLOT (handleDataChanged (QModelIndex, QModelIndex)));
86  }
87 
88  beginResetModel ();
89  SourceIndexes_.clear ();
90  Source_ = model;
91  endResetModel ();
92  connect (Source_,
93  SIGNAL (rowsInserted (QModelIndex, int, int)),
94  this,
95  SLOT (handleRowsInserted (QModelIndex, int, int)));
96  connect (Source_,
97  SIGNAL (rowsAboutToBeRemoved (QModelIndex, int, int)),
98  this,
99  SLOT (handleRowsAboutRemoved (QModelIndex, int, int)));
100  connect (Source_,
101  SIGNAL (dataChanged (QModelIndex, QModelIndex)),
102  this,
103  SLOT (handleDataChanged (QModelIndex, QModelIndex)));
104  }
105 
106  bool FlattenFilterModel::IsIndexAccepted (const QModelIndex&) const
107  {
108  return true;
109  }
110 
111  void FlattenFilterModel::handleDataChanged (const QModelIndex& top, const QModelIndex& bottom)
112  {
113  const auto& parent = top.parent ();
114  for (int i = top.row (); i <= bottom.row (); ++i)
115  {
116  const auto& child = Source_->index (i, 0, parent);
117  const int pos = SourceIndexes_.indexOf (child);
118  if (pos < 0)
119  continue;
120 
121  const auto& ourIdx = index (pos, 0);
122  emit dataChanged (ourIdx, ourIdx);
123  }
124  }
125 
126  void FlattenFilterModel::handleRowsInserted (const QModelIndex& parent, int start, int end)
127  {
128  for (int i = start; i <= end; ++i)
129  {
130  const auto& child = Source_->index (i, 0, parent);
131  if (IsIndexAccepted (child))
132  {
133  beginInsertRows (QModelIndex (), SourceIndexes_.size (), SourceIndexes_.size ());
134  SourceIndexes_ << child;
135  endInsertRows ();
136  }
137 
138  if (int rc = Source_->rowCount (child))
139  handleRowsInserted (child, 0, rc - 1);
140  }
141  }
142 
143  void FlattenFilterModel::handleRowsAboutRemoved (const QModelIndex& parent, int start, int end)
144  {
145  for (int i = start; i <= end; ++i)
146  {
147  const auto& child = Source_->index (i, 0, parent);
148  const int pos = SourceIndexes_.indexOf (child);
149 
150  if (pos >= 0)
151  {
152  beginRemoveRows (QModelIndex (), pos, pos);
153  SourceIndexes_.removeAt (pos);
154  endRemoveRows ();
155  }
156 
157  if (int rc = Source_->rowCount (child))
158  handleRowsAboutRemoved (child, 0, rc - 1);
159  }
160  }
161 }
162 }
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented from QAbstractItemModel.
void SetSource(QAbstractItemModel *model)
Sets the source model to model.
QList< QPersistentModelIndex > SourceIndexes_
FlattenFilterModel(QObject *parent=0)
Constructs the model with the given parent.
virtual bool IsIndexAccepted(const QModelIndex &index) const
Checks whether the given index should be included in the model.
QModelIndex parent(const QModelIndex &) const
Reimplemented from QAbstractItemModel.
QModelIndex index(int, int, const QModelIndex &=QModelIndex()) const
Reimplemented from QAbstractItemModel.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Reimplemented from QAbstractItemModel.
QAbstractItemModel * Source_
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented from QAbstractItemModel.
Definition: constants.h:35