Initial commit
[keepassx] / src / lib / WaitAnimationWidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2006 by Tarek Saidi                                *
3  *   tarek.saidi@arcor.de                                                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; version 2 of the License.               *
8
9  *                                                                         *
10  *   This program 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         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "WaitAnimationWidget.h"
22
23 WaitAnimationWidget::WaitAnimationWidget(QWidget* parent):QWidget(parent){
24         speed=60;
25         setRefreshRate(25);
26         CurAngle=0;
27         for(int i=0;i<6;i++){
28                 float diff=CurAngle-i*0.16666667f;
29                 if(diff>0.5f)
30                         diff=1.0f-diff;
31                 if(diff<-0.5f)
32                         diff=1.0f+diff;
33                 CircSizes[i]=1.0+exp(-14.0f*diff*diff);
34         }
35         connect(&timer,SIGNAL(timeout()),this,SLOT(refreshAnimation()));
36 }
37
38 WaitAnimationWidget::~WaitAnimationWidget(){
39         timer.stop();
40 }
41
42 void WaitAnimationWidget::start(){
43         timer.start();
44 }
45
46 void WaitAnimationWidget::stop(){
47         timer.stop();
48         repaint();
49 }
50
51 void WaitAnimationWidget::setRefreshRate(int fps){
52         DiffAngle=1.0f/((60.0f/(float)speed)*(float)fps);
53         timer.setInterval((int) ((1.0f/(float)fps)*1000.0f));
54 }
55
56 void WaitAnimationWidget::refreshAnimation(){
57         CurAngle+=DiffAngle;
58         if(CurAngle>1.0f)CurAngle-=1.0f;
59         for(int i=0;i<6;i++){
60                 float diff=CurAngle-i*0.16666667f;
61                 if(diff>0.5f)
62                         diff=1.0f-diff;
63                 if(diff<-0.5f)
64                         diff=1.0f+diff;
65                 CircSizes[i]=1.0+exp(-14.0f*diff*diff);
66         }
67         repaint();
68 }
69
70 void WaitAnimationWidget::paintEvent(QPaintEvent* event){
71         Q_UNUSED(event);
72         if(timer.isActive()){
73                 QPainter painter(this);
74                 painter.setRenderHints(QPainter::Antialiasing,true);
75                 painter.setBrush(Qt::black);
76                 painter.setPen(Qt::black);
77                 for(int i=0;i<6;i++){
78                         float d=CircSizes[i]*5.0;
79                         QRectF rect(CircPositions[i].x()-d/2,CircPositions[i].y()-d/2,d,d);
80                         painter.drawEllipse(rect);
81                 }
82         }
83 }
84
85 void WaitAnimationWidget::resizeEvent(QResizeEvent* event){
86         Q_UNUSED(event);
87         float r;
88         if(width()>height())
89                 r=height()/2;
90         else
91                 r=width()/2;
92         for(int i=0;i<6;i++){
93                 CircPositions[i].setX((r-10)*cos(-2.0*3.14159265*(0.16666667*i))+r);
94                 CircPositions[i].setY((r-10)*sin(-2.0*3.14159265*(0.16666667*i))+r);
95         }
96 }