Skip to content

Commit a8a1ef7

Browse files
author
telwertowski
committed
Add QgsColorButton class to display a color which can be altered using QColorDialog::getColor. Fix for bug #407.
A subclass of QToolButton is needed to draw the button content because some platforms such as Mac OS X and Windows XP enforce a consistent GUI look by always using the button color of the current style and not allowing button backgrounds to be changed on a button by button basis. This class is a simplified version of QtColorButton, an internal class used by Qt Designer to do the same thing. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6251 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 701011c commit a8a1ef7

19 files changed

+259
-409
lines changed

src/gui/Makefile.am

+4-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ libqgis_guiHEADERS = \
7676
qgsattributetable.h \
7777
qgsbookmarks.h \
7878
qgsclipboard.h \
79+
qgscolorbutton.h \
7980
qgscontinuouscolordialog.h \
8081
qgscontinuouscolorrenderer.h \
81-
qgscsexception.h \
8282
qgscoordinatetransform.h \
83+
qgscsexception.h \
8384
qgscustomprojectiondialog.h \
8485
qgscursors.h \
8586
qgsdelattrdialog.h \
@@ -199,13 +200,14 @@ libqgis_gui_la_SOURCES = \
199200
qgisinterface.cpp \
200201
qgsabout.cpp \
201202
qgsaddattrdialog.cpp \
202-
qgsattributeaction.cpp \
203+
qgsattributeaction.cpp \
203204
qgsattributeactiondialog.cpp \
204205
qgsattributedialog.cpp \
205206
qgsattributetable.cpp \
206207
qgsattributetabledisplay.cpp \
207208
qgsbookmarks.cpp \
208209
qgsclipboard.cpp \
210+
qgscolorbutton.cpp \
209211
qgscontinuouscolordialog.cpp \
210212
qgscoordinatetransform.cpp \
211213
qgscontinuouscolorrenderer.cpp \

src/gui/qgscolorbutton.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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$ */
16+
17+
#include "qgscolorbutton.h"
18+
#include <QPainter>
19+
20+
/*!
21+
\class QgsColorButton
22+
23+
\brief The QgsColorButton class provides a tool button widget displaying
24+
a color which can be altered by calling QColorDialog::getColor.
25+
26+
A subclass of QToolButton is needed to draw the button content because
27+
some platforms such as Mac OS X and Windows XP enforce a consistent
28+
GUI look by always using the button color of the current style and
29+
not allowing button backgrounds to be changed on a button by button basis.
30+
31+
This class is a simplified version of QtColorButton, an internal class used
32+
by Qt Designer to do the same thing.
33+
*/
34+
35+
QgsColorButton::QgsColorButton(QWidget *parent)
36+
: QToolButton(parent)
37+
{}
38+
39+
QgsColorButton::~QgsColorButton()
40+
{}
41+
42+
/*!
43+
Paints button in response to a paint event.
44+
*/
45+
void QgsColorButton::paintEvent(QPaintEvent *e)
46+
{
47+
QToolButton::paintEvent(e);
48+
if (isEnabled())
49+
{
50+
QPainter p(this);
51+
int margin = 2; // Leave some space for highlighting
52+
QRect r = rect().adjusted(margin, margin, -margin, -margin);
53+
p.fillRect(r, mColor);
54+
}
55+
}

src/gui/qgscolorbutton.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/***************************************************************************
2+
qgscolorbutton.h - Color button
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$ */
16+
#ifndef QGSCOLORBUTTON_H
17+
#define QGSCOLORBUTTON_H
18+
19+
#include <QToolButton>
20+
21+
class QgsColorButton: public QToolButton
22+
{
23+
public:
24+
QgsColorButton(QWidget *parent = 0);
25+
~QgsColorButton();
26+
27+
void setColor(const QColor &color) { mColor = color; }
28+
QColor color() const { return mColor; }
29+
30+
protected:
31+
void paintEvent(QPaintEvent *e);
32+
33+
private:
34+
QColor mColor;
35+
};
36+
37+
#endif

src/gui/qgscontinuouscolordialog.cpp

+17-38
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ QgsContinuousColorDialog::QgsContinuousColorDialog(QgsVectorLayer * layer)
6565
return;
6666
}
6767

68-
// new Qt4 idiom
69-
#ifdef Q_WS_WIN
70-
// Coloured buttons do not work under the Windows XP style - use plain Windows instead
71-
btnMinValue->setStyle(&mWindowsStyle);
72-
btnMaxValue->setStyle(&mWindowsStyle);
73-
#endif
74-
7568
//restore the correct colors for minimum and maximum values
7669

7770
const QgsContinuousColorRenderer* renderer = dynamic_cast < const QgsContinuousColorRenderer * >(layer->renderer());;
@@ -98,23 +91,15 @@ QgsContinuousColorDialog::QgsContinuousColorDialog(QgsVectorLayer * layer)
9891
const QgsSymbol* maxsymbol = renderer->maximumSymbol();
9992

10093
if (mVectorLayer->vectorType() == QGis::Line || mVectorLayer->vectorType() == QGis::Point)
101-
{
102-
// old Qt3 idiom
103-
// lblMinValue->setPaletteBackgroundColor(minsymbol->pen().color());
104-
// lblMaxValue->setPaletteBackgroundColor(maxsymbol->pen().color());
105-
// new Qt4 idiom
106-
btnMinValue->setPalette( minsymbol->pen().color() );
107-
btnMaxValue->setPalette( maxsymbol->pen().color() );
94+
{
95+
btnMinValue->setColor( minsymbol->pen().color() );
96+
btnMaxValue->setColor( maxsymbol->pen().color() );
10897
}
10998
else
110-
{
111-
// old Qt3 idiom
112-
// lblMinValue->setPaletteBackgroundColor(minsymbol->brush().color());
113-
// lblMaxValue->setPaletteBackgroundColor(maxsymbol->brush().color());
114-
// new Qt4 idiom
115-
btnMinValue->setPalette( minsymbol->brush().color() );
116-
btnMaxValue->setPalette( maxsymbol->brush().color() );
117-
}
99+
{
100+
btnMinValue->setColor( minsymbol->brush().color() );
101+
btnMaxValue->setColor( maxsymbol->brush().color() );
102+
}
118103

119104
outlinewidthspinbox->setMinValue(0);
120105
outlinewidthspinbox->setValue(minsymbol->pen().width());
@@ -133,8 +118,8 @@ QgsContinuousColorDialog::QgsContinuousColorDialog(QgsVectorLayer * layer)
133118
if (mVectorLayer->vectorType() != QGis::Polygon)
134119
cb_polygonOutline->setVisible(false);
135120

136-
btnMinValue->setPalette(Qt::black);
137-
btnMaxValue->setPalette(Qt::white);
121+
btnMinValue->setColor(Qt::black);
122+
btnMaxValue->setColor(Qt::white);
138123

139124
}
140125
// Ensure that the state of other widgets is appropriate for the
@@ -189,22 +174,22 @@ void QgsContinuousColorDialog::apply()
189174
QgsSymbol* minsymbol = new QgsSymbol(mVectorLayer->vectorType(), QString::number(minimum, 'f'), "", "");
190175
if (mVectorLayer->vectorType() == QGis::Line || mVectorLayer->vectorType() == QGis::Point)
191176
{
192-
minsymbol->setPen(QPen(btnMinValue->paletteBackgroundColor(),outlinewidthspinbox->value()));
177+
minsymbol->setPen(QPen(btnMinValue->color(), outlinewidthspinbox->value()));
193178
}
194179
else
195180
{
196-
minsymbol->setBrush(QBrush(btnMinValue->paletteBackgroundColor()));
181+
minsymbol->setBrush(QBrush(btnMinValue->color()));
197182
minsymbol->setPen(QPen(QColor(0, 0, 0), outlinewidthspinbox->value()));
198183
}
199184

200185
QgsSymbol* maxsymbol = new QgsSymbol(mVectorLayer->vectorType(), QString::number(maximum, 'f'), "", "");
201186
if (mVectorLayer->vectorType() == QGis::Line || mVectorLayer->vectorType() == QGis::Point)
202187
{
203-
maxsymbol->setPen(QPen(btnMaxValue->paletteBackgroundColor(),outlinewidthspinbox->value()));
188+
maxsymbol->setPen(QPen(btnMaxValue->color(), outlinewidthspinbox->value()));
204189
}
205190
else
206191
{
207-
maxsymbol->setBrush(QBrush(btnMaxValue->paletteBackgroundColor()));
192+
maxsymbol->setBrush(QBrush(btnMaxValue->color()));
208193
maxsymbol->setPen(QPen(QColor(0, 0, 0), outlinewidthspinbox->value()));
209194
}
210195

@@ -222,26 +207,20 @@ void QgsContinuousColorDialog::apply()
222207

223208
void QgsContinuousColorDialog::selectMinimumColor()
224209
{
225-
QColor mincolor = QColorDialog::getColor(btnMinValue->paletteBackgroundColor(), this);
210+
QColor mincolor = QColorDialog::getColor(btnMinValue->color(), this);
226211
if(mincolor.isValid())
227212
{
228-
// old Qt3 idiom
229-
// lblMinValue->setPaletteBackgroundColor(mincolor);
230-
// new Qt4 idiom
231-
btnMinValue->setPalette(mincolor);
213+
btnMinValue->setColor(mincolor);
232214
}
233215
setActiveWindow();
234216
}
235217

236218
void QgsContinuousColorDialog::selectMaximumColor()
237219
{
238-
QColor maxcolor = QColorDialog::getColor(btnMaxValue->paletteBackgroundColor(), this);
220+
QColor maxcolor = QColorDialog::getColor(btnMaxValue->color(), this);
239221
if(maxcolor.isValid())
240222
{
241-
// old Qt3 idiom
242-
// lblMaxValue->setPaletteBackgroundColor(maxcolor);
243-
// new Qt4 idiom
244-
btnMaxValue->setPalette(maxcolor);
223+
btnMaxValue->setColor(maxcolor);
245224
}
246225
setActiveWindow();
247226
}

src/gui/qgscontinuouscolordialog.h

-9
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
#include "ui_qgscontinuouscolordialogbase.h"
2424
#include <map>
2525

26-
#ifdef Q_WS_WIN
27-
#include <QWindowsStyle>
28-
#endif
29-
3026
class QgsVectorLayer;
3127

3228

@@ -56,11 +52,6 @@ class QgsContinuousColorDialog: public QDialog, private Ui::QgsContinuousColorDi
5652
/** Default constructor is private, do not use this */
5753
QgsContinuousColorDialog();
5854

59-
#ifdef Q_WS_WIN
60-
//! Holds the classic Windows style that is used to render labels with a background color
61-
QWindowsStyle mWindowsStyle;
62-
#endif
63-
6455
};
6556

6657
#endif

src/gui/qgsoptions.cpp

+8-42
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,13 @@ QgsOptions::QgsOptions(QWidget *parent, Qt::WFlags fl) :
115115
int myRed = settings.value("/qgis/default_selection_color_red",255).toInt();
116116
int myGreen = settings.value("/qgis/default_selection_color_green",255).toInt();
117117
int myBlue = settings.value("/qgis/default_selection_color_blue",0).toInt();
118-
// old Qt3 idiom
119-
// pbnSelectionColour->setPaletteBackgroundColor(QColor(myRed,myGreen,myBlue));
120-
// new Qt4 idiom - see http://lists.trolltech.com/qt4-preview-feedback/2005-04/thread00270-0.html for reasoning
121-
#ifdef Q_WS_WIN
122-
// Coloured buttons do not work under the Windows XP style - use plain Windows instead
123-
pbnSelectionColour->setStyle(&mWindowsStyle);
124-
#endif
125-
pbnSelectionColour->setPalette( QColor(myRed,myGreen,myBlue) );
118+
pbnSelectionColour->setColor( QColor(myRed,myGreen,myBlue) );
126119

127120
//set the default color for canvas background
128121
myRed = settings.value("/qgis/default_canvas_color_red",255).toInt();
129122
myGreen = settings.value("/qgis/default_canvas_color_green",255).toInt();
130123
myBlue = settings.value("/qgis/default_canvas_color_blue",255).toInt();
131-
// old Qt3 idiom
132-
// pbnCanvasColor->setPaletteBackgroundColor(QColor(myRed,myGreen,myBlue));
133-
// new Qt4 idiom - see http://lists.trolltech.com/qt4-preview-feedback/2005-04/thread00270-0.html for reasoning
134-
#ifdef Q_WS_WIN
135-
// Coloured buttons do not work under the Windows XP style - use plain Windows instead
136-
pbnCanvasColor->setStyle(&mWindowsStyle);
137-
#endif
138-
pbnCanvasColor->setPalette( QColor(myRed,myGreen,myBlue) );
124+
pbnCanvasColor->setColor( QColor(myRed,myGreen,myBlue) );
139125

140126
capitaliseCheckBox->setChecked(settings.value("qgis/capitaliseLayerName", QVariant(false)).toBool());
141127

@@ -150,33 +136,19 @@ QgsOptions::~QgsOptions(){}
150136

151137
void QgsOptions::on_pbnSelectionColour_clicked()
152138
{
153-
// old Qt3 idiom
154-
// QColor color = QColorDialog::getColor(pbnSelectionColour->paletteBackgroundColor(),this);
155-
// new Qt4 idiom
156-
QPalette palSelectionColour = pbnSelectionColour->palette();
157-
QColor color = QColorDialog::getColor( palSelectionColour.color(QPalette::Window), this );
139+
QColor color = QColorDialog::getColor(pbnSelectionColour->color(), this);
158140
if (color.isValid())
159141
{
160-
// old Qt3 idiom
161-
// pbnSelectionColour->setPaletteBackgroundColor(color);
162-
// new Qt4 idiom - see http://lists.trolltech.com/qt4-preview-feedback/2005-04/thread00270-0.html for reasoning
163-
pbnSelectionColour->setPalette(color);
142+
pbnSelectionColour->setColor(color);
164143
}
165144
}
166145

167146
void QgsOptions::on_pbnCanvasColor_clicked()
168147
{
169-
// old Qt3 idiom
170-
// QColor color = QColorDialog::getColor(pbnCanvasColor->paletteBackgroundColor(),this);
171-
// new Qt4 idiom
172-
QPalette palCanvasColor = pbnCanvasColor->palette();
173-
QColor color = QColorDialog::getColor( palCanvasColor.color(QPalette::Window), this );
148+
QColor color = QColorDialog::getColor(pbnCanvasColor->color(), this);
174149
if (color.isValid())
175150
{
176-
// old Qt3 idiom
177-
// pbnCanvasColor->setPaletteBackgroundColor(color);
178-
// new Qt4 idiom - see http://lists.trolltech.com/qt4-preview-feedback/2005-04/thread00270-0.html for reasoning
179-
pbnCanvasColor->setPalette(color);
151+
pbnCanvasColor->setColor(color);
180152
}
181153
}
182154
void QgsOptions::themeChanged(const QString &newThemeName)
@@ -231,19 +203,13 @@ void QgsOptions::saveOptions()
231203
settings.writeEntry("/qgis/measure/ellipsoid", getEllipsoidAcronym(cmbEllipsoid->currentText()));
232204

233205
//set the colour for selections
234-
// old Qt3 idiom
235-
// QColor myColor = pbnSelectionColour->paletteBackgroundColor();
236-
// new Qt4 idiom
237-
QColor myColor = pbnSelectionColour->palette().color(QPalette::Window);
206+
QColor myColor = pbnSelectionColour->color();
238207
int myRed = settings.writeEntry("/qgis/default_selection_color_red",myColor.red());
239208
int myGreen = settings.writeEntry("/qgis/default_selection_color_green",myColor.green());
240209
int myBlue = settings.writeEntry("/qgis/default_selection_color_blue",myColor.blue());
241210

242211
//set the default color for canvas background
243-
// old Qt3 idiom
244-
// myColor = pbnCanvasColor->paletteBackgroundColor();
245-
// new Qt4 idiom
246-
myColor = pbnCanvasColor->palette().color(QPalette::Window);
212+
myColor = pbnCanvasColor->color();
247213
myRed = settings.writeEntry("/qgis/default_canvas_color_red",myColor.red());
248214
myGreen = settings.writeEntry("/qgis/default_canvas_color_green",myColor.green());
249215
myBlue = settings.writeEntry("/qgis/default_canvas_color_blue",myColor.blue());

src/gui/qgsoptions.h

-10
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
#include "ui_qgsoptionsbase.h"
2323
#include "qgisgui.h"
2424

25-
#ifdef Q_WS_WIN
26-
#include <QWindowsStyle>
27-
#endif
28-
29-
3025

3126
/**
3227
* \class QgsOptions
@@ -91,11 +86,6 @@ class QgsOptions :public QDialog, private Ui::QgsOptionsBase
9186
//!Global default projection used for new layers added that have no projection
9287
long mGlobalSRSID;
9388

94-
#ifdef Q_WS_WIN
95-
//! Holds the classic Windows style that is used to render buttons with a background color
96-
QWindowsStyle mWindowsStyle;
97-
#endif
98-
9989
};
10090

10191
#endif // #ifndef QGSOPTIONS_H

0 commit comments

Comments
 (0)