KD Chart 2
[rev.2.5]
|
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 "kdganttproxymodel.h" 00024 #include "kdganttproxymodel_p.h" 00025 00026 00027 using namespace KDGantt; 00028 00029 typedef ForwardingProxyModel BASE; 00030 00031 ProxyModel::Private::Private( ProxyModel* _q ) 00032 #if 0 00033 : calendarMode( false ) 00034 #endif 00035 { 00036 Q_UNUSED( _q ); // for now 00037 00038 columnMap[Qt::DisplayRole] = 0; 00039 columnMap[ItemTypeRole] = 1; 00040 columnMap[StartTimeRole] = 2; 00041 columnMap[EndTimeRole] = 3; 00042 columnMap[TaskCompletionRole] = 4; 00043 columnMap[LegendRole] = 5; 00044 00045 roleMap[Qt::DisplayRole] = Qt::DisplayRole; 00046 roleMap[ItemTypeRole] = Qt::DisplayRole; 00047 roleMap[StartTimeRole] = StartTimeRole; 00048 roleMap[EndTimeRole] = EndTimeRole; 00049 roleMap[TaskCompletionRole] = Qt::DisplayRole; 00050 roleMap[LegendRole] = Qt::DisplayRole; 00051 } 00052 00053 ProxyModel::ProxyModel( QObject* parent ) 00054 : BASE( parent ), _d( new Private( this ) ) 00055 { 00056 init(); 00057 } 00058 00059 ProxyModel::~ProxyModel() 00060 { 00061 delete _d; _d = 0; 00062 } 00063 00064 #define d d_func() 00065 00066 void ProxyModel::init() 00067 { 00068 } 00069 00070 QModelIndex ProxyModel::mapFromSource( const QModelIndex& sourceIdx ) const 00071 { 00072 #if 0 00073 if( sourceIdx.isValid() ) { 00074 if ( calendarMode() ) { 00075 const QAbstractItemModel* model = sourceIdx.model(); 00076 if ( model->hasChildren( sourceIdx ) ) { 00077 return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent())); 00078 } else { 00079 // Map children to columns 00080 return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent())) 00081 .child( 0, sourceIdx.column() ); 00082 } 00083 } 00084 return BASE::mapFromSource( sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent())); 00085 } 00086 else return QModelIndex(); 00087 #else 00088 return BASE::mapFromSource( sourceIdx.model()?sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()):QModelIndex()); 00089 #endif 00090 } 00091 00092 QModelIndex ProxyModel::mapToSource( const QModelIndex& proxyIdx ) const 00093 { 00094 #if 0 00095 if( proxyIdx.isValid() ) { 00096 if ( calendarMode() && proxyIdx.column() > 0 ) { 00097 return BASE::mapToSource( proxyIdx.model()->index( proxyIdx.column(), 0, proxyIdx ) ); 00098 } 00099 return BASE::mapToSource( proxyIdx ); 00100 } 00101 else return QModelIndex(); 00102 #else 00103 return BASE::mapToSource( proxyIdx ); 00104 #endif 00105 } 00106 00107 void ProxyModel::setColumn( int ganttrole, int col ) 00108 { 00109 d->columnMap[ganttrole] = col; 00110 } 00111 00112 int ProxyModel::column( int ganttrole ) const 00113 { 00114 return d->columnMap[ganttrole]; 00115 } 00116 00117 void ProxyModel::setRole( int ganttrole, int role ) 00118 { 00119 d->roleMap[ganttrole] = role; 00120 } 00121 00122 int ProxyModel::role( int ganttrole ) const 00123 { 00124 return d->roleMap[ganttrole]; 00125 } 00126 00127 #if 0 00128 void ProxyModel::setCalendarMode( bool enable ) 00129 { 00130 if ( d->calendarMode != enable ) { 00131 d->calendarMode = enable; 00132 reset(); 00133 } 00134 } 00135 00136 bool ProxyModel::calendarMode() const 00137 { 00138 return d->calendarMode; 00139 } 00140 #endif 00141 00142 int ProxyModel::rowCount( const QModelIndex& proxyIndex ) const 00143 { 00144 // TODO 00145 return BASE::rowCount( proxyIndex ); 00146 } 00147 00148 int ProxyModel::columnCount( const QModelIndex& proxyIndex ) const 00149 { 00150 return qMin( sourceModel()->columnCount( mapToSource( proxyIndex ) ), 1 ); 00151 } 00152 00153 QVariant ProxyModel::data( const QModelIndex& proxyIdx, int role ) const 00154 { 00155 int srole = role; 00156 int scol = proxyIdx.column(); 00157 QHash<int, int>::const_iterator it = d->roleMap.find( role ); 00158 if ( it != d->roleMap.end() ) srole = *it; 00159 it = d->columnMap.find( role ); 00160 if ( it != d->columnMap.end() ) scol = *it; 00161 00162 #if 0 00163 qDebug() << "mapping "<<static_cast<ItemDataRole>(role)<<", "<<proxyIdx.column() 00164 << " => " << static_cast<ItemDataRole>(srole)<<", " << scol 00165 << "value=" 00166 << sourceModel()->data( sourceModel()->index( proxyIdx.row(), scol, 00167 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.constFind( role ); 00179 if ( it != d->roleMap.constEnd() ) srole = *it; 00180 it = d->columnMap.constFind( role ); 00181 if ( it != d->columnMap.constEnd() ) 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"