clock/clockui/clockwidget/src/skinnableclock.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 // skinnableclock.cpp
       
    19 
       
    20 #include <QTimer>
       
    21 #include <QTime>
       
    22 #include <QGraphicsLinearLayout>
       
    23 #include <hbaction.h>
       
    24 
       
    25 #include "skinnableclock.h"
       
    26 #include "analogclock.h"
       
    27 #include "digitalclock.h"
       
    28 #include "timezoneclient.h"
       
    29 #include "settingsutility.h"
       
    30 
       
    31 SkinnableClock::SkinnableClock(QGraphicsItem *parent)
       
    32 :HbWidget(parent),
       
    33  mAnalogClock(0),
       
    34  mDigitalClock(0),
       
    35  mAnalog(true),
       
    36  mClockFormat24(true),
       
    37  mUpdateNeeded(true)
       
    38 {
       
    39 	// Enable the gestures for the widget and subscribe for "PAN" gestures.
       
    40 /*	QApplication::setAttribute(Qt::AA_EnableGestures);
       
    41 	grabGesture(Qt::PanGesture);*/
       
    42 
       
    43 	qDebug("clock: SkinnableClock::SkinnableClock() -->");
       
    44 
       
    45 	// Construct the settings utility.
       
    46 	mSettingsUtility = new SettingsUtility(this);
       
    47 
       
    48 	qDebug("clock: SkinnableClock::SkinnableClock() - constructed settingsutility.");
       
    49 
       
    50 	// Construct the layout and the widgets.
       
    51 	mLayout = new QGraphicsLinearLayout(Qt::Vertical, this);
       
    52 	mAnalogClock = new AnalogClock(this);
       
    53 	mDigitalClock = new DigitalClock(this);
       
    54 
       
    55     // Set the clock type according to the seetings.
       
    56 	// And construct the corresponding widget.
       
    57 	QStringList clockTypeList;
       
    58 	int typeIndex = mSettingsUtility->clockType(clockTypeList);
       
    59 	if (0 == typeIndex) {
       
    60 		setClockTypeAnalog(true);
       
    61     } else {
       
    62     	setClockTypeAnalog(false);
       
    63     }
       
    64 
       
    65     setLayout(mLayout);
       
    66 
       
    67     // Add the required effects.
       
    68     HbEffect::add(mAnalogClock,
       
    69                   QString(":/clock/disappear.fxml"),
       
    70                   "disappear");
       
    71     HbEffect::add(mDigitalClock,
       
    72                   QString(":/clock/disappear.fxml"),
       
    73                   "disappear");
       
    74     HbEffect::add(mAnalogClock,
       
    75                   QString(":/clock/appear.fxml"),
       
    76                   "appear");
       
    77     HbEffect::add(mDigitalClock,
       
    78                   QString(":/clock/appear.fxml"),
       
    79                   "appear");
       
    80 
       
    81     mClient = new TimezoneClient(this);
       
    82     qDebug("clock: SkinnableClock::SkinnableClock() - constructed tzclient");
       
    83 
       
    84     connect(mClient, SIGNAL(timechanged()),
       
    85             this, SLOT(updateDisplay()));
       
    86     connect(mClient, SIGNAL(timechanged()),
       
    87             this, SLOT(updateClockType()));
       
    88 
       
    89     // Start a timer.
       
    90     mTickTimer = new QTimer(this);
       
    91     QObject::connect(mTickTimer, SIGNAL(timeout()),
       
    92                      this, SLOT(updateDisplay()));
       
    93     // TODO: mTickTimer->start(60000 - 1000 * QTime::currentTime().second());
       
    94     mTickTimer->start(1000);
       
    95 
       
    96     qDebug("clock: SkinnableClock::SkinnableClock() <--");
       
    97 }
       
    98 
       
    99 SkinnableClock::~SkinnableClock()
       
   100 {
       
   101     // No implementation yet.
       
   102 }
       
   103 
       
   104 
       
   105 bool SkinnableClock::clockTypeAnalog()
       
   106 {
       
   107     return mAnalog;
       
   108 }
       
   109 
       
   110 bool SkinnableClock::clockFormat24()
       
   111 {
       
   112     return mClockFormat24;
       
   113 }
       
   114 
       
   115 void SkinnableClock::updateDisplay(bool newTimer)
       
   116 {
       
   117 	if (newTimer) {
       
   118 		// Start the timer again for 1 minute.
       
   119 		// TODO: mTickTimer->start(60000 - 1000 * QTime::currentTime().second());
       
   120 		mTickTimer->start(1000);
       
   121 	}
       
   122 
       
   123 	if (mAnalog) {
       
   124 		mAnalogClock->updateDisplay();
       
   125 	} else {
       
   126 		mDigitalClock->updateDisplay();
       
   127 	}
       
   128 }
       
   129 
       
   130 void SkinnableClock::updateClockType()
       
   131 {
       
   132 	// Update the clock type according to the seetings.
       
   133 	QStringList clockTypeList;
       
   134 	int typeIndex = mSettingsUtility->clockType(clockTypeList);
       
   135 
       
   136 	if ((mAnalog && 1 == typeIndex) || (!mAnalog && 0 == typeIndex)) {
       
   137 	    mUpdateNeeded = true;
       
   138 	    mAnalog = !mAnalog;
       
   139 		mDigitalClock->hide();
       
   140 		mAnalogClock->hide();
       
   141     } else {
       
   142     	mUpdateNeeded = false;
       
   143     }
       
   144 }
       
   145 
       
   146 /*bool SkinnableClock::sceneEvent(QEvent *event)
       
   147 {
       
   148 	if (event->type() == QEvent::GraphicsSceneGesture) {
       
   149 		QGraphicsSceneGestureEvent *gestureEvent =
       
   150 			static_cast<QGraphicsSceneGestureEvent*>(event);
       
   151 		if (const QGesture *gesture = gestureEvent->gesture(Qt::PanGesture)) {
       
   152 			const QPanningGesture *panningGesture =
       
   153 				static_cast<const QPanningGesture*>(gesture);
       
   154 
       
   155 			if (Qt::GestureFinished == gesture->state()) {
       
   156 				if (Qt::LeftDirection == panningGesture->direction()) {
       
   157 					if (mAnalog) {
       
   158 						mAnalogClock->showPrev();
       
   159 					}
       
   160 				} else if (Qt::RightDirection == panningGesture->direction()) {
       
   161 					if (mAnalog) {
       
   162 						mAnalogClock->showNext();
       
   163 					}
       
   164 				} else if (Qt::UpDirection == panningGesture->direction() ||
       
   165 						Qt::DownDirection == panningGesture->direction()) {
       
   166 					startChangeType();
       
   167 				}
       
   168 				event->accept();
       
   169 				return true;
       
   170 			}
       
   171 			event->accept();
       
   172 			return true;
       
   173 		}
       
   174 	}
       
   175 	return HbWidget::sceneEvent(event);
       
   176 }*/
       
   177 
       
   178 void SkinnableClock::paint(QPainter *painter,
       
   179 						   const QStyleOptionGraphicsItem *option,
       
   180 						   QWidget *widget)
       
   181 {
       
   182 	Q_UNUSED(painter)
       
   183 	Q_UNUSED(option)
       
   184 	Q_UNUSED(widget)
       
   185 
       
   186 	if (mUpdateNeeded) {
       
   187 	    setClockTypeAnalog(mAnalog);
       
   188 	    mUpdateNeeded = !mUpdateNeeded;
       
   189     }
       
   190 }
       
   191 
       
   192 void SkinnableClock::setClockTypeAnalog(bool analog)
       
   193 {
       
   194 	if (analog) {
       
   195 		mDigitalClock->hide();
       
   196 		mLayout->removeItem(mDigitalClock);
       
   197 		mAnalogClock->show();
       
   198 		mLayout->addItem(mAnalogClock);
       
   199 	} else {
       
   200 		mAnalogClock->hide();
       
   201 		mLayout->removeItem(mAnalogClock);
       
   202 		mDigitalClock->show();
       
   203 		mLayout->addItem(mDigitalClock);
       
   204 	}
       
   205 
       
   206 	mAnalog = analog;
       
   207 
       
   208 	// Update the display.
       
   209 	updateDisplay(false);
       
   210 }
       
   211 
       
   212 void SkinnableClock::startChangeType()
       
   213 {
       
   214 	if (mAnalog) {
       
   215 		HbEffect::start(mAnalogClock,
       
   216 		                "disappear",
       
   217 		                this,
       
   218 		                "finishChangeType");
       
   219 	} else {
       
   220 		HbEffect::start(mDigitalClock,
       
   221 		                "disappear",
       
   222 		                this,
       
   223 		                "finishChangeType");
       
   224 	}
       
   225 }
       
   226 
       
   227 void SkinnableClock::finishChangeType(const HbEffect::EffectStatus &status)
       
   228 {
       
   229 	Q_UNUSED(status)
       
   230 
       
   231 	setClockTypeAnalog(!mAnalog);
       
   232 
       
   233 	if (mAnalog) {
       
   234 		HbEffect::start(mAnalogClock,
       
   235 		                "appear");
       
   236 	} else {
       
   237 		HbEffect::start(mDigitalClock,
       
   238 		                "appear");
       
   239 	}
       
   240 }
       
   241 
       
   242 // End of file