calendarui/views/dayview/src/calendaystatusstrip.cpp
author hgs
Mon, 28 Jun 2010 15:22:02 +0530
changeset 45 b6db4fd4947b
child 55 2c54b51f39c4
child 58 ef813d54df51
permissions -rw-r--r--
201025
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
 * All rights reserved.
hgs
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
 * under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
 *
hgs
parents:
diff changeset
     9
 * Initial Contributors:
hgs
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
 *
hgs
parents:
diff changeset
    12
 * Contributors:
hgs
parents:
diff changeset
    13
 *
hgs
parents:
diff changeset
    14
 * Description:  Day view control of calendar
hgs
parents:
diff changeset
    15
 *
hgs
parents:
diff changeset
    16
 */
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
#include <QPainter>
hgs
parents:
diff changeset
    19
#include <QPen>
hgs
parents:
diff changeset
    20
hgs
parents:
diff changeset
    21
#include "calendaystatusstrip.h"
hgs
parents:
diff changeset
    22
hgs
parents:
diff changeset
    23
qreal const CalenDayStatusStrip::mMinute = 60;
hgs
parents:
diff changeset
    24
qreal const CalenDayStatusStrip::mMinimumTime = 5;
hgs
parents:
diff changeset
    25
hgs
parents:
diff changeset
    26
/*!
hgs
parents:
diff changeset
    27
  Constructor
hgs
parents:
diff changeset
    28
 */
hgs
parents:
diff changeset
    29
CalenDayStatusStrip::CalenDayStatusStrip(HbWidget *parent)
hgs
parents:
diff changeset
    30
    : HbWidget(parent), mRange(2), 
hgs
parents:
diff changeset
    31
      mDrawingStyle(CalenDayStatusStrip::Filled)      
hgs
parents:
diff changeset
    32
{
hgs
parents:
diff changeset
    33
    setFlag(QGraphicsItem::ItemHasNoContents,false);
hgs
parents:
diff changeset
    34
hgs
parents:
diff changeset
    35
}
hgs
parents:
diff changeset
    36
hgs
parents:
diff changeset
    37
/*!
hgs
parents:
diff changeset
    38
  Destructor
hgs
parents:
diff changeset
    39
 */
hgs
parents:
diff changeset
    40
CalenDayStatusStrip::~CalenDayStatusStrip()
hgs
parents:
diff changeset
    41
{
hgs
parents:
diff changeset
    42
hgs
parents:
diff changeset
    43
}
hgs
parents:
diff changeset
    44
hgs
parents:
diff changeset
    45
/*!
hgs
parents:
diff changeset
    46
   \brief Return range beatween two filled lines in StripWithLines drawing 
hgs
parents:
diff changeset
    47
   styles.
hgs
parents:
diff changeset
    48
   
hgs
parents:
diff changeset
    49
   \sa CalenDayStatusStrip::DrawingStyle, setRange
hgs
parents:
diff changeset
    50
 */
hgs
parents:
diff changeset
    51
qreal CalenDayStatusStrip::range() const
hgs
parents:
diff changeset
    52
{
hgs
parents:
diff changeset
    53
    return mRange;
hgs
parents:
diff changeset
    54
}
hgs
parents:
diff changeset
    55
hgs
parents:
diff changeset
    56
/*!
hgs
parents:
diff changeset
    57
   \brief Return color drawing
hgs
parents:
diff changeset
    58
   
hgs
parents:
diff changeset
    59
   \sa setColor
hgs
parents:
diff changeset
    60
 */
hgs
parents:
diff changeset
    61
QColor CalenDayStatusStrip::color() const
hgs
parents:
diff changeset
    62
{
hgs
parents:
diff changeset
    63
    return mColor;
hgs
parents:
diff changeset
    64
}
hgs
parents:
diff changeset
    65
hgs
parents:
diff changeset
    66
/*!
hgs
parents:
diff changeset
    67
   \brief Return style of drawing.
hgs
parents:
diff changeset
    68
   
hgs
parents:
diff changeset
    69
   \sa CalenDayStatusStrip::DrawingStyle, setDrawingStyle
hgs
parents:
diff changeset
    70
 */
hgs
parents:
diff changeset
    71
CalenDayStatusStrip::DrawingStyle CalenDayStatusStrip::drawingStyle() const
hgs
parents:
diff changeset
    72
{
hgs
parents:
diff changeset
    73
    return mDrawingStyle;
hgs
parents:
diff changeset
    74
}
hgs
parents:
diff changeset
    75
hgs
parents:
diff changeset
    76
/*!
hgs
parents:
diff changeset
    77
   \brief Return orginal start and entime fo event
hgs
parents:
diff changeset
    78
   
hgs
parents:
diff changeset
    79
   \sa setStartEndTime
hgs
parents:
diff changeset
    80
 */
hgs
parents:
diff changeset
    81
QPair<QTime,QTime> CalenDayStatusStrip::startEndTime() const
hgs
parents:
diff changeset
    82
{
hgs
parents:
diff changeset
    83
    return mStartEndEventTime;
hgs
parents:
diff changeset
    84
}
hgs
parents:
diff changeset
    85
hgs
parents:
diff changeset
    86
/*!
hgs
parents:
diff changeset
    87
   \brief It set range beatween two filled lines in StripWithLines drawing 
hgs
parents:
diff changeset
    88
   styles.
hgs
parents:
diff changeset
    89
   
hgs
parents:
diff changeset
    90
   \param range Range beatween two filled lines
hgs
parents:
diff changeset
    91
   
hgs
parents:
diff changeset
    92
   \sa CalenDayStatusStrip::DrawingStyle, range
hgs
parents:
diff changeset
    93
 */
hgs
parents:
diff changeset
    94
void CalenDayStatusStrip::setRange(qreal range)
hgs
parents:
diff changeset
    95
{
hgs
parents:
diff changeset
    96
    mRange = range;
hgs
parents:
diff changeset
    97
}
hgs
parents:
diff changeset
    98
hgs
parents:
diff changeset
    99
/*!
hgs
parents:
diff changeset
   100
   \brief It set color drawing
hgs
parents:
diff changeset
   101
   
hgs
parents:
diff changeset
   102
   \param stripColor Color of drawing
hgs
parents:
diff changeset
   103
   
hgs
parents:
diff changeset
   104
   \sa stripColor
hgs
parents:
diff changeset
   105
 */
hgs
parents:
diff changeset
   106
void CalenDayStatusStrip::setColor(QColor stripColor)
hgs
parents:
diff changeset
   107
{
hgs
parents:
diff changeset
   108
    mColor = stripColor;
hgs
parents:
diff changeset
   109
}
hgs
parents:
diff changeset
   110
hgs
parents:
diff changeset
   111
/*!
hgs
parents:
diff changeset
   112
   \brief It set drawing style of strip 
hgs
parents:
diff changeset
   113
   
hgs
parents:
diff changeset
   114
   \param drawingStyle Style of strip drawing.
hgs
parents:
diff changeset
   115
   
hgs
parents:
diff changeset
   116
   \sa CalenDayStatusStrip::DrawingStyle, drawingStyle
hgs
parents:
diff changeset
   117
 */
hgs
parents:
diff changeset
   118
void 
hgs
parents:
diff changeset
   119
CalenDayStatusStrip::setDrawingStyle(CalenDayStatusStrip::DrawingStyle drawingStyle)
hgs
parents:
diff changeset
   120
{
hgs
parents:
diff changeset
   121
    mDrawingStyle = drawingStyle;
hgs
parents:
diff changeset
   122
}
hgs
parents:
diff changeset
   123
hgs
parents:
diff changeset
   124
/*!
hgs
parents:
diff changeset
   125
   It set start and end time of event
hgs
parents:
diff changeset
   126
   
hgs
parents:
diff changeset
   127
   \param startTime Start of event
hgs
parents:
diff changeset
   128
   \param endTime End of event
hgs
parents:
diff changeset
   129
 */
hgs
parents:
diff changeset
   130
void CalenDayStatusStrip::setStartEndTime(const QTime &startTime, 
hgs
parents:
diff changeset
   131
                                            const QTime &endTime)
hgs
parents:
diff changeset
   132
{
hgs
parents:
diff changeset
   133
    mStartEndEventTime.first = startTime;
hgs
parents:
diff changeset
   134
    mStartEndEventTime.second = endTime;
hgs
parents:
diff changeset
   135
hgs
parents:
diff changeset
   136
    //check if startEndEvent is longer than mMinimumTimeminutes;
hgs
parents:
diff changeset
   137
    if (mStartEndEventTime.first.secsTo(mStartEndEventTime.second) < mMinute * mMinimumTime) {
hgs
parents:
diff changeset
   138
        mStartEndEventTime.second = QTime(mStartEndEventTime.first.hour(),
hgs
parents:
diff changeset
   139
            mStartEndEventTime.first.minute() + mMinimumTime);
hgs
parents:
diff changeset
   140
    }
hgs
parents:
diff changeset
   141
}
hgs
parents:
diff changeset
   142
hgs
parents:
diff changeset
   143
/*!
hgs
parents:
diff changeset
   144
   \brief Reimplemented function...
hgs
parents:
diff changeset
   145
   
hgs
parents:
diff changeset
   146
   Reimplemented function to draw status strip. 
hgs
parents:
diff changeset
   147
   It is based on CalenDayStatusStrip::DrawingStyle, range and color
hgs
parents:
diff changeset
   148
 */
hgs
parents:
diff changeset
   149
void CalenDayStatusStrip::paint(
hgs
parents:
diff changeset
   150
    QPainter *painter,
hgs
parents:
diff changeset
   151
    const QStyleOptionGraphicsItem *option,
hgs
parents:
diff changeset
   152
    QWidget *widget)
hgs
parents:
diff changeset
   153
{
hgs
parents:
diff changeset
   154
    Q_UNUSED(option);
hgs
parents:
diff changeset
   155
    Q_UNUSED(widget);
hgs
parents:
diff changeset
   156
    
hgs
parents:
diff changeset
   157
    //calculate bubble start and end time of bubble
hgs
parents:
diff changeset
   158
    QPair<QTime, QTime> startEndEvent = 
hgs
parents:
diff changeset
   159
        calculateStartEndPostion(mStartEndEventTime.first,
hgs
parents:
diff changeset
   160
                                 mStartEndEventTime.second
hgs
parents:
diff changeset
   161
                                );
hgs
parents:
diff changeset
   162
    //calculate how big is Minute
hgs
parents:
diff changeset
   163
    qreal minuteHeight = calculateMinuteHeight(startEndEvent.first, 
hgs
parents:
diff changeset
   164
                                               startEndEvent.second
hgs
parents:
diff changeset
   165
                                              );
hgs
parents:
diff changeset
   166
hgs
parents:
diff changeset
   167
    painter->save();//   saves the painter state.
hgs
parents:
diff changeset
   168
    
hgs
parents:
diff changeset
   169
hgs
parents:
diff changeset
   170
    //calculate how long is event in minutes
hgs
parents:
diff changeset
   171
    qreal eventMinutes = 
hgs
parents:
diff changeset
   172
           mStartEndEventTime.first.secsTo(mStartEndEventTime.second) / mMinute;
hgs
parents:
diff changeset
   173
    //calculate height and width of status stripe
hgs
parents:
diff changeset
   174
    qreal dx = size().width() - 1;
hgs
parents:
diff changeset
   175
    qreal dy = eventMinutes * minuteHeight;
hgs
parents:
diff changeset
   176
hgs
parents:
diff changeset
   177
    //calculate time from wehre it should be drawed
hgs
parents:
diff changeset
   178
    qreal startTime = 
hgs
parents:
diff changeset
   179
                 startEndEvent.first.secsTo(mStartEndEventTime.first) / mMinute;
hgs
parents:
diff changeset
   180
    //this is done because bubble can be drawed from half hour
hgs
parents:
diff changeset
   181
    startTime = startTime > 30 ? startTime - 30 : startTime;
hgs
parents:
diff changeset
   182
    //calculate status stripe height
hgs
parents:
diff changeset
   183
    qreal startTimeHeight = startTime * minuteHeight;
hgs
parents:
diff changeset
   184
hgs
parents:
diff changeset
   185
    //set bounding rect of drawed area
hgs
parents:
diff changeset
   186
    QRectF bounding(boundingRect());
hgs
parents:
diff changeset
   187
    //set size smaller by 1px in each side
hgs
parents:
diff changeset
   188
    bounding.setRect(bounding.left() + 1, bounding.top() + startTimeHeight, 
hgs
parents:
diff changeset
   189
                     dx - 1, dy - 1
hgs
parents:
diff changeset
   190
                    );
hgs
parents:
diff changeset
   191
hgs
parents:
diff changeset
   192
    //set clip region
hgs
parents:
diff changeset
   193
    painter->setClipRect(bounding, Qt::IntersectClip);
hgs
parents:
diff changeset
   194
hgs
parents:
diff changeset
   195
    //prepare brush and paint
hgs
parents:
diff changeset
   196
    QBrush brush(mColor);
hgs
parents:
diff changeset
   197
    painter->setBrush(brush);
hgs
parents:
diff changeset
   198
    QPen pen;
hgs
parents:
diff changeset
   199
    pen.setWidth(1);
hgs
parents:
diff changeset
   200
    pen.setBrush(brush);
hgs
parents:
diff changeset
   201
    pen.setCapStyle(Qt::RoundCap);
hgs
parents:
diff changeset
   202
    pen.setJoinStyle(Qt::RoundJoin);
hgs
parents:
diff changeset
   203
hgs
parents:
diff changeset
   204
    painter->setPen(pen);
hgs
parents:
diff changeset
   205
    QPointF startPoint(0, dy + dx);
hgs
parents:
diff changeset
   206
    
hgs
parents:
diff changeset
   207
    switch (mDrawingStyle) {
hgs
parents:
diff changeset
   208
        case StripWithLines:
hgs
parents:
diff changeset
   209
            for (int i = 0; startPoint.y() > 0; i++) {
hgs
parents:
diff changeset
   210
                painter->drawPolygon(diagonalLine(startPoint, dx, 3));
hgs
parents:
diff changeset
   211
                startPoint.setY(startPoint.y() - 6 - mRange);
hgs
parents:
diff changeset
   212
            }
hgs
parents:
diff changeset
   213
        case OnlyFrame:
hgs
parents:
diff changeset
   214
            painter->setBrush(Qt::NoBrush);
hgs
parents:
diff changeset
   215
            break;
hgs
parents:
diff changeset
   216
    }
hgs
parents:
diff changeset
   217
hgs
parents:
diff changeset
   218
    //draw rectangle
hgs
parents:
diff changeset
   219
    painter->drawRect(bounding);
hgs
parents:
diff changeset
   220
hgs
parents:
diff changeset
   221
    // restore the painter
hgs
parents:
diff changeset
   222
    painter->restore();
hgs
parents:
diff changeset
   223
}
hgs
parents:
diff changeset
   224
hgs
parents:
diff changeset
   225
/*!
hgs
parents:
diff changeset
   226
   \brief It preapre points to draw filled polygon when StripWithLines style is
hgs
parents:
diff changeset
   227
   on.
hgs
parents:
diff changeset
   228
 */
hgs
parents:
diff changeset
   229
QPolygonF 
hgs
parents:
diff changeset
   230
CalenDayStatusStrip::diagonalLine(QPointF startPoint, qreal dx, qreal dy)
hgs
parents:
diff changeset
   231
{
hgs
parents:
diff changeset
   232
    QPolygonF polygon;
hgs
parents:
diff changeset
   233
    polygon << QPointF(startPoint.x(), startPoint.y());
hgs
parents:
diff changeset
   234
    polygon << QPointF(startPoint.x(), startPoint.y() - dy);
hgs
parents:
diff changeset
   235
    polygon << QPointF(startPoint.x() + dx, startPoint.y() - 2 * dy);
hgs
parents:
diff changeset
   236
    polygon << QPointF(startPoint.x() + dx, startPoint.y() - dy);
hgs
parents:
diff changeset
   237
    return polygon;
hgs
parents:
diff changeset
   238
}
hgs
parents:
diff changeset
   239
hgs
parents:
diff changeset
   240
/*!
hgs
parents:
diff changeset
   241
    Return time for position 0 and height in widget 
hgs
parents:
diff changeset
   242
    
hgs
parents:
diff changeset
   243
    \param startTime Start of event
hgs
parents:
diff changeset
   244
    \param endTime End of event
hgs
parents:
diff changeset
   245
    \return Draw region of bubble
hgs
parents:
diff changeset
   246
  */
hgs
parents:
diff changeset
   247
 QPair<QTime,QTime> CalenDayStatusStrip::calculateStartEndPostion(
hgs
parents:
diff changeset
   248
                                                 const QTime &startTime, 
hgs
parents:
diff changeset
   249
                                                 const QTime &endTime
hgs
parents:
diff changeset
   250
                                             )
hgs
parents:
diff changeset
   251
{
hgs
parents:
diff changeset
   252
     
hgs
parents:
diff changeset
   253
    QTime start;
hgs
parents:
diff changeset
   254
    QTime end;
hgs
parents:
diff changeset
   255
hgs
parents:
diff changeset
   256
    if (startTime.minute() < 30) {
hgs
parents:
diff changeset
   257
        start = QTime(startTime.hour(), 0);
hgs
parents:
diff changeset
   258
    }
hgs
parents:
diff changeset
   259
    else {
hgs
parents:
diff changeset
   260
        start = QTime(startTime.hour(), 30);
hgs
parents:
diff changeset
   261
    }
hgs
parents:
diff changeset
   262
hgs
parents:
diff changeset
   263
    if (endTime.minute() == 0) {
hgs
parents:
diff changeset
   264
        end = endTime;
hgs
parents:
diff changeset
   265
    }
hgs
parents:
diff changeset
   266
    else if (endTime.hour() == 23) {
hgs
parents:
diff changeset
   267
        end = QTime(endTime.hour(), 59);
hgs
parents:
diff changeset
   268
    }
hgs
parents:
diff changeset
   269
    else if (endTime.minute() <= 30) {
hgs
parents:
diff changeset
   270
        end = QTime(endTime.hour(), 30);
hgs
parents:
diff changeset
   271
    }
hgs
parents:
diff changeset
   272
    else {
hgs
parents:
diff changeset
   273
        end = QTime(endTime.hour() + 1, 0);
hgs
parents:
diff changeset
   274
    }
hgs
parents:
diff changeset
   275
hgs
parents:
diff changeset
   276
    return QPair<QTime, QTime> (start, end);
hgs
parents:
diff changeset
   277
}
hgs
parents:
diff changeset
   278
hgs
parents:
diff changeset
   279
/*!
hgs
parents:
diff changeset
   280
   Calculate height of one minute from widget height, and start/end time. 
hgs
parents:
diff changeset
   281
 */
hgs
parents:
diff changeset
   282
qreal CalenDayStatusStrip::calculateMinuteHeight(const QTime &start, 
hgs
parents:
diff changeset
   283
                                                   const QTime &end)
hgs
parents:
diff changeset
   284
{
hgs
parents:
diff changeset
   285
    qreal min = start.secsTo(end) / mMinute;
hgs
parents:
diff changeset
   286
hgs
parents:
diff changeset
   287
    qreal height = size().height();
hgs
parents:
diff changeset
   288
hgs
parents:
diff changeset
   289
    return height / min;
hgs
parents:
diff changeset
   290
}