1
/***************************************************************************
2
qgsuniquevaluedialog.cpp - description
5
copyright : (C) 2004 by Marco Hugentobler
6
email : marco.hugentobler@autoform.ch
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
17
/* $Id: qgsuniquevaluedialog.cpp 6087 2006-11-15 17:35:28Z mhugent $ */
19
#include "qgsuniquevaluedialog.h"
20
#include "qgsfeature.h"
21
#include "qgsfeatureattribute.h"
22
#include "qgssymbol.h"
23
#include "qgsuniquevaluerenderer.h"
24
#include "qgsvectordataprovider.h"
25
#include "qgsvectorlayer.h"
28
QgsUniqueValueDialog::QgsUniqueValueDialog(QgsVectorLayer* vl): QDialog(), mVectorLayer(vl), sydialog(vl)
31
setSizeGripEnabled(true);
33
//find out the fields of mVectorLayer
34
QgsVectorDataProvider *provider;
35
if (provider = dynamic_cast<QgsVectorDataProvider *>(mVectorLayer->getDataProvider()))
37
std::vector < QgsField > const & fields = provider->fields();
40
for (std::vector < QgsField >::const_iterator it = fields.begin();
45
mClassificationComboBox->insertItem(str);
50
qWarning("Warning, data provider is null in QgsUniqueValueDialog::QgsUniqueValueDialog");
54
const QgsUniqueValueRenderer* renderer = dynamic_cast < const QgsUniqueValueRenderer * >(mVectorLayer->renderer());
58
mClassBreakBox->clear();
60
// XXX - mloskot - fix for Ticket #31 (bug)
61
std::list<int> attributes = renderer->classificationAttributes();
62
std::list<int>::iterator iter = attributes.begin();
63
int classattr = *iter;
64
mClassificationComboBox->setCurrentItem(classattr);
66
const std::list<QgsSymbol*> list = renderer->symbols();
67
//fill the items of the renderer into mValues
68
for(std::list<QgsSymbol*>::const_iterator iter=list.begin();iter!=list.end();++iter)
70
QgsSymbol* symbol=(*iter);
71
QString symbolvalue=symbol->lowerValue();
72
QgsSymbol* sym=new QgsSymbol(mVectorLayer->vectorType(), symbol->lowerValue(), symbol->upperValue(), symbol->label());
73
sym->setPen(symbol->pen());
74
sym->setBrush(symbol->brush());
75
sym->setNamedPointSymbol(symbol->pointSymbolName());
76
sym->setPointSize(symbol->pointSize());
77
mValues.insert(std::make_pair(symbolvalue,sym));
78
mClassBreakBox->insertItem(symbolvalue);
83
changeClassificationAttribute(0);
86
QObject::connect(mClassificationComboBox, SIGNAL(activated(int)), this, SLOT(changeClassificationAttribute(int)));
87
QObject::connect(mClassBreakBox, SIGNAL(selectionChanged()), this, SLOT(changeCurrentValue()));
88
QObject::connect(&sydialog, SIGNAL(settingsChanged()), this, SLOT(applySymbologyChanges()));
89
mSymbolWidgetStack->addWidget(&sydialog);
90
mSymbolWidgetStack->raiseWidget(&sydialog);
92
mClassBreakBox->setCurrentItem(0);
95
QgsUniqueValueDialog::~QgsUniqueValueDialog()
97
std::map<QString, QgsSymbol *>::iterator myValueIterator = mValues.begin();
98
while ( myValueIterator != mValues.end() )
100
delete myValueIterator->second;
102
mValues.erase( myValueIterator );
104
myValueIterator = mValues.begin(); // since iterator invalidated due to
105
// erase(), reset to new first element
107
mClassBreakBox->setCurrentItem(0);
110
void QgsUniqueValueDialog::apply()
112
QgsUniqueValueRenderer *renderer = new QgsUniqueValueRenderer(mVectorLayer->vectorType());
114
//go through mValues and add the entries to the renderer
115
for(std::map<QString,QgsSymbol*>::iterator it=mValues.begin();it!=mValues.end();++it)
117
QgsSymbol* symbol=it->second;
118
QgsSymbol* newsymbol=new QgsSymbol(mVectorLayer->vectorType(), symbol->lowerValue(), symbol->upperValue(), symbol->label());
119
newsymbol->setPen(symbol->pen());
120
newsymbol->setBrush(symbol->brush());
121
newsymbol->setNamedPointSymbol(symbol->pointSymbolName());
122
newsymbol->setPointSize(symbol->pointSize());
123
renderer->insertValue(it->first,newsymbol);
126
renderer->setClassificationField(mClassificationComboBox->currentItem());
127
mVectorLayer->setRenderer(renderer);
128
mVectorLayer->refreshLegend();
131
void QgsUniqueValueDialog::changeClassificationAttribute(int nr)
134
for(std::map<QString,QgsSymbol*>::iterator it=mValues.begin();it!=mValues.end();++it)
140
QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider *>(mVectorLayer->getDataProvider());
144
std::list<int> attlist;
145
attlist.push_back(nr);
146
std::vector < QgsFeatureAttribute > vec;
152
//go through all the features and insert their value into the map and into mClassBreakBox
153
mClassBreakBox->clear();
154
while((f=provider->getNextFeature(attlist)))
156
vec = f->attributeMap();
157
value=vec[0].fieldValue();
159
if(mValues.find(value)==mValues.end())
161
symbol=new QgsSymbol(mVectorLayer->vectorType(), value);
162
mValues.insert(std::make_pair(value,symbol));
167
//set symbology for all QgsSiSyDialogs
171
for(std::map<QString,QgsSymbol*>::iterator it=mValues.begin();it!=mValues.end();++it)
173
//insert a random color
174
int red = 1 + (int) (255.0 * rand() / (RAND_MAX + 1.0));
175
int green = 1 + (int) (255.0 * rand() / (RAND_MAX + 1.0));
176
int blue = 1 + (int) (255.0 * rand() / (RAND_MAX + 1.0));
177
thecolor.setRgb(red, green, blue);
178
mClassBreakBox->insertItem(it->first);
179
QgsSymbol* sym=it->second;
182
if(mVectorLayer->vectorType() == QGis::Line)
184
pen.setColor(thecolor);
185
pen.setStyle(Qt::SolidLine);
190
brush.setColor(thecolor);
191
brush.setStyle(Qt::SolidPattern);
192
pen.setColor(Qt::black);
193
pen.setStyle(Qt::SolidLine);
197
sym->setBrush(brush);
200
mClassBreakBox->setCurrentItem(0);
203
void QgsUniqueValueDialog::changeCurrentValue()
205
sydialog.blockSignals(true);//block signal to prevent sydialog from changing the current QgsRenderItem
206
Q3ListBoxItem* item=mClassBreakBox->selectedItem();
207
QString value=item->text();
208
std::map<QString,QgsSymbol*>::iterator it=mValues.find(value);
209
if(it!=mValues.end())
211
sydialog.set( it->second);
212
sydialog.setLabel(it->second->label());
218
sydialog.blockSignals(false);
221
void QgsUniqueValueDialog::applySymbologyChanges()
223
Q3ListBoxItem* item=mClassBreakBox->selectedItem();
224
QString value=item->text();
225
std::map<QString,QgsSymbol*>::iterator it=mValues.find(value);
226
if(it!=mValues.end())
228
it->second->setLabel(sydialog.label());
229
it->second->setLowerValue(value);
230
sydialog.apply( it->second );