/
qgscontinuouscolordialog.cpp
235 lines (205 loc) · 7.97 KB
/
qgscontinuouscolordialog.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
/***************************************************************************
qgscontinuouscolordialog.cpp
Continuous color renderer dialog
-------------------
begin : 2004-02-11
copyright : (C) 2004 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* 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 "qgscontinuouscolordialog.h"
#include "qgscontinuouscolorrenderer.h"
#include "qgsfield.h"
#include "qgssymbol.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include <QColorDialog>
QgsContinuousColorDialog::QgsContinuousColorDialog(QgsVectorLayer * layer)
: QDialog(), mVectorLayer(layer)
{
setupUi(this);
#ifdef QGISDEBUG
qWarning("constructor QgsContinuousColorDialog");
#endif
QObject::connect(btnMinValue, SIGNAL(clicked()), this, SLOT(selectMinimumColor()));
QObject::connect(btnMaxValue, SIGNAL(clicked()), this, SLOT(selectMaximumColor()));
//find out the numerical fields of mVectorLayer
QgsVectorDataProvider *provider;
if (provider = dynamic_cast<QgsVectorDataProvider*>(mVectorLayer->getDataProvider()))
{
std::vector < QgsField > const & fields = provider->fields();
int fieldnumber = 0;
QString str;
for (std::vector < QgsField >::const_iterator it = fields.begin(); it != fields.end(); ++it)
{
QString type = (*it).type();
if (type != "String" && type != "varchar" && type != "geometry")
{
str = (*it).name();
str = str.left(1).upper() + str.right(str.length() - 1); //make the first letter uppercase
classificationComboBox->insertItem(str);
mFieldMap.insert(std::make_pair(str, fieldnumber));
}
fieldnumber++;
}
}
else
{
qWarning("Warning, data provider is null in QgsContinuousColorDialog::QgsContinuousColorDialog(...)");
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());;
if (renderer)
{
classificationComboBox->setCurrentItem(renderer->classificationField());
const QgsSymbol* minsymbol = renderer->minimumSymbol();
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() );
}
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() );
}
outlinewidthspinbox->setMinValue(0);
outlinewidthspinbox->setValue(minsymbol->pen().width());
if (renderer->drawPolygonOutline())
cb_polygonOutline->setCheckState(Qt::Checked);
else
cb_polygonOutline->setCheckState(Qt::Unchecked);
}
else
{
cb_polygonOutline->setCheckState(Qt::Checked);
outlinewidthspinbox->setValue(1);
if (mVectorLayer->vectorType() != QGis::Polygon)
cb_polygonOutline->setVisible(false);
btnMinValue->setPalette(Qt::black);
btnMaxValue->setPalette(Qt::white);
}
// Ensure that the state of other widgets is appropriate for the
// state of the polygonoutline checkbox.
on_cb_polygonOutline_clicked();
}
QgsContinuousColorDialog::QgsContinuousColorDialog()
{
setupUi(this);
#ifdef QGISDEBUG
qWarning("constructor QgsContinuousColorDialog");
#endif
}
QgsContinuousColorDialog::~QgsContinuousColorDialog()
{
#ifdef QGISDEBUG
qWarning("destructor QgsContinuousColorDialog");
#endif
}
void QgsContinuousColorDialog::apply()
{
QString fieldstring = classificationComboBox->currentText();
if (fieldstring.isEmpty()) //don't do anything, it there is no classification field
{
return;
}
std::map < QString, int >::iterator iter = mFieldMap.find(fieldstring);
int classfield = iter->second;
//find the minimum and maximum for the classification variable
double minimum, maximum;
QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider*>(mVectorLayer->getDataProvider());
if (provider)
{
minimum = provider->minValue(classfield).toDouble();
maximum = provider->maxValue(classfield).toDouble();
}
else
{
qWarning("Warning, provider is null in QgsGraSyExtensionWidget::QgsGraSyExtensionWidget(...)");
return;
}
//create the render items for minimum and maximum value
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()));
}
else
{
minsymbol->setBrush(QBrush(btnMinValue->paletteBackgroundColor()));
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()));
}
else
{
maxsymbol->setBrush(QBrush(btnMaxValue->paletteBackgroundColor()));
maxsymbol->setPen(QPen(QColor(0, 0, 0), outlinewidthspinbox->value()));
}
QgsContinuousColorRenderer* renderer = new QgsContinuousColorRenderer(mVectorLayer->vectorType());
mVectorLayer->setRenderer(renderer);
renderer->setMinimumSymbol(minsymbol);
renderer->setMaximumSymbol(maxsymbol);
renderer->setClassificationField(classfield);
bool drawOutline = (cb_polygonOutline->checkState() == Qt::Checked) ? true:false;
renderer->setDrawPolygonOutline(drawOutline);
mVectorLayer->refreshLegend();
}
void QgsContinuousColorDialog::selectMinimumColor()
{
QColor mincolor = QColorDialog::getColor(btnMinValue->paletteBackgroundColor(), this);
if(mincolor.isValid())
{
// old Qt3 idiom
// lblMinValue->setPaletteBackgroundColor(mincolor);
// new Qt4 idiom
btnMinValue->setPalette(mincolor);
}
setActiveWindow();
}
void QgsContinuousColorDialog::selectMaximumColor()
{
QColor maxcolor = QColorDialog::getColor(btnMaxValue->paletteBackgroundColor(), this);
if(maxcolor.isValid())
{
// old Qt3 idiom
// lblMaxValue->setPaletteBackgroundColor(maxcolor);
// new Qt4 idiom
btnMaxValue->setPalette(maxcolor);
}
setActiveWindow();
}
void QgsContinuousColorDialog::on_cb_polygonOutline_clicked()
{
if (cb_polygonOutline->checkState() == Qt::Checked)
outlinewidthspinbox->setEnabled(true);
else
outlinewidthspinbox->setEnabled(false);
}