Skip to content

Commit

Permalink
Add QgsColorButton class to display a color which can be altered usin…
Browse files Browse the repository at this point in the history
…g 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
  • Loading branch information
telwertowski committed Dec 13, 2006
1 parent 701011c commit a8a1ef7
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 409 deletions.
6 changes: 4 additions & 2 deletions src/gui/Makefile.am
Expand Up @@ -76,10 +76,11 @@ libqgis_guiHEADERS = \
qgsattributetable.h \
qgsbookmarks.h \
qgsclipboard.h \
qgscolorbutton.h \
qgscontinuouscolordialog.h \
qgscontinuouscolorrenderer.h \
qgscsexception.h \
qgscoordinatetransform.h \
qgscsexception.h \
qgscustomprojectiondialog.h \
qgscursors.h \
qgsdelattrdialog.h \
Expand Down Expand Up @@ -199,13 +200,14 @@ libqgis_gui_la_SOURCES = \
qgisinterface.cpp \
qgsabout.cpp \
qgsaddattrdialog.cpp \
qgsattributeaction.cpp \
qgsattributeaction.cpp \
qgsattributeactiondialog.cpp \
qgsattributedialog.cpp \
qgsattributetable.cpp \
qgsattributetabledisplay.cpp \
qgsbookmarks.cpp \
qgsclipboard.cpp \
qgscolorbutton.cpp \
qgscontinuouscolordialog.cpp \
qgscoordinatetransform.cpp \
qgscontinuouscolorrenderer.cpp \
Expand Down
55 changes: 55 additions & 0 deletions src/gui/qgscolorbutton.cpp
@@ -0,0 +1,55 @@
/***************************************************************************
qgscolorbutton.cpp - Button which displays a color
--------------------------------------
Date : 12-Dec-2006
Copyright : (C) 2006 by Tom Elwertowski
Email : telwertowski at users dot sourceforge dot net
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */

#include "qgscolorbutton.h"
#include <QPainter>

/*!
\class QgsColorButton
\brief The QgsColorButton class provides a tool button widget displaying
a color which can be altered by calling QColorDialog::getColor.
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.
*/

QgsColorButton::QgsColorButton(QWidget *parent)
: QToolButton(parent)
{}

QgsColorButton::~QgsColorButton()
{}

/*!
Paints button in response to a paint event.
*/
void QgsColorButton::paintEvent(QPaintEvent *e)
{
QToolButton::paintEvent(e);
if (isEnabled())
{
QPainter p(this);
int margin = 2; // Leave some space for highlighting
QRect r = rect().adjusted(margin, margin, -margin, -margin);
p.fillRect(r, mColor);
}
}
37 changes: 37 additions & 0 deletions src/gui/qgscolorbutton.h
@@ -0,0 +1,37 @@
/***************************************************************************
qgscolorbutton.h - Color button
--------------------------------------
Date : 12-Dec-2006
Copyright : (C) 2006 by Tom Elwertowski
Email : telwertowski at users dot sourceforge dot net
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#ifndef QGSCOLORBUTTON_H
#define QGSCOLORBUTTON_H

#include <QToolButton>

class QgsColorButton: public QToolButton
{
public:
QgsColorButton(QWidget *parent = 0);
~QgsColorButton();

void setColor(const QColor &color) { mColor = color; }
QColor color() const { return mColor; }

protected:
void paintEvent(QPaintEvent *e);

private:
QColor mColor;
};

#endif
55 changes: 17 additions & 38 deletions src/gui/qgscontinuouscolordialog.cpp
Expand Up @@ -65,13 +65,6 @@ QgsContinuousColorDialog::QgsContinuousColorDialog(QgsVectorLayer * layer)
return;
}

// new Qt4 idiom
#ifdef Q_WS_WIN
// Coloured buttons do not work under the Windows XP style - use plain Windows instead
btnMinValue->setStyle(&mWindowsStyle);
btnMaxValue->setStyle(&mWindowsStyle);
#endif

//restore the correct colors for minimum and maximum values

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

if (mVectorLayer->vectorType() == QGis::Line || mVectorLayer->vectorType() == QGis::Point)
{
// old Qt3 idiom
// lblMinValue->setPaletteBackgroundColor(minsymbol->pen().color());
// lblMaxValue->setPaletteBackgroundColor(maxsymbol->pen().color());
// new Qt4 idiom
btnMinValue->setPalette( minsymbol->pen().color() );
btnMaxValue->setPalette( maxsymbol->pen().color() );
{
btnMinValue->setColor( minsymbol->pen().color() );
btnMaxValue->setColor( maxsymbol->pen().color() );
}
else
{
// old Qt3 idiom
// lblMinValue->setPaletteBackgroundColor(minsymbol->brush().color());
// lblMaxValue->setPaletteBackgroundColor(maxsymbol->brush().color());
// new Qt4 idiom
btnMinValue->setPalette( minsymbol->brush().color() );
btnMaxValue->setPalette( maxsymbol->brush().color() );
}
{
btnMinValue->setColor( minsymbol->brush().color() );
btnMaxValue->setColor( maxsymbol->brush().color() );
}

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

btnMinValue->setPalette(Qt::black);
btnMaxValue->setPalette(Qt::white);
btnMinValue->setColor(Qt::black);
btnMaxValue->setColor(Qt::white);

}
// Ensure that the state of other widgets is appropriate for the
Expand Down Expand Up @@ -189,22 +174,22 @@ void QgsContinuousColorDialog::apply()
QgsSymbol* minsymbol = new QgsSymbol(mVectorLayer->vectorType(), QString::number(minimum, 'f'), "", "");
if (mVectorLayer->vectorType() == QGis::Line || mVectorLayer->vectorType() == QGis::Point)
{
minsymbol->setPen(QPen(btnMinValue->paletteBackgroundColor(),outlinewidthspinbox->value()));
minsymbol->setPen(QPen(btnMinValue->color(), outlinewidthspinbox->value()));
}
else
{
minsymbol->setBrush(QBrush(btnMinValue->paletteBackgroundColor()));
minsymbol->setBrush(QBrush(btnMinValue->color()));
minsymbol->setPen(QPen(QColor(0, 0, 0), outlinewidthspinbox->value()));
}

QgsSymbol* maxsymbol = new QgsSymbol(mVectorLayer->vectorType(), QString::number(maximum, 'f'), "", "");
if (mVectorLayer->vectorType() == QGis::Line || mVectorLayer->vectorType() == QGis::Point)
{
maxsymbol->setPen(QPen(btnMaxValue->paletteBackgroundColor(),outlinewidthspinbox->value()));
maxsymbol->setPen(QPen(btnMaxValue->color(), outlinewidthspinbox->value()));
}
else
{
maxsymbol->setBrush(QBrush(btnMaxValue->paletteBackgroundColor()));
maxsymbol->setBrush(QBrush(btnMaxValue->color()));
maxsymbol->setPen(QPen(QColor(0, 0, 0), outlinewidthspinbox->value()));
}

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

void QgsContinuousColorDialog::selectMinimumColor()
{
QColor mincolor = QColorDialog::getColor(btnMinValue->paletteBackgroundColor(), this);
QColor mincolor = QColorDialog::getColor(btnMinValue->color(), this);
if(mincolor.isValid())
{
// old Qt3 idiom
// lblMinValue->setPaletteBackgroundColor(mincolor);
// new Qt4 idiom
btnMinValue->setPalette(mincolor);
btnMinValue->setColor(mincolor);
}
setActiveWindow();
}

void QgsContinuousColorDialog::selectMaximumColor()
{
QColor maxcolor = QColorDialog::getColor(btnMaxValue->paletteBackgroundColor(), this);
QColor maxcolor = QColorDialog::getColor(btnMaxValue->color(), this);
if(maxcolor.isValid())
{
// old Qt3 idiom
// lblMaxValue->setPaletteBackgroundColor(maxcolor);
// new Qt4 idiom
btnMaxValue->setPalette(maxcolor);
btnMaxValue->setColor(maxcolor);
}
setActiveWindow();
}
Expand Down
9 changes: 0 additions & 9 deletions src/gui/qgscontinuouscolordialog.h
Expand Up @@ -23,10 +23,6 @@
#include "ui_qgscontinuouscolordialogbase.h"
#include <map>

#ifdef Q_WS_WIN
#include <QWindowsStyle>
#endif

class QgsVectorLayer;


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

#ifdef Q_WS_WIN
//! Holds the classic Windows style that is used to render labels with a background color
QWindowsStyle mWindowsStyle;
#endif

};

#endif
50 changes: 8 additions & 42 deletions src/gui/qgsoptions.cpp
Expand Up @@ -115,27 +115,13 @@ QgsOptions::QgsOptions(QWidget *parent, Qt::WFlags fl) :
int myRed = settings.value("/qgis/default_selection_color_red",255).toInt();
int myGreen = settings.value("/qgis/default_selection_color_green",255).toInt();
int myBlue = settings.value("/qgis/default_selection_color_blue",0).toInt();
// old Qt3 idiom
// pbnSelectionColour->setPaletteBackgroundColor(QColor(myRed,myGreen,myBlue));
// new Qt4 idiom - see http://lists.trolltech.com/qt4-preview-feedback/2005-04/thread00270-0.html for reasoning
#ifdef Q_WS_WIN
// Coloured buttons do not work under the Windows XP style - use plain Windows instead
pbnSelectionColour->setStyle(&mWindowsStyle);
#endif
pbnSelectionColour->setPalette( QColor(myRed,myGreen,myBlue) );
pbnSelectionColour->setColor( QColor(myRed,myGreen,myBlue) );

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

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

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

void QgsOptions::on_pbnSelectionColour_clicked()
{
// old Qt3 idiom
// QColor color = QColorDialog::getColor(pbnSelectionColour->paletteBackgroundColor(),this);
// new Qt4 idiom
QPalette palSelectionColour = pbnSelectionColour->palette();
QColor color = QColorDialog::getColor( palSelectionColour.color(QPalette::Window), this );
QColor color = QColorDialog::getColor(pbnSelectionColour->color(), this);
if (color.isValid())
{
// old Qt3 idiom
// pbnSelectionColour->setPaletteBackgroundColor(color);
// new Qt4 idiom - see http://lists.trolltech.com/qt4-preview-feedback/2005-04/thread00270-0.html for reasoning
pbnSelectionColour->setPalette(color);
pbnSelectionColour->setColor(color);
}
}

void QgsOptions::on_pbnCanvasColor_clicked()
{
// old Qt3 idiom
// QColor color = QColorDialog::getColor(pbnCanvasColor->paletteBackgroundColor(),this);
// new Qt4 idiom
QPalette palCanvasColor = pbnCanvasColor->palette();
QColor color = QColorDialog::getColor( palCanvasColor.color(QPalette::Window), this );
QColor color = QColorDialog::getColor(pbnCanvasColor->color(), this);
if (color.isValid())
{
// old Qt3 idiom
// pbnCanvasColor->setPaletteBackgroundColor(color);
// new Qt4 idiom - see http://lists.trolltech.com/qt4-preview-feedback/2005-04/thread00270-0.html for reasoning
pbnCanvasColor->setPalette(color);
pbnCanvasColor->setColor(color);
}
}
void QgsOptions::themeChanged(const QString &newThemeName)
Expand Down Expand Up @@ -231,19 +203,13 @@ void QgsOptions::saveOptions()
settings.writeEntry("/qgis/measure/ellipsoid", getEllipsoidAcronym(cmbEllipsoid->currentText()));

//set the colour for selections
// old Qt3 idiom
// QColor myColor = pbnSelectionColour->paletteBackgroundColor();
// new Qt4 idiom
QColor myColor = pbnSelectionColour->palette().color(QPalette::Window);
QColor myColor = pbnSelectionColour->color();
int myRed = settings.writeEntry("/qgis/default_selection_color_red",myColor.red());
int myGreen = settings.writeEntry("/qgis/default_selection_color_green",myColor.green());
int myBlue = settings.writeEntry("/qgis/default_selection_color_blue",myColor.blue());

//set the default color for canvas background
// old Qt3 idiom
// myColor = pbnCanvasColor->paletteBackgroundColor();
// new Qt4 idiom
myColor = pbnCanvasColor->palette().color(QPalette::Window);
myColor = pbnCanvasColor->color();
myRed = settings.writeEntry("/qgis/default_canvas_color_red",myColor.red());
myGreen = settings.writeEntry("/qgis/default_canvas_color_green",myColor.green());
myBlue = settings.writeEntry("/qgis/default_canvas_color_blue",myColor.blue());
Expand Down
10 changes: 0 additions & 10 deletions src/gui/qgsoptions.h
Expand Up @@ -22,11 +22,6 @@
#include "ui_qgsoptionsbase.h"
#include "qgisgui.h"

#ifdef Q_WS_WIN
#include <QWindowsStyle>
#endif



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

#ifdef Q_WS_WIN
//! Holds the classic Windows style that is used to render buttons with a background color
QWindowsStyle mWindowsStyle;
#endif

};

#endif // #ifndef QGSOPTIONS_H

0 comments on commit a8a1ef7

Please sign in to comment.