Quantum GIS API Documentation  1.8
src/gui/qgscolorbutton.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgscolorbutton.cpp - Button which displays a color
00003      --------------------------------------
00004     Date                 : 12-Dec-2006
00005     Copyright            : (C) 2006 by Tom Elwertowski
00006     Email                : telwertowski at users dot sourceforge dot net
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgscolorbutton.h"
00017 #include <QPainter>
00018 
00034 QgsColorButton::QgsColorButton( QWidget *parent )
00035     : QToolButton( parent )
00036 {
00037   setToolButtonStyle( Qt::ToolButtonTextOnly ); // decrease default button height
00038 }
00039 
00040 QgsColorButton::~QgsColorButton()
00041 {}
00042 
00046 void QgsColorButton::paintEvent( QPaintEvent *e )
00047 {
00048   QToolButton::paintEvent( e );
00049   if (
00050 #ifdef Q_WS_MAC
00051     // Mac shows color only a when a window is active
00052     isActiveWindow() &&
00053 #endif
00054     isEnabled() )
00055   {
00056     QPainter p( this );
00057     int margin = 2;  // Leave some space for highlighting
00058     QRect r = rect().adjusted( margin, margin, -margin, -margin );
00059     p.fillRect( r, mColor );
00060   }
00061 }
00062 
00063 void QgsColorButton::setColor( const QColor &color )
00064 {
00065   mColor = color;
00066   update();
00067 }
00068 
00069 
00071 
00072 QgsColorButtonV2::QgsColorButtonV2( QWidget* parent )
00073     : QPushButton( parent )
00074 {
00075 }
00076 
00077 QgsColorButtonV2::QgsColorButtonV2( QString text, QWidget* parent )
00078     : QPushButton( text, parent )
00079 {
00080 }
00081 
00082 void QgsColorButtonV2::setColor( const QColor &color )
00083 {
00084   mColor = color;
00085 
00086   QPixmap pixmap( iconSize() );
00087   pixmap.fill( QColor( 0, 0, 0, 0 ) );
00088 
00089   QRect rect( 1, 1, iconSize().width() - 2, iconSize().height() - 2 );
00090 
00091   // draw a slightly rounded rectangle
00092   QPainter p;
00093   p.begin( &pixmap );
00094   p.setPen( Qt::NoPen );
00095   p.setRenderHint( QPainter::Antialiasing );
00096   p.setBrush( color );
00097   p.drawRoundedRect( rect, 4, 4 );
00098   p.end();
00099 
00100   // set this pixmap as icon
00101   setIcon( QIcon( pixmap ) );
00102 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines