[Panel-devel] Smooth movement of clock hands on plasma applet

Fred Emmott mail at fredemmott.co.uk
Sat May 26 23:50:42 CEST 2007


Hi,

The attached patch does 3 changes:
- Adds a 'Milliseconds' key to the 'time' data source
- Decreases the time between updates to 50msec
- Adjusts the clock applet to take milliseconds into account when
rendering the second and minute hands

Advantages:
- The clock hands appear to be constantly moving smoothly (shiny/sexy :D
)

Disadvantages:
- Eats CPU (plasma at 30% useage of one core on an athlon64 4200+
- Wakes up the CPU 20 times a second (power usage)

If this is actually integrated, I'd suggest making the amount of time
between updates in the engine user-configurable, and preferably
integrated into performance vs shinyness bit of kpersonalizer or KDE4
equivalent; 500msec for low end, 50 for high-end.

Regards,

-- 
Fred Emmott
(http://www.fredemmott.co.uk)
(http://www.slamd64.com)
-------------- next part --------------
Index: applets/clock/clock.h
===================================================================
--- applets/clock/clock.h	(revision 668524)
+++ applets/clock/clock.h	(working copy)
@@ -68,6 +68,7 @@
         QTimer *m_timer;
         Plasma::Svg* m_theme;
         QTime m_time;
+	int m_msec;
 };
 
 K_EXPORT_PLASMA_APPLET(clock, Clock)
Index: applets/clock/clock.cpp
===================================================================
--- applets/clock/clock.cpp	(revision 668524)
+++ applets/clock/clock.cpp	(working copy)
@@ -66,6 +66,7 @@
 {
     Q_UNUSED(source)
     m_time = data[i18n("Local")].toTime();
+    m_msec = data[i18n("Milliseconds")].toInt();
     QGraphicsItem::update();
 }
 
@@ -81,8 +82,8 @@
     Q_UNUSED(option)
     p->setRenderHint(QPainter::SmoothPixmapTransform);
 
-    qreal seconds = 6.0 * m_time.second() - 180;
-    qreal minutes = 6.0 * m_time.minute() - 180;
+    qreal seconds = 6.0 * (m_time.second() + (m_msec / 999.0)) - 180;
+    qreal minutes = 6.0 * (m_time.minute() + ((m_time.second() + (m_msec/999.0))/ 59.999)) - 180;
     qreal hours = 30.0 * m_time.hour() - 180 + ((m_time.minute() / 59.0) * 30.0);
 
     QRectF tempRect(0, 0, 0, 0);
@@ -137,7 +138,7 @@
     QString time = m_time.toString();
     QFontMetrics fm(QApplication::font());
     p->drawText(static_cast<int>(boundRect.width()/2 - fm.width(time) / 2),
-                static_cast<int>((boundRect.height()/2) - fm.xHeight()*3), m_time.toString());
+                static_cast<int>((boundRect.height()/2) - fm.xHeight()*3), time);
 
     m_theme->resize(boundSize);
     m_theme->paint(p, boundRect, "Glass");
Index: engines/time/timeengine.cpp
===================================================================
--- engines/time/timeengine.cpp	(revision 668524)
+++ engines/time/timeengine.cpp	(working copy)
@@ -34,7 +34,7 @@
     m_timer = new QTimer(this);
     m_timer->setSingleShot(false);
     connect(m_timer, SIGNAL(timeout()), this, SLOT(updateTime()));
-    m_timer->start(500);
+    m_timer->start(50);
     updateTime();
 }
 
@@ -49,6 +49,7 @@
 void TimeEngine::updateTime()
 {
     //TODO: should these keys be translated? probably not, methinks.
+    setData("time", i18n("Milliseconds"), QTime::currentTime().msec());
     setData("time", i18n("Local"), QTime::currentTime());
     setData("date", i18n("Date"), QDate::currentDate());
 }


More information about the Panel-devel mailing list