WidgetPack/simpleiconbutton.h

Go to the documentation of this file.
00001 /*  -*- c++ -*-  (for Emacs)
00002  *
00003  *  simpleiconbutton.h
00004  *
00005  *  Created by Aidan Lane on Thu Apr 14 2005.
00006  *  Copyright (c) 2005 CEMA, Monash University. All rights reserved.
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program; if not, write to the Free Software
00020  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
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 ); // we respect the icon, which as a fixed size (layout uses sizeHint)
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     // From the Qt4.0.0 Doc for actualSize(): "The result might be smaller than requested, but never larger."
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   // TODO: optimise me! possibly introduce caching...
00074   virtual void paintEvent( QPaintEvent* )
00075   {
00076     QStylePainter p( this ); // assume we're using the screen -> no error checking
00077 
00078     // Get the appropriate pixmap from the icon
00079     // WRANING! DON'T use icon().paint(), as it won't actually scale the icon,
00080     //          it just clips it. Also, it seems like it doesn't draw until
00081     //          the entire app has loaded and is idling (very noticeable).
00082     // TODO: cache the pixmaps!
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),                   // TODO: cleanup!
00091                                          mode,
00092                                          T::isChecked() ? QIcon::On : QIcon::Off );
00093 
00094     QPixmap bgPixmap = m_backgroundIcon.pixmap( QSize(w, h),                   // TODO: cleanup!
00095                                                 mode,
00096                                                 T::isChecked() ? QIcon::On : QIcon::Off );
00097 
00098     // Draw the icon pixmaps centred within us
00099     // TODO: cache positions!
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     // Draw the text last, on-top of the icons
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

Generated on Mon Jul 30 09:46:50 2007 for Digest by  doxygen 1.5.2