code 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 dimmer::~dimmer()
50 {
51         // iTimer and other widgets are automatically deleted by their parent
52 }
53
54 void dimmer::paintEvent(QPaintEvent *)
55 {
56         QPainter painter(this);
57         QColor dim("black");
58         dim.setAlpha(iAlpha);
59         painter.setPen(dim);
60         painter.setBrush(dim);
61         painter.drawRect(rect());
62
63         painter.setBrush( QApplication::palette().color(QPalette::Highlight) );
64
65         int step = 15;
66         int animMax = 10;
67
68         if( iProgress == -1 ) {
69                 painter.drawEllipse(QPoint(rect().left()+(rect().width()/2-(animMax*step)/2)+iAnim*step,
70                                                         iAnimY), 10, 10);
71         }
72
73         if( iUpdateAnim ) {
74                 iAnim += iAnimDir;
75                 if( iAnim>animMax )
76                         iAnimDir=-1;
77                 if( iAnim==-1 )
78                         iAnimDir=1;
79         }
80
81         if( iProgress >= 0 ) {
82                 if( iProgress>100 )
83                         iProgress = 100;
84
85                 painter.setBrush( QApplication::palette().color(QPalette::Window) );
86                 painter.setPen( dim );
87                 painter.drawRect(rect().left()+30, rect().bottom()-30, rect().right()-rect().left()-60, 10 );
88                 painter.setBrush( QApplication::palette().color(QPalette::Highlight) );
89                 painter.setPen( QApplication::palette().color(QPalette::Highlight) );
90                 int pw = ( rect().right()-rect().left()-60 ) * iProgress / 100;
91                 painter.drawRect(rect().left()+30, rect().bottom()-30, pw, 10 );
92
93                 if( iDownloadSpeed >= 0 ) {
94                         painter.setBrush( QApplication::palette().color(QPalette::BrightText) );
95                         painter.setPen( QApplication::palette().color(QPalette::BrightText) );
96                         QRect textrect(rect().left(),rect().bottom()-70,rect().width(),30);
97                         painter.drawText(textrect,QString("%1 kB/s").arg(iDownloadSpeed),Qt::AlignHCenter|Qt::AlignVCenter);
98                 }
99         }
100 }
101
102 void dimmer::timerEvent()
103 {
104         iUpdateAnim = true;
105         repaint(0,iAnimY-10,rect().width(),22);
106         iUpdateAnim = false;
107 }
108
109 void dimmer::resizeEvent(QResizeEvent *)
110 {
111         this->resize(iParent->size());
112         iLayout->setGeometry(iParent->rect());
113         iLayout->setSizeConstraint(QLayout::SetMaximumSize);
114         iAnimY = this->rect().height()-this->rect().height()/4;
115 }
116
117 void dimmer::orientationChanged()
118 {
119         resizeEvent(0);
120 }
121
122 void dimmer::dim(QString title, QString message)
123 {
124         QString colorname = QApplication::palette().color(QPalette::BrightText).name();
125         iTitle = title;
126         iBusy = true;
127         iLabel->setText("<font color=\"" + colorname + "\"><b><u>" + iTitle + "</u></b><br><br>" + message + "</font>");
128         iProgress = -1;
129         iDownloadSpeed = -1;
130         iAnim = 0;
131         iAnimDir = 1;
132
133         show();
134         iTimer->start(250);
135 }
136
137 void dimmer::updateText(QString message)
138 {
139         QString colorname = QApplication::palette().color(QPalette::BrightText).name();
140         iLabel->setText("<font color=\"" + colorname + "\"><b><u>" + iTitle + "</u></b><br><br>" + message + "</font>");
141 }
142
143 void dimmer::setProgress(int p_)
144 {
145         iProgress = p_;
146         if( p_==-1 )
147                 iDownloadSpeed = -1;
148         repaint(0,rect().bottom()-30,rect().width(),20);
149 }
150
151 void dimmer::setDownloadSpeed(int kbps_)
152 {
153         iDownloadSpeed = kbps_;
154         repaint(QRect(rect().left(),rect().bottom()-70,rect().width(),30));
155 }
156
157 void dimmer::undim()
158 {
159         iBusy = false;
160         iTimer->stop();
161         hide();
162 }