clock/clockui/clockwidget/clockwidgetimpl/src/analogclockwidget.cpp
changeset 55 2c54b51f39c4
parent 45 b6db4fd4947b
child 57 bb2d3e476f29
equal deleted inserted replaced
51:0b38fc5b94c6 55:2c54b51f39c4
    30 /*!
    30 /*!
    31     \class AnalogClockWidget
    31     \class AnalogClockWidget
    32 
    32 
    33     This class implements the analogclock widget which gets displayed
    33     This class implements the analogclock widget which gets displayed
    34     in the clockmainview when the clocktype is set to analog type.
    34     in the clockmainview when the clocktype is set to analog type.
    35 */
    35  */
    36 
    36 
    37 /*!
    37 /*!
    38     Constructor.
    38     Constructor.
    39     \param parent The parent of type QGraphicsItem.
    39     \param parent The parent of type QGraphicsItem.
    40 */
    40 */
    41 AnalogClockWidget::AnalogClockWidget(QGraphicsItem *parent)
    41 AnalogClockWidget::AnalogClockWidget(QGraphicsItem *parent) : 
    42     : HbWidget(parent)
    42 		HbWidget(parent)
    43 {
    43 {
    44     bool result = HbStyleLoader::registerFilePath(":/resource/analogclockwidget.widgetml");
    44 	bool result = HbStyleLoader::registerFilePath(
    45     result = HbStyleLoader::registerFilePath(":/resource/analogclockwidget.css");
    45 			":/resource/analogclockwidget.widgetml");
    46         
    46 	result = HbStyleLoader::registerFilePath(
    47     updatePrimitives();
    47 			":/resource/analogclockwidget.css");
    48     mTimer = new QTimer(this);
    48 
    49     connect(mTimer, SIGNAL(timeout()), SLOT(tick()));
    49 	constructPrimitives();
    50     mTimer->start(clockUpdateInterval);
    50 	mTimer = new QTimer(this);
       
    51 	connect(mTimer, SIGNAL(timeout()), SLOT(tick()));
    51 }
    52 }
    52 
    53 
    53 /*!
    54 /*!
    54     Destructor.
    55     Destructor.
    55  */
    56  */
    56 AnalogClockWidget::~AnalogClockWidget()
    57 AnalogClockWidget::~AnalogClockWidget()
    57 {    
    58 {
    58     mTimer->stop(); 
    59 	mTimer->stop(); 
    59     HbStyleLoader::unregisterFilePath(":/resource");
    60 	HbStyleLoader::unregisterFilePath(":/resource/analogclockwidget.widgetml");
       
    61 	HbStyleLoader::unregisterFilePath(":/resource/analogclockwidget.css");
    60 }
    62 }
    61 
    63 
    62 /*!
    64 /*!
    63     Handles resize event from HbWidget
    65 	Constructs the widget primitive items.
    64  */
    66  */
    65 void AnalogClockWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
    67 void AnalogClockWidget::constructPrimitives()
    66 {
    68 {
    67     QGraphicsWidget::resizeEvent(event);
    69 	if (!mClockBackground) {
    68     updatePrimitives();   
    70 		mClockBackground = new HbIconItem(
       
    71 				QLatin1String("qtg_graf_clock_day_bg"), this);
       
    72 		HbStyle::setItemName(
       
    73 				mClockBackground, QLatin1String("clock_background"));
       
    74 	}
       
    75 	
       
    76 	if (!mClockHourHand) {
       
    77 		mClockHourHand = new HbIconItem(
       
    78 				QLatin1String("qtg_graf_clock_day_hour"), this);
       
    79 		HbStyle::setItemName(
       
    80 				mClockHourHand, QLatin1String("clock_hour_hand"));
       
    81 	}
       
    82 	
       
    83 	if (!mClockMinuteHand) {
       
    84 		mClockMinuteHand = new HbIconItem(
       
    85 				QLatin1String("qtg_graf_clock_day_min"), this);
       
    86 		HbStyle::setItemName(
       
    87 				mClockMinuteHand, QLatin1String("clock_minute_hand"));
       
    88 	}
       
    89 	
       
    90 	if (!mClockSecondHand) {
       
    91 		mClockSecondHand = new HbIconItem(
       
    92 				QLatin1String("qtg_graf_clock_day_sec"), this);
       
    93 		HbStyle::setItemName(
       
    94 				mClockSecondHand, QLatin1String("clock_second_hand"));
       
    95 	}
    69 }
    96 }
    70 
    97 
    71 /*!
    98 /*!
    72     @copydoc HbWidget::updatePrimitives()
    99 	Called on the derived classes to notify in cases when 
       
   100 	the style primitives need to be updated.
    73  */
   101  */
    74 void AnalogClockWidget::updatePrimitives()
   102 void AnalogClockWidget::updatePrimitives()
    75 {
   103 {
    76     if (!mClockBackground) {
   104 	if (!mClockBackground) {
    77         mClockBackground = new HbIconItem(QLatin1String("qtg_graf_clock_day_bg"), this);
   105 		mClockBackground = new HbIconItem(
    78         HbStyle::setItemName(mClockBackground, QLatin1String("clock_background"));
   106 				QLatin1String("qtg_graf_clock_day_bg"), this);
    79     }
   107 		HbStyle::setItemName(
       
   108 				mClockBackground, QLatin1String("clock_background"));
       
   109 	}
       
   110 	
       
   111 	if (!mClockHourHand) {
       
   112 		mClockHourHand = new HbIconItem(
       
   113 				QLatin1String("qtg_graf_clock_day_hour"), this);
       
   114 		HbStyle::setItemName(
       
   115 				mClockHourHand, QLatin1String("clock_hour_hand"));
       
   116 	}
       
   117 	
       
   118 	if (!mClockMinuteHand) {
       
   119 		mClockMinuteHand = new HbIconItem(
       
   120 				QLatin1String("qtg_graf_clock_day_min"), this);
       
   121 		HbStyle::setItemName(
       
   122 				mClockMinuteHand, QLatin1String("clock_minute_hand"));
       
   123 	}
       
   124 	
       
   125 	if (!mClockSecondHand) {
       
   126 		mClockSecondHand = new HbIconItem(
       
   127 				QLatin1String("qtg_graf_clock_day_sec"), this);
       
   128 		HbStyle::setItemName(
       
   129 				mClockSecondHand, QLatin1String("clock_second_hand"));
       
   130 	}
       
   131 	
       
   132 	QRectF hourHandGeometry = mClockHourHand->geometry();
       
   133 	QRectF minHandGeometry = mClockMinuteHand->geometry();
       
   134 	QRectF secHandGeometry = mClockSecondHand->geometry();
       
   135 	
       
   136 	if ((hourHandGeometry.width() && hourHandGeometry.height()) &&
       
   137 			(minHandGeometry.width() && minHandGeometry.height()) &&
       
   138 			(secHandGeometry.width() && secHandGeometry.height())) {
       
   139 		// Calculate angles for clock hands.
       
   140 		QTime curTime = QTime::currentTime();
       
   141 		qreal seconds = 6 * curTime.second();
       
   142 		qreal minutes = 6 * (curTime.minute() + seconds / 360);
       
   143 		qreal hours = 30 * ((curTime.hour() % 12) + minutes / 360);
    80 
   144 
    81     // Calculate angles for clock hands.
   145 		int x = hourHandGeometry.width()/2;
    82     QTime time = QTime::currentTime();
   146 		int y = hourHandGeometry.height()/2;
    83     qreal s = 6 * time.second();
   147 		mClockHourHand->setTransform(
    84     qreal m = 6 * (time.minute() + s/360);
   148 				QTransform().translate(x, y).rotate(hours).translate(-x, -y));
    85     qreal h = 30 * ((time.hour() % 12) + m/360);
   149 		
       
   150 		x = minHandGeometry.width()/2;
       
   151 		y = minHandGeometry.height()/2;
       
   152 		mClockMinuteHand->setTransform(
       
   153 				QTransform().translate(x, y).rotate(minutes).translate(-x, -y));
    86 
   154 
    87 	if (!mClockHourHand) {
   155 		x = secHandGeometry.width()/2;
    88         mClockHourHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_hour"), this);
   156 		y = secHandGeometry.height()/2;
    89         HbStyle::setItemName(mClockHourHand, QLatin1String("clock_hour_hand"));
   157 		mClockSecondHand->setTransform(
    90     }
   158 				QTransform().translate(x, y).rotate(seconds).translate(-x, -y));
    91 
   159 	}
    92     int x = mClockHourHand->geometry().width()/2;
   160 	
    93     int y = mClockHourHand->geometry().height()/2;
   161 	if (!mTimer->isActive()) {
    94     mClockHourHand->setTransform(QTransform().translate(x, y).rotate(h).translate(-x, -y));
   162 		mTimer->start(clockUpdateInterval);
    95 
   163 	}
    96 	if (!mClockMinuteHand) {
       
    97         mClockMinuteHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_min"), this);
       
    98         HbStyle::setItemName(mClockMinuteHand, QLatin1String("clock_minute_hand"));
       
    99     }
       
   100 
       
   101     x = mClockMinuteHand->geometry().width()/2;
       
   102     y = mClockMinuteHand->geometry().height()/2;
       
   103     mClockMinuteHand->setTransform(QTransform().translate(x, y).rotate(m).translate(-x, -y));
       
   104     
       
   105       
       
   106     if (!mClockSecondHand) {
       
   107          mClockSecondHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_sec"), this);
       
   108         HbStyle::setItemName(mClockSecondHand, QLatin1String("clock_second_hand"));
       
   109         }
       
   110 
       
   111     x = mClockSecondHand->geometry().width()/2;
       
   112     y = mClockSecondHand->geometry().height()/2;
       
   113     mClockSecondHand->setTransform(QTransform().translate(x, y).rotate(s).translate(-x, -y));
       
   114 
       
   115 }
   164 }
   116 
   165 
   117 /*!
   166 /*!
   118     Updates clock visualization according to current time
   167     Updates clock visualization according to current time.
   119  */
   168  */
   120 void AnalogClockWidget::tick()
   169 void AnalogClockWidget::tick()
   121 {
   170 {
   122     updatePrimitives();
   171 	updatePrimitives();
   123     update();
   172 	update();
   124 }
   173 }
   125 
   174 
   126 /*!
   175 /*!
   127     Handles polish event
   176 	Sets the item's geometry to rect.
   128  */
   177  */
   129 void AnalogClockWidget::polish( HbStyleParameters& params ) 
   178 void AnalogClockWidget::setGeometry(const QRectF &rect)
   130 {  
   179 {
   131     HbWidget::polish(params); 
   180 	HbWidget::setGeometry(rect);
   132     updatePrimitives();
   181 	updatePrimitives();
   133 } 
   182 }
   134 
   183 
   135 // End of file  --Don't remove this.
   184 // End of file  --Don't remove this.