File indexing completed on 2024-05-12 15:54:28

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "kganttproxymodel.h"
0021 #include "kganttproxymodel_p.h"
0022 
0023 
0024 using namespace KGantt;
0025 
0026 typedef ForwardingProxyModel BASE;
0027 
0028 ProxyModel::Private::Private( ProxyModel* _q )
0029 #if 0
0030     : calendarMode( false )
0031 #endif
0032 {
0033     Q_UNUSED( _q ); // for now
0034 
0035     columnMap[Qt::DisplayRole]    = 0;
0036     columnMap[ItemTypeRole]       = 1;
0037     columnMap[StartTimeRole]      = 2;
0038     columnMap[EndTimeRole]        = 3;
0039     columnMap[TaskCompletionRole] = 4;
0040     columnMap[LegendRole]         = 5;
0041 
0042     roleMap[Qt::DisplayRole]    = Qt::DisplayRole;
0043     roleMap[ItemTypeRole]       = Qt::DisplayRole;
0044     roleMap[StartTimeRole]      = StartTimeRole;
0045     roleMap[EndTimeRole]        = EndTimeRole;
0046     roleMap[TaskCompletionRole] = Qt::DisplayRole;
0047     roleMap[LegendRole]         = Qt::DisplayRole;
0048 }
0049 
0050 ProxyModel::ProxyModel( QObject* parent )
0051     : BASE( parent ), _d( new Private( this ) )
0052 {
0053     init();
0054 }
0055 
0056 ProxyModel::~ProxyModel()
0057 {
0058     delete _d; _d = nullptr;
0059 }
0060 
0061 #define d d_func()
0062 
0063 void ProxyModel::init()
0064 {
0065 }
0066 
0067 QModelIndex ProxyModel::mapFromSource( const QModelIndex& sourceIdx ) const
0068 {
0069 #if 0
0070     if ( sourceIdx.isValid() ) {
0071         if ( calendarMode() ) {
0072             const QAbstractItemModel* model = sourceIdx.model();
0073             if ( model->hasChildren( sourceIdx ) ) {
0074                 return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()));
0075             } else {
0076                 // Map children to columns
0077                 return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()))
0078                     .child( 0, sourceIdx.column() );
0079             }
0080         }
0081         return BASE::mapFromSource( sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()));
0082     }
0083     else return QModelIndex();
0084 #else
0085     // danders:
0086     // this was:
0087     // return BASE::mapFromSource( sourceIdx.model()?sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()):QModelIndex());
0088     // with column hardcoded to 0.
0089     // Please notify if this *really* needs to be hardcoded, afaics it makes it impossible to get at anything else
0090     // than column 0 from e.g. an item delegate.
0091     return BASE::mapFromSource( sourceIdx.model()?sourceIdx.model()->index( sourceIdx.row(),sourceIdx.column(),sourceIdx.parent()):QModelIndex());
0092 #endif
0093 }
0094 
0095 QModelIndex ProxyModel::mapToSource( const QModelIndex& proxyIdx ) const
0096 {
0097 #if 0
0098     if ( proxyIdx.isValid() ) {
0099         if ( calendarMode() && proxyIdx.column() > 0 ) {
0100             return BASE::mapToSource( proxyIdx.model()->index( proxyIdx.column(), 0, proxyIdx ) );
0101         }
0102         return BASE::mapToSource( proxyIdx );
0103     }
0104     else return QModelIndex();
0105 #else
0106     return BASE::mapToSource( proxyIdx );
0107 #endif
0108 }
0109 
0110 void ProxyModel::setColumn( int ganttrole, int col )
0111 {
0112     d->columnMap[ganttrole] = col;
0113 }
0114 
0115 void ProxyModel::removeColumn( int ganttrole )
0116 {
0117     d->columnMap.remove( ganttrole );
0118 }
0119 
0120 int ProxyModel::column( int ganttrole ) const
0121 {
0122     return d->columnMap[ganttrole];
0123 }
0124 
0125 void ProxyModel::removeRole( int ganttrole )
0126 {
0127     d->roleMap.remove( ganttrole );
0128 }
0129 
0130 void ProxyModel::setRole( int ganttrole, int role )
0131 {
0132     d->roleMap[ganttrole] = role;
0133 }
0134 
0135 int ProxyModel::role( int ganttrole ) const
0136 {
0137     return d->roleMap[ganttrole];
0138 }
0139 
0140 #if 0
0141 void ProxyModel::setCalendarMode( bool enable )
0142 {
0143     if ( d->calendarMode != enable ) {
0144         d->calendarMode = enable;
0145         reset();
0146     }
0147 }
0148 
0149 bool ProxyModel::calendarMode() const
0150 {
0151     return d->calendarMode;
0152 }
0153 #endif
0154 
0155 int ProxyModel::rowCount( const QModelIndex& proxyIndex ) const
0156 {
0157     // TODO
0158     return BASE::rowCount( proxyIndex );
0159 }
0160 
0161 int ProxyModel::columnCount( const QModelIndex& proxyIndex ) const
0162 {
0163     return qMin( sourceModel()->columnCount( mapToSource( proxyIndex ) ), 1 );
0164 }
0165 
0166 QVariant ProxyModel::data( const QModelIndex& proxyIdx, int role ) const
0167 {
0168     int srole = role;
0169     int scol  = proxyIdx.column();
0170     QHash<int, int>::const_iterator it = d->roleMap.find( role );
0171     if ( it != d->roleMap.end() ) srole = *it;
0172     it = d->columnMap.find( role );
0173     if ( it != d->columnMap.end() ) scol = *it;
0174 
0175 #if 0
0176     qDebug() << "mapping "<<static_cast<ItemDataRole>(role)<<", "<<proxyIdx.column()
0177              << " => " << static_cast<ItemDataRole>(srole)<<", " << scol
0178              << "value="
0179              << sourceModel()->data( sourceModel()->index( proxyIdx.row(), scol,
0180                                                            mapToSource( proxyIdx.parent() ) ), srole );
0181 #endif
0182 
0183     const QAbstractItemModel* model = sourceModel();
0184     return model->data( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), srole );
0185 }
0186 
0187 bool ProxyModel::setData( const QModelIndex& proxyIdx, const QVariant& value, int role )
0188 {
0189     int srole = role;
0190     int scol  = proxyIdx.column();
0191     QHash<int, int>::const_iterator it = d->roleMap.constFind( role );
0192     if ( it != d->roleMap.constEnd() ) srole = *it;
0193     it = d->columnMap.constFind( role );
0194     if ( it != d->columnMap.constEnd() ) scol = *it;
0195 
0196     QAbstractItemModel* model = sourceModel();
0197     return model->setData( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), value, srole );
0198 }
0199 
0200 #include "moc_kganttproxymodel.cpp"