• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
circular-countdown.cpp
Go to the documentation of this file.
1 /*
2  Circular countdown widget
3  Copyright (C) 2011 Martin Klapetek <martin.klapetek@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #include "circular-countdown.h"
21 
22 #include <QPainter>
23 #include <QPaintEvent>
24 #include <QTimeLine>
25 
26 namespace KTp
27 {
28 
29 class CircularCountdown::Private
30 {
31 public:
32  Private(CircularCountdown *parent)
33  : q(parent)
34  {
35 
36  }
37 
38  CircularCountdown *q;
39  QTimeLine *timeLine;
40 };
41 
42 CircularCountdown::CircularCountdown(int msec, QWidget *parent)
43  : QWidget(parent),
44  d(new Private(this))
45 {
46  setAutoFillBackground(false);
47 
48  d->timeLine = new QTimeLine(msec, this);
49  //circle has 360 degrees, for better smoothness we use 2x as much
50  d->timeLine->setFrameRange(0, 720);
51  //to paint the subtraction animation, we start from full circle to 0
52  d->timeLine->setDirection(QTimeLine::Backward);
53 
54  //repaint on every frame change for smooth animation
55  connect(d->timeLine, SIGNAL(frameChanged(int)), this, SLOT(repaint()));
56 
57  //repaint after animation is finished
58  connect(d->timeLine, SIGNAL(finished()), this, SLOT(repaint()));
59 
60  //emit timeoutReached() when the timeout is reached
61  connect(d->timeLine, SIGNAL(finished()), this, SIGNAL(timeout()));
62 }
63 
64 CircularCountdown::~CircularCountdown()
65 {
66 }
67 
68 void CircularCountdown::paintEvent(QPaintEvent *event) {
69  Q_UNUSED(event);
70 
71  if (d->timeLine->state() == QTimeLine::Running || d->timeLine->state() == QTimeLine::Paused) {
72  QPainter painter(this);
73  //always take parent widget's palette and use it's Base color
74  painter.setBrush(QBrush(parentWidget()->palette().color(QPalette::Base), Qt::SolidPattern));
75  painter.setRenderHint(QPainter::Antialiasing);
76  /* drawPie always paints 1/16th of a degree, the total circle is 5760 (16 * 360)
77  * the first argument is this widget size with 2px padding
78  * second argument is start position, which is 3 o'clock by default,
79  * to move it to 12 o'clock we need to start at 90 degrees, hence 90 * 16
80  * third argument tells how much of the current circle is painted
81  * the range is [0..720], hence the *8 (to get 5760 in total)
82  * and it's minus because we want it to rotate in the other direction
83  */
84  painter.drawPie(this->rect().adjusted(2, 2, -2, -2), 90*16, -d->timeLine->currentFrame()*8);
85  }
86 }
87 
88 QSize CircularCountdown::sizeHint() const
89 {
90  return QSize(16, 16);
91 }
92 
93 void CircularCountdown::setDuration(int msec) {
94  d->timeLine->setDuration(msec);
95 }
96 
97 int CircularCountdown::duration() const
98 {
99  return d->timeLine->duration();
100 }
101 
102 void CircularCountdown::start() {
103  d->timeLine->start();
104 }
105 
106 void CircularCountdown::stop() {
107  d->timeLine->stop();
108 }
109 
110 void CircularCountdown::pause() {
111  //no, there really is no ->pause() if you're thinking about that ;)
112  d->timeLine->setPaused(true);
113 }
114 
115 void CircularCountdown::resume() {
116  d->timeLine->resume();
117 }
118 
119 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal