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