User interface update
[qtmeetings] / src / UserInterface / Components / BorderedBarWidget.cpp
1 #include <QPainter>
2 #include "BorderedBarWidget.h"
3
4 BorderedBarWidget::BorderedBarWidget( QWidget *aParent ) :
5         QWidget( aParent ){
6         iCenterLabel = new QLabel(this);
7         iCenterLabel->setAlignment( Qt::AlignCenter );
8         iLeftLabel = new QLabel(this);
9         iLeftLabel->setAlignment( Qt::AlignLeading | Qt::AlignVCenter);
10         iRightLabel = new QLabel(this);
11         iRightLabel->setAlignment( Qt::AlignTrailing | Qt::AlignVCenter);
12 }
13
14 BorderedBarWidget::~BorderedBarWidget() {
15         delete iCenterLabel;
16         delete iLeftLabel;
17         delete iRightLabel;
18 }
19
20 QColor BorderedBarWidget::backgroundColor()
21 {
22         return iPalette.color( QPalette::Window );;
23 }
24
25 QColor BorderedBarWidget::faceColor()
26 {
27         return iPalette.color( QPalette::WindowText );;
28 }
29
30 int BorderedBarWidget::borderWidth()
31 {
32         return iBorderWidth;
33 }
34
35 QString BorderedBarWidget::text( TextPosition aPos )
36 {
37         if ( aPos == CenterAlign )
38                 return iCenterLabel->text();
39         else if ( aPos == LeftAlign )
40                 return iLeftLabel->text();
41         else if ( aPos == RightAlign )
42                 return iRightLabel->text();
43         return "";
44 }
45
46 void BorderedBarWidget::setBackgroundColor( QColor aColor )
47 {
48         iPalette.setColor( QPalette::Window, aColor );
49         iCenterLabel->setPalette( iPalette );
50         iLeftLabel->setPalette( iPalette );
51         iRightLabel->setPalette( iPalette );
52 }
53
54 void BorderedBarWidget::setFaceColor( QColor aColor )
55 {
56         iPalette.setColor( QPalette::WindowText, aColor );
57         iCenterLabel->setPalette( iPalette );
58         iLeftLabel->setPalette( iPalette );
59         iRightLabel->setPalette( iPalette );
60 }
61
62 void BorderedBarWidget::setBorderWidth( int aWidth )
63 {
64         iBorderWidth = aWidth;
65         iRightLabel->setMargin( 2*iBorderWidth );
66         iLeftLabel->setMargin( 2*iBorderWidth );
67 }
68
69 void BorderedBarWidget::setText( QString aText, TextPosition aPos )
70 {
71         if ( aPos == CenterAlign )
72                 iCenterLabel->setText( aText );
73         else if ( aPos == LeftAlign )
74                 iLeftLabel->setText( aText );
75         else if ( aPos == RightAlign )
76                 iRightLabel->setText( aText );
77 }
78
79 void BorderedBarWidget::setPixmap( QPixmap aPixmap, TextPosition aPos )
80 {
81         if ( aPos == CenterAlign )
82                 iCenterLabel->setPixmap( aPixmap );
83         else if ( aPos == LeftAlign )
84                 iLeftLabel->setPixmap( aPixmap );
85         else if ( aPos == RightAlign )
86                 iRightLabel->setPixmap( aPixmap );
87 }
88
89 void BorderedBarWidget::paintEvent(QPaintEvent *)
90 {
91         drawBorder();
92         iCenterLabel->setGeometry( rect() );
93         iLeftLabel->setGeometry( rect() );
94         iRightLabel->setGeometry( rect() );
95 }
96
97 void BorderedBarWidget::drawCorner( QPainter &aPainter, QPoint &aCenter )
98 {
99         QRadialGradient radialGrad(QPointF(aCenter), iBorderWidth);
100         radialGrad.setColorAt(0, iPalette.color( QPalette::WindowText));
101         radialGrad.setColorAt(1, iPalette.color( QPalette::Window));
102     aPainter.setBrush(radialGrad);
103         aPainter.drawEllipse(QPoint(aCenter), iBorderWidth, iBorderWidth);
104 }
105
106 void BorderedBarWidget::drawSide( QPainter &aPainter, QPoint aStartPoint, QPoint aEndPoint )
107 {
108         QPoint d = aEndPoint - aStartPoint;
109         QPoint gradEnd = aStartPoint;
110         if ( abs( d.x() ) < abs( d.y() ) )
111                 gradEnd.setX( aEndPoint.x()+1 );
112         else
113                 gradEnd.setY( aEndPoint.y()+1 );
114
115         QLinearGradient linearGradTop(aStartPoint, gradEnd);
116         linearGradTop.setColorAt(0, iPalette.color( QPalette::Window));
117         linearGradTop.setColorAt(1, iPalette.color( QPalette::WindowText));
118     aPainter.setBrush(linearGradTop);
119         aPainter.drawRect( QRect(aStartPoint,aEndPoint) );
120 }
121
122 void BorderedBarWidget::drawBorder()
123 {
124         QPainter painter(this);
125     painter.setPen(Qt::NoPen);
126         painter.setRenderHint(QPainter::Antialiasing, true);
127
128         /*top left corner*/
129         QPoint center(iBorderWidth,iBorderWidth);
130         drawCorner( painter, center );
131
132         /*top right corner*/
133         center.setX( this->rect().right()-iBorderWidth );
134         center.setY( iBorderWidth );
135         drawCorner( painter, center );
136
137         /*bottom left corner*/
138         center.setX( iBorderWidth );
139         center.setY( this->rect().bottom()-iBorderWidth );
140         drawCorner( painter, center );
141
142         /*bottom right corner*/
143         center.setX( this->rect().right()-iBorderWidth );
144         center.setY( this->rect().bottom()-iBorderWidth );
145         drawCorner( painter, center );
146
147         /*top*/
148         drawSide( painter, QPoint(iBorderWidth,0), QPoint(this->rect().right()-(iBorderWidth+1),(iBorderWidth-1)) );
149
150         /*right*/
151         drawSide( painter, QPoint(this->rect().right(),this->rect().bottom()-iBorderWidth), QPoint(this->rect().right()-(iBorderWidth+1),iBorderWidth-1) );
152
153         /*bottom*/
154         drawSide( painter, QPoint(this->rect().right()-iBorderWidth, this->rect().bottom()), QPoint((iBorderWidth-1),this->rect().bottom()-(iBorderWidth+1)) );
155
156         /*left*/
157         drawSide( painter, QPoint(0,iBorderWidth), QPoint((iBorderWidth-1), this->rect().bottom()-(iBorderWidth+1)) );
158
159         /*inside*/
160         QBrush brush( iPalette.color( QPalette::Window ) );
161         painter.setBrush(brush);
162         QRect inside(iBorderWidth,iBorderWidth,this->rect().right()-2*iBorderWidth,this->rect().bottom()-2*iBorderWidth);
163         painter.drawRoundRect(inside,iBorderWidth,iBorderWidth);
164
165 }