[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/base/plasma/applets/timer
From:       Davide Bettio <davbet () aliceposta ! it>
Date:       2008-04-19 22:00:44
Message-ID: 1208642444.733001.4221.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 798915 by bettio:

* resize now works.


 M  +78 -28    timer.cpp  
 M  +26 -10    timer.h  


--- trunk/playground/base/plasma/applets/timer/timer.cpp #798914:798915
@@ -1,22 +1,39 @@
+/***************************************************************************
+ *   Copyright 2008 by Davide Bettio <davide.bettio@kdemail.net>           *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
+ ***************************************************************************/
+
 #include "timer.h"
+
 #include <QPainter>
-#include <QFontMetrics>
-#include <QSizeF>
 #include <QTimer>
 #include <KDebug>
 
-#include <plasma/svg.h>
-#include <plasma/theme.h>
+#include <Plasma/Svg>
+#include <Plasma/Theme>
  
 
 Timer::Timer(QObject *parent, const QVariantList &args)
     : Plasma::Applet(parent, args),
-      m_seconds(60),
-      m_runningTimer(false)
+      m_seconds(),
+      m_running(false)
 {
-    // this will get us the standard applet background, for free!
     setDrawStandardBackground(true);
-    resize(200, 200);
+    resize(150, 50);
 }
 
 Timer::~Timer()
@@ -38,7 +55,9 @@
         timer.stop();
         m_runningTimer = false;
     }
+
     if (m_seconds != 0) m_seconds--;
+
     update();
 }
 
@@ -48,10 +67,8 @@
 
     if (m_runningTimer){
         timer.start(1000);
-        kDebug() << ":P :P :p\n";
     }else{
         timer.stop();
-        kDebug() << "STOP\n";
     }
 }
 
@@ -60,37 +77,67 @@
 
 }
 
+int Timer::getHeightFromWidth(int w) const
+{
+    return w / 6;
+}
+
+int Timer::getWidthFromHeight(int h) const
+{
+    return h * 12;
+}
+
+QSizeF Timer::contentSizeHint() const
+{
+    QSizeF sizeHint = geometry().size();
+
+    switch (formFactor()) {
+        case Plasma::Vertical:
+            sizeHint.setHeight(getHeightFromWidth((int) sizeHint.width()));
+            break;
+
+        case Plasma::Horizontal:
+            sizeHint.setWidth(getWidthFromHeight((int) sizeHint.height()));
+            break;
+
+        default:
+            sizeHint.setWidth(getWidthFromHeight((int) sizeHint.height()));
+            break;
+    }
+
+    return sizeHint;
+}
+
 void Timer::wheelEvent(QGraphicsSceneWheelEvent * event)
 {
-    int delta = 0; //10
+    int delta = 0;
+    int w = geometry().height() / 2;
 
-    if ((event->pos().x() > 0) && (event->pos().x() < 25)){
+    if ((event->pos().x() > 0) && (event->pos().x() < w)){
         delta = 36000;
 
-    }else if ((event->pos().x() > 25) && (event->pos().x() < 50)){
+    }else if ((event->pos().x() > w) && (event->pos().x() < w*2)){
         delta = 3600;
 
-    }else if ((event->pos().x() > 50) && (event->pos().x() < 75)){
+    }else if ((event->pos().x() > w*2) && (event->pos().x() < w*3)){
         delta = 600;
 
-    }else if ((event->pos().x() > 75) && (event->pos().x() < 100)){
+    }else if ((event->pos().x() > w*3) && (event->pos().x() < w*4)){
         delta = 60;
 
-    }else if ((event->pos().x() > 100) && (event->pos().x() < 125)){
+    }else if ((event->pos().x() > w*4) && (event->pos().x() < w*5)){
         delta = 10;
 
-    }else if ((event->pos().x() > 125) && (event->pos().x() < 150)){
+    }else if ((event->pos().x() > w*5) && (event->pos().x() < w*6)){
         delta = 1;
     }
 
     if (event->delta() < 0){
-        if (m_seconds > 0){
-            m_seconds = (m_seconds - delta) % 864000;
-        }else{
-            m_seconds = 9;
+        if (m_seconds >= delta){
+            m_seconds = (m_seconds - delta) % 86400;
         }
     }else{
-        m_seconds = (m_seconds + delta) % 864000;
+        m_seconds = (m_seconds + delta) % 86400;
     }
 
     update();
@@ -104,14 +151,17 @@
     int mins = (m_seconds % (60*60)) / 60;
     int seconds =  m_seconds % 60;
 
-    m_svg->paint(p, QRectF(0, 0, 25, 50), QString::number(hours / 10));
-    m_svg->paint(p, QRectF(25, 0, 25, 50), QString::number(hours % 10));
+    int h = contentsRect.height();
+    int w = h / 2;
 
-    m_svg->paint(p, QRectF(50, 0, 25, 50), QString::number(mins / 10));
-    m_svg->paint(p, QRectF(75, 0, 25, 50), QString::number(mins % 10));
+    m_svg->paint(p, QRectF(0, 0, w, h), QString::number(hours / 10));
+    m_svg->paint(p, QRectF(w, 0, w, h), QString::number(hours % 10));
 
-    m_svg->paint(p, QRectF(100, 0, 25, 50), QString::number(seconds / 10));
-    m_svg->paint(p, QRectF(125, 0, 25, 50), QString::number(seconds % 10));
+    m_svg->paint(p, QRectF(w*2, 0, w, h), QString::number(mins / 10));
+    m_svg->paint(p, QRectF(w*3, 0, w, h), QString::number(mins % 10));
+
+    m_svg->paint(p, QRectF(w*4, 0, w, h), QString::number(seconds / 10));
+    m_svg->paint(p, QRectF(w*5, 0, w, h), QString::number(seconds % 10));
 }
 
 #include "timer.moc"
--- trunk/playground/base/plasma/applets/timer/timer.h #798914:798915
@@ -1,22 +1,35 @@
+/***************************************************************************
+ *   Copyright 2008 by Davide Bettio <davide.bettio@kdemail.net>           *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
+ ***************************************************************************/
+
 #ifndef TIMER_HEADER
 #define TIMER_HEADER
-// We need the Plasma Applet headers
-#include <KIcon>
- 
-#include <Plasma/Applet>
-#include <Plasma/Svg>
-#include <plasma/widgets/pushbutton.h>
+
 #include <QGraphicsSceneMouseEvent>
 #include <QTimer>
 
-class QSizeF;
+#include <Plasma/Applet>
+#include <Plasma/Svg>
 
-// Define our plasma Applet
 class Timer : public Plasma::Applet
 {
     Q_OBJECT
     public:
-        // Basic Create/Destroy
         Timer(QObject *parent, const QVariantList &args);
         ~Timer();
 
@@ -28,13 +41,16 @@
         void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
         void mousePressEvent(QGraphicsSceneMouseEvent * event);
         void wheelEvent(QGraphicsSceneWheelEvent * event);
+        QSizeF contentSizeHint() const;
 
     private slots:
         void updateTimer();
 
     private:
         int m_seconds;
-        bool m_runningTimer;
+        bool m_running;
+        int getHeightFromWidth(int w) const;
+        int getWidthFromHeight(int h) const;
 
         QTimer timer;
         Plasma::Svg *m_svg;
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic