KGantt

kganttproxymodel.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KGantt library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "kganttproxymodel.h"
10#include "kganttproxymodel_p.h"
11
12
13using namespace KGantt;
14
15typedef ForwardingProxyModel BASE;
16
17ProxyModel::Private::Private( ProxyModel* _q )
18#if 0
19 : calendarMode( false )
20#endif
21{
22 Q_UNUSED( _q ); // for now
23
24 columnMap[Qt::DisplayRole] = 0;
25 columnMap[ItemTypeRole] = 1;
26 columnMap[StartTimeRole] = 2;
27 columnMap[EndTimeRole] = 3;
28 columnMap[TaskCompletionRole] = 4;
29 columnMap[LegendRole] = 5;
30
34 roleMap[EndTimeRole] = EndTimeRole;
36 roleMap[LegendRole] = Qt::DisplayRole;
37}
38
39ProxyModel::ProxyModel( QObject* parent )
40 : BASE( parent ), _d( new Private( this ) )
41{
42 init();
43}
44
45ProxyModel::~ProxyModel()
46{
47 delete _d; _d = nullptr;
48}
49
50#define d d_func()
51
52void ProxyModel::init()
53{
54}
55
56QModelIndex ProxyModel::mapFromSource( const QModelIndex& sourceIdx ) const
57{
58#if 0
59 if ( sourceIdx.isValid() ) {
60 if ( calendarMode() ) {
61 const QAbstractItemModel* model = sourceIdx.model();
62 if ( model->hasChildren( sourceIdx ) ) {
63 return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()));
64 } else {
65 // Map children to columns
66 return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()))
67 .child( 0, sourceIdx.column() );
68 }
69 }
70 return BASE::mapFromSource( sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()));
71 }
72 else return QModelIndex();
73#else
74 // danders:
75 // this was:
76 // return BASE::mapFromSource( sourceIdx.model()?sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()):QModelIndex());
77 // with column hardcoded to 0.
78 // Please notify if this *really* needs to be hardcoded, afaics it makes it impossible to get at anything else
79 // than column 0 from e.g. an item delegate.
80 return BASE::mapFromSource( sourceIdx.model()?sourceIdx.model()->index( sourceIdx.row(),sourceIdx.column(),sourceIdx.parent()):QModelIndex());
81#endif
82}
83
84QModelIndex ProxyModel::mapToSource( const QModelIndex& proxyIdx ) const
85{
86#if 0
87 if ( proxyIdx.isValid() ) {
88 if ( calendarMode() && proxyIdx.column() > 0 ) {
89 return BASE::mapToSource( proxyIdx.model()->index( proxyIdx.column(), 0, proxyIdx ) );
90 }
91 return BASE::mapToSource( proxyIdx );
92 }
93 else return QModelIndex();
94#else
95 return BASE::mapToSource( proxyIdx );
96#endif
97}
98
99void ProxyModel::setColumn( int ganttrole, int col )
100{
101 d->columnMap[ganttrole] = col;
102}
103
104void ProxyModel::removeColumn( int ganttrole )
105{
106 d->columnMap.remove( ganttrole );
107}
108
109int ProxyModel::column( int ganttrole ) const
110{
111 return d->columnMap[ganttrole];
112}
113
114void ProxyModel::removeRole( int ganttrole )
115{
116 d->roleMap.remove( ganttrole );
117}
118
119void ProxyModel::setRole( int ganttrole, int role )
120{
121 d->roleMap[ganttrole] = role;
122}
123
124int ProxyModel::role( int ganttrole ) const
125{
126 return d->roleMap[ganttrole];
127}
128
129#if 0
130void ProxyModel::setCalendarMode( bool enable )
131{
132 if ( d->calendarMode != enable ) {
133 d->calendarMode = enable;
134 reset();
135 }
136}
137
138bool ProxyModel::calendarMode() const
139{
140 return d->calendarMode;
141}
142#endif
143
144int ProxyModel::rowCount( const QModelIndex& proxyIndex ) const
145{
146 // TODO
147 return BASE::rowCount( proxyIndex );
148}
149
150int ProxyModel::columnCount( const QModelIndex& proxyIndex ) const
151{
152 return qMin( sourceModel()->columnCount( mapToSource( proxyIndex ) ), 1 );
153}
154
155QVariant ProxyModel::data( const QModelIndex& proxyIdx, int role ) const
156{
157 int srole = role;
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;
163
164#if 0
165 qDebug() << "mapping "<<static_cast<ItemDataRole>(role)<<", "<<proxyIdx.column()
166 << " => " << static_cast<ItemDataRole>(srole)<<", " << scol
167 << "value="
168 << sourceModel()->data( sourceModel()->index( proxyIdx.row(), scol,
169 mapToSource( proxyIdx.parent() ) ), srole );
170#endif
171
172 const QAbstractItemModel* model = sourceModel();
173 return model->data( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), srole );
174}
175
176bool ProxyModel::setData( const QModelIndex& proxyIdx, const QVariant& value, int role )
177{
178 int srole = role;
179 int scol = proxyIdx.column();
180 QHash<int, int>::const_iterator it = d->roleMap.constFind( role );
181 if ( it != d->roleMap.constEnd() ) srole = *it;
182 it = d->columnMap.constFind( role );
183 if ( it != d->columnMap.constEnd() ) scol = *it;
184
186 return model->setData( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), value, srole );
187}
188
189#include "moc_kganttproxymodel.cpp"
Global namespace.
@ ItemTypeRole
The item type.
@ StartTimeRole
Start time (or other start value) for a gantt item.
@ TaskCompletionRole
Task completion percentage used by Task items. Should be an integer og a qreal between 0 and 100.
@ LegendRole
The Legend text.
@ EndTimeRole
End time (or other end value) for a gantt item.
KGuiItem reset()
QCA_EXPORT void init()
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual bool hasChildren(const QModelIndex &parent) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const=0
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const=0
int column() const const
bool isValid() const const
const QAbstractItemModel * model() const const
QModelIndex parent() const const
int row() const const
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.