cleanup
[fapman] / dimmer.cpp
1 /*
2         This file is part of Faster Application Manager.
3
4         Faster Application Manager is free software: you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation, either version 3 of the License, or
7         (at your option) any later version.
8
9         Faster Application Manager is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with Faster Application Manager.  If not, see <http://www.gnu.org/licenses/>.
16
17         (C) Heikki Holstila 2010
18 */
19
20 #include <QtGui>
21 #include "dimmer.h"
22
23 dimmer::dimmer(QWidget *parent) :
24     QWidget(parent)
25 {
26         iParent = parent;
27         iAlpha = 205;
28         iLayout = new QVBoxLayout(this);
29         iLabel = new QLabel(this);
30         iLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
31         iLabel->setAlignment(Qt::AlignCenter);
32         iLayout->addWidget(iLabel);
33         iBusy = false;
34         iAnim = 0;
35         iAnimDir = 1;
36         iAnimY = this->rect().height()-this->rect().height()/4;
37         iProgress = -1;
38         iUpdateAnim = false;
39         iDownloadSpeed = -1;
40
41         iTimer = new QTimer(this);
42         connect(iTimer,SIGNAL(timeout()),this,SLOT(timerEvent()));
43
44         connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
45
46         hide();
47 }
48
49 void dimmer::paintEvent(QPaintEvent *)
50 {
51         QPainter painter(this);
52         QColor dim("black");
53         dim.setAlpha(iAlpha);
54         painter.setPen(dim);
55         painter.setBrush(dim);
56         painter.drawRect(rect());
57
58         painter.setBrush( QApplication::palette().color(QPalette::Highlight) );
59
60         int step = 15;
61         int animMax = 10;
62
63         if( iProgress == -1 ) {
64                 painter.drawEllipse(QPoint(rect().left()+(rect().width()/2-(animMax*step)/2)+iAnim*step,
65                                                         iAnimY), 10, 10);
66         }
67
68         if( iUpdateAnim ) {
69                 iAnim += iAnimDir;
70                 if( iAnim>animMax )
71                         iAnimDir=-1;
72                 if( iAnim==-1 )
73                         iAnimDir=1;
74         }
75
76         if( iProgress >= 0 ) {
77                 if( iProgress>100 )
78                         iProgress = 100;
79
80                 painter.setBrush( QApplication::palette().color(QPalette::Window) );
81                 painter.setPen( dim );
82                 painter.drawRect(rect().left()+30, rect().bottom()-30, rect().right()-rect().left()-60, 10 );
83                 painter.setBrush( QApplication::palette().color(QPalette::Highlight) );
84                 painter.setPen( QApplication::palette().color(QPalette::Highlight) );
85                 int pw = ( rect().right()-rect().left()-60 ) * iProgress / 100;
86                 painter.drawRect(rect().left()+30, rect().bottom()-30, pw, 10 );
87
88                 if( iDownloadSpeed >= 0 ) {
89                         painter.setBrush( QApplication::palette().color(QPalette::BrightText) );
90                         painter.setPen( QApplication::palette().color(QPalette::BrightText) );
91                         QRect textrect(rect().left(),rect().bottom()-70,rect().width(),30);
92                         painter.drawText(textrect,QString("%1 kB/s").arg(iDownloadSpeed),Qt::AlignHCenter|Qt::AlignVCenter);
93                 }
94         }
95 }
96
97 void dimmer::timerEvent()
98 {
99         iUpdateAnim = true;
100         repaint(0,iAnimY-10,rect().width(),22);
101         iUpdateAnim = false;
102 }
103
104 void dimmer::resizeEvent(QResizeEvent *)
105 {
106         this->resize(iParent->size());
107         iLayout->setGeometry(iParent->rect());
108         iLayout->setSizeConstraint(QLayout::SetMaximumSize);
109         iAnimY = this->rect().height()-this->rect().height()/4;
110 }
111
112 void dimmer::orientationChanged()
113 {
114         resizeEvent(0);
115 }
116
117 void dimmer::dim(QString title, QString message)
118 {
119         QString colorname = QApplication::palette().color(QPalette::BrightText).name();
120         iTitle = title;
121         iBusy = true;
122         iLabel->setText("<font color=\"" + colorname + "\"><b><u>" + iTitle + "</u></b><br><br>" + message + "</font>");
123         iProgress = -1;
124         iDownloadSpeed = -1;
125         iAnim = 0;
126         iAnimDir = 1;
127
128         show();
129         iTimer->start(250);
130 }
131
132 void dimmer::updateText(QString message)
133 {
134         QString colorname = QApplication::palette().color(QPalette::BrightText).name();
135         iLabel->setText("<font color=\"" + colorname + "\"><b><u>" + iTitle + "</u></b><br><br>" + message + "</font>");
136 }
137
138 void dimmer::setProgress(int p_)
139 {
140         iProgress = p_;
141         if( p_==-1 )
142                 iDownloadSpeed = -1;
143         repaint(0,rect().bottom()-30,rect().width(),20);
144 }
145
146 void dimmer::setDownloadSpeed(int kbps_)
147 {
148         iDownloadSpeed = kbps_;
149         repaint(QRect(rect().left(),rect().bottom()-70,rect().width(),30));
150 }
151
152 void dimmer::undim()
153 {
154         iBusy = false;
155         iTimer->stop();
156         hide();
157 }