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 
13 using namespace KGantt;
14 
15 typedef ForwardingProxyModel BASE;
16 
17 ProxyModel::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 
32  roleMap[ItemTypeRole] = Qt::DisplayRole;
33  roleMap[StartTimeRole] = StartTimeRole;
34  roleMap[EndTimeRole] = EndTimeRole;
36  roleMap[LegendRole] = Qt::DisplayRole;
37 }
38 
39 ProxyModel::ProxyModel( QObject* parent )
40  : BASE( parent ), _d( new Private( this ) )
41 {
42  init();
43 }
44 
45 ProxyModel::~ProxyModel()
46 {
47  delete _d; _d = nullptr;
48 }
49 
50 #define d d_func()
51 
52 void ProxyModel::init()
53 {
54 }
55 
56 QModelIndex 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 
84 QModelIndex 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 
99 void ProxyModel::setColumn( int ganttrole, int col )
100 {
101  d->columnMap[ganttrole] = col;
102 }
103 
104 void ProxyModel::removeColumn( int ganttrole )
105 {
106  d->columnMap.remove( ganttrole );
107 }
108 
109 int ProxyModel::column( int ganttrole ) const
110 {
111  return d->columnMap[ganttrole];
112 }
113 
114 void ProxyModel::removeRole( int ganttrole )
115 {
116  d->roleMap.remove( ganttrole );
117 }
118 
119 void ProxyModel::setRole( int ganttrole, int role )
120 {
121  d->roleMap[ganttrole] = role;
122 }
123 
124 int ProxyModel::role( int ganttrole ) const
125 {
126  return d->roleMap[ganttrole];
127 }
128 
129 #if 0
130 void ProxyModel::setCalendarMode( bool enable )
131 {
132  if ( d->calendarMode != enable ) {
133  d->calendarMode = enable;
134  reset();
135  }
136 }
137 
138 bool ProxyModel::calendarMode() const
139 {
140  return d->calendarMode;
141 }
142 #endif
143 
144 int ProxyModel::rowCount( const QModelIndex& proxyIndex ) const
145 {
146  // TODO
147  return BASE::rowCount( proxyIndex );
148 }
149 
150 int ProxyModel::columnCount( const QModelIndex& proxyIndex ) const
151 {
152  return qMin( sourceModel()->columnCount( mapToSource( proxyIndex ) ), 1 );
153 }
154 
155 QVariant 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 
176 bool 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 
185  QAbstractItemModel* model = sourceModel();
186  return model->setData( model->index( proxyIdx.row(), scol, mapToSource( proxyIdx.parent() ) ), value, srole );
187 }
188 
189 #include "moc_kganttproxymodel.cpp"
The item type.
Definition: kganttglobal.h:217
virtual int rowCount(const QModelIndex &parent) const const =0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const =0
The Legend text.
Definition: kganttglobal.h:218
Start time (or other start value) for a gantt item.
Definition: kganttglobal.h:214
bool isValid() const const
DisplayRole
End time (or other end value) for a gantt item.
Definition: kganttglobal.h:215
int row() const const
virtual QVariant data(const QModelIndex &index, int role) const const =0
Global namespace.
QModelIndex parent() const const
QCA_EXPORT void init()
const QAbstractItemModel * model() const const
virtual bool hasChildren(const QModelIndex &parent) const const
int column() const const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const =0
KGuiItem reset()
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const =0
QModelIndex child(int row, int column) const const
Task completion percentage used by Task items. Should be an integer og a qreal between 0 and 100...
Definition: kganttglobal.h:216
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 22:32:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.