Logo Search packages:      
Sourcecode: kamoso version File versions

countdownwidget.cpp

/*************************************************************************************
 *  Copyright (C) 2008-2009 by Aleix Pol <aleixpol@kde.org>                          *
 *  Copyright (C) 2008-2009 by Alex Fiestas <alex@eyeos.org>                         *
 *                                                                                   *
 *  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 "countdownwidget.h"

#include <QApplication>
#include <QPainter>
#include <QDebug>
#include <QTimeLine>

static const int colorCount=3;
static const QColor colors[colorCount]={ Qt::red, Qt::yellow, Qt::green };

CountdownWidget::CountdownWidget(QWidget* parent)
      : QWidget(parent)
{}

void CountdownWidget::start(int timeInterval)
{
      timeInterval = 3*timeInterval*1000;//We need miliseconds
      QTimeLine *timer=new QTimeLine(timeInterval, this);
      connect(timer, SIGNAL(valueChanged(qreal)), SLOT(tick(qreal)));
      connect(timer, SIGNAL(finished()), this, SLOT(hide()));
      connect(timer, SIGNAL(finished()), this, SIGNAL(finished()));
      
      timer->setCurveShape(QTimeLine::LinearCurve); //FIXME
      timer->start();
}

void CountdownWidget::tick(qreal progress)
{
      mProgress=progress;
      repaint();
}

void CountdownWidget::paintEvent(QPaintEvent* )
{
      QPainter painter(this);
      painter.setRenderHint(QPainter::Antialiasing);
      
      const int margin=5;
      int rad=height()/2-margin;
      int dist=(width()-rad*colorCount)/(colorCount-1);
      
      int current=int(mProgress*colorCount);
      qDebug() << "XXXX " << left;
      
      for(int i=0; i<colorCount; i++) {
            QColor color=colors[i];
            if(i>=current)
                  color=color.dark(125);
            
            QPointF tl(margin+dist*i+rad, margin+height()/2);
            
            painter.setPen(color);
            painter.setBrush(color);
            painter.drawEllipse(tl, rad, rad);
            
            if(current==i) {
                  QColor color=colors[i];
                  painter.setPen(color);
                  painter.setBrush(color);
                  
                  double progUnit=1./colorCount;
                  double prog=(mProgress-i*progUnit)/progUnit;
                  
                  painter.drawEllipse(tl, rad*prog, rad*prog);
            }
      }
}

Generated by  Doxygen 1.6.0   Back to index