-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsgraduatedsymbolrendererv2widget.cpp
335 lines (259 loc) · 9.78 KB
/
qgsgraduatedsymbolrendererv2widget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "qgsgraduatedsymbolrendererv2widget.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"
#include "qgsstylev2.h"
#include "qgsvectorlayer.h"
#include "qgsgraduatedsymbolrendererv2.h"
#include "qgssymbolv2selectordialog.h"
#include "qgsludialog.h"
#include <QMessageBox>
#include <QStandardItemModel>
#include <QStandardItem>
QgsRendererV2Widget* QgsGraduatedSymbolRendererV2Widget::create( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer )
{
return new QgsGraduatedSymbolRendererV2Widget( layer, style, renderer );
}
QgsGraduatedSymbolRendererV2Widget::QgsGraduatedSymbolRendererV2Widget( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer )
: QgsRendererV2Widget( layer, style )
{
// try to recognize the previous renderer
// (null renderer means "no previous renderer")
if ( !renderer || renderer->type() != "graduatedSymbol" )
{
// we're not going to use it - so let's delete the renderer
delete renderer;
mRenderer = new QgsGraduatedSymbolRendererV2( "", QgsRangeList() );
}
else
{
mRenderer = static_cast<QgsGraduatedSymbolRendererV2*>( renderer );
}
// setup user interface
setupUi( this );
populateColumns();
populateColorRamps();
QStandardItemModel* mg = new QStandardItemModel( this );
QStringList labels;
labels << "Range" << "Label";
mg->setHorizontalHeaderLabels( labels );
viewGraduated->setModel( mg );
mGraduatedSymbol = QgsSymbolV2::defaultSymbol( mLayer->geometryType() );
connect( cboGraduatedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( graduatedColumnChanged() ) );
connect( viewGraduated, SIGNAL( doubleClicked( const QModelIndex & ) ), this, SLOT( rangesDoubleClicked( const QModelIndex & ) ) );
connect( viewGraduated, SIGNAL( clicked( const QModelIndex & ) ), this, SLOT( rangesClicked( const QModelIndex & ) ) );
connect( mg, SIGNAL( itemChanged( QStandardItem * ) ), this, SLOT( changeCurrentValue( QStandardItem * ) ) );
connect( btnGraduatedClassify, SIGNAL( clicked() ), this, SLOT( classifyGraduated() ) );
connect( btnChangeGraduatedSymbol, SIGNAL( clicked() ), this, SLOT( changeGraduatedSymbol() ) );
connect( btnGraduatedDelete, SIGNAL( clicked() ), this, SLOT( deleteCurrentClass() ) );
connect( btnGraduatedAdd, SIGNAL( clicked() ), this, SLOT( addClass() ) );
// initialize from previously set renderer
updateUiFromRenderer();
}
QgsGraduatedSymbolRendererV2Widget::~QgsGraduatedSymbolRendererV2Widget()
{
delete mRenderer;
}
QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2Widget::renderer()
{
return mRenderer;
}
void QgsGraduatedSymbolRendererV2Widget::updateUiFromRenderer()
{
updateGraduatedSymbolIcon();
populateRanges();
// update UI from the graduated renderer (update combo boxes, view)
if ( mRenderer->mode() < cboGraduatedMode->count() )
cboGraduatedMode->setCurrentIndex( mRenderer->mode() );
if ( mRenderer->ranges().count() )
spinGraduatedClasses->setValue( mRenderer->ranges().count() );
// set column
disconnect( cboGraduatedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( graduatedColumnChanged() ) );
QString attrName = mRenderer->classAttribute();
int idx = cboGraduatedColumn->findText( attrName, Qt::MatchExactly );
cboGraduatedColumn->setCurrentIndex( idx >= 0 ? idx : 0 );
connect( cboGraduatedColumn, SIGNAL( currentIndexChanged( int ) ), this, SLOT( graduatedColumnChanged() ) );
// set source symbol
if ( mRenderer->sourceSymbol() )
{
delete mGraduatedSymbol;
mGraduatedSymbol = mRenderer->sourceSymbol()->clone();
updateGraduatedSymbolIcon();
}
// set source color ramp
if ( mRenderer->sourceColorRamp() )
{
QSize rampIconSize( 50, 16 );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( mRenderer->sourceColorRamp(), rampIconSize );
if ( cboGraduatedColorRamp->itemText( 0 ) == "[source]" )
cboGraduatedColorRamp->setItemIcon( 0, icon );
else
cboGraduatedColorRamp->insertItem( 0, icon, "[source]" );
cboGraduatedColorRamp->setCurrentIndex( 0 );
}
}
void QgsGraduatedSymbolRendererV2Widget::populateColumns()
{
cboGraduatedColumn->clear();
const QgsFieldMap& flds = mLayer->pendingFields();
QgsFieldMap::ConstIterator it = flds.begin();
for ( ; it != flds.end(); ++it )
{
if ( it->type() == QVariant::Double || it->type() == QVariant::Int )
cboGraduatedColumn->addItem( it->name() );
}
}
void QgsGraduatedSymbolRendererV2Widget::graduatedColumnChanged()
{
mRenderer->setClassAttribute( cboGraduatedColumn->currentText() );
}
void QgsGraduatedSymbolRendererV2Widget::populateColorRamps()
{
QSize rampIconSize( 50, 16 );
cboGraduatedColorRamp->setIconSize( rampIconSize );
QStringList rampNames = mStyle->colorRampNames();
for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
{
QgsVectorColorRampV2* ramp = mStyle->colorRamp( *it );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, rampIconSize );
cboGraduatedColorRamp->addItem( icon, *it );
delete ramp;
}
}
void QgsGraduatedSymbolRendererV2Widget::classifyGraduated()
{
QString attrName = cboGraduatedColumn->currentText();
int classes = spinGraduatedClasses->value();
QgsVectorColorRampV2* ramp = NULL;
QString rampName = cboGraduatedColorRamp->currentText();
if ( rampName == "[source]" )
ramp = mRenderer->sourceColorRamp()->clone();
else
ramp = mStyle->colorRamp( rampName );
if ( ramp == NULL )
{
if ( cboGraduatedColorRamp->count() == 0 )
QMessageBox::critical( this, tr( "Error" ), tr( "There are no available color ramps. You can add them in Style Manager." ) );
else
QMessageBox::critical( this, tr( "Error" ), tr( "The selected color ramp is not available." ) );
return;
}
QgsGraduatedSymbolRendererV2::Mode mode;
if ( cboGraduatedMode->currentIndex() == 0 )
mode = QgsGraduatedSymbolRendererV2::EqualInterval;
else
mode = QgsGraduatedSymbolRendererV2::Quantile;
// create and set new renderer
delete mRenderer;
mRenderer = QgsGraduatedSymbolRendererV2::createRenderer(
mLayer, attrName, classes, mode, mGraduatedSymbol, ramp );
populateRanges();
}
void QgsGraduatedSymbolRendererV2Widget::changeGraduatedSymbol()
{
QgsSymbolV2SelectorDialog dlg( mGraduatedSymbol, mStyle, this );
if ( !dlg.exec() )
return;
updateGraduatedSymbolIcon();
}
void QgsGraduatedSymbolRendererV2Widget::updateGraduatedSymbolIcon()
{
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mGraduatedSymbol, btnChangeGraduatedSymbol->iconSize() );
btnChangeGraduatedSymbol->setIcon( icon );
}
void QgsGraduatedSymbolRendererV2Widget::populateRanges()
{
QStandardItemModel* m = qobject_cast<QStandardItemModel*>( viewGraduated->model() );
m->clear();
mRowSelected = -1;
QStringList labels;
labels << "Symbol" << "Range" << "Label";
m->setHorizontalHeaderLabels( labels );
QSize iconSize( 16, 16 );
int i, count = mRenderer->ranges().count();
for ( i = 0; i < count; i++ )
{
const QgsRendererRangeV2& range = mRenderer->ranges()[i];
QString rangeStr = QString::number( range.lowerValue(), 'f', 4 ) + " - " + QString::number( range.upperValue(), 'f', 4 );
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( range.symbol(), iconSize );
QStandardItem* item = new QStandardItem( icon, "" );
//item->setData(k); // set attribute value as user role
item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
QStandardItem* item2 = new QStandardItem( rangeStr );
item2->setEditable( 0 );
QList<QStandardItem *> list;
list << item << item2 << new QStandardItem( range.label() );
m->appendRow( list );
}
// make sure that the "range" column has visible context
viewGraduated->resizeColumnToContents( 0 );
}
#if 0
int QgsRendererV2PropertiesDialog::currentRangeRow()
{
QModelIndex idx = viewGraduated->selectionModel()->currentIndex();
if ( !idx.isValid() )
return -1;
return idx.row();
}
#endif
void QgsGraduatedSymbolRendererV2Widget::rangesDoubleClicked( const QModelIndex & idx )
{
if ( idx.isValid() && idx.column() == 0 )
changeRangeSymbol( idx.row() );
if ( idx.isValid() && idx.column() == 1 )
changeRange( idx.row() );
}
void QgsGraduatedSymbolRendererV2Widget::rangesClicked( const QModelIndex & idx )
{
if ( !idx.isValid() )
mRowSelected = -1;
else
mRowSelected = idx.row();
}
void QgsGraduatedSymbolRendererV2Widget::changeRangeSymbol( int rangeIdx )
{
QgsSymbolV2* newSymbol = mRenderer->ranges()[rangeIdx].symbol()->clone();
QgsSymbolV2SelectorDialog dlg( newSymbol, mStyle, this );
if ( !dlg.exec() )
{
delete newSymbol;
return;
}
mRenderer->updateRangeSymbol( rangeIdx, newSymbol );
populateRanges();
}
void QgsGraduatedSymbolRendererV2Widget::changeRange( int rangeIdx )
{
QgsLUDialog dialog( this );
const QgsRendererRangeV2& range = mRenderer->ranges()[rangeIdx];
dialog.setLowerValue( QString( "%1" ).arg( range.lowerValue() ) );
dialog.setUpperValue( QString( "%1" ).arg( range.upperValue() ) );
if ( dialog.exec() == QDialog::Accepted )
{
double lowerValue = dialog.lowerValue().toDouble();
double upperValue = dialog.upperValue().toDouble();
mRenderer->updateRangeUpperValue( rangeIdx, upperValue );
mRenderer->updateRangeLowerValue( rangeIdx, lowerValue );
populateRanges();
}
}
void QgsGraduatedSymbolRendererV2Widget::addClass()
{
mRenderer->addClass( mGraduatedSymbol );
populateRanges();
}
void QgsGraduatedSymbolRendererV2Widget::deleteCurrentClass()
{
mRenderer->deleteClass( mRowSelected );
populateRanges();
}
void QgsGraduatedSymbolRendererV2Widget::changeCurrentValue( QStandardItem * item )
{
if ( item->column() == 2 )
{
QString label = item->text();
int idx = item->row();
mRenderer->updateRangeLabel( idx, label );
}
}