00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "qgscontinuouscolordialog.h"
00021 #include "qgscontinuouscolorrenderer.h"
00022 #include "qgsfield.h"
00023 #include "qgssymbol.h"
00024 #include "qgsvectordataprovider.h"
00025 #include "qgsvectorlayer.h"
00026 #include "qgslogger.h"
00027
00028 #include <QColorDialog>
00029
00030
00031 QgsContinuousColorDialog::QgsContinuousColorDialog( QgsVectorLayer * layer )
00032 : QDialog(), mVectorLayer( layer )
00033 {
00034 setupUi( this );
00035 #ifdef QGISDEBUG
00036 qWarning( "constructor QgsContinuousColorDialog" );
00037 #endif
00038
00039 QObject::connect( btnMinValue, SIGNAL( clicked() ), this, SLOT( selectMinimumColor() ) );
00040 QObject::connect( btnMaxValue, SIGNAL( clicked() ), this, SLOT( selectMaximumColor() ) );
00041
00042
00043 QgsVectorDataProvider *provider = mVectorLayer->dataProvider();
00044 if ( provider )
00045 {
00046 const QgsFieldMap & fields = provider->fields();
00047 int fieldNumber( 0 ), comboNumber( 0 );
00048 QString str;
00049
00050 for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
00051 {
00052 QVariant::Type type = ( *it ).type();
00053 if ( type == QVariant::Int || type == QVariant::Double )
00054 {
00055 str = ( *it ).name();
00056 classificationComboBox->addItem( str );
00057 mFieldMap.insert( std::make_pair( comboNumber, fieldNumber ) );
00058 comboNumber++;
00059 }
00060 fieldNumber++;
00061 }
00062 }
00063 else
00064 {
00065 qWarning( "Warning, data provider is null in QgsContinuousColorDialog::QgsContinuousColorDialog(...)" );
00066 return;
00067 }
00068
00069
00070
00071 const QgsContinuousColorRenderer* renderer = dynamic_cast < const QgsContinuousColorRenderer * >( layer->renderer() );;
00072
00073 if ( renderer )
00074 {
00075
00076
00077
00078
00079 std::map<int, int>::const_iterator iter = mFieldMap.begin();
00080 while ( iter != mFieldMap.end() )
00081 {
00082 if ( iter->second == renderer->classificationField() )
00083 break;
00084 iter++;
00085 }
00086 if ( iter != mFieldMap.end() )
00087 classificationComboBox->setCurrentIndex( iter->first );
00088 else
00089 classificationComboBox->setCurrentIndex( -1 );
00090
00091 const QgsSymbol* minsymbol = renderer->minimumSymbol();
00092 const QgsSymbol* maxsymbol = renderer->maximumSymbol();
00093
00094 if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
00095 {
00096 btnMinValue->setColor( minsymbol->pen().color() );
00097 btnMaxValue->setColor( maxsymbol->pen().color() );
00098 }
00099 else
00100 {
00101 btnMinValue->setColor( minsymbol->brush().color() );
00102 btnMaxValue->setColor( maxsymbol->brush().color() );
00103 }
00104
00105 outlinewidthspinbox->setMinimum( 0 );
00106 outlinewidthspinbox->setValue( minsymbol->pen().widthF() );
00107
00108 if ( renderer->drawPolygonOutline() )
00109 {
00110 cb_polygonOutline->setCheckState( Qt::Checked );
00111 }
00112 else
00113 {
00114 cb_polygonOutline->setCheckState( Qt::Unchecked );
00115 }
00116
00117 if ( mVectorLayer->geometryType() != QGis::Polygon )
00118 {
00119 cb_polygonOutline->setVisible( false );
00120 }
00121 }
00122 else
00123 {
00124 cb_polygonOutline->setCheckState( Qt::Checked );
00125 outlinewidthspinbox->setValue( 0.4 );
00126 if ( mVectorLayer->geometryType() != QGis::Polygon )
00127 cb_polygonOutline->setVisible( false );
00128
00129 btnMinValue->setColor( Qt::black );
00130 btnMaxValue->setColor( Qt::white );
00131
00132 }
00133
00134
00135 on_cb_polygonOutline_clicked();
00136 }
00137
00138 QgsContinuousColorDialog::QgsContinuousColorDialog()
00139 {
00140 setupUi( this );
00141 #ifdef QGISDEBUG
00142 qWarning( "constructor QgsContinuousColorDialog" );
00143 #endif
00144 }
00145
00146 QgsContinuousColorDialog::~QgsContinuousColorDialog()
00147 {
00148 #ifdef QGISDEBUG
00149 qWarning( "destructor QgsContinuousColorDialog" );
00150 #endif
00151 }
00152
00153 void QgsContinuousColorDialog::apply()
00154 {
00155 int comboIndex = classificationComboBox->currentIndex();
00156 if ( comboIndex == -1 )
00157 {
00158 return;
00159 }
00160 std::map < int, int >::iterator iter = mFieldMap.find( comboIndex );
00161
00162 assert( iter != mFieldMap.end() );
00163
00164 int classfield = iter->second;
00165
00166
00167 double minimum, maximum;
00168 QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider*>( mVectorLayer->dataProvider() );
00169 if ( provider )
00170 {
00171 minimum = provider->minimumValue( classfield ).toDouble();
00172 maximum = provider->maximumValue( classfield ).toDouble();
00173 }
00174 else
00175 {
00176 QgsDebugMsg( "Warning, provider is null" );
00177 return;
00178 }
00179
00180
00181
00182 QgsSymbol* minsymbol = new QgsSymbol( mVectorLayer->geometryType(), QString::number( minimum, 'f' ), "", "" );
00183 QPen minPen;
00184 minPen.setColor( btnMinValue->color() );
00185 minPen.setWidthF( outlinewidthspinbox->value() );
00186 if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
00187 {
00188 minsymbol->setPen( minPen );
00189 }
00190 else
00191 {
00192 minsymbol->setBrush( QBrush( btnMinValue->color() ) );
00193 minsymbol->setPen( minPen );
00194 }
00195
00196 QgsSymbol* maxsymbol = new QgsSymbol( mVectorLayer->geometryType(), QString::number( maximum, 'f' ), "", "" );
00197 QPen maxPen;
00198 maxPen.setColor( btnMaxValue->color() );
00199 maxPen.setWidthF( outlinewidthspinbox->value() );
00200 if ( mVectorLayer->geometryType() == QGis::Line || mVectorLayer->geometryType() == QGis::Point )
00201 {
00202 maxsymbol->setPen( maxPen );
00203 }
00204 else
00205 {
00206 maxsymbol->setBrush( QBrush( btnMaxValue->color() ) );
00207 maxsymbol->setPen( maxPen );
00208 }
00209
00210 QgsContinuousColorRenderer* renderer = new QgsContinuousColorRenderer( mVectorLayer->geometryType() );
00211 mVectorLayer->setRenderer( renderer );
00212
00213 renderer->setMinimumSymbol( minsymbol );
00214 renderer->setMaximumSymbol( maxsymbol );
00215 renderer->setClassificationField( classfield );
00216 bool drawOutline = ( cb_polygonOutline->checkState() == Qt::Checked ) ? true : false;
00217 renderer->setDrawPolygonOutline( drawOutline );
00218 }
00219
00220 void QgsContinuousColorDialog::selectMinimumColor()
00221 {
00222 QColor mincolor = QColorDialog::getColor( btnMinValue->color(), this );
00223 if ( mincolor.isValid() )
00224 {
00225 btnMinValue->setColor( mincolor );
00226 }
00227 activateWindow();
00228 }
00229
00230 void QgsContinuousColorDialog::selectMaximumColor()
00231 {
00232 QColor maxcolor = QColorDialog::getColor( btnMaxValue->color(), this );
00233 if ( maxcolor.isValid() )
00234 {
00235 btnMaxValue->setColor( maxcolor );
00236 }
00237 activateWindow();
00238 }
00239
00240 void QgsContinuousColorDialog::on_cb_polygonOutline_clicked()
00241 {
00242 if ( cb_polygonOutline->checkState() == Qt::Checked )
00243 outlinewidthspinbox->setEnabled( true );
00244 else
00245 outlinewidthspinbox->setEnabled( false );
00246 }