LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
flattenfiltermodel.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 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 LeechCraft
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  SourceIndexes_.clear ();
89  Source_ = model;
90  reset ();
91  connect (Source_,
92  SIGNAL (rowsInserted (QModelIndex, int, int)),
93  this,
94  SLOT (handleRowsInserted (QModelIndex, int, int)));
95  connect (Source_,
96  SIGNAL (rowsAboutToBeRemoved (QModelIndex, int, int)),
97  this,
98  SLOT (handleRowsAboutRemoved (QModelIndex, int, int)));
99  connect (Source_,
100  SIGNAL (dataChanged (QModelIndex, QModelIndex)),
101  this,
102  SLOT (handleDataChanged (QModelIndex, QModelIndex)));
103  }
104 
105  bool FlattenFilterModel::IsIndexAccepted (const QModelIndex&) const
106  {
107  return true;
108  }
109 
110  void FlattenFilterModel::handleDataChanged (const QModelIndex& top, const QModelIndex& bottom)
111  {
112  const auto& parent = top.parent ();
113  for (int i = top.row (); i <= bottom.row (); ++i)
114  {
115  const auto& child = Source_->index (i, 0, parent);
116  const int pos = SourceIndexes_.indexOf (child);
117  if (pos < 0)
118  continue;
119 
120  const auto& ourIdx = index (pos, 0);
121  emit dataChanged (ourIdx, ourIdx);
122  }
123  }
124 
125  void FlattenFilterModel::handleRowsInserted (const QModelIndex& parent, int start, int end)
126  {
127  for (int i = start; i <= end; ++i)
128  {
129  const auto& child = Source_->index (i, 0, parent);
130  if (IsIndexAccepted (child))
131  {
132  beginInsertRows (QModelIndex (), SourceIndexes_.size (), SourceIndexes_.size ());
133  SourceIndexes_ << child;
134  endInsertRows ();
135  }
136 
137  if (int rc = Source_->rowCount (child))
138  handleRowsInserted (child, 0, rc - 1);
139  }
140  }
141 
142  void FlattenFilterModel::handleRowsAboutRemoved (const QModelIndex& parent, int start, int end)
143  {
144  for (int i = start; i <= end; ++i)
145  {
146  const auto& child = Source_->index (i, 0, parent);
147  const int pos = SourceIndexes_.indexOf (child);
148 
149  if (pos >= 0)
150  {
151  beginRemoveRows (QModelIndex (), pos, pos);
152  SourceIndexes_.removeAt (pos);
153  endRemoveRows ();
154  }
155 
156  if (int rc = Source_->rowCount (child))
157  handleRowsAboutRemoved (child, 0, rc - 1);
158  }
159  }
160 }
161 }
QList< QPersistentModelIndex > SourceIndexes_
QModelIndex index(int, int, const QModelIndex &=QModelIndex()) const
int rowCount(const QModelIndex &parent=QModelIndex()) const
void SetSource(QAbstractItemModel *)
int columnCount(const QModelIndex &parent=QModelIndex()) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
QModelIndex parent(const QModelIndex &) const
virtual bool IsIndexAccepted(const QModelIndex &) const