root/trunk/qt4-gui/src/widgets/skinnablelabel.cpp

Revision 5890, 3.6 kB (checked in by eugene, 12 months ago)

This shuts Qt's warnings about several QPainters being active on the same widget.

Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of Licq, an instant messaging client for UNIX.
4 * Copyright (C) 2007 Licq developers
5 *
6 * Licq 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 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Licq 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 Licq; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20
21#include "skinnablelabel.h"
22
23#include <QMenu>
24#include <QMouseEvent>
25#include <QPainter>
26
27#include "config/skin.h"
28
29using namespace LicqQtGui;
30
31SkinnableLabel::SkinnableLabel(const Config::LabelSkin& skin, QMenu* popupMenu, QWidget* parent)
32  : QLabel(parent),
33    myPopupMenu(popupMenu)
34{
35  applySkin(skin);
36}
37
38SkinnableLabel::SkinnableLabel(QMenu* popupMenu, QWidget* parent)
39  : QLabel(parent),
40    myPopupMenu(popupMenu)
41{
42}
43
44void SkinnableLabel::applySkin(const Config::LabelSkin& skin)
45{
46  setFrameStyle(skin.frameStyle);
47  setIndent(skin.margin);
48
49  // Set colors
50  QPalette pal = palette();
51  if (skin.background.isValid())
52  {
53    setAutoFillBackground(skin.background.alpha() != 0);
54    pal.setColor(QPalette::Window, skin.background);
55  }
56  if (skin.foreground.isValid())
57    pal.setColor(QPalette::WindowText, skin.foreground);
58  setPalette(pal);
59
60  // Set background image
61  myBackgroundImage = skin.pixmap;
62
63  update();
64}
65
66void SkinnableLabel::setPrependPixmap(const QPixmap& p)
67{
68  if (!myAddPix.isNull())
69    clearPrependPixmap();
70
71  myAddPix = p;
72  myAddIndent = indent();
73  setIndent(indent() + p.width() + 2);
74
75  update();
76}
77
78void SkinnableLabel::clearPrependPixmap()
79{
80  if (myAddPix.isNull())
81    return;
82
83  setIndent(myAddIndent);
84  myAddPix = QPixmap();
85
86  update();
87}
88
89void SkinnableLabel::addPixmap(const QPixmap& p)
90{
91  myPixmaps.push_back(p);
92  if (myPixmaps.size() == 1)
93    myStartingIndent = indent();
94  update();
95}
96
97void SkinnableLabel::clearPixmaps()
98{
99  if (myPixmaps.size() == 0)
100    return;
101
102  myPixmaps.clear();
103  setIndent(myStartingIndent);
104
105  update();
106}
107
108void SkinnableLabel::setBold(bool enable)
109{
110  QFont newFont(font());
111  newFont.setBold(enable);
112  setFont(newFont);
113}
114
115void SkinnableLabel::setItalic(bool enable)
116{
117  QFont newFont(font());
118  newFont.setItalic(enable);
119  setFont(newFont);
120}
121
122void SkinnableLabel::paintEvent(QPaintEvent* e)
123{
124  QPainter p(this);
125
126  if (!myBackgroundImage.isNull())
127    p.drawImage(0, 0, myBackgroundImage.toImage().scaled(width(), height()));
128
129  if (!myAddPix.isNull())
130    p.drawPixmap(myAddIndent, height() / 2 - myAddPix.height() / 2, myAddPix);
131
132  if (myPixmaps.size())
133  {
134    QList<QPixmap>::iterator it;
135    int i = indent();
136    for (it = myPixmaps.begin(); it != myPixmaps.end(); it++)
137    {
138      p.drawPixmap(i, height() / 2 - it->height() / 2, *it);
139      i += it->width() + 2;
140    }
141  }
142
143  p.end();
144
145  QLabel::paintEvent(e);
146}
147
148void SkinnableLabel::mousePressEvent(QMouseEvent* e)
149{
150  if(e->button() == Qt::MidButton)
151  {
152    emit doubleClicked();
153  }
154  else if (e->button() == Qt::RightButton)
155  {
156    if (myPopupMenu != NULL)
157    {
158      QPoint clickPoint(e->x(), e->y());
159      myPopupMenu->popup(mapToGlobal(clickPoint));
160    }
161  }
162  else
163  {
164    QLabel::mousePressEvent(e);
165  }
166}
167
168void SkinnableLabel::mouseDoubleClickEvent(QMouseEvent* /* e */)
169{
170  emit doubleClicked();
171}
Note: See TracBrowser for help on using the browser.