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 "skinnablelabel.h"
21 
22 #include <QMenu>
23 #include <QMouseEvent>
24 #include <QPainter>
25 
26 #include "config/skin.h"
27 
28 using namespace LicqQtGui;
29 
SkinnableLabel(const Config::LabelSkin & skin,QMenu * popupMenu,QWidget * parent)30 SkinnableLabel::SkinnableLabel(const Config::LabelSkin& skin, QMenu* popupMenu, QWidget* parent)
31   : QLabel(parent),
32     myPopupMenu(popupMenu)
33 {
34   applySkin(skin);
35 }
36 
SkinnableLabel(QMenu * popupMenu,QWidget * parent)37 SkinnableLabel::SkinnableLabel(QMenu* popupMenu, QWidget* parent)
38   : QLabel(parent),
39     myPopupMenu(popupMenu)
40 {
41 }
42 
applySkin(const Config::LabelSkin & skin)43 void SkinnableLabel::applySkin(const Config::LabelSkin& skin)
44 {
45   setFrameStyle(skin.frameStyle);
46   setIndent(skin.margin);
47 
48   // Set colors
49   QPalette pal = palette();
50   if (skin.background.isValid())
51   {
52     setAutoFillBackground(skin.background.alpha() != 0);
53     pal.setColor(QPalette::Window, skin.background);
54   }
55   if (skin.foreground.isValid())
56     pal.setColor(QPalette::WindowText, skin.foreground);
57   setPalette(pal);
58 
59   // Set background image
60   myBackgroundImage = skin.pixmap;
61 
62   update();
63 }
64 
setPrependPixmap(const QPixmap & p)65 void SkinnableLabel::setPrependPixmap(const QPixmap& p)
66 {
67   if (!myAddPix.isNull())
68     clearPrependPixmap();
69 
70   myAddPix = p;
71   myAddIndent = indent();
72   setIndent(indent() + p.width() + 2);
73 
74   update();
75 }
76 
clearPrependPixmap()77 void SkinnableLabel::clearPrependPixmap()
78 {
79   if (myAddPix.isNull())
80     return;
81 
82   setIndent(myAddIndent);
83   myAddPix = QPixmap();
84 
85   update();
86 }
87 
addPixmap(const QPixmap & p)88 void SkinnableLabel::addPixmap(const QPixmap& p)
89 {
90   myPixmaps.push_back(p);
91   if (myPixmaps.size() == 1)
92     myStartingIndent = indent();
93   update();
94 }
95 
clearPixmaps()96 void SkinnableLabel::clearPixmaps()
97 {
98   if (myPixmaps.empty())
99     return;
100 
101   myPixmaps.clear();
102   setIndent(myStartingIndent);
103 
104   update();
105 }
106 
setBold(bool enable)107 void SkinnableLabel::setBold(bool enable)
108 {
109   QFont newFont(font());
110   newFont.setBold(enable);
111   setFont(newFont);
112 }
113 
setItalic(bool enable)114 void SkinnableLabel::setItalic(bool enable)
115 {
116   QFont newFont(font());
117   newFont.setItalic(enable);
118   setFont(newFont);
119 }
120 
paintEvent(QPaintEvent * e)121 void SkinnableLabel::paintEvent(QPaintEvent* e)
122 {
123   QPainter p(this);
124 
125   if (!myBackgroundImage.isNull())
126     p.drawImage(0, 0, myBackgroundImage.toImage().scaled(width(), height()));
127 
128   if (!myAddPix.isNull())
129     p.drawPixmap(myAddIndent, height() / 2 - myAddPix.height() / 2, myAddPix);
130 
131   if (myPixmaps.size())
132   {
133     QList<QPixmap>::iterator it;
134     int i = indent();
135     for (it = myPixmaps.begin(); it != myPixmaps.end(); it++)
136     {
137       p.drawPixmap(i, height() / 2 - it->height() / 2, *it);
138       i += it->width() + 2;
139     }
140   }
141 
142   p.end();
143 
144   QLabel::paintEvent(e);
145 }
146 
mousePressEvent(QMouseEvent * e)147 void SkinnableLabel::mousePressEvent(QMouseEvent* e)
148 {
149   if(e->button() == Qt::MidButton)
150   {
151     emit doubleClicked();
152   }
153   else if (e->button() == Qt::RightButton)
154   {
155     if (myPopupMenu != NULL)
156     {
157       QPoint clickPoint(e->x(), e->y());
158       myPopupMenu->popup(mapToGlobal(clickPoint));
159     }
160   }
161   else
162   {
163     QLabel::mousePressEvent(e);
164   }
165 }
166 
mouseDoubleClickEvent(QMouseEvent *)167 void SkinnableLabel::mouseDoubleClickEvent(QMouseEvent* /* e */)
168 {
169   emit doubleClicked();
170 }
171 
wheelEvent(QWheelEvent * event)172 void SkinnableLabel::wheelEvent(QWheelEvent* event)
173 {
174   // Ignore the events for horizontal wheel movements
175   if (event->orientation() != Qt::Vertical)
176     return QLabel::wheelEvent(event);
177 
178   if (event->delta() < 0)
179     emit wheelDown();
180   else
181     emit wheelUp();
182   event->accept();
183 }
184