1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2007-2009 Licq developers
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "skinnablebutton.h"
21 
22 #include <QMouseEvent>
23 #include <QPainter>
24 
25 #include "config/skin.h"
26 
27 using namespace LicqQtGui;
28 
SkinnableButton(const Config::ButtonSkin & skin,const QString & defaultText,QWidget * parent)29 SkinnableButton::SkinnableButton(const Config::ButtonSkin& skin, const QString& defaultText, QWidget* parent)
30   : QPushButton(parent),
31     myDefaultText(defaultText),
32     myPressedModifiers(Qt::NoModifier)
33 {
34   applySkin(skin);
35 }
36 
SkinnableButton(const QString & defaultText,QWidget * parent)37 SkinnableButton::SkinnableButton(const QString& defaultText, QWidget* parent)
38   : QPushButton(defaultText, parent),
39     myDefaultText(defaultText),
40     myPressedModifiers(Qt::NoModifier)
41 {
42 }
43 
applySkin(const Config::ButtonSkin & skin)44 void SkinnableButton::applySkin(const Config::ButtonSkin& skin)
45 {
46   // Load new images, set to null pixmaps if any fails to load
47   myNormalImage = skin.pixmapUpNoFocus;
48   myHighlightedImage = skin.pixmapUpFocus;
49   myPressedImage = skin.pixmapDown;
50 
51   // Set button text (only used if images are not used or not loadable)
52   setText(skin.caption.isNull() ? myDefaultText : skin.caption);
53 
54   // Set colors
55   QPalette pal;
56   if (skin.background.isValid())
57     pal.setColor(QPalette::Window, skin.background);
58   if (skin.foreground.isValid())
59     pal.setColor(QPalette::Text, skin.foreground);
60   setPalette(pal);
61 }
62 
modifiersWhenPressed()63 Qt::KeyboardModifiers SkinnableButton::modifiersWhenPressed()
64 {
65   Qt::KeyboardModifiers b = myPressedModifiers;
66   myPressedModifiers = Qt::NoModifier;
67   return b;
68 }
69 
mousePressEvent(QMouseEvent * e)70 void SkinnableButton::mousePressEvent(QMouseEvent* e)
71 {
72   myPressedModifiers = e->modifiers();
73   QPushButton::mousePressEvent(e);
74 }
75 
enterEvent(QEvent * e)76 void SkinnableButton::enterEvent(QEvent* e)
77 {
78   // Trigger a paintEvent to redraw button in new state
79   update();
80   QPushButton::enterEvent(e);
81 }
82 
leaveEvent(QEvent * e)83 void SkinnableButton::leaveEvent(QEvent* e)
84 {
85   // Trigger a paintEvent to redraw button in new state
86   update();
87   QPushButton::leaveEvent(e);
88 }
89 
paintEvent(QPaintEvent * e)90 void SkinnableButton::paintEvent(QPaintEvent* e)
91 {
92   QPixmap* image;
93 
94   // Check button state to select which image to use
95   if (isDown())
96     image = &myPressedImage;
97   else if (underMouse())
98     image = &myHighlightedImage;
99   else
100     image = &myNormalImage;
101 
102   // Use default paint function if no image is defined for this state
103   if (image->isNull())
104   {
105     QPushButton::paintEvent(e);
106     return;
107   }
108 
109   QPainter p(this);
110   p.drawPixmap(0, 0, *image);
111 }
112