| 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 "skinnablebutton.h" |
|---|
| 22 | |
|---|
| 23 | #include <QMouseEvent> |
|---|
| 24 | #include <QPainter> |
|---|
| 25 | |
|---|
| 26 | #include "config/skin.h" |
|---|
| 27 | |
|---|
| 28 | using namespace LicqQtGui; |
|---|
| 29 | |
|---|
| 30 | SkinnableButton::SkinnableButton(const Config::ButtonSkin& skin, QString defaultText, QWidget* parent) |
|---|
| 31 | : QPushButton(parent), |
|---|
| 32 | myDefaultText(defaultText), |
|---|
| 33 | myPressedModifiers(Qt::NoModifier) |
|---|
| 34 | { |
|---|
| 35 | applySkin(skin); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | SkinnableButton::SkinnableButton(QString defaultText, QWidget* parent) |
|---|
| 39 | : QPushButton(defaultText, parent), |
|---|
| 40 | myDefaultText(defaultText), |
|---|
| 41 | myPressedModifiers(Qt::NoModifier) |
|---|
| 42 | { |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void SkinnableButton::applySkin(const Config::ButtonSkin& skin) |
|---|
| 46 | { |
|---|
| 47 | // Load new images, set to null pixmaps if any fails to load |
|---|
| 48 | myNormalImage = skin.pixmapUpNoFocus; |
|---|
| 49 | myHighlightedImage = skin.pixmapUpFocus; |
|---|
| 50 | myPressedImage = skin.pixmapDown; |
|---|
| 51 | |
|---|
| 52 | // Set button text (only used if images are not used or not loadable) |
|---|
| 53 | setText(skin.caption.isNull() ? myDefaultText : skin.caption); |
|---|
| 54 | |
|---|
| 55 | // Set colors |
|---|
| 56 | QPalette pal; |
|---|
| 57 | if (skin.background.isValid()) |
|---|
| 58 | pal.setColor(QPalette::Window, skin.background); |
|---|
| 59 | if (skin.foreground.isValid()) |
|---|
| 60 | pal.setColor(QPalette::Text, skin.foreground); |
|---|
| 61 | setPalette(pal); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | Qt::KeyboardModifiers SkinnableButton::modifiersWhenPressed() |
|---|
| 65 | { |
|---|
| 66 | Qt::KeyboardModifiers b = myPressedModifiers; |
|---|
| 67 | myPressedModifiers = Qt::NoModifier; |
|---|
| 68 | return b; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void SkinnableButton::mousePressEvent(QMouseEvent* e) |
|---|
| 72 | { |
|---|
| 73 | myPressedModifiers = e->modifiers(); |
|---|
| 74 | QPushButton::mousePressEvent(e); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void SkinnableButton::enterEvent(QEvent* e) |
|---|
| 78 | { |
|---|
| 79 | // Trigger a paintEvent to redraw button in new state |
|---|
| 80 | update(); |
|---|
| 81 | QPushButton::enterEvent(e); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void SkinnableButton::leaveEvent(QEvent* e) |
|---|
| 85 | { |
|---|
| 86 | // Trigger a paintEvent to redraw button in new state |
|---|
| 87 | update(); |
|---|
| 88 | QPushButton::leaveEvent(e); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void SkinnableButton::paintEvent(QPaintEvent* e) |
|---|
| 92 | { |
|---|
| 93 | QPixmap* image; |
|---|
| 94 | |
|---|
| 95 | // Check button state to select which image to use |
|---|
| 96 | if (isDown()) |
|---|
| 97 | image = &myPressedImage; |
|---|
| 98 | else if (underMouse()) |
|---|
| 99 | image = &myHighlightedImage; |
|---|
| 100 | else |
|---|
| 101 | image = &myNormalImage; |
|---|
| 102 | |
|---|
| 103 | // Use default paint function if no image is defined for this state |
|---|
| 104 | if (image->isNull()) |
|---|
| 105 | { |
|---|
| 106 | QPushButton::paintEvent(e); |
|---|
| 107 | return; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | QPainter p(this); |
|---|
| 111 | p.drawPixmap(0, 0, *image); |
|---|
| 112 | } |
|---|