KD Chart 2 [rev.2.4]

kdganttforwardingproxymodel.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2012 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include "kdganttforwardingproxymodel.h"
00024 
00025 #include <cassert>
00026 #include <QStringList>
00027 
00028 using namespace KDGantt;
00029 
00030 typedef QAbstractProxyModel BASE;
00031 
00035 ForwardingProxyModel::ForwardingProxyModel( QObject* parent )
00036     : BASE( parent )
00037 {
00038 }
00039 
00040 ForwardingProxyModel::~ForwardingProxyModel()
00041 {
00042 }
00043 
00045 QModelIndex ForwardingProxyModel::mapFromSource ( const QModelIndex & sourceIndex ) const
00046 {
00047     if ( !sourceIndex.isValid() )
00048         return QModelIndex();
00049     assert( sourceIndex.model() == sourceModel() );
00050 
00051     // Create an index that preserves the internal pointer from the source;
00052     // this way KDDataConverterProxyModel preserves the structure of the source model
00053     return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer() );
00054 }
00055 #ifdef __GNUC__
00056 #if __GNUC__ > 3
00057 #define ATTRIBUTE __attribute__((__may_alias__))
00058 #endif
00059 #else
00060 #define ATTRIBUTE
00061 #endif
00062 namespace {
00063     // Think this is ugly? Well, it's not from me, it comes from QProxyModel
00064     struct ATTRIBUTE KDPrivateModelIndex {
00065         int r, c;
00066         void *p;
00067         const QAbstractItemModel *m;
00068     };
00069 }
00070 
00072 QModelIndex ForwardingProxyModel::mapToSource ( const QModelIndex & proxyIndex ) const
00073 {
00074     if ( !proxyIndex.isValid() )
00075         return QModelIndex();
00076     assert( proxyIndex.model() == this );
00077     // So here we need to create a source index which holds that internal pointer.
00078     // No way to pass it to sourceModel()->index... so we have to do the ugly way:
00079     QModelIndex sourceIndex;
00080     KDPrivateModelIndex* hack = reinterpret_cast<KDPrivateModelIndex*>(&sourceIndex);
00081     hack->r = proxyIndex.row();
00082     hack->c = proxyIndex.column();
00083     hack->p = proxyIndex.internalPointer();
00084     hack->m = sourceModel();
00085     assert( sourceIndex.isValid() );
00086     return sourceIndex;
00087 }
00088 
00093 void ForwardingProxyModel::setSourceModel( QAbstractItemModel* model )
00094 {
00095     if ( sourceModel() ) sourceModel()->disconnect( this );
00096     BASE::setSourceModel( model );
00097 
00098     if(!model) return;
00099 
00100     connect( model, SIGNAL(modelAboutToBeReset()), this, SLOT(sourceModelAboutToBeReset()) );
00101     connect( model, SIGNAL(modelReset()), this, SLOT(sourceModelReset()) );
00102     connect( model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(sourceLayoutAboutToBeChanged()) );
00103     connect( model, SIGNAL(layoutChanged()), this, SLOT(sourceLayoutChanged()) );
00104 
00105     connect( model, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)),
00106              this, SLOT(sourceDataChanged(const QModelIndex&,const QModelIndex&)) );
00107 
00108 
00109     connect( model,  SIGNAL(columnsAboutToBeInserted(const QModelIndex&, int,int)),
00110              this, SLOT(sourceColumnsAboutToBeInserted(const QModelIndex&,int,int)) );
00111     connect( model,  SIGNAL(columnsInserted(const QModelIndex&, int,int)),
00112              this, SLOT(sourceColumnsInserted(const QModelIndex&,int,int)) );
00113     connect( model,  SIGNAL(columnsAboutToBeRemoved(const QModelIndex&, int,int)),
00114              this, SLOT(sourceColumnsAboutToBeRemoved(const QModelIndex&,int,int)) );
00115     connect( model,  SIGNAL(columnsRemoved(const QModelIndex&, int,int)),
00116              this, SLOT(sourceColumnsRemoved(const QModelIndex&,int,int)) );
00117 
00118     connect( model,  SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int,int)),
00119              this, SLOT(sourceRowsAboutToBeInserted(const QModelIndex&,int,int)) );
00120     connect( model,  SIGNAL(rowsInserted(const QModelIndex&, int,int)),
00121              this, SLOT(sourceRowsInserted(const QModelIndex&,int,int)) );
00122     connect( model,  SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int,int)),
00123              this, SLOT(sourceRowsAboutToBeRemoved(const QModelIndex&,int,int)) );
00124     connect( model,  SIGNAL(rowsRemoved(const QModelIndex&, int,int)),
00125              this, SLOT(sourceRowsRemoved(const QModelIndex&,int,int)) );
00126 }
00127 
00131 void ForwardingProxyModel::sourceModelAboutToBeReset()
00132 {
00133     // The matching signal is emitted be reset()
00134 }
00135 
00139 void ForwardingProxyModel::sourceModelReset()
00140 {
00141   //qDebug() << "ForwardingProxyModel::sourceModelReset()";
00142     reset();
00143 }
00144 
00149 void ForwardingProxyModel::sourceLayoutAboutToBeChanged()
00150 {
00151   //qDebug() << "ForwardingProxyModel::sourceLayoutAboutToBeChanged()";
00152     emit layoutAboutToBeChanged();
00153 }
00154 
00158 void ForwardingProxyModel::sourceLayoutChanged()
00159 {
00160   //qDebug() << "ForwardingProxyModel::sourceLayoutChanged()";
00161     reset();
00162 }
00163 
00167 void ForwardingProxyModel::sourceDataChanged( const QModelIndex& from, const QModelIndex& to )
00168 {
00169   //qDebug() << "ForwardingProxyModel::sourceDataChanged("<<from<<to<<")";
00170     emit dataChanged( mapFromSource( from ), mapFromSource( to ) );
00171 }
00172 
00176 void ForwardingProxyModel::sourceColumnsAboutToBeInserted( const QModelIndex& parentIdx,
00177                                                                     int start,
00178                                                                     int end )
00179 {
00180     beginInsertColumns( mapFromSource( parentIdx ), start, end );
00181 }
00182 
00186 void ForwardingProxyModel::sourceColumnsInserted( const QModelIndex& parentIdx, int start, int end )
00187 {
00188     Q_UNUSED( parentIdx );
00189     Q_UNUSED( start );
00190     Q_UNUSED( end );
00191     endInsertColumns();
00192 }
00193 
00197 void ForwardingProxyModel::sourceColumnsAboutToBeRemoved( const QModelIndex& parentIdx,
00198                                                                     int start,
00199                                                                     int end )
00200 {
00201     beginRemoveColumns( mapFromSource( parentIdx ), start, end );
00202 }
00203 
00207 void ForwardingProxyModel::sourceColumnsRemoved( const QModelIndex& parentIdx, int start, int end )
00208 {
00209     Q_UNUSED( parentIdx );
00210     Q_UNUSED( start );
00211     Q_UNUSED( end );
00212     endRemoveColumns();
00213 }
00214 
00218 void ForwardingProxyModel::sourceRowsAboutToBeInserted( const QModelIndex & parentIdx, int start, int end )
00219 {
00220     beginInsertRows( mapFromSource( parentIdx ), start, end );
00221 }
00222 
00226 void ForwardingProxyModel::sourceRowsInserted( const QModelIndex& parentIdx, int start, int end )
00227 {
00228     Q_UNUSED( parentIdx );
00229     Q_UNUSED( start );
00230     Q_UNUSED( end );
00231     endInsertRows();
00232 }
00233 
00237 void ForwardingProxyModel::sourceRowsAboutToBeRemoved( const QModelIndex & parentIdx, int start, int end )
00238 {
00239     beginRemoveRows( mapFromSource( parentIdx ), start, end );
00240 }
00241 
00245 void ForwardingProxyModel::sourceRowsRemoved( const QModelIndex& parentIdx, int start, int end )
00246 {
00247     Q_UNUSED( parentIdx );
00248     Q_UNUSED( start );
00249     Q_UNUSED( end );
00250     endRemoveRows();
00251 }
00252 
00254 int ForwardingProxyModel::rowCount( const QModelIndex& idx ) const
00255 {
00256     return sourceModel()->rowCount( mapToSource( idx ) );
00257 }
00258 
00260 int ForwardingProxyModel::columnCount( const QModelIndex& idx ) const
00261 {
00262     return sourceModel()->columnCount( mapToSource( idx ) );
00263 }
00264 
00266 QModelIndex ForwardingProxyModel::index( int row, int column, const QModelIndex& parent ) const
00267 {
00268     return mapFromSource( sourceModel()->index( row, column, mapToSource( parent ) ) );
00269 }
00270 
00272 QModelIndex ForwardingProxyModel::parent( const QModelIndex& idx ) const
00273 {
00274     return mapFromSource( sourceModel()->parent( mapToSource( idx ) ) );
00275 }
00276 
00278 bool ForwardingProxyModel::setData( const QModelIndex& index, const QVariant& value, int role )
00279 {
00280   //qDebug() << "ForwardingProxyModel::setData( " << index<<value<< role<<")";
00281     return sourceModel()->setData( mapToSource( index ), value, role );
00282 }
00283 
00284 QMimeData *ForwardingProxyModel::mimeData(const QModelIndexList &indexes) const
00285 {
00286     QModelIndexList source_indexes;
00287     for (int i = 0; i < indexes.count(); ++i)
00288         source_indexes << mapToSource(indexes.at(i));
00289     return sourceModel()->mimeData(source_indexes);
00290 }
00291 
00292 bool ForwardingProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
00293 {
00294     if ((row == -1) && (column == -1))
00295         return sourceModel()->dropMimeData(data, action, -1, -1, mapToSource(parent));
00296     int source_destination_row = -1;
00297     int source_destination_column = -1;
00298     QModelIndex source_parent;
00299     if (row == rowCount(parent)) {
00300         source_parent = mapToSource(parent);
00301         source_destination_row = sourceModel()->rowCount(source_parent);
00302     } else {
00303         QModelIndex proxy_index = index(row, column, parent);
00304         QModelIndex source_index = mapToSource(proxy_index);
00305         source_destination_row = source_index.row();
00306         source_destination_column = source_index.column();
00307         source_parent = source_index.parent();
00308     }
00309     return sourceModel()->dropMimeData(data, action, source_destination_row, source_destination_column, source_parent);
00310 }
00311 
00312 QStringList ForwardingProxyModel::mimeTypes() const
00313 {
00314     return sourceModel()->mimeTypes();
00315 }
00316 
00317 Qt::DropActions ForwardingProxyModel::supportedDropActions() const
00318 {
00319     return sourceModel()->supportedDropActions();
00320 }
00321         
00322 #include "moc_kdganttforwardingproxymodel.cpp"
00323 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines

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