1
/****************************************************************************
2
** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB. All rights reserved.
4
** This file is part of the KD Gantt library.
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).
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.
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.
22
**********************************************************************/
23
#include "kdganttdatetimegrid.h"
24
#include "kdganttdatetimegrid_p.h"
26
#include "kdganttabstractrowcontroller.h"
28
#include <QApplication>
32
#include <QStyleOptionHeader>
38
using namespace KDGantt;
40
/*!\class KDGantt::DateTimeGrid
43
* This implementation of AbstractGrid works with QDateTime
44
* and shows days and week numbers in the header
46
* \todo Extend to work with hours, minutes,... as units too.
49
// TODO: I think maybe this class should be responsible
50
// for unit-transformation of the scene...
52
qreal DateTimeGrid::Private::dateTimeToChartX( const QDateTime& dt ) const
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. );
62
QDateTime DateTimeGrid::Private::chartXtoDateTime( qreal x ) const
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. ) );
76
DateTimeGrid::DateTimeGrid() : AbstractGrid( new Private )
80
DateTimeGrid::~DateTimeGrid()
84
/*! \returns The QDateTime used as start date for the grid.
86
* The default is three days before the current date.
88
QDateTime DateTimeGrid::startDateTime() const
90
return d->startDateTime;
93
/*! \param dt The start date of the grid. It is used as the beginning of the
94
* horizontal scrollbar in the view.
96
* Emits gridChanged() after the start date has changed.
98
void DateTimeGrid::setStartDateTime( const QDateTime& dt )
100
d->startDateTime = dt;
104
/*! \returns The width in pixels for each day in the grid.
106
* The default is 100 pixels.
108
qreal DateTimeGrid::dayWidth() const
113
/*! \param w The width in pixels for each day in the grid.
114
* Day width is limited to minimum 1.0.
116
* The signal gridChanged() is emitted after the day width is changed.
118
void DateTimeGrid::setDayWidth( qreal w )
120
qDebug()<<"DateTimeGrid::setDayWidth"<<w;
121
d->dayWidth = qMax( w, qreal(0.1) );
125
/*! \param s The scale to be used to paint the grid.
127
* The signal gridChanged() is emitted after the scale has changed.
130
void DateTimeGrid::setScale( Scale s )
136
/*! \returns The scale used to paint the grid.
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.
142
DateTimeGrid::Scale DateTimeGrid::scale() const
147
/*! \param factor The zoom factor
150
void DateTimeGrid::zoomIn( qreal factor )
152
setDayWidth( d->dayWidth * factor );
155
/*! \param factor The zoom factor
158
void DateTimeGrid::zoomOut( qreal factor )
160
setDayWidth( d->dayWidth * factor );
163
/*! \param ws The start day of the week.
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.
168
void DateTimeGrid::setWeekStart( Qt::DayOfWeek ws )
174
/*! \returns The start day of the week */
175
Qt::DayOfWeek DateTimeGrid::weekStart() const
180
/*! \param fd A set of days to mark as free in the grid.
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.
186
void DateTimeGrid::setFreeDays( const QSet<Qt::DayOfWeek>& fd )
192
/*! \returns true if row separators are used. */
193
bool DateTimeGrid::rowSeparators() const
195
return d->rowSeparators;
197
/*! \param enable Whether to use row separators or not. */
198
void DateTimeGrid::setRowSeparators( bool enable )
200
d->rowSeparators = enable;
203
/*! \returns The days marked as free in the grid. */
204
QSet<Qt::DayOfWeek> DateTimeGrid::freeDays() const
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.
213
qreal DateTimeGrid::mapToChart( const QVariant& value ) const
215
if ( ! qVariantCanConvert<QDateTime>( value ) ||
216
( value.type() == QVariant::String && qVariantValue<QString>(value).isEmpty() ) )
220
return d->dateTimeToChartX( value.toDateTime() );
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.
227
QVariant DateTimeGrid::mapFromChart( qreal x ) const
229
return d->chartXtoDateTime( x );
232
/*! \param idx The index to get the Span for.
233
* \returns The start and end pixels, in a Span, of the specified index.
235
Span DateTimeGrid::mapToChart( const QModelIndex& idx ) const
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())
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);
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 );
268
static void debug_print_idx( const QModelIndex& idx )
270
if ( !idx.isValid() ) {
271
qDebug() << "[Invalid]";
274
QDateTime st = idx.data( StartTimeRole ).toDateTime();
275
QDateTime et = idx.data( StartTimeRole ).toDateTime();
276
qDebug() << idx << "["<<st<<et<<"]";
280
/*! Maps the supplied Span to QDateTimes, and puts them as start time and
281
* end time for the supplied index.
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.
288
* \returns true if the start time and time was successfully added to the model, or false
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.
294
bool DateTimeGrid::mapFromChart( const Span& span, const QModelIndex& idx,
295
const QList<Constraint>& constraints ) const
298
if ( !idx.isValid() ) return false;
299
assert( idx.model()==model() );
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;
316
return model()->setData( idx, qVariantFromValue(st), StartTimeRole )
317
&& model()->setData( idx, qVariantFromValue(et), EndTimeRole );
320
int DateTimeGrid::autoScale() const
322
int scale = ScaleDay;
323
if ( dayWidth() > 450) {
325
} else if (dayWidth() * 7 < 20) {
327
} else if (dayWidth() < 12) {
333
void DateTimeGrid::paintGrid( QPainter* painter,
334
const QRectF& sceneRect,
335
const QRectF& exposedRect,
336
AbstractRowController* rowController,
339
//qDebug()<<"paintGrid()"<<scale()<<dayWidth();
341
paintRowGrid(painter,sceneRect,exposedRect,rowController,widget);
345
paintHourGrid(painter,sceneRect,exposedRect,rowController,widget);
348
paintDayGrid(painter,sceneRect,exposedRect,rowController,widget);
351
paintWeekGrid(painter,sceneRect,exposedRect,rowController,widget);
354
paintMonthGrid(painter,sceneRect,exposedRect,rowController,widget);
357
switch(autoScale()) {
359
paintHourGrid(painter,sceneRect,exposedRect,rowController,widget);
362
paintDayGrid(painter,sceneRect,exposedRect,rowController,widget);
365
paintWeekGrid(painter,sceneRect,exposedRect,rowController,widget);
368
paintMonthGrid(painter,sceneRect,exposedRect,rowController,widget);
375
void DateTimeGrid::paintHourGrid( QPainter* painter,
376
const QRectF& sceneRect,
377
const QRectF& exposedRect,
378
AbstractRowController* rowController,
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 );
390
pen.setStyle( Qt::DashLine );
392
painter->setPen( pen );
393
x += ( dayWidth() / 24.0 ) - 1;
394
painter->drawLine( QPointF( x, exposedRect.top() ), QPointF( x, exposedRect.bottom() ) );
398
void DateTimeGrid::paintDayGrid( QPainter* painter,
399
const QRectF& sceneRect,
400
const QRectF& exposedRect,
401
AbstractRowController* rowController,
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 );
413
pen.setStyle( Qt::DashLine );
415
painter->setPen( pen );
416
paintFreeDay( painter, x, exposedRect, dt.date(), widget );
418
painter->drawLine( QPointF( x, exposedRect.top() ), QPointF( x, exposedRect.bottom() ) );
422
void DateTimeGrid::paintWeekGrid( QPainter* painter,
423
const QRectF& sceneRect,
424
const QRectF& exposedRect,
425
AbstractRowController* rowController,
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 );
440
pen.setStyle( Qt::NoPen );
442
painter->setPen( pen );
443
paintFreeDay( painter, x, exposedRect, dt.date(), widget );
444
if ( pen.style() != Qt::NoPen ) {
445
//qDebug()<<"paintWeekGrid()"<<dt;
447
painter->drawLine( QPointF( x, exposedRect.top() ), QPointF( x, exposedRect.bottom() ) );
452
void DateTimeGrid::paintMonthGrid( QPainter* painter,
453
const QRectF& sceneRect,
454
const QRectF& exposedRect,
455
AbstractRowController* rowController,
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 );
470
pen.setStyle( Qt::NoPen );
472
painter->setPen( pen );
473
paintFreeDay( painter, x, exposedRect, dt.date(), widget );
474
if ( pen.style() != Qt::NoPen ) {
475
//qDebug()<<"paintMonthGrid()"<<dt;
477
painter->drawLine( QPointF( x, exposedRect.top() ), QPointF( x, exposedRect.bottom() ) );
482
void DateTimeGrid::paintFreeDay( QPainter* painter, qreal x, const QRectF& exposedRect, const QDate &dt, QWidget* widget )
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() );
491
void DateTimeGrid::paintRowGrid( QPainter* painter,
492
const QRectF& sceneRect,
493
const QRectF& exposedRect,
494
AbstractRowController* rowController,
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() ) );
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 );
516
void DateTimeGrid::render( QPainter* painter, const QRectF &target, const QRectF& headerRect, const QRectF& exposedRect, QWidget *widget, Qt::AspectRatioMode aspectRatioMode )
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);
528
case Qt::KeepAspectRatioByExpanding:
529
xratio = yratio = qMax(xratio, yratio);
531
case Qt::IgnoreAspectRatio:
535
//qDebug()<<"DateTimeGrid::render()"<<"target="<<target<<"exposedRect="<<exposedRect<<"xr="<<xratio<<"yr="<<yratio;
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);
545
paintHeader( painter, headerRect, exposedRect, 0.0, widget );
549
void DateTimeGrid::paintHeader( QPainter* painter, const QRectF& headerRect, const QRectF& exposedRect,
550
qreal offset, QWidget* widget )
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;
558
switch(autoScale()) {
560
paintHourScaleHeader(painter,headerRect,exposedRect,offset,widget);
563
paintDayScaleHeader(painter,headerRect,exposedRect,offset,widget);
566
paintWeekScaleHeader(painter,headerRect,exposedRect,offset,widget);
569
paintMonthScaleHeader(painter,headerRect,exposedRect,offset,widget);
576
/*! Paints the hour scale header.
579
void DateTimeGrid::paintHourScaleHeader( QPainter* painter, const QRectF& headerRect, const QRectF& exposedRect,
580
qreal offset, QWidget* widget )
582
QStyle* style = widget?widget->style():QApplication::style();
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;
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);
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;
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);
611
/*! Paints the day scale header.
614
void DateTimeGrid::paintDayScaleHeader( QPainter* painter, const QRectF& headerRect, const QRectF& exposedRect,
615
qreal offset, QWidget* widget )
617
// For starters, support only the regular tab-per-day look
618
QStyle* style = widget?widget->style():QApplication::style();
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;
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);
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;
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);
649
/*! Paints the week scale header.
652
void DateTimeGrid::paintWeekScaleHeader( QPainter* painter, const QRectF& headerRect, const QRectF& exposedRect,
653
qreal offset, QWidget* widget )
655
QStyle* style = widget?widget->style():QApplication::style();
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 );
663
for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right()+offset;
664
dt = dt.addDays( 7 ),x=d->dateTimeToChartX( dt ) ) {
665
QStyleOptionHeader opt;
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);
673
// Paint a section for each month
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() );
680
QStyleOptionHeader opt;
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);
691
/*! Paints the month scale header.
694
void DateTimeGrid::paintMonthScaleHeader( QPainter* painter, const QRectF& headerRect, const QRectF& exposedRect,
695
qreal offset, QWidget* widget )
697
QStyle* style = widget?widget->style():QApplication::style();
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() );
704
for ( qreal x = d->dateTimeToChartX( dt ); x < exposedRect.right()+offset;
705
dt = dt.addMonths( 1 ),x=d->dateTimeToChartX( dt ) ) {
706
QStyleOptionHeader opt;
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);
714
// Paint a section for each year
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() );
721
QStyleOptionHeader opt;
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);
734
#ifndef KDAB_NO_UNIT_TESTS
736
#include <QStandardItemModel>
737
#include "unittest/test.h"
740
std::ostream& operator<<( std::ostream& os, const QDateTime& dt )
742
os << dt.toString().toStdString();
747
KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, DateTimeGrid, "test" ) {
748
QStandardItemModel model( 3, 2 );
750
QDateTime dt = QDateTime::currentDateTime();
751
grid.setModel( &model );
752
grid.setStartDateTime( dt.addDays( -10 ) );
754
model.setData( model.index( 0, 0 ), dt, StartTimeRole );
755
model.setData( model.index( 0, 0 ), dt.addDays( 17 ), EndTimeRole );
757
model.setData( model.index( 2, 0 ), dt.addDays( 18 ), StartTimeRole );
758
model.setData( model.index( 2, 0 ), dt.addDays( 19 ), EndTimeRole );
760
Span s = grid.mapToChart( model.index( 0, 0 ) );
761
//qDebug() << "span="<<s;
763
assertTrue( s.start()>0 );
764
assertTrue( s.length()>0 );
766
grid.mapFromChart( s, model.index( 1, 0 ) );
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();
773
assertTrue( s1.isValid() );
774
assertTrue( e1.isValid() );
775
assertTrue( s2.isValid() );
776
assertTrue( e2.isValid() );
778
assertEqual( s1, s2 );
779
assertEqual( e1, e2 );
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 ) ) ) );
784
s = grid.mapToChart( model.index( 0, 0 ) );
785
s.setEnd( s.end()+100000. );
786
bool rc = grid.mapFromChart( s, model.index( 0, 0 ) );
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() );
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. );
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. ) );
808
assertEqual( dt, result2 );
812
#endif /* KDAB_NO_UNIT_TESTS */
814
#include "moc_kdganttdatetimegrid.cpp"