24 #include "kdganttsummaryhandlingproxymodel_p.h" 50 bool SummaryHandlingProxyModel::Private::cacheLookup(
const QModelIndex& idx,
54 QHash<QModelIndex,QPair<QDateTime,QDateTime> >::const_iterator it =
55 cached_summary_items.constFind( idx );
56 if ( it != cached_summary_items.constEnd() ) {
65 const QModelIndex& sourceIdx )
const 67 QAbstractItemModel* sourceModel = model->sourceModel();
68 const QModelIndex& mainIdx = sourceIdx;
72 for (
int r = 0; r < sourceModel->rowCount( mainIdx ); ++r ) {
73 QModelIndex pdIdx = model->
mapFromSource( sourceModel->index( r, 0, mainIdx ) );
77 if ( !tmpsv.canConvert( QVariant::DateTime ) ||
78 !tmpev.canConvert( QVariant::DateTime ) ) {
79 qDebug() <<
"Skipping item " << sourceIdx <<
" because it doesn't contain QDateTime";
84 if ( tmpsv.type() == QVariant::DateTime && !tmpsv.value<QDateTime>().isValid())
continue;
85 if ( tmpev.type() == QVariant::DateTime && !tmpev.value<QDateTime>().isValid())
continue;
89 if ( tmpsv.type() == QVariant::String && tmpsv.value<QString>().isEmpty())
continue;
90 if ( tmpev.type() == QVariant::String && tmpev.value<QString>().isEmpty())
continue;
91 QDateTime tmpst = tmpsv.toDateTime();
92 QDateTime tmpet = tmpev.toDateTime();
93 if ( st.isNull() || st > tmpst ) st = tmpst;
94 if ( et.isNull() || et < tmpet ) et = tmpet;
96 QVariant tmpssv = sourceModel->data( mainIdx,
StartTimeRole );
97 QVariant tmpsev = sourceModel->data( mainIdx,
EndTimeRole );
98 if ( tmpssv.canConvert( QVariant::DateTime )
99 && !( tmpssv.canConvert( QVariant::String ) && tmpssv.toString().isEmpty() )
100 && tmpssv.toDateTime() != st )
102 if ( tmpsev.canConvert( QVariant::DateTime )
103 && !( tmpsev.canConvert( QVariant::String ) && tmpsev.toString().isEmpty() )
104 && tmpsev.toDateTime() != et )
106 cached_summary_items[sourceIdx]=qMakePair( st, et );
109 void SummaryHandlingProxyModel::Private::removeFromCache(
const QModelIndex& idx )
const 111 cached_summary_items.remove( idx );
114 void SummaryHandlingProxyModel::Private::clearCache()
const 116 cached_summary_items.clear();
123 :
BASE( parent ), _d( new Private )
134 void SummaryHandlingProxyModel::init()
141 struct KDPrivateModelIndex {
144 const QAbstractItemModel *m;
154 BASE::setSourceModel( model );
161 BASE::sourceModelReset();
167 BASE::sourceLayoutChanged();
172 QAbstractItemModel* model = sourceModel();
173 QModelIndex parentIdx = from;
175 const QModelIndex& dataIdx = parentIdx;
178 d->removeFromCache( dataIdx );
180 emit dataChanged( proxyDataIdx, proxyDataIdx );
182 }
while ( ( parentIdx=model->parent( parentIdx ) ) != QModelIndex() );
184 BASE::sourceDataChanged( from, to );
191 BASE::sourceColumnsAboutToBeInserted( parentIdx, start, end );
199 BASE::sourceColumnsAboutToBeRemoved( parentIdx, start, end );
205 BASE::sourceRowsAboutToBeInserted( parentIdx, start, end );
211 BASE::sourceRowsAboutToBeRemoved( parentIdx, start, end );
219 const QAbstractItemModel* model = sourceModel();
220 Qt::ItemFlags f = model->flags( sidx );
221 if (
d->isSummary(sidx) ) {
222 f &= ~Qt::ItemIsEditable;
231 const QModelIndex sidx =
mapToSource( proxyIndex );
232 const QAbstractItemModel* model = sourceModel();
236 if (
d->cacheLookup( sidx, &result ) ) {
244 d->insertInCache(
this, sidx );
245 return data( proxyIndex, role );
248 return model->data( sidx, role );
254 QAbstractItemModel* model = sourceModel();
258 if (
d->isSummary(parentIdx) ) {
260 d->removeFromCache( parentIdx );
262 emit dataChanged( proxyParentIdx, proxyParentIdx );
264 }
while ( ( parentIdx=model->parent( parentIdx ) ) != QModelIndex() );
266 return BASE::setData( index, value, role );
271 #ifndef KDAB_NO_UNIT_TESTS 275 #include <QStandardItemModel> 277 static std::ostream&
operator<<( std::ostream& os,
const QDateTime& dt )
280 os << dt.toString().toLatin1().constData();
282 os << dt.toString().toStdString();
289 QStandardItemModel sourceModel;
293 QStandardItem* topitem =
new QStandardItem( QString::fromLatin1(
"Summary" ) );
295 sourceModel.appendRow( topitem );
297 QStandardItem* task1 =
new QStandardItem( QString::fromLatin1(
"Task1" ) );
299 QStandardItem* task2 =
new QStandardItem( QString::fromLatin1(
"Task2" ) );
301 topitem->appendRow( task1 );
302 topitem->appendRow( task2 );
305 QDateTime startdt = QDateTime::currentDateTime();
306 QDateTime enddt = startdt.addDays( 1 );
314 const QModelIndex topidx = model.
index( 0, 0, QModelIndex() );
330 #include "moc_kdganttsummaryhandlingproxymodel.cpp"
void sourceColumnsAboutToBeRemoved(const QModelIndex &idx, int start, int end) override
void sourceRowsAboutToBeInserted(const QModelIndex &idx, int start, int end) override
Proxy model that supports summary gantt items.
void sourceModelReset() override
void sourceRowsAboutToBeRemoved(const QModelIndex &, int start, int end) override
void sourceDataChanged(const QModelIndex &from, const QModelIndex &to) override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
~SummaryHandlingProxyModel() override
QVariant data(const QModelIndex &proxyIndex, int role=Qt::DisplayRole) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void sourceLayoutChanged() override
Class only listed here to document inheritance of some KDChart classes.
ForwardingProxyModel BASE
static std::ostream & operator<<(std::ostream &os, const QDateTime &dt)
void sourceColumnsAboutToBeInserted(const QModelIndex &idx, int start, int end) override
KDAB_SCOPED_UNITTEST_SIMPLE(KDGantt, SummaryHandlingProxyModel,"test")
void setSourceModel(QAbstractItemModel *model) override
Qt::ItemFlags flags(const QModelIndex &idx) const override
Class only listed here to document inheritance of some KDChart classes.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
SummaryHandlingProxyModel(QObject *parent=0)
#define assertEqual(x, y)