~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to kdgantt/kdganttdatetimegrid.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB.  All rights reserved.
3
 
 **
4
 
 ** This file is part of the KD Gantt library.
5
 
 **
6
 
 ** This file may be used under the terms of the GNU General Public
7
 
 ** License versions 2.0 or 3.0 as published by the Free Software
8
 
 ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
9
 
 ** included in the packaging of this file.  Alternatively you may (at
10
 
 ** your option) use any later version of the GNU General Public
11
 
 ** License if such license has been publicly approved by
12
 
 ** Klarälvdalens Datakonsult AB (or its successors, if any).
13
 
 ** 
14
 
 ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
15
 
 ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
16
 
 ** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
17
 
 ** not expressly granted herein.
18
 
 ** 
19
 
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20
 
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21
 
 **
22
 
 **********************************************************************/
23
 
#include "kdganttdatetimegrid.h"
24
 
#include "kdganttdatetimegrid_p.h"
25
 
 
26
 
#include "kdganttabstractrowcontroller.h"
27
 
 
28
 
#include <QApplication>
29
 
#include <QDateTime>
30
 
#include <QPainter>
31
 
#include <QStyle>
32
 
#include <QStyleOptionHeader>
33
 
#include <QWidget>
34
 
#include <QDebug>
35
 
 
36
 
#include <cassert>
37
 
 
38
 
using namespace KDGantt;
39
 
 
40
 
/*!\class KDGantt::DateTimeGrid
41
 
 * \ingroup KDGantt
42
 
 *
43
 
 * This implementation of AbstractGrid works with QDateTime
44
 
 * and shows days and week numbers in the header
45
 
 *
46
 
 * \todo Extend to work with hours, minutes,... as units too.
47
 
 */
48
 
 
49
 
// TODO: I think maybe this class should be responsible
50
 
// for unit-transformation of the scene...
51
 
 
52
 
qreal DateTimeGrid::Private::dateTimeToChartX( const QDateTime& dt ) const
53
 
{
54
 
    assert( startDateTime.isValid() );
55
 
    qreal result = startDateTime.date().daysTo(dt.date())*24.*60.*60.;
56
 
    result += startDateTime.time().msecsTo(dt.time())/1000.;
57
 
    result *= dayWidth/( 24.*60.*60. );
58
 
 
59
 
    return result;
60
 
}
61
 
 
62
 
QDateTime DateTimeGrid::Private::chartXtoDateTime( qreal x ) const
63
 
{
64
 
    assert( startDateTime.isValid() );
65
 
    int days = static_cast<int>( x/dayWidth );
66
 
    qreal secs = x*( 24.*60.*60. )/dayWidth;
67
 
    QDateTime dt = startDateTime;
68
 
    QDateTime result = dt.addDays( days )
69
 
                       .addSecs( static_cast<int>(secs-(days*24.*60.*60.) ) )
70
 
                       .addMSecs( qRound( ( secs-static_cast<int>( secs ) )*1000. ) );
71
 
    return result;
72
 
}
73
 
 
74
 
#define d d_func()
75
 
 
76
 
DateTimeGrid::DateTimeGrid() : AbstractGrid( new Private )
77
 
{
78
 
}
79
 
 
80
 
DateTimeGrid::~DateTimeGrid()
81
 
{
82
 
}
83
 
 
84
 
/*! \returns The QDateTime used as start date for the grid.
85
 
 *
86
 
 * The default is three days before the current date.
87
 
 */
88
 
QDateTime DateTimeGrid::startDateTime() const
89
 
{
90
 
    return d->startDateTime;
91
 
}
92
 
 
93
 
/*! \param dt The start date of the grid. It is used as the beginning of the
94
 
 * horizontal scrollbar in the view.
95
 
 *
96
 
 * Emits gridChanged() after the start date has changed.
97
 
 */
98
 
void DateTimeGrid::setStartDateTime( const QDateTime& dt )
99
 
{
100
 
    d->startDateTime = dt;
101
 
    emit gridChanged();
102
 
}
103
 
 
104
 
/*! \returns The width in pixels for each day in the grid.
105
 
 *
106
 
 * The default is 100 pixels.
107
 
 */
108
 
qreal DateTimeGrid::dayWidth() const
109
 
{
110
 
    return d->dayWidth;
111
 
}
112
 
 
113
 
/*! \param w The width in pixels for each day in the grid.
114
 
 * Day width is limited to minimum 1.0.
115
 
 *
116
 
 * The signal gridChanged() is emitted after the day width is changed.
117
 
 */
118
 
void DateTimeGrid::setDayWidth( qreal w )
119
 
{
120
 
    qDebug()<<"DateTimeGrid::setDayWidth"<<w;
121
 
    d->dayWidth = qMax( w, qreal(0.1) );
122
 
    emit gridChanged();
123
 
}
124
 
 
125
 
/*! \param s The scale to be used to paint the grid.
126
 
 *
127
 
 * The signal gridChanged() is emitted after the scale has changed.
128
 
 * \sa Scale
129
 
 */
130
 
void DateTimeGrid::setScale( Scale s )
131
 
{
132
 
        d->scale = s;
133
 
        emit gridChanged();
134
 
}
135
 
 
136
 
/*! \returns The scale used to paint the grid.
137
 
 *
138
 
 * The default is ScaleAuto, which means the day scale will be used
139
 
 * as long as the day width is less or equal to 500.
140
 
 * \sa Scale
141
 
 */
142
 
DateTimeGrid::Scale DateTimeGrid::scale() const
143
 
{
144
 
        return d->scale;
145
 
}
146
 
 
147
 
/*! \param factor The zoom factor
148
 
 *
149
 
 */
150
 
void DateTimeGrid::zoomIn( qreal factor )
151
 
{
152
 
    setDayWidth( d->dayWidth * factor );
153
 
}
154
 
 
155
 
/*! \param factor The zoom factor
156
 
 *
157
 
 */
158
 
void DateTimeGrid::zoomOut( qreal factor )
159
 
{
160
 
    setDayWidth( d->dayWidth * factor );
161
 
}
162
 
 
163
 
/*! \param ws The start day of the week.
164
 
 *
165
 
 * A solid line is drawn on the grid to mark the beginning of a new week.
166
 
 * Emits gridChanged() after the start day has changed.
167
 
 */
168
 
void DateTimeGrid::setWeekStart( Qt::DayOfWeek ws )
169
 
{
170
 
    d->weekStart = ws;
171
 
    emit gridChanged();
172
 
}
173
 
 
174
 
/*! \returns The start day of the week */
175
 
Qt::DayOfWeek DateTimeGrid::weekStart() const
176
 
{
177
 
    return d->weekStart;
178
 
}
179
 
 
180
 
/*! \param fd A set of days to mark as free in the grid.
181
 
 *
182
 
 * Free days are filled with the alternate base brush of the
183
 
 * palette used by the view.
184
 
 * The signal gridChanged() is emitted after the free days are changed.
185
 
 */
186
 
void DateTimeGrid::setFreeDays( const QSet<Qt::DayOfWeek>& fd )
187
 
{
188
 
    d->freeDays = fd;
189
 
    emit gridChanged();
190
 
}
191
 
 
192
 
/*! \returns true if row separators are used. */
193
 
bool DateTimeGrid::rowSeparators() const
194
 
{
195
 
    return d->rowSeparators;
196
 
}
197
 
/*! \param enable Whether to use row separators or not. */
198
 
void DateTimeGrid::setRowSeparators( bool enable )
199
 
{
200
 
    d->rowSeparators = enable;
201
 
}
202
 
 
203
 
/*! \returns The days marked as free in the grid. */
204
 
QSet<Qt::DayOfWeek> DateTimeGrid::freeDays() const
205
 
{
206
 
    return d->freeDays;
207
 
}
208
 
 
209
 
/*!
210
 
 * \param value The datetime to get the x value for.
211
 
 * \returns The x value corresponding to \a value or -1.0 if \a value is not a datetime variant.
212
 
 */
213
 
qreal DateTimeGrid::mapToChart( const QVariant& value ) const
214
 
{
215
 
    if ( ! qVariantCanConvert<QDateTime>( value ) ||
216
 
         ( value.type() == QVariant::String && qVariantValue<QString>(value).isEmpty() ) )
217
 
    {
218
 
        return -1.0;
219
 
    }
220
 
    return d->dateTimeToChartX( value.toDateTime() );
221
 
}
222
 
 
223
 
/*!
224
 
 * \param x The x value get the datetime for.
225
 
 * \returns The datetime corresponding to \a x or an invalid datetime if x cannot be mapped.
226
 
 */
227
 
QVariant DateTimeGrid::mapFromChart( qreal x ) const
228
 
{
229
 
    return d->chartXtoDateTime( x );
230
 
}
231
 
 
232
 
/*! \param idx The index to get the Span for.
233
 
 * \returns The start and end pixels, in a Span, of the specified index.
234
 
 */
235
 
Span DateTimeGrid::mapToChart( const QModelIndex& idx ) const
236
 
{
237
 
    assert( model() );
238
 
    if ( !idx.isValid() ) return Span();
239
 
    assert( idx.model()==model() );
240
 
    const QVariant sv = model()->data( idx, StartTimeRole );
241
 
    const QVariant ev = model()->data( idx, EndTimeRole );
242
 
    if( qVariantCanConvert<QDateTime>(sv) &&
243
 
        qVariantCanConvert<QDateTime>(ev) &&
244
 
        !(sv.type() == QVariant::String && qVariantValue<QString>(sv).isEmpty()) &&
245
 
        !(ev.type() == QVariant::String && qVariantValue<QString>(ev).isEmpty())
246
 
        ) {
247
 
      QDateTime st = sv.toDateTime();
248
 
      QDateTime et = ev.toDateTime();
249
 
      if ( et.isValid() && st.isValid() ) {
250
 
        qreal sx = d->dateTimeToChartX( st );
251
 
        qreal ex = d->dateTimeToChartX( et )-sx;
252
 
        //qDebug() << "DateTimeGrid::mapToChart("<<st<<et<<") => "<< Span( sx, ex );
253
 
        return Span( sx, ex);
254
 
      }
255
 
    }
256
 
    // Special case for Events with only a start date
257
 
    if( qVariantCanConvert<QDateTime>(sv) && !(sv.type() == QVariant::String && qVariantValue<QString>(sv).isEmpty()) ) {
258
 
      QDateTime st = sv.toDateTime();
259
 
      if ( st.isValid() ) {
260
 
        qreal sx = d->dateTimeToChartX( st );
261
 
        return Span( sx, 0 );
262
 
      }
263
 
    }
264
 
    return Span();
265
 
}
266
 
 
267
 
#if 0
268
 
static void debug_print_idx( const QModelIndex& idx )
269
 
{
270
 
    if ( !idx.isValid() ) {
271
 
        qDebug() << "[Invalid]";
272
 
        return;
273
 
    }
274
 
    QDateTime st = idx.data( StartTimeRole ).toDateTime();
275
 
    QDateTime et = idx.data( StartTimeRole ).toDateTime();
276
 
    qDebug() << idx << "["<<st<<et<<"]";
277
 
}
278
 
#endif
279
 
 
280
 
/*! Maps the supplied Span to QDateTimes, and puts them as start time and
281
 
 * end time for the supplied index.
282
 
 *
283
 
 * \param span The span used to map from.
284
 
 * \param idx The index used for setting the start time and end time in the model.
285
 
 * \param constraints A list of hard constraints to match against the start time and
286
 
 * end time mapped from the span.
287
 
 *
288
 
 * \returns true if the start time and time was successfully added to the model, or false
289
 
 * if unsucessful.
290
 
 * Also returns false if any of the constraints isn't satisfied. That is, if the start time of
291
 
 * the constrained index is before the end time of the dependency index, or the end time of the
292
 
 * constrained index is before the start time of the dependency index.
293
 
 */
294
 
bool DateTimeGrid::mapFromChart( const Span& span, const QModelIndex& idx,
295
 
    const QList<Constraint>& constraints ) const
296
 
{
297
 
    assert( model() );
298
 
    if ( !idx.isValid() ) return false;
299
 
    assert( idx.model()==model() );
300
 
 
301
 
    QDateTime st = d->chartXtoDateTime(span.start());
302
 
    QDateTime et = d->chartXtoDateTime(span.start()+span.length());
303
 
    //qDebug() << "DateTimeGrid::mapFromChart("<<span<<") => "<< st << et;
304
 
    Q_FOREACH( const Constraint& c, constraints ) {
305
 
        if ( c.type() != Constraint::TypeHard || !isSatisfiedConstraint( c )) continue;
306
 
        if ( c.startIndex() == idx ) {
307
 
            QDateTime tmpst = model()->data( c.endIndex(), StartTimeRole ).toDateTime();
308
 
            //qDebug() << tmpst << "<" << et <<"?";
309
 
            if ( tmpst<et ) return false;
310
 
        } else if ( c.endIndex() == idx ) {
311
 
            QDateTime tmpet = model()->data( c.startIndex(), EndTimeRole ).toDateTime();
312
 
            //qDebug() << tmpet << ">" << st <<"?";
313
 
            if ( tmpet>st ) return false;
314
 
        }
315
 
    }
316
 
    return model()->setData( idx, qVariantFromValue(st), StartTimeRole )
317
 
        && model()->setData( idx, qVariantFromValue(et), EndTimeRole );
318
 
}
319
 
 
320
 
int DateTimeGrid::autoScale() const
321
 
{
322
 
    int scale = ScaleDay;
323
 
    if ( dayWidth() > 450) {
324
 
        scale = ScaleHour;
325
 
    } else if (dayWidth() * 7 < 20) {
326
 
        scale = ScaleMonth;
327
 
    } else if (dayWidth() < 12) {
328
 
        scale = ScaleWeek;
329
 
    }
330
 
    return scale;
331
 
}
332
 
 
333
 
void DateTimeGrid::paintGrid( QPainter* painter,
334
 
                              const QRectF& sceneRect,
335
 
                              const QRectF& exposedRect,
336
 
                              AbstractRowController* rowController,
337
 
                              QWidget* widget )
338
 
{
339
 
    //qDebug()<<"paintGrid()"<<scale()<<dayWidth();
340
 
    
341
 
    paintRowGrid(painter,sceneRect,exposedRect,rowController,widget);
342
 
    
343
 
    switch(scale()) {
344
 
        case ScaleHour:
345
 
            paintHourGrid(painter,sceneRect,exposedRect,rowController,widget);
346
 
            break;
347
 
        case ScaleDay:
348
 
            paintDayGrid(painter,sceneRect,exposedRect,rowController,widget);
349
 
            break;
350
 
        case ScaleWeek:
351
 
            paintWeekGrid(painter,sceneRect,exposedRect,rowController,widget);
352
 
            break;
353
 
        case ScaleMonth:
354
 
            paintMonthGrid(painter,sceneRect,exposedRect,rowController,widget);
355
 
            break;
356
 
        case ScaleAuto:
357
 
            switch(autoScale()) {
358
 
                case ScaleHour:
359
 
                    paintHourGrid(painter,sceneRect,exposedRect,rowController,widget);
360
 
                    break;
361
 
                case ScaleDay:
362
 
                    paintDayGrid(painter,sceneRect,exposedRect,rowController,widget);
363
 
                    break;
364
 
                case ScaleWeek:
365
 
                    paintWeekGrid(painter,sceneRect,exposedRect,rowController,widget);
366
 
                    break;
367
 
                case ScaleMonth:
368
 
                    paintMonthGrid(painter,sceneRect,exposedRect,rowController,widget);
369
 
                    break;
370
 
            }
371
 
            break;
372
 
    }
373
 
}
374
 
 
375
 
void DateTimeGrid::paintHourGrid( QPainter* painter,
376
 
                              const QRectF& sceneRect,
377
 
                              const QRectF& exposedRect,
378
 
                              AbstractRowController* rowController,
379
 
                              QWidget* widget )
380
 
{
381
 
    //qDebug()<<"paintHourGrid()"<<scale()<<dayWidth();
382
 
    QDateTime dt = d->chartXtoDateTime( exposedRect.left() );
383
 
    dt.setTime( QTime( dt.time().hour(), 0, 0, 0 ) );
384
 
    for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right(); dt = dt.addSecs( 60*60 ),x=d->dateTimeToChartX( dt ) ) {
385
 
        QPen pen = painter->pen();
386
 
        pen.setBrush( QApplication::palette().dark() );
387
 
        if ( dt.time() == QTime( 23, 0, 0 ) ) {
388
 
            pen.setStyle( Qt::SolidLine );
389
 
        } else {
390
 
            pen.setStyle( Qt::DashLine );
391
 
        }
392
 
        painter->setPen( pen );
393
 
        x += ( dayWidth() / 24.0 ) - 1;
394
 
        painter->drawLine( QPointF( x, exposedRect.top() ), QPointF( x, exposedRect.bottom() ) );
395
 
    }
396
 
}
397
 
 
398
 
void DateTimeGrid::paintDayGrid( QPainter* painter,
399
 
                                  const QRectF& sceneRect,
400
 
                                  const QRectF& exposedRect,
401
 
                                  AbstractRowController* rowController,
402
 
                                  QWidget* widget )
403
 
{
404
 
    //qDebug()<<"paintDayGrid()"<<scale()<<dayWidth();
405
 
    QDateTime dt = d->chartXtoDateTime( exposedRect.left() );
406
 
    dt.setTime( QTime( 0, 0, 0, 0 ) );
407
 
    for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right(); dt = dt.addDays( 1 ),x=d->dateTimeToChartX( dt ) ) {
408
 
        QPen pen = painter->pen();
409
 
        pen.setBrush( QApplication::palette().dark() );
410
 
        if ( dt.date().addDays( 1 ).dayOfWeek() == d->weekStart ) {
411
 
            pen.setStyle( Qt::SolidLine );
412
 
        } else {
413
 
            pen.setStyle( Qt::DashLine );
414
 
        }
415
 
        painter->setPen( pen );
416
 
        paintFreeDay( painter, x, exposedRect, dt.date(), widget );
417
 
        x += dayWidth() - 1;
418
 
        painter->drawLine( QPointF( x, exposedRect.top() ), QPointF( x, exposedRect.bottom() ) );
419
 
    }
420
 
}
421
 
 
422
 
void DateTimeGrid::paintWeekGrid( QPainter* painter,
423
 
                                  const QRectF& sceneRect,
424
 
                                  const QRectF& exposedRect,
425
 
                                  AbstractRowController* rowController,
426
 
                                  QWidget* widget )
427
 
{
428
 
    //qDebug()<<"paintWeekGrid()"<<scale()<<dayWidth();
429
 
    QDateTime dt = d->chartXtoDateTime( exposedRect.left() );
430
 
    dt.setTime( QTime( 0, 0, 0, 0 ) );
431
 
    while ( dt.date().dayOfWeek() != d->weekStart ) dt = dt.addDays( -1 );
432
 
    for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right(); dt = dt.addDays( 1 ),x=d->dateTimeToChartX( dt ) ) {
433
 
        QPen pen = painter->pen();
434
 
        pen.setBrush( QApplication::palette().dark() );
435
 
        if ( dt.date().addDays( 1 ).day() == 1 ) {
436
 
            pen.setStyle( Qt::SolidLine );
437
 
        } else if ( dt.date().addDays( 1 ).dayOfWeek() == d->weekStart ) {
438
 
            pen.setStyle( Qt::DashLine );
439
 
        } else {
440
 
            pen.setStyle( Qt::NoPen );
441
 
        }
442
 
        painter->setPen( pen );
443
 
        paintFreeDay( painter, x, exposedRect, dt.date(), widget );
444
 
        if ( pen.style() != Qt::NoPen ) {
445
 
            //qDebug()<<"paintWeekGrid()"<<dt;
446
 
            x += dayWidth() - 1;
447
 
            painter->drawLine( QPointF( x, exposedRect.top() ), QPointF( x, exposedRect.bottom() ) );
448
 
        }
449
 
    }
450
 
}
451
 
 
452
 
void DateTimeGrid::paintMonthGrid( QPainter* painter,
453
 
                                  const QRectF& sceneRect,
454
 
                                  const QRectF& exposedRect,
455
 
                                  AbstractRowController* rowController,
456
 
                                  QWidget* widget )
457
 
{
458
 
    //qDebug()<<"paintMonthGrid()"<<scale()<<dayWidth();
459
 
    QDateTime dt = d->chartXtoDateTime( exposedRect.left() );
460
 
    dt.setTime( QTime( 0, 0, 0, 0 ) );
461
 
    dt = dt.addDays( 1 - dt.date().day() );
462
 
    for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right(); dt = dt.addDays( 1 ),x=d->dateTimeToChartX( dt ) ) {
463
 
        QPen pen = painter->pen();
464
 
        pen.setBrush( QApplication::palette().dark() );
465
 
        if ( dt.date().addMonths( 1 ).month() == 1 && dt.date().addDays( 1 ).day() == 1 ) {
466
 
            pen.setStyle( Qt::SolidLine );
467
 
        } else if ( dt.date().addDays( 1 ).day() == 1 ) {
468
 
            pen.setStyle( Qt::DashLine );
469
 
        } else {
470
 
            pen.setStyle( Qt::NoPen );
471
 
        }
472
 
        painter->setPen( pen );
473
 
        paintFreeDay( painter, x, exposedRect, dt.date(), widget );
474
 
        if ( pen.style() != Qt::NoPen ) {
475
 
            //qDebug()<<"paintMonthGrid()"<<dt;
476
 
            x += dayWidth() - 1;
477
 
            painter->drawLine( QPointF( x, exposedRect.top() ), QPointF( x, exposedRect.bottom() ) );
478
 
        }
479
 
    }
480
 
}
481
 
 
482
 
void DateTimeGrid::paintFreeDay( QPainter* painter, qreal x, const QRectF& exposedRect, const QDate &dt, QWidget* widget )
483
 
{
484
 
    if ( d->freeDays.contains( static_cast<Qt::DayOfWeek>( dt.dayOfWeek() ) ) ) {
485
 
        //FIXME We now use same color for alternating rows and free days
486
 
        painter->setBrush( widget ? widget->palette().alternateBase() : QApplication::palette().alternateBase() );
487
 
        painter->fillRect( QRectF( x, exposedRect.top(), dayWidth(), exposedRect.height() ), painter->brush() );
488
 
    }
489
 
}
490
 
 
491
 
void DateTimeGrid::paintRowGrid( QPainter* painter,
492
 
                                  const QRectF& sceneRect,
493
 
                                  const QRectF& exposedRect,
494
 
                                  AbstractRowController* rowController,
495
 
                                  QWidget* widget )
496
 
{
497
 
    if ( rowController && rowSeparators() ) {
498
 
        // First draw the rows
499
 
        QPen pen = painter->pen();
500
 
        pen.setBrush( QApplication::palette().dark() );
501
 
        pen.setStyle( Qt::DashLine );
502
 
        painter->setPen( pen );
503
 
        QModelIndex idx = rowController->indexAt( qRound( exposedRect.top() ) );
504
 
        qreal y = 0;
505
 
        while ( y < exposedRect.bottom() && idx.isValid() ) {
506
 
            const Span s = rowController->rowGeometry( idx );
507
 
            y = s.start()+s.length();
508
 
            //painter->drawLine( QPointF( sceneRect.left(), y ), QPointF( sceneRect.right(), y ) );
509
 
            // Is alternating background better?
510
 
            if ( idx.row()%2 ) painter->fillRect( QRectF( exposedRect.x(), s.start(), exposedRect.width(), s.length() ), QApplication::palette().alternateBase() );
511
 
            idx =  rowController->indexBelow( idx );
512
 
        }
513
 
    }
514
 
}
515
 
 
516
 
void DateTimeGrid::render( QPainter* painter,  const QRectF &target, const QRectF& headerRect, const QRectF& exposedRect, QWidget *widget, Qt::AspectRatioMode aspectRatioMode )
517
 
{
518
 
    painter->save();
519
 
    
520
 
    qreal xratio = target.width() / exposedRect.width();
521
 
    qreal yratio = target.height() / exposedRect.height();
522
 
    //qDebug()<<"QGraphicsScene::render()"<<xratio<<yratio;
523
 
    // Scale according to the aspect ratio mode.
524
 
    switch (aspectRatioMode) {
525
 
        case Qt::KeepAspectRatio:
526
 
            xratio = yratio = qMin(xratio, yratio);
527
 
            break;
528
 
        case Qt::KeepAspectRatioByExpanding:
529
 
            xratio = yratio = qMax(xratio, yratio);
530
 
            break;
531
 
        case Qt::IgnoreAspectRatio:
532
 
            break;
533
 
    }
534
 
 
535
 
    //qDebug()<<"DateTimeGrid::render()"<<"target="<<target<<"exposedRect="<<exposedRect<<"xr="<<xratio<<"yr="<<yratio;
536
 
    
537
 
    painter->setClipRect( target );
538
 
    QTransform painterTransform;
539
 
    painterTransform *= QTransform()
540
 
            .translate(target.left(), target.top())
541
 
            .scale(xratio, yratio)
542
 
            .translate(-exposedRect.left(), -exposedRect.top());
543
 
    painter->setWorldTransform(painterTransform, true);
544
 
    
545
 
    paintHeader( painter, headerRect, exposedRect, 0.0, widget );
546
 
    painter->restore();
547
 
}
548
 
 
549
 
void DateTimeGrid::paintHeader( QPainter* painter,  const QRectF& headerRect, const QRectF& exposedRect,
550
 
                                qreal offset, QWidget* widget )
551
 
{
552
 
        switch(scale()) {
553
 
                case ScaleHour: paintHourScaleHeader(painter,headerRect,exposedRect,offset,widget); break;
554
 
        case ScaleDay: paintDayScaleHeader(painter,headerRect,exposedRect,offset,widget); break;
555
 
        case ScaleWeek: paintWeekScaleHeader(painter,headerRect,exposedRect,offset,widget); break;
556
 
        case ScaleMonth: paintMonthScaleHeader(painter,headerRect,exposedRect,offset,widget); break;
557
 
        case ScaleAuto:
558
 
            switch(autoScale()) {
559
 
                case ScaleHour:
560
 
                    paintHourScaleHeader(painter,headerRect,exposedRect,offset,widget);
561
 
                    break;
562
 
                case ScaleDay:
563
 
                    paintDayScaleHeader(painter,headerRect,exposedRect,offset,widget);
564
 
                    break;
565
 
                case ScaleWeek:
566
 
                    paintWeekScaleHeader(painter,headerRect,exposedRect,offset,widget);
567
 
                    break;
568
 
                case ScaleMonth:
569
 
                    paintMonthScaleHeader(painter,headerRect,exposedRect,offset,widget);
570
 
                    break;
571
 
            }
572
 
            break;
573
 
        }
574
 
}
575
 
 
576
 
/*! Paints the hour scale header.
577
 
 * \sa paintHeader()
578
 
 */
579
 
void DateTimeGrid::paintHourScaleHeader( QPainter* painter,  const QRectF& headerRect, const QRectF& exposedRect,
580
 
                                qreal offset, QWidget* widget )
581
 
{
582
 
    QStyle* style = widget?widget->style():QApplication::style();
583
 
 
584
 
    // Paint a section for each hour
585
 
    QDateTime dt = d->chartXtoDateTime( offset+exposedRect.left() );
586
 
    dt.setTime( QTime( dt.time().hour(), 0, 0, 0 ) );
587
 
    for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right()+offset;
588
 
          dt = dt.addSecs( 60*60 /*1 hour*/ ),x=d->dateTimeToChartX( dt ) ) {
589
 
        QStyleOptionHeader opt;
590
 
        opt.init( widget );
591
 
        opt.rect = QRectF( x-offset, headerRect.top()+headerRect.height()/2., dayWidth()/24., headerRect.height()/2. ).toRect();
592
 
        opt.text = dt.time().toString( QString::fromAscii( "hh" ) );
593
 
        opt.textAlignment = Qt::AlignCenter;
594
 
        style->drawControl(QStyle::CE_Header, &opt, painter, widget);
595
 
    }
596
 
 
597
 
    dt = d->chartXtoDateTime( offset+exposedRect.left() );
598
 
    dt.setTime( QTime( 0, 0, 0, 0 ) );
599
 
    // Paint a section for each day
600
 
    for ( qreal x2 = d->dateTimeToChartX( dt ); x2 < exposedRect.right()+offset;
601
 
          dt = dt.addDays( 1 ),x2=d->dateTimeToChartX( dt ) ) {
602
 
        QStyleOptionHeader opt;
603
 
        opt.init( widget );
604
 
        opt.rect = QRectF( x2-offset, headerRect.top(), dayWidth(), headerRect.height()/2. ).toRect();
605
 
        opt.text = QDate::longDayName( dt.date().dayOfWeek() );
606
 
        opt.textAlignment = Qt::AlignCenter;
607
 
        style->drawControl(QStyle::CE_Header, &opt, painter, widget);
608
 
    }
609
 
}
610
 
 
611
 
/*! Paints the day scale header.
612
 
 * \sa paintHeader()
613
 
 */
614
 
void DateTimeGrid::paintDayScaleHeader( QPainter* painter,  const QRectF& headerRect, const QRectF& exposedRect,
615
 
                                qreal offset, QWidget* widget )
616
 
{
617
 
    // For starters, support only the regular tab-per-day look
618
 
    QStyle* style = widget?widget->style():QApplication::style();
619
 
 
620
 
    // Paint a section for each day
621
 
    QDateTime dt = d->chartXtoDateTime( offset+exposedRect.left() );
622
 
    dt.setTime( QTime( 0, 0, 0, 0 ) );
623
 
    for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right()+offset;
624
 
          dt = dt.addDays( 1 ),x=d->dateTimeToChartX( dt ) ) {
625
 
        QStyleOptionHeader opt;
626
 
        opt.init( widget );
627
 
        opt.rect = QRectF( x-offset, headerRect.top()+headerRect.height()/2., dayWidth(), headerRect.height()/2. ).toRect();
628
 
        opt.text = dt.toString( QString::fromAscii( "ddd" ) ).left( 1 );
629
 
        opt.textAlignment = Qt::AlignCenter;
630
 
        style->drawControl(QStyle::CE_Header, &opt, painter, widget);
631
 
    }
632
 
 
633
 
    dt = d->chartXtoDateTime( offset+exposedRect.left() );
634
 
    dt.setTime( QTime( 0, 0, 0, 0 ) );
635
 
    // Go backwards until start of week
636
 
    while ( dt.date().dayOfWeek() != d->weekStart ) dt = dt.addDays( -1 );
637
 
    // Paint a section for each week
638
 
    for ( qreal x2 = d->dateTimeToChartX( dt ); x2 < exposedRect.right()+offset;
639
 
          dt = dt.addDays( 7 ),x2=d->dateTimeToChartX( dt ) ) {
640
 
        QStyleOptionHeader opt;
641
 
        opt.init( widget );
642
 
        opt.rect = QRectF( x2-offset, headerRect.top(), dayWidth()*7., headerRect.height()/2. ).toRect();
643
 
        opt.text = QString::number( dt.date().weekNumber() );
644
 
        opt.textAlignment = Qt::AlignCenter;
645
 
        style->drawControl(QStyle::CE_Header, &opt, painter, widget);
646
 
    }
647
 
}
648
 
 
649
 
/*! Paints the week scale header.
650
 
 * \sa paintHeader()
651
 
 */
652
 
void DateTimeGrid::paintWeekScaleHeader( QPainter* painter,  const QRectF& headerRect, const QRectF& exposedRect,
653
 
                                        qreal offset, QWidget* widget )
654
 
{
655
 
    QStyle* style = widget?widget->style():QApplication::style();
656
 
 
657
 
    // Paint a section for each week
658
 
    QDateTime sdt = d->chartXtoDateTime( offset+exposedRect.left() );
659
 
    sdt.setTime( QTime( 0, 0, 0, 0 ) );
660
 
    // Go backwards until start of week
661
 
    while ( sdt.date().dayOfWeek() != d->weekStart ) sdt = sdt.addDays( -1 );
662
 
    QDateTime dt = sdt;
663
 
    for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right()+offset;
664
 
            dt = dt.addDays( 7 ),x=d->dateTimeToChartX( dt ) ) {
665
 
        QStyleOptionHeader opt;
666
 
        opt.init( widget );
667
 
        opt.rect = QRectF( x-offset, headerRect.top()+headerRect.height()/2., dayWidth()*7, headerRect.height()/2. ).toRect();
668
 
        opt.text = QString::number( dt.date().weekNumber() );
669
 
        opt.textAlignment = Qt::AlignCenter;
670
 
        style->drawControl(QStyle::CE_Header, &opt, painter, widget);
671
 
    }
672
 
 
673
 
    // Paint a section for each month
674
 
    dt = sdt;
675
 
    for ( qreal x2 = d->dateTimeToChartX( dt ); x2 < exposedRect.right()+offset; x2=d->dateTimeToChartX( dt ) ) {
676
 
        //qDebug()<<"paintWeekScaleHeader()"<<dt;
677
 
        QDate next = dt.date().addMonths( 1 );
678
 
        next = next.addDays( 1 - next.day() );
679
 
 
680
 
        QStyleOptionHeader opt;
681
 
        opt.init( widget );
682
 
        opt.rect = QRectF( x2-offset, headerRect.top(), dayWidth()*dt.date().daysTo( next ), headerRect.height()/2. ).toRect();
683
 
        opt.text = QDate::longMonthName( dt.date().month() );
684
 
        opt.textAlignment = Qt::AlignCenter;
685
 
        style->drawControl(QStyle::CE_Header, &opt, painter, widget);
686
 
        
687
 
        dt.setDate( next );
688
 
    }
689
 
}
690
 
 
691
 
/*! Paints the month scale header.
692
 
 * \sa paintHeader()
693
 
 */
694
 
void DateTimeGrid::paintMonthScaleHeader( QPainter* painter,  const QRectF& headerRect, const QRectF& exposedRect,
695
 
                                        qreal offset, QWidget* widget )
696
 
{
697
 
    QStyle* style = widget?widget->style():QApplication::style();
698
 
 
699
 
    // Paint a section for each month
700
 
    QDateTime sdt = d->chartXtoDateTime( offset+exposedRect.left() );
701
 
    sdt.setTime( QTime( 0, 0, 0, 0 ) );
702
 
    sdt = sdt.addDays( 1 - sdt.date().day() );
703
 
    QDateTime dt = sdt;
704
 
    for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right()+offset;
705
 
            dt = dt.addMonths( 1 ),x=d->dateTimeToChartX( dt ) ) {
706
 
        QStyleOptionHeader opt;
707
 
        opt.init( widget );
708
 
        opt.rect = QRectF( x-offset, headerRect.top()+headerRect.height()/2., dayWidth()*dt.date().daysInMonth(), headerRect.height()/2. ).toRect();
709
 
        opt.text = QDate::shortMonthName( dt.date().month() );
710
 
        opt.textAlignment = Qt::AlignCenter;
711
 
        style->drawControl(QStyle::CE_Header, &opt, painter, widget);
712
 
    }
713
 
 
714
 
    // Paint a section for each year
715
 
    dt = sdt;
716
 
    for ( qreal x2 = d->dateTimeToChartX( dt ); x2 < exposedRect.right()+offset; x2=d->dateTimeToChartX( dt ) ) {
717
 
        //qDebug()<<"paintMonthScaleHeader()"<<dt;
718
 
        QDate next = dt.date().addYears( 1 );
719
 
        next = next.addMonths( 1 - next.month() );
720
 
        
721
 
        QStyleOptionHeader opt;
722
 
        opt.init( widget );
723
 
        opt.rect = QRectF( x2-offset, headerRect.top(), dayWidth()*dt.date().daysTo( next ), headerRect.height()/2. ).toRect();
724
 
        opt.text = QString::number( dt.date().year() );
725
 
        opt.textAlignment = Qt::AlignCenter;
726
 
        style->drawControl(QStyle::CE_Header, &opt, painter, widget);
727
 
        
728
 
        dt.setDate( next );
729
 
    }
730
 
}
731
 
 
732
 
#undef d
733
 
 
734
 
#ifndef KDAB_NO_UNIT_TESTS
735
 
 
736
 
#include <QStandardItemModel>
737
 
#include "unittest/test.h"
738
 
 
739
 
namespace {
740
 
    std::ostream& operator<<( std::ostream& os, const QDateTime& dt )
741
 
    {
742
 
        os << dt.toString().toStdString();
743
 
        return os;
744
 
    }
745
 
}
746
 
 
747
 
KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, DateTimeGrid, "test" ) {
748
 
    QStandardItemModel model( 3, 2 );
749
 
    DateTimeGrid grid;
750
 
    QDateTime dt = QDateTime::currentDateTime();
751
 
    grid.setModel( &model );
752
 
    grid.setStartDateTime( dt.addDays( -10 ) );
753
 
 
754
 
    model.setData( model.index( 0, 0 ), dt,               StartTimeRole );
755
 
    model.setData( model.index( 0, 0 ), dt.addDays( 17 ), EndTimeRole );
756
 
 
757
 
    model.setData( model.index( 2, 0 ), dt.addDays( 18 ), StartTimeRole );
758
 
    model.setData( model.index( 2, 0 ), dt.addDays( 19 ), EndTimeRole );
759
 
 
760
 
    Span s = grid.mapToChart( model.index( 0, 0 ) );
761
 
    //qDebug() << "span="<<s;
762
 
 
763
 
    assertTrue( s.start()>0 );
764
 
    assertTrue( s.length()>0 );
765
 
 
766
 
    grid.mapFromChart( s, model.index( 1, 0 ) );
767
 
 
768
 
    QDateTime s1 = model.data( model.index( 0, 0 ), StartTimeRole ).toDateTime();
769
 
    QDateTime e1 = model.data( model.index( 0, 0 ), EndTimeRole ).toDateTime();
770
 
    QDateTime s2 = model.data( model.index( 1, 0 ), StartTimeRole ).toDateTime();
771
 
    QDateTime e2 = model.data( model.index( 1, 0 ), EndTimeRole ).toDateTime();
772
 
 
773
 
    assertTrue( s1.isValid() );
774
 
    assertTrue( e1.isValid() );
775
 
    assertTrue( s2.isValid() );
776
 
    assertTrue( e2.isValid() );
777
 
 
778
 
    assertEqual( s1, s2 );
779
 
    assertEqual( e1, e2 );
780
 
 
781
 
    assertTrue( grid.isSatisfiedConstraint( Constraint( model.index( 0, 0 ), model.index( 2, 0 ) ) ) );
782
 
    assertFalse( grid.isSatisfiedConstraint( Constraint( model.index( 2, 0 ), model.index( 0, 0 ) ) ) );
783
 
 
784
 
    s = grid.mapToChart( model.index( 0, 0 ) );
785
 
    s.setEnd( s.end()+100000. );
786
 
    bool rc = grid.mapFromChart( s, model.index( 0, 0 ) );
787
 
    assertTrue( rc );
788
 
    assertEqual( s1, model.data( model.index( 0, 0 ), StartTimeRole ).toDateTime() );
789
 
    Span newspan = grid.mapToChart( model.index( 0, 0 ) );
790
 
    assertEqual( newspan.start(), s.start() );
791
 
    assertEqual( newspan.length(), s.length() );
792
 
 
793
 
    {
794
 
        QDateTime startDateTime = QDateTime::currentDateTime();
795
 
        qreal dayWidth = 100;
796
 
        QDate currentDate = QDate::currentDate();
797
 
        QDateTime dt( QDate(currentDate.year(), 1, 1),  QTime( 0, 0, 0, 0 ) );
798
 
        assert( dt.isValid() );
799
 
        qreal result = startDateTime.date().daysTo(dt.date())*24.*60.*60.;
800
 
        result += startDateTime.time().msecsTo(dt.time())/1000.;
801
 
        result *= dayWidth/( 24.*60.*60. );
802
 
 
803
 
        int days = static_cast<int>( result/dayWidth );
804
 
        qreal secs = result*( 24.*60.*60. )/dayWidth;
805
 
        QDateTime dt2 = startDateTime;
806
 
        QDateTime result2 = dt2.addDays( days ).addSecs( static_cast<int>(secs-(days*24.*60.*60.) ) ).addMSecs( qRound( ( secs-static_cast<int>( secs ) )*1000. ) );
807
 
 
808
 
        assertEqual( dt, result2 );
809
 
    }
810
 
}
811
 
 
812
 
#endif /* KDAB_NO_UNIT_TESTS */
813
 
 
814
 
#include "moc_kdganttdatetimegrid.cpp"