1 /****************************************************************************
2 ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB. All rights reserved.
4 ** This file is part of the KD Gantt library.
6 ** This file may be distributed and/or modified under the terms of the
7 ** GNU General Public License version 2 as published by the Free Software
8 ** Foundation and appearing in the file LICENSE.GPL included in the
9 ** packaging of this file.
11 ** Licensees holding valid commercial KD Gantt licenses may use this file in
12 ** accordance with the KD Gantt Commercial License Agreement provided with
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 ** See http://www.kdab.net/kdgantt for
19 ** information about KD Gantt Commercial License Agreements.
21 ** Contact info@kdab.net if any conditions of this
22 ** licensing are not clear to you.
24 **********************************************************************/
25 #include "kdganttproxymodel.h"
26 #include "kdganttproxymodel_p.h"
29 using namespace KDGantt
;
31 typedef ForwardingProxyModel BASE
;
33 ProxyModel::Private::Private( ProxyModel
* _q
)
35 : calendarMode( false )
38 Q_UNUSED( _q
); // for now
40 columnMap
[Qt::DisplayRole
] = 0;
41 columnMap
[ItemTypeRole
] = 1;
42 columnMap
[StartTimeRole
] = 2;
43 columnMap
[EndTimeRole
] = 3;
44 columnMap
[TaskCompletionRole
] = 4;
45 columnMap
[LegendRole
] = 5;
47 roleMap
[Qt::DisplayRole
] = Qt::DisplayRole
;
48 roleMap
[ItemTypeRole
] = Qt::DisplayRole
;
49 roleMap
[StartTimeRole
] = Qt::DisplayRole
;
50 roleMap
[EndTimeRole
] = Qt::DisplayRole
;
51 roleMap
[TaskCompletionRole
] = Qt::DisplayRole
;
52 roleMap
[LegendRole
] = Qt::DisplayRole
;
55 ProxyModel::ProxyModel( QObject
* parent
)
56 : BASE( parent
), _d( new Private( this ) )
61 ProxyModel::~ProxyModel()
68 void ProxyModel::init()
72 QModelIndex
ProxyModel::mapFromSource( const QModelIndex
& sourceIdx
) const
75 if( sourceIdx
.isValid() ) {
76 if ( calendarMode() ) {
77 const QAbstractItemModel
* model
= sourceIdx
.model();
78 if ( model
->hasChildren( sourceIdx
) ) {
79 return BASE::mapFromSource( model
->index( sourceIdx
.row(),0,sourceIdx
.parent()));
81 // Map children to columns
82 return BASE::mapFromSource( model
->index( sourceIdx
.row(),0,sourceIdx
.parent()))
83 .child( 0, sourceIdx
.column() );
86 return BASE::mapFromSource( sourceIdx
.model()->index( sourceIdx
.row(),0,sourceIdx
.parent()));
88 else return QModelIndex();
90 return BASE::mapFromSource( sourceIdx
.model()?sourceIdx
.model()->index( sourceIdx
.row(),0,sourceIdx
.parent()):QModelIndex());
94 QModelIndex
ProxyModel::mapToSource( const QModelIndex
& proxyIdx
) const
97 if( proxyIdx
.isValid() ) {
98 if ( calendarMode() && proxyIdx
.column() > 0 ) {
99 return BASE::mapToSource( proxyIdx
.model()->index( proxyIdx
.column(), 0, proxyIdx
) );
101 return BASE::mapToSource( proxyIdx
);
103 else return QModelIndex();
105 return BASE::mapToSource( proxyIdx
);
109 void ProxyModel::setColumn( int ganttrole
, int col
)
111 d
->columnMap
[ganttrole
] = col
;
114 int ProxyModel::column( int ganttrole
) const
116 return d
->columnMap
[ganttrole
];
119 void ProxyModel::setRole( int ganttrole
, int role
)
121 d
->roleMap
[ganttrole
] = role
;
124 int ProxyModel::role( int ganttrole
) const
126 return d
->roleMap
[ganttrole
];
130 void ProxyModel::setCalendarMode( bool enable
)
132 if ( d
->calendarMode
!= enable
) {
133 d
->calendarMode
= enable
;
138 bool ProxyModel::calendarMode() const
140 return d
->calendarMode
;
144 int ProxyModel::rowCount( const QModelIndex
& proxyIndex
) const
147 return BASE::rowCount( proxyIndex
);
150 int ProxyModel::columnCount( const QModelIndex
& proxyIndex
) const
152 return qMin( sourceModel()->columnCount( mapToSource( proxyIndex
) ), 1 );
155 QVariant
ProxyModel::data( const QModelIndex
& proxyIdx
, int role
) const
158 int scol
= proxyIdx
.column();
159 QHash
<int, int>::const_iterator it
= d
->roleMap
.find( role
);
160 if ( it
!= d
->roleMap
.end() ) srole
= *it
;
161 it
= d
->columnMap
.find( role
);
162 if ( it
!= d
->columnMap
.end() ) scol
= *it
;
165 qDebug() << "mapping role"<<static_cast<ItemDataRole
>(role
)<<"to"<<static_cast<ItemDataRole
>(srole
);
166 qDebug() << "mapping column"<<proxyIdx
.column()<<"to"<<scol
167 << "value="<<sourceModel()->data( sourceModel()->index( proxyIdx
.row(), scol
, mapToSource( proxyIdx
.parent() ) ), srole
);
170 const QAbstractItemModel
* model
= sourceModel();
171 return model
->data( model
->index( proxyIdx
.row(), scol
, mapToSource( proxyIdx
.parent() ) ), srole
);
174 bool ProxyModel::setData( const QModelIndex
& proxyIdx
, const QVariant
& value
, int role
)
177 int scol
= proxyIdx
.column();
178 QHash
<int, int>::const_iterator it
= d
->roleMap
.constFind( role
);
179 if ( it
!= d
->roleMap
.constEnd() ) srole
= *it
;
180 it
= d
->columnMap
.constFind( role
);
181 if ( it
!= d
->columnMap
.constEnd() ) scol
= *it
;
183 QAbstractItemModel
* model
= sourceModel();
184 return model
->setData( model
->index( proxyIdx
.row(), scol
, mapToSource( proxyIdx
.parent() ) ), value
, srole
);
187 #include "moc_kdganttproxymodel.cpp"