Quantum GIS API Documentation  1.7.4
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 /* $Id: qgscolorbutton.cpp 6251 2006-12-13 23:23:50Z telwertowski $ */
00016 
00017 #include "qgscolorbutton.h"
00018 #include <QPainter>
00019 
00035 QgsColorButton::QgsColorButton( QWidget *parent )
00036     : QToolButton( parent )
00037 {
00038   setToolButtonStyle( Qt::ToolButtonTextOnly ); // decrease default button height
00039 }
00040 
00041 QgsColorButton::~QgsColorButton()
00042 {}
00043 
00047 void QgsColorButton::paintEvent( QPaintEvent *e )
00048 {
00049   QToolButton::paintEvent( e );
00050   if (
00051 #ifdef Q_WS_MAC
00052     // Mac shows color only a when a window is active
00053     isActiveWindow() &&
00054 #endif
00055     isEnabled() )
00056   {
00057     QPainter p( this );
00058     int margin = 2;  // Leave some space for highlighting
00059     QRect r = rect().adjusted( margin, margin, -margin, -margin );
00060     p.fillRect( r, mColor );
00061   }
00062 }
00063 
00064 void QgsColorButton::setColor( const QColor &color )
00065 {
00066   mColor = color;
00067   update();
00068 }
00069 
00070 
00072 
00073 QgsColorButtonV2::QgsColorButtonV2( QWidget* parent )
00074     : QPushButton( parent )
00075 {
00076 }
00077 
00078 QgsColorButtonV2::QgsColorButtonV2( QString text, QWidget* parent )
00079     : QPushButton( text, parent )
00080 {
00081 }
00082 
00083 void QgsColorButtonV2::setColor( const QColor &color )
00084 {
00085   mColor = color;
00086 
00087   QPixmap pixmap( iconSize() );
00088   pixmap.fill( QColor( 0, 0, 0, 0 ) );
00089 
00090   QRect rect( 1, 1, iconSize().width() - 2, iconSize().height() - 2 );
00091 
00092   // draw a slightly rounded rectangle
00093   QPainter p;
00094   p.begin( &pixmap );
00095   p.setPen( Qt::NoPen );
00096   p.setRenderHint( QPainter::Antialiasing );
00097   p.setBrush( color );
00098   p.drawRoundedRect( rect, 4, 4 );
00099   p.end();
00100 
00101   // set this pixmap as icon
00102   setIcon( QIcon( pixmap ) );
00103 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines