kdganttproxymodel.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002  ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB.  All rights reserved.
00003  **
00004  ** This file is part of the KD Gantt library.
00005  **
00006  ** This file may be distributed and/or modified under the terms of the
00007  ** GNU General Public License version 2 as published by the Free Software
00008  ** Foundation and appearing in the file LICENSE.GPL included in the
00009  ** packaging of this file.
00010  **
00011  ** Licensees holding valid commercial KD Gantt licenses may use this file in
00012  ** accordance with the KD Gantt Commercial License Agreement provided with
00013  ** the Software.
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  ** See http://www.kdab.net/kdgantt for
00019  **   information about KD Gantt Commercial License Agreements.
00020  **
00021  ** Contact info@kdab.net if any conditions of this
00022  ** licensing are not clear to you.
00023  **
00024  **********************************************************************/
00025 #include "kdganttproxymodel.h"
00026 #include "kdganttproxymodel_p.h"
00027 
00028 
00029 using namespace KDGantt;
00030 
00031 typedef ForwardingProxyModel BASE;
00032 
00033 ProxyModel::Private::Private( ProxyModel* _q )
00034 #if 0
00035     : calendarMode( false )
00036 #endif
00037 {
00038     Q_UNUSED( _q ); // for now
00039 
00040     columnMap[Qt::DisplayRole]    = 0;
00041     columnMap[ItemTypeRole]       = 1;
00042     columnMap[StartTimeRole]      = 2;
00043     columnMap[EndTimeRole]        = 3;
00044     columnMap[TaskCompletionRole] = 4;
00045     columnMap[LegendRole]         = 5;
00046 
00047     roleMap[Qt::DisplayRole]    = Qt::DisplayRole;
00048     roleMap[ItemTypeRole]       = Qt::DisplayRole;
00049     roleMap[StartTimeRole]      = Qt::DisplayRole;
00050     roleMap[EndTimeRole]        = Qt::DisplayRole;
00051     roleMap[TaskCompletionRole] = Qt::DisplayRole;
00052     roleMap[LegendRole]         = Qt::DisplayRole;
00053 }
00054 
00055 ProxyModel::ProxyModel( QObject* parent )
00056     : BASE( parent ), _d( new Private( this ) )
00057 {
00058     init();
00059 }
00060 
00061 ProxyModel::~ProxyModel()
00062 {
00063     delete _d; _d = 0;
00064 }
00065 
00066 #define d d_func()
00067 
00068 void ProxyModel::init()
00069 {
00070 }
00071 
00072 QModelIndex ProxyModel::mapFromSource( const QModelIndex& sourceIdx ) const
00073 {
00074 #if 0
00075     if( sourceIdx.isValid() ) {
00076         if ( calendarMode() ) {
00077             const QAbstractItemModel* model = sourceIdx.model();
00078             if ( model->hasChildren( sourceIdx ) ) {
00079                 return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()));
00080             } else {
00081                 // Map children to columns
00082                 return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()))
00083                     .child( 0, sourceIdx.column() );
00084             }
00085         }
00086         return BASE::mapFromSource( sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()));
00087     }
00088     else return QModelIndex();
00089 #else
00090     return BASE::mapFromSource( sourceIdx.model()?sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()):QModelIndex());
00091 #endif
00092 }
00093 
00094 QModelIndex ProxyModel::mapToSource( const QModelIndex& proxyIdx ) const
00095 {
00096 #if 0
00097     if( proxyIdx.isValid() ) {
00098         if ( calendarMode() && proxyIdx.column() > 0 ) {
00099             return BASE::mapToSource( proxyIdx.model()->index( proxyIdx.column(), 0, proxyIdx ) );
00100         }
00101         return BASE::mapToSource( proxyIdx );
00102     }
00103     else return QModelIndex();
00104 #else
00105     return BASE::mapToSource( proxyIdx );
00106 #endif
00107 }
00108 
00109 void ProxyModel::setColumn( int ganttrole, int col )
00110 {
00111     d->columnMap[ganttrole] = col;
00112 }
00113 
00114 int ProxyModel::column( int ganttrole ) const
00115 {
00116     return d->columnMap[ganttrole];
00117 }
00118 
00119 void ProxyModel::setRole( int ganttrole, int role )
00120 {
00121     d->roleMap[ganttrole] = role;
00122 }
00123 
00124 int ProxyModel::role( int ganttrole ) const
00125 {
00126     return d->roleMap[ganttrole];
00127 }
00128 
00129 #if 0
00130 void ProxyModel::setCalendarMode( bool enable )
00131 {
00132     if ( d->calendarMode != enable ) {
00133         d->calendarMode = enable;
00134         reset();
00135     }
00136 }
00137 
00138 bool ProxyModel::calendarMode() const
00139 {
00140     return d->calendarMode;
00141 }
00142 #endif
00143 
00144 int ProxyModel::rowCount( const QModelIndex& proxyIndex ) const
00145 {
00146     // TODO
00147     return BASE::rowCount( proxyIndex );
00148 }
00149 
00150 int ProxyModel::columnCount( const QModelIndex& proxyIndex ) const
00151 {
00152     return qMin( sourceModel()->columnCount( mapToSource( proxyIndex ) ), 1 );
00153 }
00154 
00155 QVariant ProxyModel::data( const QModelIndex& proxyIdx, int role ) const
00156 {
00157     int srole = role;
00158     int scol  = proxyIdx.column();
00159     QHash<int, int>::const_iterator it = d->roleMap.find( role );
00160     if ( it != d->roleMap.end() ) srole = *it;
00161     it = d->columnMap.find( role );
00162     if ( it != d->columnMap.end() ) scol = *it;
00163 
00164 #if 0
00165     qDebug() << "mapping role"<<static_cast<ItemDataRole>(role)<<"to"<<static_cast<ItemDataRole>(srole);
00166     qDebug() << "mapping column"<<proxyIdx.column()<<"to"<<scol
00167              << "value="<<sourceModel()->data( sourceModel()->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), srole );
00168 #endif
00169 
00170     const QAbstractItemModel* model = sourceModel();
00171     return model->data( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), srole );
00172 }
00173 
00174 bool ProxyModel::setData( const QModelIndex& proxyIdx, const QVariant& value, int role )
00175 {
00176     int srole = role;
00177     int scol  = proxyIdx.column();
00178     QHash<int, int>::const_iterator it = d->roleMap.find( role );
00179     if ( it != d->roleMap.end() ) srole = *it;
00180     it = d->columnMap.find( role );
00181     if ( it != d->columnMap.end() ) scol = *it;
00182 
00183     QAbstractItemModel* model = sourceModel();
00184     return model->setData( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), value, srole );
00185 }
00186 
00187 #include "moc_kdganttproxymodel.cpp"

Generated on Thu Mar 4 23:19:13 2010 for KD Chart 2 by  doxygen 1.5.4