• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

QtExtraComponents

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • qtextracomponents
columnproxymodel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2012 by Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "columnproxymodel.h"
21 #include <KDebug>
22 
23 ColumnProxyModel::ColumnProxyModel(QObject* parent)
24  : QAbstractListModel(parent)
25  , m_column(0)
26  , m_sourceModel(0)
27 {}
28 
29 void ColumnProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
30 {
31  if(sourceModel==m_sourceModel) {
32  return;
33  }
34 
35  beginResetModel();
36  if(m_sourceModel) {
37  disconnect(m_sourceModel, SIGNAL(destroyed(QObject*)),
38  this, SLOT(sourceDestroyed(QObject*)));
39 
40  disconnect(m_sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
41  this, SLOT(considerDataChanged(QModelIndex,QModelIndex)));
42  disconnect(m_sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
43  this, SLOT(considerRowsAboutToBeInserted(QModelIndex,int,int)));
44  disconnect(m_sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
45  this, SLOT(considerRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
46  disconnect(m_sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
47  this, SLOT(considerRowsAboutToBeRemoved(QModelIndex,int,int)));
48  disconnect(m_sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
49  this, SLOT(considerRowsInserted(QModelIndex,int,int)));
50  disconnect(m_sourceModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
51  this, SLOT(considerRowsMoved(QModelIndex,int,int,QModelIndex,int)));
52  disconnect(m_sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
53  this, SLOT(considerRowsRemoved(QModelIndex,int,int)));
54 
55  disconnect(m_sourceModel, SIGNAL(modelAboutToBeReset()),
56  this, SIGNAL(modelAboutToBeReset()));
57  disconnect(m_sourceModel, SIGNAL(modelReset()),
58  this, SIGNAL(modelReset()));
59  disconnect(m_sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
60  this, SIGNAL(headerDataChanged(Qt::Orientation,int,int)));
61  disconnect(m_sourceModel, SIGNAL(layoutAboutToBeChanged()),
62  this, SIGNAL(layoutAboutToBeChanged()));
63  disconnect(m_sourceModel, SIGNAL(layoutChanged()),
64  this, SIGNAL(layoutChanged()));
65  }
66  m_sourceModel = sourceModel;
67  if(m_sourceModel) {
68  setRoleNames(m_sourceModel->roleNames());
69  connect(m_sourceModel, SIGNAL(destroyed(QObject*)),
70  this, SLOT(sourceDestroyed(QObject*)));
71 
72  connect(m_sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
73  this, SLOT(considerDataChanged(QModelIndex,QModelIndex)));
74  connect(m_sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
75  this, SLOT(considerRowsAboutToBeInserted(QModelIndex,int,int)));
76  connect(m_sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
77  this, SLOT(considerRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
78  connect(m_sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
79  this, SLOT(considerRowsAboutToBeRemoved(QModelIndex,int,int)));
80  connect(m_sourceModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
81  this, SLOT(considerRowsInserted(QModelIndex,int,int)));
82  connect(m_sourceModel, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
83  this, SLOT(considerRowsMoved(QModelIndex,int,int,QModelIndex,int)));
84  connect(m_sourceModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
85  this, SLOT(considerRowsRemoved(QModelIndex,int,int)));
86 
87  connect(m_sourceModel, SIGNAL(modelAboutToBeReset()),
88  this, SIGNAL(modelAboutToBeReset()));
89  connect(m_sourceModel, SIGNAL(modelReset()),
90  this, SIGNAL(modelReset()));
91  connect(m_sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
92  this, SIGNAL(headerDataChanged(Qt::Orientation,int,int)));
93  connect(m_sourceModel, SIGNAL(layoutAboutToBeChanged()),
94  this, SIGNAL(layoutAboutToBeChanged()));
95  connect(m_sourceModel, SIGNAL(layoutChanged()),
96  this, SIGNAL(layoutChanged()));
97  }
98  endResetModel();
99 }
100 
101 void ColumnProxyModel::setColumn(int col)
102 {
103  beginResetModel();
104  m_column = col;
105  endResetModel();
106 }
107 
108 int ColumnProxyModel::column() const
109 {
110  return m_column;
111 }
112 
113 QModelIndex ColumnProxyModel::rootIndex() const
114 {
115  return m_index;
116 }
117 
118 void ColumnProxyModel::setRootIndex(const QModelIndex& index)
119 {
120  if(index.isValid()) {
121  setSourceModel(const_cast<QAbstractItemModel*>(index.model()));
122  }
123  beginResetModel();
124  m_index = index;
125  endResetModel();
126 
127  emit rootIndexChanged();
128 }
129 
130 QModelIndex ColumnProxyModel::indexFromModel(QAbstractItemModel* model, int row, int column, const QModelIndex& parent)
131 {
132  return model ? model->index(row, column, parent) : QModelIndex();
133 }
134 
135 QVariant ColumnProxyModel::data(const QModelIndex& index, int role) const
136 {
137  return m_sourceModel ? m_sourceModel->data(sourceIndex(index), role) : QVariant();
138 }
139 
140 QVariant ColumnProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
141 {
142  return m_sourceModel ? m_sourceModel->headerData(section, orientation, role) : QVariant();
143 }
144 
145 QModelIndex ColumnProxyModel::sourceIndex(const QModelIndex& proxyIndex) const
146 {
147  return m_sourceModel ? m_sourceModel->index(proxyIndex.row(), m_column, m_index) : QModelIndex();
148 }
149 
150 int ColumnProxyModel::rowCount(const QModelIndex& parent) const
151 {
152  return (!m_sourceModel || parent.isValid()) ? 0 : m_sourceModel->rowCount(m_index);
153 }
154 
155 QModelIndex ColumnProxyModel::proxyIndex(const QModelIndex& sourceIndex) const
156 {
157  if(sourceIndex.parent()==m_index)
158  return index(sourceIndex.row(), sourceIndex.column(), QModelIndex());
159 
160  return QModelIndex();
161 }
162 
163 void ColumnProxyModel::sourceDestroyed(QObject* source)
164 {
165  Q_ASSERT(source==m_sourceModel);
166 
167  beginResetModel();
168  m_sourceModel = 0;
169  endResetModel();
170 }
171 
172 QModelIndex ColumnProxyModel::indexAt(int row, const QModelIndex& parent) const
173 {
174  return m_sourceModel ? m_sourceModel->index(row, m_column, parent) : QModelIndex();
175 }
176 
178 
179 void ColumnProxyModel::considerDataChanged(const QModelIndex& idxA, const QModelIndex& idxB)
180 {
181  if(idxA.parent()==m_index && idxB.parent()==m_index) {
182  emit dataChanged(proxyIndex(idxA), proxyIndex(idxB));
183  }
184 }
185 
186 void ColumnProxyModel::considerRowsAboutToBeInserted(const QModelIndex& parent, int rA, int rB)
187 {
188  if(parent==m_index) {
189  beginInsertRows(QModelIndex(), rA, rB);
190  }
191 }
192 
193 void ColumnProxyModel::considerRowsAboutToBeMoved(const QModelIndex &sourceParent, int rA, int rB, const QModelIndex& destParent, int rD)
194 {
195  if(sourceParent==m_index && destParent==m_index) {
196  beginMoveRows(QModelIndex(), rA, rB, QModelIndex(), rD);
197  } else if(sourceParent==m_index) {
198  beginRemoveRows(sourceParent, rA, rB);
199  } else if(destParent==m_index) {
200  beginInsertRows(destParent, rD, rD+(rB-rA));
201  }
202 }
203 
204 void ColumnProxyModel::considerRowsAboutToBeRemoved(const QModelIndex& parent, int rA, int rB)
205 {
206  if(parent==m_index) {
207  beginRemoveRows(QModelIndex(), rA, rB);
208  }
209 }
210 
211 void ColumnProxyModel::considerRowsInserted(const QModelIndex& parent, int , int )
212 {
213  if(parent==m_index) {
214  endInsertRows();
215  }
216 }
217 
218 void ColumnProxyModel::considerRowsMoved(const QModelIndex& sourceParent, int , int , const QModelIndex& destParent, int )
219 {
220  if(sourceParent==m_index && destParent==m_index) {
221  endMoveRows();
222  } else if(sourceParent==m_index) {
223  endRemoveRows();
224  } else if(destParent==m_index) {
225  endInsertRows();
226  }
227 }
228 
229 void ColumnProxyModel::considerRowsRemoved(const QModelIndex& parent, int , int )
230 {
231  if(parent==m_index) {
232  endInsertRows();
233  }
234 }
235 
236 #include "columnproxymodel.moc"
QModelIndex
QAbstractItemModel::rowCount
virtual int rowCount(const QModelIndex &parent) const =0
QAbstractItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
QAbstractItemModel::layoutChanged
void layoutChanged()
QAbstractItemModel::setRoleNames
void setRoleNames(const QHash< int, QByteArray > &roleNames)
QAbstractItemModel::beginMoveRows
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
ColumnProxyModel::setColumn
void setColumn(int col)
Definition: columnproxymodel.cpp:101
ColumnProxyModel::setSourceModel
void setSourceModel(QAbstractItemModel *sourceModel)
Definition: columnproxymodel.cpp:29
QAbstractItemModel::roleNames
const QHash< int, QByteArray > & roleNames() const
columnproxymodel.h
QAbstractItemModel::modelAboutToBeReset
void modelAboutToBeReset()
QAbstractItemModel::modelReset
void modelReset()
ColumnProxyModel::setRootIndex
void setRootIndex(const QModelIndex &idx)
Definition: columnproxymodel.cpp:118
QAbstractItemModel::rowsAboutToBeRemoved
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
ColumnProxyModel::rootIndex
QModelIndex rootIndex() const
QAbstractItemModel::beginResetModel
void beginResetModel()
QAbstractItemModel::layoutAboutToBeChanged
void layoutAboutToBeChanged()
QModelIndex::isValid
bool isValid() const
QAbstractItemModel::rowsMoved
void rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
QAbstractItemModel::rowsAboutToBeInserted
void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
QAbstractItemModel::dataChanged
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QAbstractItemModel::endInsertRows
void endInsertRows()
QObject
QAbstractListModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
QAbstractListModel
QAbstractItemModel::beginRemoveRows
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex::row
int row() const
QAbstractItemModel::endMoveRows
void endMoveRows()
ColumnProxyModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: columnproxymodel.cpp:150
ColumnProxyModel::rootIndexChanged
void rootIndexChanged()
QAbstractItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const =0
QModelIndex::parent
QModelIndex parent() const
QAbstractItemModel::rowsRemoved
void rowsRemoved(const QModelIndex &parent, int start, int end)
QAbstractItemModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
ColumnProxyModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: columnproxymodel.cpp:140
QAbstractItemModel::beginInsertRows
void beginInsertRows(const QModelIndex &parent, int first, int last)
ColumnProxyModel::indexAt
Q_SCRIPTABLE QModelIndex indexAt(int row, const QModelIndex &parent=QModelIndex()) const
Definition: columnproxymodel.cpp:172
ColumnProxyModel::indexFromModel
static Q_SCRIPTABLE QModelIndex indexFromModel(QAbstractItemModel *model, int row, int column=0, const QModelIndex &parent=QModelIndex())
Definition: columnproxymodel.cpp:130
QModelIndex::model
const QAbstractItemModel * model() const
ColumnProxyModel::column
int column() const
QAbstractItemModel::headerDataChanged
void headerDataChanged(Qt::Orientation orientation, int first, int last)
QModelIndex::column
int column() const
QAbstractItemModel
ColumnProxyModel::data
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: columnproxymodel.cpp:135
QAbstractItemModel::rowsAboutToBeMoved
void rowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
QAbstractItemModel::endRemoveRows
void endRemoveRows()
QAbstractItemModel::rowsInserted
void rowsInserted(const QModelIndex &parent, int start, int end)
QAbstractItemModel::endResetModel
void endResetModel()
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::destroyed
void destroyed(QObject *obj)
ColumnProxyModel::ColumnProxyModel
ColumnProxyModel(QObject *parent=0)
Definition: columnproxymodel.cpp:23
ColumnProxyModel::sourceModel
QAbstractItemModel * sourceModel() const
Definition: columnproxymodel.h:38
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:49 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

QtExtraComponents

Skip menu "QtExtraComponents"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal