KD Chart 2  [rev.2.6]
kdganttforwardingproxymodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2018 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
24 
25 #include <cassert>
26 #include <QStringList>
27 
28 using namespace KDGantt;
29 
31 
36  : BASE( parent )
37 {
38 }
39 
41 {
42 }
43 
45 QModelIndex ForwardingProxyModel::mapFromSource ( const QModelIndex & sourceIndex ) const
46 {
47  if ( !sourceIndex.isValid() )
48  return QModelIndex();
49  assert( sourceIndex.model() == sourceModel() );
50 
51  // Create an index that preserves the internal pointer from the source;
52  // this way KDDataConverterProxyModel preserves the structure of the source model
53  return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
54 }
55 #ifdef __GNUC__
56 #if __GNUC__ > 3
57 #define ATTRIBUTE __attribute__((__may_alias__))
58 #endif
59 #else
60 #define ATTRIBUTE
61 #endif
62 namespace {
63  // Think this is ugly? Well, it's not from me, it comes from QProxyModel
64  struct ATTRIBUTE KDPrivateModelIndex {
65  int r, c;
66  void *p;
67  const QAbstractItemModel *m;
68  };
69 }
70 
72 QModelIndex ForwardingProxyModel::mapToSource ( const QModelIndex & proxyIndex ) const
73 {
74  if ( !proxyIndex.isValid() )
75  return QModelIndex();
76  assert( proxyIndex.model() == this );
77  // So here we need to create a source index which holds that internal pointer.
78  // No way to pass it to sourceModel()->index... so we have to do the ugly way:
79  QModelIndex sourceIndex;
80  KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
81  hack->r = proxyIndex.row();
82  hack->c = proxyIndex.column();
83  hack->p = proxyIndex.internalPointer();
84  hack->m = sourceModel();
85  assert( sourceIndex.isValid() );
86  return sourceIndex;
87 }
88 
93 void ForwardingProxyModel::setSourceModel( QAbstractItemModel* model )
94 {
95  if ( sourceModel() ) sourceModel()->disconnect( this );
96  BASE::setSourceModel( model );
97 
98  if (!model) return;
99 
100  connect( model, SIGNAL(modelAboutToBeReset()), this, SLOT(sourceModelAboutToBeReset()) );
101  connect( model, SIGNAL(modelReset()), this, SLOT(sourceModelReset()) );
102  connect( model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(sourceLayoutAboutToBeChanged()) );
103  connect( model, SIGNAL(layoutChanged()), this, SLOT(sourceLayoutChanged()) );
104 
105  connect( model, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)),
106  this, SLOT(sourceDataChanged(const QModelIndex&,const QModelIndex&)) );
107 
108 
109  connect( model, SIGNAL(columnsAboutToBeInserted(const QModelIndex&, int,int)),
110  this, SLOT(sourceColumnsAboutToBeInserted(const QModelIndex&,int,int)) );
111  connect( model, SIGNAL(columnsInserted(const QModelIndex&, int,int)),
112  this, SLOT(sourceColumnsInserted(const QModelIndex&,int,int)) );
113  connect( model, SIGNAL(columnsAboutToBeRemoved(const QModelIndex&, int,int)),
114  this, SLOT(sourceColumnsAboutToBeRemoved(const QModelIndex&,int,int)) );
115  connect( model, SIGNAL(columnsRemoved(const QModelIndex&, int,int)),
116  this, SLOT(sourceColumnsRemoved(const QModelIndex&,int,int)) );
117 
118  connect( model, SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int,int)),
119  this, SLOT(sourceRowsAboutToBeInserted(const QModelIndex&,int,int)) );
120  connect( model, SIGNAL(rowsInserted(const QModelIndex&, int,int)),
121  this, SLOT(sourceRowsInserted(const QModelIndex&,int,int)) );
122  connect( model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int,int)),
123  this, SLOT(sourceRowsAboutToBeRemoved(const QModelIndex&,int,int)) );
124  connect( model, SIGNAL(rowsRemoved(const QModelIndex&, int,int)),
125  this, SLOT(sourceRowsRemoved(const QModelIndex&,int,int)) );
126 }
127 
132 {
133  // The matching signal is emitted be reset()
134 }
135 
140 {
141  //qDebug() << "ForwardingProxyModel::sourceModelReset()";
142  beginResetModel();
143  endResetModel();
144 }
145 
151 {
152  //qDebug() << "ForwardingProxyModel::sourceLayoutAboutToBeChanged()";
153  emit layoutAboutToBeChanged();
154 }
155 
160 {
161  //qDebug() << "ForwardingProxyModel::sourceLayoutChanged()";
162  beginResetModel();
163  endResetModel();
164 }
165 
169 void ForwardingProxyModel::sourceDataChanged( const QModelIndex& from, const QModelIndex& to )
170 {
171  //qDebug() << "ForwardingProxyModel::sourceDataChanged("<<from<<to<<")";
172  emit dataChanged( mapFromSource( from ), mapFromSource( to ) );
173 }
174 
178 void ForwardingProxyModel::sourceColumnsAboutToBeInserted( const QModelIndex& parentIdx,
179  int start,
180  int end )
181 {
182  beginInsertColumns( mapFromSource( parentIdx ), start, end );
183 }
184 
188 void ForwardingProxyModel::sourceColumnsInserted( const QModelIndex& parentIdx, int start, int end )
189 {
190  Q_UNUSED( parentIdx );
191  Q_UNUSED( start );
192  Q_UNUSED( end );
193  endInsertColumns();
194 }
195 
199 void ForwardingProxyModel::sourceColumnsAboutToBeRemoved( const QModelIndex& parentIdx,
200  int start,
201  int end )
202 {
203  beginRemoveColumns( mapFromSource( parentIdx ), start, end );
204 }
205 
209 void ForwardingProxyModel::sourceColumnsRemoved( const QModelIndex& parentIdx, int start, int end )
210 {
211  Q_UNUSED( parentIdx );
212  Q_UNUSED( start );
213  Q_UNUSED( end );
214  endRemoveColumns();
215 }
216 
220 void ForwardingProxyModel::sourceRowsAboutToBeInserted( const QModelIndex & parentIdx, int start, int end )
221 {
222  beginInsertRows( mapFromSource( parentIdx ), start, end );
223 }
224 
228 void ForwardingProxyModel::sourceRowsInserted( const QModelIndex& parentIdx, int start, int end )
229 {
230  Q_UNUSED( parentIdx );
231  Q_UNUSED( start );
232  Q_UNUSED( end );
233  endInsertRows();
234 }
235 
239 void ForwardingProxyModel::sourceRowsAboutToBeRemoved( const QModelIndex & parentIdx, int start, int end )
240 {
241  beginRemoveRows( mapFromSource( parentIdx ), start, end );
242 }
243 
247 void ForwardingProxyModel::sourceRowsRemoved( const QModelIndex& parentIdx, int start, int end )
248 {
249  Q_UNUSED( parentIdx );
250  Q_UNUSED( start );
251  Q_UNUSED( end );
252  endRemoveRows();
253 }
254 
256 int ForwardingProxyModel::rowCount( const QModelIndex& idx ) const
257 {
258  return sourceModel()->rowCount( mapToSource( idx ) );
259 }
260 
262 int ForwardingProxyModel::columnCount( const QModelIndex& idx ) const
263 {
264  return sourceModel()->columnCount( mapToSource( idx ) );
265 }
266 
268 QModelIndex ForwardingProxyModel::index( int row, int column, const QModelIndex& parent ) const
269 {
270  return mapFromSource( sourceModel()->index( row, column, mapToSource( parent ) ) );
271 }
272 
274 QModelIndex ForwardingProxyModel::parent( const QModelIndex& idx ) const
275 {
276  return mapFromSource( sourceModel()->parent( mapToSource( idx ) ) );
277 }
278 
280 bool ForwardingProxyModel::setData( const QModelIndex& index, const QVariant& value, int role )
281 {
282  //qDebug() << "ForwardingProxyModel::setData( " << index<<value<< role<<")";
283  return sourceModel()->setData( mapToSource( index ), value, role );
284 }
285 
286 QMimeData *ForwardingProxyModel::mimeData(const QModelIndexList &indexes) const
287 {
288  QModelIndexList source_indexes;
289  for (int i = 0; i < indexes.count(); ++i)
290  source_indexes << mapToSource(indexes.at(i));
291  return sourceModel()->mimeData(source_indexes);
292 }
293 
294 bool ForwardingProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
295 {
296  if ((row == -1) && (column == -1))
297  return sourceModel()->dropMimeData(data, action, -1, -1, mapToSource(parent));
298  int source_destination_row = -1;
299  int source_destination_column = -1;
300  QModelIndex source_parent;
301  if (row == rowCount(parent)) {
302  source_parent = mapToSource(parent);
303  source_destination_row = sourceModel()->rowCount(source_parent);
304  } else {
305  QModelIndex proxy_index = index(row, column, parent);
306  QModelIndex source_index = mapToSource(proxy_index);
307  source_destination_row = source_index.row();
308  source_destination_column = source_index.column();
309  source_parent = source_index.parent();
310  }
311  return sourceModel()->dropMimeData(data, action, source_destination_row, source_destination_column, source_parent);
312 }
313 
315 {
316  return sourceModel()->mimeTypes();
317 }
318 
320 {
321  return sourceModel()->supportedDropActions();
322 }
323 
324 #include "moc_kdganttforwardingproxymodel.cpp"
325 
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
virtual void sourceRowsRemoved(const QModelIndex &, int start, int end)
QAbstractProxyModel BASE
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
#define ATTRIBUTE
virtual void sourceColumnsAboutToBeRemoved(const QModelIndex &idx, int start, int end)
virtual void sourceColumnsAboutToBeInserted(const QModelIndex &idx, int start, int end)
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
Class only listed here to document inheritance of some KDChart classes.
int rowCount(const QModelIndex &idx=QModelIndex()) const
virtual void sourceRowsAboutToBeInserted(const QModelIndex &idx, int start, int end)
virtual void sourceColumnsRemoved(const QModelIndex &idx, int start, int end)
void setSourceModel(QAbstractItemModel *model)
virtual void sourceDataChanged(const QModelIndex &from, const QModelIndex &to)
int columnCount(const QModelIndex &idx=QModelIndex()) const
virtual void sourceRowsAboutToBeRemoved(const QModelIndex &, int start, int end)
Class only listed here to document inheritance of some KDChart classes.
virtual void sourceRowsInserted(const QModelIndex &idx, int start, int end)
QModelIndex parent(const QModelIndex &idx) const
virtual void sourceColumnsInserted(const QModelIndex &idx, int start, int end)
QMimeData * mimeData(const QModelIndexList &indexes) const

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/