Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgscolorbutton.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorbutton.cpp - Button which displays a color
3  --------------------------------------
4  Date : 12-Dec-2006
5  Copyright : (C) 2006 by Tom Elwertowski
6  Email : telwertowski at users dot sourceforge dot net
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 /* $Id: qgscolorbutton.cpp 6251 2006-12-13 23:23:50Z telwertowski $ */
16 
17 #include "qgscolorbutton.h"
18 #include <QPainter>
19 
36  : QToolButton( parent )
37 {
38  setToolButtonStyle( Qt::ToolButtonTextOnly ); // decrease default button height
39 }
40 
42 {}
43 
47 void QgsColorButton::paintEvent( QPaintEvent *e )
48 {
50  if (
51 #ifdef Q_WS_MAC
52  // Mac shows color only a when a window is active
53  isActiveWindow() &&
54 #endif
55  isEnabled() )
56  {
57  QPainter p( this );
58  int margin = 2; // Leave some space for highlighting
59  QRect r = rect().adjusted( margin, margin, -margin, -margin );
60  p.fillRect( r, mColor );
61  }
62 }
63 
64 void QgsColorButton::setColor( const QColor &color )
65 {
66  mColor = color;
67  update();
68 }
69 
70 
72 
74  : QPushButton( parent )
75 {
76 }
77 
78 QgsColorButtonV2::QgsColorButtonV2( QString text, QWidget* parent )
79  : QPushButton( text, parent )
80 {
81 }
82 
83 void QgsColorButtonV2::setColor( const QColor &color )
84 {
85  mColor = color;
86 
87  QPixmap pixmap( iconSize() );
88  pixmap.fill( QColor( 0, 0, 0, 0 ) );
89 
90  QRect rect( 1, 1, iconSize().width() - 2, iconSize().height() - 2 );
91 
92  // draw a slightly rounded rectangle
93  QPainter p;
94  p.begin( &pixmap );
95  p.setPen( Qt::NoPen );
96  p.setRenderHint( QPainter::Antialiasing );
97  p.setBrush( color );
98  p.drawRoundedRect( rect, 4, 4 );
99  p.end();
100 
101  // set this pixmap as icon
102  setIcon( QIcon( pixmap ) );
103 }