clock/clockui/clockwidget/src/analogclock.cpp
changeset 45 b6db4fd4947b
parent 23 fd30d51f876b
child 46 ecd7b9840282
equal deleted inserted replaced
23:fd30d51f876b 45:b6db4fd4947b
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 // analogclock.cpp
       
    19 
       
    20 #include <QTime>
       
    21 #include <QTimer>
       
    22 #include <QPainter>
       
    23 #include <QStyleOptionGraphicsItem>
       
    24 #include <QGraphicsWidget>
       
    25 #include <hbiconitem.h>
       
    26 #include <hbstackedlayout.h>
       
    27 
       
    28 #include "analogclock.h"
       
    29 
       
    30 const QString KPrefix(":/clock/");
       
    31 const QString KUnderScore("_");
       
    32 const QString KFaceDay(":/clock/analog_face_day");
       
    33 const QString KHourDay(":/clock/hour_hand_day");
       
    34 const QString KMinDay(":/clock/minute_hand_day");
       
    35 const QString KFaceNight("analog_face_night");
       
    36 const QString KHourNight("hour_hand_night");
       
    37 const QString KMinNight("minute_hand_night");
       
    38 
       
    39 AnalogClock::AnalogClock(QGraphicsWidget *parent)
       
    40 :HbWidget(parent),
       
    41  mIsDay(true),
       
    42  mFirstDraw(true),
       
    43  mCurrentClock(0)
       
    44 {
       
    45 	// Add the required effects.
       
    46 	HbEffect::add(this, 
       
    47 	              QString(":/clock/hide_widget.fxml"), 
       
    48 	              "hide_widget");
       
    49 	
       
    50 	// Construct the images here.
       
    51 	constructImages();
       
    52 
       
    53 	// A single shot timer after which the hands are updated to show the
       
    54 	// correct time.
       
    55 	// TODO: QTimer::singleShot(500, this, SLOT(updateDisplay()));
       
    56 }
       
    57 
       
    58 AnalogClock::~AnalogClock()
       
    59 {
       
    60 	// No implementation yet.
       
    61 }
       
    62 
       
    63 void AnalogClock::showPrev()
       
    64 {
       
    65 	mCurrentClock--;
       
    66 	if (mCurrentClock < 0) {
       
    67 		mCurrentClock = 3;
       
    68 	}
       
    69 	// Start the effect.
       
    70 	HbEffect::start(this, "hide_widget", this, "widget_hidden");
       
    71 }
       
    72 
       
    73 void AnalogClock::showNext()
       
    74 {
       
    75 	mCurrentClock++;
       
    76 	mCurrentClock = mCurrentClock % 4;
       
    77 	
       
    78 	// Start the effect.
       
    79 	HbEffect::start(this, "hide_widget", this, "widget_hidden");
       
    80 }
       
    81 
       
    82 void AnalogClock::updateDisplay()
       
    83 {
       
    84 	// Take the current time.
       
    85 	bool changeClock = false;
       
    86 	QTime timeNow = QTime::currentTime();
       
    87 	int hourNow = timeNow.hour();
       
    88 
       
    89 	if (17 < hourNow || 6 > hourNow) {
       
    90 		if (mIsDay) {
       
    91 			// It was previously day. We need to change the clock
       
    92 			changeClock = true;
       
    93 		}
       
    94 		mIsDay = false;
       
    95 	} else {
       
    96 		if (!mIsDay) {
       
    97 			// It was previously night. We need to change the clock
       
    98 			changeClock = true;
       
    99 		}
       
   100 		mIsDay = true;
       
   101 	}
       
   102 
       
   103 	if (changeClock) {
       
   104 		if (mIsDay) {
       
   105 			mClockFace->setIconName(mFaceName);
       
   106 			mHourHand->setIconName(mHourName);
       
   107 			mMinuteHand->setIconName(mMinuteName);
       
   108 		} else {
       
   109 			mClockFace->setIconName(mFaceName);
       
   110 			mHourHand->setIconName(mHourName);
       
   111 			mMinuteHand->setIconName(mMinuteName);
       
   112 		}
       
   113 	}
       
   114 
       
   115 	// Calculate the rotation angle for hour hand.
       
   116 	qreal hourAngle = 30.0*((timeNow.hour()+timeNow.minute()/60.0)); 
       
   117 	hourAngle += 270;
       
   118 
       
   119 	// Rotate the hour hand.
       
   120 	mHourHand->setTransform(
       
   121 			QTransform().translate(mHourHand->iconItemSize().width()/2, 
       
   122 					mHourHand->iconItemSize().height()/2).rotate(hourAngle)
       
   123 					.translate(-mHourHand->iconItemSize().width()/2,
       
   124 							-mHourHand->iconItemSize().height()/2));
       
   125 
       
   126 	// Rotate the minute hand.
       
   127 	mMinuteHand->setTransform(
       
   128 			QTransform().translate(mMinuteHand->iconItemSize().width()/2, 
       
   129 					mMinuteHand->iconItemSize().height()/2)
       
   130 					.rotate(6.0*(timeNow.minute()+timeNow.second()/60.0))
       
   131 					.translate(-mMinuteHand->iconItemSize().width()/2,
       
   132 							-mMinuteHand->iconItemSize().height()/2));
       
   133 }
       
   134 
       
   135 void AnalogClock::paint(QPainter *painter, 
       
   136                         const QStyleOptionGraphicsItem *option,
       
   137                         QWidget *widget)
       
   138 {
       
   139 	Q_UNUSED(painter)
       
   140 	Q_UNUSED(option)
       
   141 	Q_UNUSED(widget)
       
   142 	
       
   143 	if (mFirstDraw) {
       
   144 		mFirstDraw = false;
       
   145 		
       
   146 		QTime timeNow = QTime::currentTime();
       
   147 		int hourNow = timeNow.hour();
       
   148 
       
   149 		// Calculate the rotation angle for hour hand.
       
   150 		qreal hourAngle = 30.0 * ((timeNow.hour() + timeNow.minute() / 60.0)); 
       
   151 		hourAngle += 270;
       
   152 
       
   153 		// Rotate the hour hand.
       
   154 		mHourHand->setTransform(
       
   155 				QTransform().translate(mHourHand->iconItemSize().width()/2, 
       
   156 						mHourHand->iconItemSize().height()/2).rotate(hourAngle)
       
   157 						.translate(-mHourHand->iconItemSize().width()/2,
       
   158 								-mHourHand->iconItemSize().height()/2));
       
   159 
       
   160 		// Rotate the minute hand.
       
   161 		mMinuteHand->setTransform(
       
   162 				QTransform().translate(mMinuteHand->iconItemSize().width()/2, 
       
   163 						mMinuteHand->iconItemSize().height()/2)
       
   164 						.rotate(6.0*(timeNow.minute()+timeNow.second()/60.0))
       
   165 						.translate(-mMinuteHand->iconItemSize().width()/2,
       
   166 								-mMinuteHand->iconItemSize().height()/2));
       
   167 		
       
   168 	}
       
   169 }
       
   170 
       
   171 void AnalogClock::constructImages()
       
   172 {
       
   173 	mFaceName.append(KFaceDay);
       
   174 	mHourName.append(KHourDay);
       
   175 	mMinuteName.append(KMinDay);
       
   176 	
       
   177 	mParentLayout = new HbStackedLayout(this);
       
   178 
       
   179 	mClockFace = new HbIconItem(mFaceName, this);
       
   180 	mHourHand = new HbIconItem(mHourName, this);
       
   181 	mMinuteHand = new HbIconItem(mMinuteName, this);
       
   182 
       
   183 	mParentLayout->addItem(mClockFace);
       
   184 	mParentLayout->addItem(mHourHand);
       
   185 	mParentLayout->addItem(mMinuteHand);
       
   186 
       
   187 	setLayout(mParentLayout);
       
   188 }
       
   189 
       
   190 void AnalogClock::widget_hidden(const HbEffect::EffectStatus &status)
       
   191 {
       
   192 	Q_UNUSED(status)
       
   193 	
       
   194 	QString clockNum;
       
   195 	clockNum = clockNum.setNum(mCurrentClock);
       
   196 	
       
   197 	mFaceName.clear();
       
   198 	mHourName.clear();
       
   199 	mMinuteName.clear();
       
   200 	
       
   201 	if (mIsDay) {
       
   202 		mFaceName.append(KPrefix);
       
   203 		mFaceName.append(clockNum);
       
   204 		mFaceName.append(KUnderScore);
       
   205 		mFaceName.append(KFaceDay);
       
   206 
       
   207 		mHourName.append(KPrefix);
       
   208 		mHourName.append(clockNum);
       
   209 		mHourName.append(KUnderScore);
       
   210 		mHourName.append(KHourDay);
       
   211 
       
   212 		mMinuteName.append(KPrefix);
       
   213 		mMinuteName.append(clockNum);
       
   214 		mMinuteName.append(KUnderScore);
       
   215 		mMinuteName.append(KMinDay);
       
   216 	} else {
       
   217 		mFaceName.append(KPrefix);
       
   218 		mFaceName.append(clockNum);
       
   219 		mFaceName.append(KUnderScore);
       
   220 		mFaceName.append(KFaceNight);
       
   221 
       
   222 		mHourName.append(KPrefix);
       
   223 		mHourName.append(clockNum);
       
   224 		mHourName.append(KUnderScore);
       
   225 		mHourName.append(KHourNight);
       
   226 
       
   227 		mMinuteName.append(KPrefix);
       
   228 		mMinuteName.append(clockNum);
       
   229 		mMinuteName.append(KUnderScore);
       
   230 		mMinuteName.append(KMinNight);
       
   231 	}
       
   232 	
       
   233 	mClockFace->setIconName(mFaceName);
       
   234 	mHourHand->setIconName(mHourName);
       
   235 	mMinuteHand->setIconName(mMinuteName);
       
   236 	
       
   237 	HbEffect::add(this, QString(":/clock/show_widget.fxml"), "show_widget");
       
   238 	HbEffect::start(this, "show_widget");
       
   239 }
       
   240 
       
   241 // End of file