~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/app/qgscontinuouscolordialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          qgscontinuouscolordialog.cpp
 
3
 Continuous color renderer dialog
 
4
                             -------------------
 
5
    begin                : 2004-02-11
 
6
    copyright            : (C) 2004 by Gary E.Sherman
 
7
    email                : sherman at mrcc.com
 
8
***************************************************************************/
 
9
 
 
10
/***************************************************************************
 
11
 *                                                                         *
 
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.                                   *
 
16
 *                                                                         *
 
17
 ***************************************************************************/
 
18
/* $Id$ */
 
19
 
 
20
#include "qgscontinuouscolordialog.h"
 
21
#include "qgscontinuouscolorrenderer.h"
 
22
#include "qgis.h"
 
23
#include "qgsfield.h"
 
24
#include "qgssymbol.h"
 
25
#include "qgsvectordataprovider.h"
 
26
#include "qgsvectorlayer.h"
 
27
#include "qgslogger.h"
 
28
 
 
29
#include <QColorDialog>
 
30
#include <QKeyEvent>
 
31
 
 
32
QgsContinuousColorDialog::QgsContinuousColorDialog( QgsVectorLayer * layer )
 
33
    : QDialog(), mVectorLayer( layer )
 
34
{
 
35
  setupUi( this );
 
36
  QgsDebugMsg( "entered." );
 
37
 
 
38
  QObject::connect( btnMinValue, SIGNAL( clicked() ), this, SLOT( selectMinimumColor() ) );
 
39
  QObject::connect( btnMaxValue, SIGNAL( clicked() ), this, SLOT( selectMaximumColor() ) );
 
40
 
 
41
  //find out the numerical fields of mVectorLayer
 
42
  const QgsFieldMap & fields = mVectorLayer->pendingFields();
 
43
  QString displayName;
 
44
 
 
45
  for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
 
46
  {
 
47
    QVariant::Type type = it->type();
 
48
    if ( type == QVariant::Int || type == QVariant::Double || type == QVariant::LongLong )
 
49
    {
 
50
      displayName = mVectorLayer->attributeDisplayName( it.key() );
 
51
      classificationComboBox->addItem( displayName, it.key() );
 
52
    }
 
53
  }
 
54
 
 
55
  //restore the correct colors for minimum and maximum values
 
56
 
 
57
  const QgsContinuousColorRenderer* renderer = dynamic_cast<const QgsContinuousColorRenderer *>( layer->renderer() );;
 
58
 
 
59
  if ( renderer )
 
60
  {
 
61
    classificationComboBox->setCurrentIndex( classificationComboBox->findData( renderer->classificationField() ) );
 
62
 
 
63
    const QgsSymbol* minsymbol = renderer->minimumSymbol();
 
64
    const QgsSymbol* maxsymbol = renderer->maximumSymbol();
 
65
 
 
66
    if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
 
67
    {
 
68
      btnMinValue->setColor( minsymbol->pen().color() );
 
69
      btnMaxValue->setColor( maxsymbol->pen().color() );
 
70
    }
 
71
    else
 
72
    {
 
73
      btnMinValue->setColor( minsymbol->brush().color() );
 
74
      btnMaxValue->setColor( maxsymbol->brush().color() );
 
75
    }
 
76
 
 
77
    outlinewidthspinbox->setMinimum( 0 );
 
78
    outlinewidthspinbox->setValue( minsymbol->pen().widthF() );
 
79
 
 
80
    if ( renderer->drawPolygonOutline() )
 
81
    {
 
82
      cb_polygonOutline->setCheckState( Qt::Checked );
 
83
    }
 
84
    else
 
85
    {
 
86
      cb_polygonOutline->setCheckState( Qt::Unchecked );
 
87
    }
 
88
 
 
89
    if ( mVectorLayer->geometryType() != QGis::Polygon )
 
90
    {
 
91
      cb_polygonOutline->setVisible( false );
 
92
    }
 
93
  }
 
94
  else
 
95
  {
 
96
    cb_polygonOutline->setCheckState( Qt::Checked );
 
97
    outlinewidthspinbox->setValue( DEFAULT_LINE_WIDTH );
 
98
    if ( mVectorLayer->geometryType() != QGis::Polygon )
 
99
      cb_polygonOutline->setVisible( false );
 
100
 
 
101
    btnMinValue->setColor( Qt::black );
 
102
    btnMaxValue->setColor( Qt::white );
 
103
 
 
104
  }
 
105
  // Ensure that the state of other widgets is appropriate for the
 
106
  // state of the polygonoutline checkbox.
 
107
  on_cb_polygonOutline_clicked();
 
108
}
 
109
 
 
110
QgsContinuousColorDialog::QgsContinuousColorDialog()
 
111
{
 
112
  setupUi( this );
 
113
  QgsDebugMsg( "entered." );
 
114
}
 
115
 
 
116
QgsContinuousColorDialog::~QgsContinuousColorDialog()
 
117
{
 
118
  QgsDebugMsg( "entered." );
 
119
}
 
120
 
 
121
void QgsContinuousColorDialog::apply()
 
122
{
 
123
  int classfield = classificationComboBox->itemData( classificationComboBox->currentIndex() ).toInt();
 
124
 
 
125
  //find the minimum and maximum for the classification variable
 
126
  double minimum, maximum;
 
127
  QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider *>( mVectorLayer->dataProvider() );
 
128
  if ( provider )
 
129
  {
 
130
    minimum = provider->minimumValue( classfield ).toDouble();
 
131
    maximum = provider->maximumValue( classfield ).toDouble();
 
132
  }
 
133
  else
 
134
  {
 
135
    QgsDebugMsg( "Warning, provider is null" );
 
136
    return;
 
137
  }
 
138
 
 
139
 
 
140
  //create the render items for minimum and maximum value
 
141
  QgsSymbol* minsymbol = new QgsSymbol( mVectorLayer->geometryType(), QVariant( minimum ).toString(), "", "" );
 
142
  QPen minPen;
 
143
  minPen.setColor( btnMinValue->color() );
 
144
  minPen.setWidthF( outlinewidthspinbox->value() );
 
145
  if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
 
146
  {
 
147
    minsymbol->setPen( minPen );
 
148
  }
 
149
  else
 
150
  {
 
151
    minsymbol->setBrush( QBrush( btnMinValue->color() ) );
 
152
    minsymbol->setPen( minPen );
 
153
  }
 
154
 
 
155
  QgsSymbol* maxsymbol = new QgsSymbol( mVectorLayer->geometryType(), QVariant( maximum ).toString(), "", "" );
 
156
  QPen maxPen;
 
157
  maxPen.setColor( btnMaxValue->color() );
 
158
  maxPen.setWidthF( outlinewidthspinbox->value() );
 
159
  if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
 
160
  {
 
161
    maxsymbol->setPen( maxPen );
 
162
  }
 
163
  else
 
164
  {
 
165
    maxsymbol->setBrush( QBrush( btnMaxValue->color() ) );
 
166
    maxsymbol->setPen( maxPen );
 
167
  }
 
168
 
 
169
  QgsContinuousColorRenderer* renderer = new QgsContinuousColorRenderer( mVectorLayer->geometryType() );
 
170
  mVectorLayer->setRenderer( renderer );
 
171
 
 
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 );
 
177
}
 
178
 
 
179
void QgsContinuousColorDialog::selectMinimumColor()
 
180
{
 
181
  QColor mincolor = QColorDialog::getColor( btnMinValue->color(), this );
 
182
  if ( mincolor.isValid() )
 
183
  {
 
184
    btnMinValue->setColor( mincolor );
 
185
  }
 
186
  activateWindow();
 
187
}
 
188
 
 
189
void QgsContinuousColorDialog::selectMaximumColor()
 
190
{
 
191
  QColor maxcolor = QColorDialog::getColor( btnMaxValue->color(), this );
 
192
  if ( maxcolor.isValid() )
 
193
  {
 
194
    btnMaxValue->setColor( maxcolor );
 
195
  }
 
196
  activateWindow();
 
197
}
 
198
 
 
199
void QgsContinuousColorDialog::on_cb_polygonOutline_clicked()
 
200
{
 
201
  if ( cb_polygonOutline->checkState() == Qt::Checked )
 
202
    outlinewidthspinbox->setEnabled( true );
 
203
  else
 
204
    outlinewidthspinbox->setEnabled( false );
 
205
}
 
206
 
 
207
void QgsContinuousColorDialog::keyPressEvent( QKeyEvent * e )
 
208
{
 
209
  // Ignore the ESC key to avoid close the dialog without the properties window
 
210
  if ( e->key() == Qt::Key_Escape )
 
211
  {
 
212
    e->ignore();
 
213
  }
 
214
}