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