00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef SIMPLEICONBUTTON_H
00025 #define SIMPLEICONBUTTON_H
00026
00027
00028 #include <QStylePainter>
00029
00030 #define MAX_SIMPLEICONBUTTON_WIDTH 1024
00031 #define MAX_SIMPLEICONBUTTON_HEIGHT 1024
00032
00033
00041 template <class T>
00042 class SimpleIconButton : public T {
00043
00044 public:
00045 SimpleIconButton( QWidget* parent = 0 ) : T(parent) {
00046 T::setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00047 }
00048
00049 virtual ~SimpleIconButton() {}
00050
00051 const QIcon& backgroundIcon() const { return m_backgroundIcon; }
00052
00053 void setBackgroundIcon( const QIcon& icon ) {
00054 m_backgroundIcon = icon;
00055 T::update();
00056 }
00057
00058
00062 virtual QSize sizeHint() const
00063 {
00064
00065 return ( T::icon().actualSize(QSize(MAX_SIMPLEICONBUTTON_WIDTH,
00066 MAX_SIMPLEICONBUTTON_HEIGHT))
00067 .expandedTo(m_backgroundIcon.actualSize(QSize(MAX_SIMPLEICONBUTTON_WIDTH,
00068 MAX_SIMPLEICONBUTTON_HEIGHT))) );
00069 }
00070
00071
00072 protected:
00073
00074 virtual void paintEvent( QPaintEvent* )
00075 {
00076 QStylePainter p( this );
00077
00078
00079
00080
00081
00082
00083
00084 int w = T::width();
00085 int h = T::height();
00086
00087 QIcon::Mode mode = QIcon::Disabled;
00088 if ( T::isEnabled() )
00089 mode = T::isDown() ? QIcon::Active : QIcon::Normal;
00090 QPixmap fgPixmap = T::icon().pixmap( QSize(w, h),
00091 mode,
00092 T::isChecked() ? QIcon::On : QIcon::Off );
00093
00094 QPixmap bgPixmap = m_backgroundIcon.pixmap( QSize(w, h),
00095 mode,
00096 T::isChecked() ? QIcon::On : QIcon::Off );
00097
00098
00099
00100 p.drawPixmap( (w - bgPixmap.width()) / 2,
00101 (h - bgPixmap.height()) / 2,
00102 bgPixmap );
00103 p.drawPixmap( (w - fgPixmap.width()) / 2,
00104 (h - fgPixmap.height()) / 2,
00105 fgPixmap );
00106
00107
00108 p.drawItemText( QRect(0, 0, w, h), Qt::AlignCenter | Qt::TextShowMnemonic,
00109 T::palette(), T::isEnabled(), T::text() );
00110 }
00111
00112
00113 private:
00114 QIcon m_backgroundIcon;
00115 };
00116
00117
00118 #endif // ! SIMPLEICONBUTTON_H