OTHER: Replace QIcon in PixmapButton with own class

This should allow to add pixmaps for disabled buttons in a sane way without
Qt beeing too 'intelligent'
This commit is contained in:
Thomas Frauendorfer 2010-03-06 17:28:01 +01:00
parent 353b7314e6
commit 81d24d1c3f
10 changed files with 262 additions and 150 deletions

View file

@ -1,7 +1,7 @@
/**
* This file is a part of Promoe, an XMMS2 Client
*
* Copyright (C) 2008-2009 XMMS2 Team
* Copyright (C) 2008-2010 XMMS2 Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -24,9 +24,25 @@
void
PixmapButton::paintEvent( QPaintEvent * event )
{
QPixmap pixmap = icon().pixmap (size(),
isDown() ? QIcon::Active : QIcon::Normal,
isChecked() ? QIcon::On : QIcon::Off);
PBPixmaps::Mode m;
if (isActiveWindow ()) {
if (isDown ()) {
m = PBPixmaps::Pressed;
} else {
m = PBPixmaps::Normal;
}
} else {
m = PBPixmaps::Inactive;
}
QPixmap pixmap = m_pixmaps.pixmap (m, isChecked () ? PBPixmaps::Checked
: PBPixmaps::Unchecked);
if (pixmap.isNull ()) {
pixmap = icon().pixmap (size(),
isDown() ? QIcon::Active : QIcon::Normal,
isChecked() ? QIcon::On : QIcon::Off);
}
QPainter p;
p.begin(this);
@ -35,4 +51,12 @@ PixmapButton::paintEvent( QPaintEvent * event )
}
void
PixmapButton::setPixmaps (const PixmapButtonPixmaps & p)
{
m_pixmaps = p;
update ();
}
#include "pixmapbutton.moc"

View file

@ -1,7 +1,7 @@
/**
* This file is a part of Promoe, an XMMS2 CLient
*
* Copyright (C) 2008-2009 XMMS2 Team
* Copyright (C) 2008-2010 XMMS2 Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -18,6 +18,10 @@
#define __PIXMAPBUTTON_H__
#include <QAbstractButton>
#include <QMap>
#include <QPixmap>
typedef QMap <int, QPixmap> PixmapMap;
class QPaintEvent;
class QWidget;
@ -31,17 +35,78 @@ class QWidget;
*
* only QIcon::Normal QIcon::Off combination is necessary
*/
class PixmapButtonPixmaps
{
public:
PixmapButtonPixmaps () :
m_pixmaps() {}
PixmapButtonPixmaps (const PixmapButtonPixmaps &p) :
m_pixmaps(p.m_pixmaps) {}
class PixmapButton : public QAbstractButton {
enum Mode {
Normal = 0, // Used when button is in active Window
Inactive = 1, // Used when button is in inactive Window
Pressed = 2 // used when button is pressed
};
enum State {
Unchecked = 0, // used when button is unchecked
Checked = 1 // used when button is checked
};
void addPixmap (const QPixmap & pixmap, Mode m = Normal,
State s = Unchecked) {
m_pixmaps[getKey(m, s)] = pixmap;
if (!m_pixmaps.contains (getKey (Normal, Unchecked))) {
m_pixmaps[getKey (Normal, Unchecked)] = pixmap;
}
}
QPixmap pixmap (Mode m = Normal, State s = Unchecked) const {
if (m_pixmaps.contains (getKey (m, s))) {
return m_pixmaps.value (getKey (m, s));
} else if (s == Checked && m != Normal &&
m_pixmaps.contains (getKey (Normal, s))) {
return m_pixmaps.value (getKey (Normal, s));
} else {
return m_pixmaps.value (getKey (Normal, Unchecked));
}
}
PixmapButtonPixmaps & operator= (const PixmapButtonPixmaps & p) {
m_pixmaps = p.m_pixmaps;
}
protected:
PixmapMap m_pixmaps;
// get the key to be used in the PixmapMap
inline int getKey (Mode m, State s) const { return (m + s*3); }
};
typedef PixmapButtonPixmaps PBPixmaps;
class PixmapButton : public QAbstractButton
{
Q_OBJECT
public:
PixmapButton (QWidget *parent) : QAbstractButton (parent) {}
~PixmapButton () {};
void setPixmaps (const PixmapButtonPixmaps & p);
protected:
void paintEvent ( QPaintEvent * event );
PBPixmaps m_pixmaps;
};
#endif