KD Chart 2  [rev.2.7]
kdganttproxymodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2020 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
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.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "kdganttproxymodel.h"
24 #include "kdganttproxymodel_p.h"
25 
26 
27 using namespace KDGantt;
28 
30 
31 ProxyModel::Private::Private( ProxyModel* _q )
32 #if 0
33  : calendarMode( false )
34 #endif
35 {
36  Q_UNUSED( _q ); // for now
37 
38  columnMap[Qt::DisplayRole] = 0;
39  columnMap[ItemTypeRole] = 1;
40  columnMap[StartTimeRole] = 2;
41  columnMap[EndTimeRole] = 3;
42  columnMap[TaskCompletionRole] = 4;
43  columnMap[LegendRole] = 5;
44 
45  roleMap[Qt::DisplayRole] = Qt::DisplayRole;
46  roleMap[ItemTypeRole] = Qt::DisplayRole;
47  roleMap[StartTimeRole] = StartTimeRole;
48  roleMap[EndTimeRole] = EndTimeRole;
49  roleMap[TaskCompletionRole] = Qt::DisplayRole;
50  roleMap[LegendRole] = Qt::DisplayRole;
51 }
52 
54  : BASE( parent ), _d( new Private( this ) )
55 {
56  init();
57 }
58 
60 {
61  delete _d; _d = 0;
62 }
63 
64 #define d d_func()
65 
66 void ProxyModel::init()
67 {
68 }
69 
70 QModelIndex ProxyModel::mapFromSource( const QModelIndex& sourceIdx ) const
71 {
72 #if 0
73  if ( sourceIdx.isValid() ) {
74  if ( calendarMode() ) {
75  const QAbstractItemModel* model = sourceIdx.model();
76  if ( model->hasChildren( sourceIdx ) ) {
77  return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()));
78  } else {
79  // Map children to columns
80  return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()))
81  .child( 0, sourceIdx.column() );
82  }
83  }
84  return BASE::mapFromSource( sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()));
85  }
86  else return QModelIndex();
87 #else
88  return BASE::mapFromSource( sourceIdx.model()?sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()):QModelIndex());
89 #endif
90 }
91 
92 QModelIndex ProxyModel::mapToSource( const QModelIndex& proxyIdx ) const
93 {
94 #if 0
95  if ( proxyIdx.isValid() ) {
96  if ( calendarMode() && proxyIdx.column() > 0 ) {
97  return BASE::mapToSource( proxyIdx.model()->index( proxyIdx.column(), 0, proxyIdx ) );
98  }
99  return BASE::mapToSource( proxyIdx );
100  }
101  else return QModelIndex();
102 #else
103  return BASE::mapToSource( proxyIdx );
104 #endif
105 }
106 
107 void ProxyModel::setColumn( int ganttrole, int col )
108 {
109  d->columnMap[ganttrole] = col;
110 }
111 
112 int ProxyModel::column( int ganttrole ) const
113 {
114  return d->columnMap[ganttrole];
115 }
116 
117 void ProxyModel::setRole( int ganttrole, int role )
118 {
119  d->roleMap[ganttrole] = role;
120 }
121 
122 int ProxyModel::role( int ganttrole ) const
123 {
124  return d->roleMap[ganttrole];
125 }
126 
127 #if 0
128 void ProxyModel::setCalendarMode( bool enable )
129 {
130  if ( d->calendarMode != enable ) {
131  d->calendarMode = enable;
132  reset();
133  }
134 }
135 
136 bool ProxyModel::calendarMode() const
137 {
138  return d->calendarMode;
139 }
140 #endif
141 
142 int ProxyModel::rowCount( const QModelIndex& proxyIndex ) const
143 {
144  // TODO
145  return BASE::rowCount( proxyIndex );
146 }
147 
148 int ProxyModel::columnCount( const QModelIndex& proxyIndex ) const
149 {
150  return qMin( sourceModel()->columnCount( mapToSource( proxyIndex ) ), 1 );
151 }
152 
153 QVariant ProxyModel::data( const QModelIndex& proxyIdx, int role ) const
154 {
155  int srole = role;
156  int scol = proxyIdx.column();
157  QHash<int, int>::const_iterator it = d->roleMap.find( role );
158  if ( it != d->roleMap.end() ) srole = *it;
159  it = d->columnMap.find( role );
160  if ( it != d->columnMap.end() ) scol = *it;
161 
162 #if 0
163  qDebug() << "mapping "<<static_cast<ItemDataRole>(role)<<", "<<proxyIdx.column()
164  << " => " << static_cast<ItemDataRole>(srole)<<", " << scol
165  << "value="
166  << sourceModel()->data( sourceModel()->index( proxyIdx.row(), scol,
167  mapToSource( proxyIdx.parent() ) ), srole );
168 #endif
169 
170  const QAbstractItemModel* model = sourceModel();
171  return model->data( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), srole );
172 }
173 
174 bool ProxyModel::setData( const QModelIndex& proxyIdx, const QVariant& value, int role )
175 {
176  int srole = 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;
182 
183  QAbstractItemModel* model = sourceModel();
184  return model->setData( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), value, srole );
185 }
186 
187 #include "moc_kdganttproxymodel.cpp"
ProxyModel(QObject *parent=0)
int rowCount(const QModelIndex &idx) const override
int role(int ganttrole) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
ForwardingProxyModel BASE
QModelIndex mapFromSource(const QModelIndex &idx) const override
void setRole(int ganttrole, int role)
bool setData(const QModelIndex &idx, const QVariant &value, int role=Qt::EditRole) override
Class only listed here to document inheritance of some KDChart classes.
int column(int ganttrole) const
void setColumn(int ganttrole, int col)
int columnCount(const QModelIndex &idx) const override
QModelIndex mapToSource(const QModelIndex &proxyIdx) const override
QVariant data(const QModelIndex &idx, int role=Qt::DisplayRole) const override
Class only listed here to document inheritance of some KDChart classes.
#define d

Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/

https://www.kdab.com/development-resources/qt-tools/kd-chart/