1
/***************************************************************************
2
qgscontinuouscolordialog.cpp
3
Continuous color renderer dialog
6
copyright : (C) 2004 by Gary E.Sherman
7
email : sherman at mrcc.com
8
***************************************************************************/
10
/***************************************************************************
12
* This program is free software; you can redistribute it and/or modify *
13
* it under the terms of the GNU General Public License as published by *
14
* the Free Software Foundation; either version 2 of the License, or *
15
* (at your option) any later version. *
17
***************************************************************************/
20
#include "qgscontinuouscolordialog.h"
21
#include "qgscontinuouscolorrenderer.h"
24
#include "qgssymbol.h"
25
#include "qgsvectordataprovider.h"
26
#include "qgsvectorlayer.h"
27
#include "qgslogger.h"
29
#include <QColorDialog>
32
QgsContinuousColorDialog::QgsContinuousColorDialog( QgsVectorLayer * layer )
33
: QDialog(), mVectorLayer( layer )
36
QgsDebugMsg( "entered." );
38
QObject::connect( btnMinValue, SIGNAL( clicked() ), this, SLOT( selectMinimumColor() ) );
39
QObject::connect( btnMaxValue, SIGNAL( clicked() ), this, SLOT( selectMaximumColor() ) );
41
//find out the numerical fields of mVectorLayer
42
const QgsFieldMap & fields = mVectorLayer->pendingFields();
45
for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
47
QVariant::Type type = it->type();
48
if ( type == QVariant::Int || type == QVariant::Double || type == QVariant::LongLong )
50
displayName = mVectorLayer->attributeDisplayName( it.key() );
51
classificationComboBox->addItem( displayName, it.key() );
55
//restore the correct colors for minimum and maximum values
57
const QgsContinuousColorRenderer* renderer = dynamic_cast<const QgsContinuousColorRenderer *>( layer->renderer() );;
61
classificationComboBox->setCurrentIndex( classificationComboBox->findData( renderer->classificationField() ) );
63
const QgsSymbol* minsymbol = renderer->minimumSymbol();
64
const QgsSymbol* maxsymbol = renderer->maximumSymbol();
66
if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
68
btnMinValue->setColor( minsymbol->pen().color() );
69
btnMaxValue->setColor( maxsymbol->pen().color() );
73
btnMinValue->setColor( minsymbol->brush().color() );
74
btnMaxValue->setColor( maxsymbol->brush().color() );
77
outlinewidthspinbox->setMinimum( 0 );
78
outlinewidthspinbox->setValue( minsymbol->pen().widthF() );
80
if ( renderer->drawPolygonOutline() )
82
cb_polygonOutline->setCheckState( Qt::Checked );
86
cb_polygonOutline->setCheckState( Qt::Unchecked );
89
if ( mVectorLayer->geometryType() != QGis::Polygon )
91
cb_polygonOutline->setVisible( false );
96
cb_polygonOutline->setCheckState( Qt::Checked );
97
outlinewidthspinbox->setValue( DEFAULT_LINE_WIDTH );
98
if ( mVectorLayer->geometryType() != QGis::Polygon )
99
cb_polygonOutline->setVisible( false );
101
btnMinValue->setColor( Qt::black );
102
btnMaxValue->setColor( Qt::white );
105
// Ensure that the state of other widgets is appropriate for the
106
// state of the polygonoutline checkbox.
107
on_cb_polygonOutline_clicked();
110
QgsContinuousColorDialog::QgsContinuousColorDialog()
113
QgsDebugMsg( "entered." );
116
QgsContinuousColorDialog::~QgsContinuousColorDialog()
118
QgsDebugMsg( "entered." );
121
void QgsContinuousColorDialog::apply()
123
int classfield = classificationComboBox->itemData( classificationComboBox->currentIndex() ).toInt();
125
//find the minimum and maximum for the classification variable
126
double minimum, maximum;
127
QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider *>( mVectorLayer->dataProvider() );
130
minimum = provider->minimumValue( classfield ).toDouble();
131
maximum = provider->maximumValue( classfield ).toDouble();
135
QgsDebugMsg( "Warning, provider is null" );
140
//create the render items for minimum and maximum value
141
QgsSymbol* minsymbol = new QgsSymbol( mVectorLayer->geometryType(), QVariant( minimum ).toString(), "", "" );
143
minPen.setColor( btnMinValue->color() );
144
minPen.setWidthF( outlinewidthspinbox->value() );
145
if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
147
minsymbol->setPen( minPen );
151
minsymbol->setBrush( QBrush( btnMinValue->color() ) );
152
minsymbol->setPen( minPen );
155
QgsSymbol* maxsymbol = new QgsSymbol( mVectorLayer->geometryType(), QVariant( maximum ).toString(), "", "" );
157
maxPen.setColor( btnMaxValue->color() );
158
maxPen.setWidthF( outlinewidthspinbox->value() );
159
if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
161
maxsymbol->setPen( maxPen );
165
maxsymbol->setBrush( QBrush( btnMaxValue->color() ) );
166
maxsymbol->setPen( maxPen );
169
QgsContinuousColorRenderer* renderer = new QgsContinuousColorRenderer( mVectorLayer->geometryType() );
170
mVectorLayer->setRenderer( renderer );
172
renderer->setMinimumSymbol( minsymbol );
173
renderer->setMaximumSymbol( maxsymbol );
174
renderer->setClassificationField( classfield );
175
bool drawOutline = ( cb_polygonOutline->checkState() == Qt::Checked ) ? true : false;
176
renderer->setDrawPolygonOutline( drawOutline );
179
void QgsContinuousColorDialog::selectMinimumColor()
181
QColor mincolor = QColorDialog::getColor( btnMinValue->color(), this );
182
if ( mincolor.isValid() )
184
btnMinValue->setColor( mincolor );
189
void QgsContinuousColorDialog::selectMaximumColor()
191
QColor maxcolor = QColorDialog::getColor( btnMaxValue->color(), this );
192
if ( maxcolor.isValid() )
194
btnMaxValue->setColor( maxcolor );
199
void QgsContinuousColorDialog::on_cb_polygonOutline_clicked()
201
if ( cb_polygonOutline->checkState() == Qt::Checked )
202
outlinewidthspinbox->setEnabled( true );
204
outlinewidthspinbox->setEnabled( false );
207
void QgsContinuousColorDialog::keyPressEvent( QKeyEvent * e )
209
// Ignore the ESC key to avoid close the dialog without the properties window
210
if ( e->key() == Qt::Key_Escape )