xref: /glogg/src/infoline.cpp (revision bb02e0acf44ddb4e4f83d6127a1e488789162922)
1 /*
2  * Copyright (C) 2009, 2010 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "log.h"
21 
22 #include "infoline.h"
23 
24 #include <QPainter>
25 
26 // This file implements InfoLine. It is responsible for decorating the
27 // widget and managing the completion gauge.
28 
InfoLine()29 InfoLine::InfoLine() :
30     QLabel(), origPalette_( palette() ),
31     backgroundColor_( origPalette_.color( QPalette::Button ) ),
32     darkBackgroundColor_( origPalette_.color( QPalette::Dark ) )
33 {
34     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
35 }
36 
displayGauge(int completion)37 void InfoLine::displayGauge( int completion )
38 {
39     int changeoverX = width() * completion / 100;
40 
41     // Create a gradient for the progress bar
42     QLinearGradient linearGrad( changeoverX - 1, 0, changeoverX + 1, 0 );
43     linearGrad.setColorAt( 0, darkBackgroundColor_ );
44     linearGrad.setColorAt( 1, backgroundColor_ );
45 
46     // Apply the gradient to the current palette (background)
47     QPalette newPalette = origPalette_;
48     newPalette.setBrush( backgroundRole(), QBrush( linearGrad ) );
49     setPalette( newPalette );
50 }
51 
hideGauge()52 void InfoLine::hideGauge()
53 {
54     setPalette( origPalette_ );
55 }
56 
57 // Custom painter: draw the background then call QLabel's painter
paintEvent(QPaintEvent * paintEvent)58 void InfoLine::paintEvent( QPaintEvent* paintEvent )
59 {
60     // Fill the widget background
61     {
62         QPainter painter( this );
63         painter.fillRect( 0, 0, this->width(), this->height(),
64                 palette().brush( backgroundRole() ) );
65     }
66 
67     // Call the parent's painter
68     QLabel::paintEvent( paintEvent );
69 }
70