clock/clockui/clockwidget/clockwidgetimpl/src/analogclockwidget.cpp
changeset 55 2c54b51f39c4
parent 45 b6db4fd4947b
child 57 bb2d3e476f29
--- a/clock/clockui/clockwidget/clockwidgetimpl/src/analogclockwidget.cpp	Mon Jul 12 02:32:28 2010 +0530
+++ b/clock/clockui/clockwidget/clockwidgetimpl/src/analogclockwidget.cpp	Mon Jul 26 13:54:38 2010 +0530
@@ -32,104 +32,153 @@
 
     This class implements the analogclock widget which gets displayed
     in the clockmainview when the clocktype is set to analog type.
-*/
+ */
 
 /*!
     Constructor.
     \param parent The parent of type QGraphicsItem.
 */
-AnalogClockWidget::AnalogClockWidget(QGraphicsItem *parent)
-    : HbWidget(parent)
+AnalogClockWidget::AnalogClockWidget(QGraphicsItem *parent) : 
+		HbWidget(parent)
 {
-    bool result = HbStyleLoader::registerFilePath(":/resource/analogclockwidget.widgetml");
-    result = HbStyleLoader::registerFilePath(":/resource/analogclockwidget.css");
-        
-    updatePrimitives();
-    mTimer = new QTimer(this);
-    connect(mTimer, SIGNAL(timeout()), SLOT(tick()));
-    mTimer->start(clockUpdateInterval);
+	bool result = HbStyleLoader::registerFilePath(
+			":/resource/analogclockwidget.widgetml");
+	result = HbStyleLoader::registerFilePath(
+			":/resource/analogclockwidget.css");
+
+	constructPrimitives();
+	mTimer = new QTimer(this);
+	connect(mTimer, SIGNAL(timeout()), SLOT(tick()));
 }
 
 /*!
     Destructor.
  */
 AnalogClockWidget::~AnalogClockWidget()
-{    
-    mTimer->stop(); 
-    HbStyleLoader::unregisterFilePath(":/resource");
+{
+	mTimer->stop(); 
+	HbStyleLoader::unregisterFilePath(":/resource/analogclockwidget.widgetml");
+	HbStyleLoader::unregisterFilePath(":/resource/analogclockwidget.css");
 }
 
 /*!
-    Handles resize event from HbWidget
+	Constructs the widget primitive items.
  */
-void AnalogClockWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
+void AnalogClockWidget::constructPrimitives()
 {
-    QGraphicsWidget::resizeEvent(event);
-    updatePrimitives();   
+	if (!mClockBackground) {
+		mClockBackground = new HbIconItem(
+				QLatin1String("qtg_graf_clock_day_bg"), this);
+		HbStyle::setItemName(
+				mClockBackground, QLatin1String("clock_background"));
+	}
+	
+	if (!mClockHourHand) {
+		mClockHourHand = new HbIconItem(
+				QLatin1String("qtg_graf_clock_day_hour"), this);
+		HbStyle::setItemName(
+				mClockHourHand, QLatin1String("clock_hour_hand"));
+	}
+	
+	if (!mClockMinuteHand) {
+		mClockMinuteHand = new HbIconItem(
+				QLatin1String("qtg_graf_clock_day_min"), this);
+		HbStyle::setItemName(
+				mClockMinuteHand, QLatin1String("clock_minute_hand"));
+	}
+	
+	if (!mClockSecondHand) {
+		mClockSecondHand = new HbIconItem(
+				QLatin1String("qtg_graf_clock_day_sec"), this);
+		HbStyle::setItemName(
+				mClockSecondHand, QLatin1String("clock_second_hand"));
+	}
 }
 
 /*!
-    @copydoc HbWidget::updatePrimitives()
+	Called on the derived classes to notify in cases when 
+	the style primitives need to be updated.
  */
 void AnalogClockWidget::updatePrimitives()
 {
-    if (!mClockBackground) {
-        mClockBackground = new HbIconItem(QLatin1String("qtg_graf_clock_day_bg"), this);
-        HbStyle::setItemName(mClockBackground, QLatin1String("clock_background"));
-    }
-
-    // Calculate angles for clock hands.
-    QTime time = QTime::currentTime();
-    qreal s = 6 * time.second();
-    qreal m = 6 * (time.minute() + s/360);
-    qreal h = 30 * ((time.hour() % 12) + m/360);
-
+	if (!mClockBackground) {
+		mClockBackground = new HbIconItem(
+				QLatin1String("qtg_graf_clock_day_bg"), this);
+		HbStyle::setItemName(
+				mClockBackground, QLatin1String("clock_background"));
+	}
+	
 	if (!mClockHourHand) {
-        mClockHourHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_hour"), this);
-        HbStyle::setItemName(mClockHourHand, QLatin1String("clock_hour_hand"));
-    }
-
-    int x = mClockHourHand->geometry().width()/2;
-    int y = mClockHourHand->geometry().height()/2;
-    mClockHourHand->setTransform(QTransform().translate(x, y).rotate(h).translate(-x, -y));
+		mClockHourHand = new HbIconItem(
+				QLatin1String("qtg_graf_clock_day_hour"), this);
+		HbStyle::setItemName(
+				mClockHourHand, QLatin1String("clock_hour_hand"));
+	}
+	
+	if (!mClockMinuteHand) {
+		mClockMinuteHand = new HbIconItem(
+				QLatin1String("qtg_graf_clock_day_min"), this);
+		HbStyle::setItemName(
+				mClockMinuteHand, QLatin1String("clock_minute_hand"));
+	}
+	
+	if (!mClockSecondHand) {
+		mClockSecondHand = new HbIconItem(
+				QLatin1String("qtg_graf_clock_day_sec"), this);
+		HbStyle::setItemName(
+				mClockSecondHand, QLatin1String("clock_second_hand"));
+	}
+	
+	QRectF hourHandGeometry = mClockHourHand->geometry();
+	QRectF minHandGeometry = mClockMinuteHand->geometry();
+	QRectF secHandGeometry = mClockSecondHand->geometry();
+	
+	if ((hourHandGeometry.width() && hourHandGeometry.height()) &&
+			(minHandGeometry.width() && minHandGeometry.height()) &&
+			(secHandGeometry.width() && secHandGeometry.height())) {
+		// Calculate angles for clock hands.
+		QTime curTime = QTime::currentTime();
+		qreal seconds = 6 * curTime.second();
+		qreal minutes = 6 * (curTime.minute() + seconds / 360);
+		qreal hours = 30 * ((curTime.hour() % 12) + minutes / 360);
 
-	if (!mClockMinuteHand) {
-        mClockMinuteHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_min"), this);
-        HbStyle::setItemName(mClockMinuteHand, QLatin1String("clock_minute_hand"));
-    }
+		int x = hourHandGeometry.width()/2;
+		int y = hourHandGeometry.height()/2;
+		mClockHourHand->setTransform(
+				QTransform().translate(x, y).rotate(hours).translate(-x, -y));
+		
+		x = minHandGeometry.width()/2;
+		y = minHandGeometry.height()/2;
+		mClockMinuteHand->setTransform(
+				QTransform().translate(x, y).rotate(minutes).translate(-x, -y));
 
-    x = mClockMinuteHand->geometry().width()/2;
-    y = mClockMinuteHand->geometry().height()/2;
-    mClockMinuteHand->setTransform(QTransform().translate(x, y).rotate(m).translate(-x, -y));
-    
-      
-    if (!mClockSecondHand) {
-         mClockSecondHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_sec"), this);
-        HbStyle::setItemName(mClockSecondHand, QLatin1String("clock_second_hand"));
-        }
-
-    x = mClockSecondHand->geometry().width()/2;
-    y = mClockSecondHand->geometry().height()/2;
-    mClockSecondHand->setTransform(QTransform().translate(x, y).rotate(s).translate(-x, -y));
-
+		x = secHandGeometry.width()/2;
+		y = secHandGeometry.height()/2;
+		mClockSecondHand->setTransform(
+				QTransform().translate(x, y).rotate(seconds).translate(-x, -y));
+	}
+	
+	if (!mTimer->isActive()) {
+		mTimer->start(clockUpdateInterval);
+	}
 }
 
 /*!
-    Updates clock visualization according to current time
+    Updates clock visualization according to current time.
  */
 void AnalogClockWidget::tick()
 {
-    updatePrimitives();
-    update();
+	updatePrimitives();
+	update();
 }
 
 /*!
-    Handles polish event
+	Sets the item's geometry to rect.
  */
-void AnalogClockWidget::polish( HbStyleParameters& params ) 
-{  
-    HbWidget::polish(params); 
-    updatePrimitives();
-} 
+void AnalogClockWidget::setGeometry(const QRectF &rect)
+{
+	HbWidget::setGeometry(rect);
+	updatePrimitives();
+}
 
 // End of file  --Don't remove this.