File: kdganttsummaryhandlingproxymodel.cpp

package info (click to toggle)
kdepim 4%3A4.4.7-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 48,972 kB
  • ctags: 68,134
  • sloc: cpp: 491,784; perl: 1,514; xml: 884; ansic: 776; sh: 650; php: 44; makefile: 8
file content (327 lines) | stat: -rw-r--r-- 12,087 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/****************************************************************************
 ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB.  All rights reserved.
 **
 ** This file is part of the KD Gantt library.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License versions 2.0 or 3.0 as published by the Free Software
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
 ** included in the packaging of this file.  Alternatively you may (at
 ** your option) use any later version of the GNU General Public
 ** License if such license has been publicly approved by
 ** Klarälvdalens Datakonsult AB (or its successors, if any).
 ** 
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
 ** not expressly granted herein.
 ** 
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 **********************************************************************/
#include "kdganttsummaryhandlingproxymodel.h"
#include "kdganttsummaryhandlingproxymodel_p.h"

#include <QDebug>

#include <cassert>

using namespace KDGantt;

/*!\class KDGantt::SummaryHandlingProxyModel
 * \brief Proxy model that supports summary gantt items.
 *
 * This proxy model provides the functionality of summary items.
 * A summary item is an item with type KDGantt::TypeSummary and
 * zero or more children in the model that it summarizes.
 * GraphicsView itself does not dictate any policy for summary items,
 * instead the logic for making the summary items start and end points
 * span it's children is provided by this proxy.
 *
 * The start and end times of a summary is the min/max of the
 * start/end times of it's children.
 *
 * \see GraphicsView::setModel
 */

typedef ForwardingProxyModel BASE;

bool SummaryHandlingProxyModel::Private::cacheLookup( const QModelIndex& idx,
                                                      QPair<QDateTime,QDateTime>* result ) const
{
    //qDebug() << "cacheLookup("<<idx<<"), cache has " << cached_summary_items.count() << "items";
    QHash<QModelIndex,QPair<QDateTime,QDateTime> >::const_iterator it =
        cached_summary_items.constFind( idx );
    if ( it != cached_summary_items.constEnd() ) {
        *result = *it;
        return true;
    } else {
        return false;
    }
}

void SummaryHandlingProxyModel::Private::insertInCache( const SummaryHandlingProxyModel* model,
                                                        const QModelIndex& sourceIdx ) const
{
    QAbstractItemModel* sourceModel = model->sourceModel();
    const QModelIndex& mainIdx = sourceIdx;
    QDateTime st;
    QDateTime et;

    for ( int r = 0; r < sourceModel->rowCount( mainIdx ); ++r ) {
        QModelIndex pdIdx = model->mapFromSource( sourceModel->index( r, 0, mainIdx ) );
        /* The probably results in recursive calls here */
	QVariant tmpsv = model->data( pdIdx, StartTimeRole );
	QVariant tmpev = model->data( pdIdx, EndTimeRole );
	if( !qVariantCanConvert<QDateTime>(tmpsv) ||
	    !qVariantCanConvert<QDateTime>(tmpev) ) {
            qDebug() << "Skipping item " << sourceIdx << " because it doesn't contain QDateTime";
            continue;
        }

    // check for valid datetimes
    if( tmpsv.type() == QVariant::DateTime && !qVariantValue<QDateTime>(tmpsv).isValid()) continue;
    if( tmpev.type() == QVariant::DateTime && !qVariantValue<QDateTime>(tmpev).isValid()) continue;

	// We need to test for empty strings to
	// avoid a stupid Qt warning
	if( tmpsv.type() == QVariant::String && qVariantValue<QString>(tmpsv).isEmpty()) continue;
	if( tmpev.type() == QVariant::String && qVariantValue<QString>(tmpev).isEmpty()) continue;
        QDateTime tmpst = tmpsv.toDateTime();
        QDateTime tmpet = tmpev.toDateTime();
        if ( st.isNull() || st > tmpst ) st = tmpst;
        if ( et.isNull() || et < tmpet ) et = tmpet;
    }
    QVariant tmpssv = sourceModel->data( mainIdx, StartTimeRole );
    QVariant tmpsev = sourceModel->data( mainIdx, EndTimeRole );
    if ( qVariantCanConvert<QDateTime>( tmpssv )
         && !( qVariantCanConvert<QString>( tmpssv ) && qVariantValue<QString>( tmpssv ).isEmpty() )
         && qVariantValue<QDateTime>( tmpssv ) != st )
        sourceModel->setData( mainIdx, st, StartTimeRole );
    if ( qVariantCanConvert<QDateTime>( tmpsev )
         && !( qVariantCanConvert<QString>( tmpsev ) && qVariantValue<QString>( tmpsev ).isEmpty() )
         && qVariantValue<QDateTime>( tmpsev ) != et )
        sourceModel->setData( mainIdx, et, EndTimeRole );
    cached_summary_items[sourceIdx]=qMakePair( st, et );
}

void SummaryHandlingProxyModel::Private::removeFromCache( const QModelIndex& idx ) const
{
    cached_summary_items.remove( idx );
}

void SummaryHandlingProxyModel::Private::clearCache() const
{
    cached_summary_items.clear();
}

/*! Constructor. Creates a new SummaryHandlingProxyModel with
 * parent \a parent
 */
SummaryHandlingProxyModel::SummaryHandlingProxyModel( QObject* parent )
    : BASE( parent ), _d( new Private )
{
    init();
}

#define d d_func()
SummaryHandlingProxyModel::~SummaryHandlingProxyModel()
{
}

void SummaryHandlingProxyModel::init()
{
}

namespace {

    // Think this is ugly? Well, it's not from me, it comes from QProxyModel
    struct KDPrivateModelIndex {
        int r, c;
        void *p;
        const QAbstractItemModel *m;
    };
}

/*! Sets the model to be used as the source model for this proxy.
 * The proxy does not take ownership of the model.
 * \see QAbstractProxyModel::setSourceModel
 */
void SummaryHandlingProxyModel::setSourceModel( QAbstractItemModel* model )
{
    BASE::setSourceModel( model );
    d->clearCache();
}

void SummaryHandlingProxyModel::sourceModelReset()
{
    d->clearCache();
    BASE::sourceModelReset();
}

void SummaryHandlingProxyModel::sourceLayoutChanged()
{
    d->clearCache();
    BASE::sourceLayoutChanged();
}

void SummaryHandlingProxyModel::sourceDataChanged( const QModelIndex& from, const QModelIndex& to )
{
    QAbstractItemModel* model = sourceModel();
    QModelIndex parentIdx = from;
    do {
        const QModelIndex& dataIdx = parentIdx;
        if ( model->data( dataIdx, ItemTypeRole )==TypeSummary ) {
            //qDebug() << "removing " << parentIdx << "from cache";
            d->removeFromCache( dataIdx );
            QModelIndex proxyDataIdx = mapFromSource( dataIdx );
            emit dataChanged( proxyDataIdx, proxyDataIdx );
        }
    } while ( ( parentIdx=model->parent( parentIdx ) ) != QModelIndex() );

    BASE::sourceDataChanged( from, to );
}

void SummaryHandlingProxyModel::sourceColumnsAboutToBeInserted( const QModelIndex& parentIdx,
                                                                    int start,
                                                                    int end )
{
    BASE::sourceColumnsAboutToBeInserted( parentIdx, start, end );
    d->clearCache();
}

void SummaryHandlingProxyModel::sourceColumnsAboutToBeRemoved( const QModelIndex& parentIdx,
                                                                    int start,
                                                                    int end )
{
    BASE::sourceColumnsAboutToBeRemoved( parentIdx, start, end );
    d->clearCache();
}

void SummaryHandlingProxyModel::sourceRowsAboutToBeInserted( const QModelIndex & parentIdx, int start, int end )
{
    BASE::sourceRowsAboutToBeInserted( parentIdx, start, end );
    d->clearCache();
}

void SummaryHandlingProxyModel::sourceRowsAboutToBeRemoved( const QModelIndex & parentIdx, int start, int end )
{
    BASE::sourceRowsAboutToBeRemoved( parentIdx, start, end );
    d->clearCache();
}

/*! \see QAbstractItemModel::flags */
Qt::ItemFlags SummaryHandlingProxyModel::flags( const QModelIndex& idx ) const
{
    const QModelIndex sidx = mapToSource( idx );
    const QAbstractItemModel* model = sourceModel();
    Qt::ItemFlags f = model->flags( sidx );
    if ( d->isSummary(sidx) ) {
        f &= !Qt::ItemIsEditable;
    }
    return f;
}

/*! \see QAbstractItemModel::data */
QVariant SummaryHandlingProxyModel::data( const QModelIndex& proxyIndex, int role) const
{
    //qDebug() << "SummaryHandlingProxyModel::data("<<proxyIndex<<role<<")";
    const QModelIndex sidx = mapToSource( proxyIndex );
    const QAbstractItemModel* model = sourceModel();
    if ( d->isSummary(sidx) && ( role==StartTimeRole || role==EndTimeRole )) {
      //qDebug() << "requested summary";
        QPair<QDateTime,QDateTime> result;
        if ( d->cacheLookup( sidx, &result ) ) {
	  //qDebug() << "SummaryHandlingProxyModel::data(): Looking up summary for " << proxyIndex << role;
            switch( role ) {
            case StartTimeRole: return result.first;
            case EndTimeRole: return result.second;
            default: /* fall thru */;
            }
        } else {
            d->insertInCache( this, sidx );
            return data( proxyIndex, role ); /* TODO: Optimise */
        }
    }
    return model->data( sidx, role );
}

/*! \see QAbstractItemModel::setData */
bool SummaryHandlingProxyModel::setData( const QModelIndex& index, const QVariant& value, int role )
{
    QAbstractItemModel* model = sourceModel();
    if ( role==StartTimeRole || role==EndTimeRole ) {
        QModelIndex parentIdx = mapToSource( index );
        do {
            if ( d->isSummary(parentIdx) ) {
	      //qDebug() << "removing " << parentIdx << "from cache";
                d->removeFromCache( parentIdx );
                QModelIndex proxyParentIdx = mapFromSource( parentIdx );
                emit dataChanged( proxyParentIdx, proxyParentIdx );
            }
        } while ( ( parentIdx=model->parent( parentIdx ) ) != QModelIndex() );
    }
    return BASE::setData( index, value, role );
}

#undef d

#ifndef KDAB_NO_UNIT_TESTS

#include "unittest/test.h"

#include <QStandardItemModel>

namespace {
    std::ostream& operator<<( std::ostream& os, const QDateTime& dt )
    {
        os << dt.toString().toStdString();
        return os;
    }
}

KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, SummaryHandlingProxyModel, "test" ) {
    SummaryHandlingProxyModel model;
    QStandardItemModel sourceModel;

    model.setSourceModel( &sourceModel );

    QStandardItem* topitem = new QStandardItem( QString::fromLatin1( "Summary" ) );
    topitem->setData( KDGantt::TypeSummary,  KDGantt::ItemTypeRole );
    sourceModel.appendRow( topitem );

    QStandardItem* task1 = new QStandardItem( QString::fromLatin1( "Task1" ) );
    task1->setData( KDGantt::TypeTask, KDGantt::ItemTypeRole );
    QStandardItem* task2 = new QStandardItem( QString::fromLatin1( "Task2" ) );
    task2->setData( KDGantt::TypeTask, KDGantt::ItemTypeRole );
    topitem->appendRow( task1 );
    topitem->appendRow( task2 );


    QDateTime startdt = QDateTime::currentDateTime();
    QDateTime enddt = startdt.addDays( 1 );


    task1->setData( startdt, KDGantt::StartTimeRole );
    task1->setData( enddt, KDGantt::EndTimeRole );
    task2->setData( startdt, KDGantt::StartTimeRole );
    task2->setData( enddt, KDGantt::EndTimeRole );

    const QModelIndex topidx = model.index( 0, 0, QModelIndex() );

    assertEqual( model.data( topidx, KDGantt::ItemTypeRole ).toInt(), KDGantt::TypeSummary );
    assertEqual( model.data( model.index( 0, 0, topidx ), KDGantt::ItemTypeRole ).toInt(), KDGantt::TypeTask );

    QDateTime task1startdt = model.data( model.index( 0, 0, topidx ), KDGantt::StartTimeRole ).toDateTime();
    assertEqual( task1startdt, startdt );

    QDateTime summarystartdt = model.data( topidx, KDGantt::StartTimeRole ).toDateTime();
    assertEqual( summarystartdt, startdt );
    assertTrue( model.flags( model.index( 0, 0, topidx ) ) & Qt::ItemIsEditable );
    assertFalse( model.flags( topidx ) & Qt::ItemIsEditable );
}

#endif /* KDAB_NO_UNIT_TESTS */

#include "moc_kdganttsummaryhandlingproxymodel.cpp"