KD Chart API Documentation  3.1
kdganttproxymodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "kdganttproxymodel.h"
12 #include "kdganttproxymodel_p.h"
13 
14 using namespace KDGantt;
15 
17 
18 ProxyModel::Private::Private(ProxyModel *_q)
19 #if 0
20  : calendarMode( false )
21 #endif
22 {
23  Q_UNUSED(_q); // for now
24 
25  columnMap[Qt::DisplayRole] = 0;
26  columnMap[ItemTypeRole] = 1;
27  columnMap[StartTimeRole] = 2;
28  columnMap[EndTimeRole] = 3;
29  columnMap[TaskCompletionRole] = 4;
30  columnMap[LegendRole] = 5;
31 
32  roleMap[Qt::DisplayRole] = Qt::DisplayRole;
33  roleMap[ItemTypeRole] = Qt::DisplayRole;
34  roleMap[StartTimeRole] = StartTimeRole;
35  roleMap[EndTimeRole] = EndTimeRole;
36  roleMap[TaskCompletionRole] = Qt::DisplayRole;
37  roleMap[LegendRole] = Qt::DisplayRole;
38 }
39 
40 ProxyModel::ProxyModel(QObject *parent)
41  : BASE(parent)
42  , _d(new Private(this))
43 {
44  init();
45 }
46 
48 {
49  delete _d;
50  _d = nullptr;
51 }
52 
53 #define d d_func()
54 
55 void ProxyModel::init()
56 {
57 }
58 
59 QModelIndex ProxyModel::mapFromSource(const QModelIndex &sourceIdx) const
60 {
61 #if 0
62  if ( sourceIdx.isValid() ) {
63  if ( calendarMode() ) {
64  const QAbstractItemModel* model = sourceIdx.model();
65  if ( model->hasChildren( sourceIdx ) ) {
66  return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()));
67  } else {
68  // Map children to columns
69  return BASE::mapFromSource( model->index( sourceIdx.row(),0,sourceIdx.parent()))
70  .child( 0, sourceIdx.column() );
71  }
72  }
73  return BASE::mapFromSource( sourceIdx.model()->index( sourceIdx.row(),0,sourceIdx.parent()));
74  }
75  else return QModelIndex();
76 #else
77  return BASE::mapFromSource(sourceIdx.model() ? sourceIdx.model()->index(sourceIdx.row(), 0, sourceIdx.parent()) : QModelIndex());
78 #endif
79 }
80 
81 QModelIndex ProxyModel::mapToSource(const QModelIndex &proxyIdx) const
82 {
83 #if 0
84  if ( proxyIdx.isValid() ) {
85  if ( calendarMode() && proxyIdx.column() > 0 ) {
86  return BASE::mapToSource( proxyIdx.model()->index( proxyIdx.column(), 0, proxyIdx ) );
87  }
88  return BASE::mapToSource( proxyIdx );
89  }
90  else return QModelIndex();
91 #else
92  return BASE::mapToSource(proxyIdx);
93 #endif
94 }
95 
96 void ProxyModel::setColumn(int ganttrole, int col)
97 {
98  d->columnMap[ganttrole] = col;
99 }
100 
101 int ProxyModel::column(int ganttrole) const
102 {
103  return d->columnMap[ganttrole];
104 }
105 
106 void ProxyModel::setRole(int ganttrole, int role)
107 {
108  d->roleMap[ganttrole] = role;
109 }
110 
111 int ProxyModel::role(int ganttrole) const
112 {
113  return d->roleMap[ganttrole];
114 }
115 
116 #if 0
117 void ProxyModel::setCalendarMode( bool enable )
118 {
119  if ( d->calendarMode != enable ) {
120  d->calendarMode = enable;
121  reset();
122  }
123 }
124 
125 bool ProxyModel::calendarMode() const
126 {
127  return d->calendarMode;
128 }
129 #endif
130 
131 int ProxyModel::rowCount(const QModelIndex &proxyIndex) const
132 {
133  // TODO
134  return BASE::rowCount(proxyIndex);
135 }
136 
137 int ProxyModel::columnCount(const QModelIndex &proxyIndex) const
138 {
139  return qMin(sourceModel()->columnCount(mapToSource(proxyIndex)), 1);
140 }
141 
142 QVariant ProxyModel::data(const QModelIndex &proxyIdx, int role) const
143 {
144  int srole = role;
145  int scol = proxyIdx.column();
146  QHash<int, int>::const_iterator it = d->roleMap.find(role);
147  if (it != d->roleMap.end())
148  srole = *it;
149  it = d->columnMap.find(role);
150  if (it != d->columnMap.end())
151  scol = *it;
152 
153 #if 0
154  qDebug() << "mapping "<<static_cast<ItemDataRole>(role)<<", "<<proxyIdx.column()
155  << " => " << static_cast<ItemDataRole>(srole)<<", " << scol
156  << "value="
157  << sourceModel()->data( sourceModel()->index( proxyIdx.row(), scol,
158  mapToSource( proxyIdx.parent() ) ), srole );
159 #endif
160 
161  const QAbstractItemModel *model = sourceModel();
162  return model->data(model->index(proxyIdx.row(), scol, mapToSource(proxyIdx.parent())), srole);
163 }
164 
165 bool ProxyModel::setData(const QModelIndex &proxyIdx, const QVariant &value, int role)
166 {
167  int srole = role;
168  int scol = proxyIdx.column();
169  QHash<int, int>::const_iterator it = d->roleMap.constFind(role);
170  if (it != d->roleMap.constEnd())
171  srole = *it;
172  it = d->columnMap.constFind(role);
173  if (it != d->columnMap.constEnd())
174  scol = *it;
175 
176  QAbstractItemModel *model = sourceModel();
177  return model->setData(model->index(proxyIdx.row(), scol, mapToSource(proxyIdx.parent())), value, srole);
178 }
179 
180 #include "moc_kdganttproxymodel.cpp"
int role(int ganttrole) const
void setRole(int ganttrole, int role)
int column(int ganttrole) const
QVariant data(const QModelIndex &idx, int role=Qt::DisplayRole) const override
int columnCount(const QModelIndex &idx) const override
void setColumn(int ganttrole, int col)
QModelIndex mapFromSource(const QModelIndex &idx) const override
QModelIndex mapToSource(const QModelIndex &proxyIdx) const override
int rowCount(const QModelIndex &idx) const override
ProxyModel(QObject *parent=nullptr)
bool setData(const QModelIndex &idx, const QVariant &value, int role=Qt::EditRole) override
QAbstractProxyModel BASE
#define d
ForwardingProxyModel BASE
ItemDataRole
The values of this enum are used for the special data roles for gantt items.
@ TaskCompletionRole

© 2001 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/
Generated by doxygen 1.9.1