File indexing completed on 2024-05-12 04:20:44

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