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

« back to all changes in this revision

Viewing changes to src/gui/qgsuniquevaluedialog.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
 
                         qgsuniquevaluedialog.cpp  -  description
3
 
                             -------------------
4
 
    begin                : July 2004
5
 
    copyright            : (C) 2004 by Marco Hugentobler
6
 
    email                : marco.hugentobler@autoform.ch
7
 
 ***************************************************************************/
8
 
 
9
 
/***************************************************************************
10
 
 *                                                                         *
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.                                   *
15
 
 *                                                                         *
16
 
 ***************************************************************************/
17
 
/* $Id: qgsuniquevaluedialog.cpp 6087 2006-11-15 17:35:28Z mhugent $ */
18
 
 
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"
26
 
 
27
 
 
28
 
QgsUniqueValueDialog::QgsUniqueValueDialog(QgsVectorLayer* vl): QDialog(), mVectorLayer(vl), sydialog(vl)
29
 
{
30
 
    setupUi(this);
31
 
    setSizeGripEnabled(true); 
32
 
 
33
 
    //find out the fields of mVectorLayer
34
 
    QgsVectorDataProvider *provider;
35
 
    if (provider = dynamic_cast<QgsVectorDataProvider *>(mVectorLayer->getDataProvider()))
36
 
    {
37
 
        std::vector < QgsField > const & fields = provider->fields();
38
 
        QString str;
39
 
        
40
 
        for (std::vector < QgsField >::const_iterator it = fields.begin(); 
41
 
             it != fields.end(); 
42
 
             ++it)
43
 
        {
44
 
            str = (*it).name();
45
 
            mClassificationComboBox->insertItem(str);
46
 
        }
47
 
    } 
48
 
    else
49
 
    {
50
 
        qWarning("Warning, data provider is null in QgsUniqueValueDialog::QgsUniqueValueDialog");
51
 
        return;
52
 
    }
53
 
 
54
 
    const QgsUniqueValueRenderer* renderer = dynamic_cast < const QgsUniqueValueRenderer * >(mVectorLayer->renderer());
55
 
    
56
 
    if (renderer)
57
 
    {
58
 
        mClassBreakBox->clear();
59
 
        
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);
65
 
 
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)
69
 
        {
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);
79
 
        }
80
 
    }
81
 
    else
82
 
    {
83
 
        changeClassificationAttribute(0);       
84
 
    }
85
 
 
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);
91
 
    
92
 
    mClassBreakBox->setCurrentItem(0);
93
 
}
94
 
 
95
 
QgsUniqueValueDialog::~QgsUniqueValueDialog()
96
 
{
97
 
    std::map<QString, QgsSymbol *>::iterator myValueIterator = mValues.begin();
98
 
    while ( myValueIterator != mValues.end() )
99
 
    {
100
 
        delete myValueIterator->second;
101
 
        
102
 
        mValues.erase( myValueIterator );
103
 
        
104
 
        myValueIterator = mValues.begin(); // since iterator invalidated due to
105
 
                                        // erase(), reset to new first element
106
 
    }
107
 
    mClassBreakBox->setCurrentItem(0);
108
 
}
109
 
 
110
 
void QgsUniqueValueDialog::apply()
111
 
{
112
 
    QgsUniqueValueRenderer *renderer = new QgsUniqueValueRenderer(mVectorLayer->vectorType());
113
 
 
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)
116
 
    {
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);
124
 
    }
125
 
 
126
 
    renderer->setClassificationField(mClassificationComboBox->currentItem());
127
 
    mVectorLayer->setRenderer(renderer);
128
 
    mVectorLayer->refreshLegend();
129
 
}
130
 
 
131
 
void QgsUniqueValueDialog::changeClassificationAttribute(int nr)
132
 
{
133
 
    //delete old entries
134
 
    for(std::map<QString,QgsSymbol*>::iterator it=mValues.begin();it!=mValues.end();++it)
135
 
    {
136
 
        delete it->second;
137
 
    }
138
 
    mValues.clear();
139
 
    
140
 
    QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider *>(mVectorLayer->getDataProvider());
141
 
    if (provider)
142
 
    {
143
 
        QString value;
144
 
        std::list<int> attlist;
145
 
        attlist.push_back(nr);
146
 
        std::vector < QgsFeatureAttribute > vec;
147
 
        QgsSymbol* symbol;
148
 
 
149
 
        provider->reset();
150
 
        QgsFeature* f;
151
 
 
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)))
155
 
        {
156
 
            vec = f->attributeMap();
157
 
            value=vec[0].fieldValue();
158
 
           
159
 
            if(mValues.find(value)==mValues.end())
160
 
            {
161
 
                symbol=new QgsSymbol(mVectorLayer->vectorType(), value);
162
 
                mValues.insert(std::make_pair(value,symbol));
163
 
            }
164
 
            delete f;
165
 
        }
166
 
 
167
 
        //set symbology for all QgsSiSyDialogs
168
 
        QColor thecolor;
169
 
        double frac;
170
 
 
171
 
        for(std::map<QString,QgsSymbol*>::iterator it=mValues.begin();it!=mValues.end();++it)
172
 
        {
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;
180
 
            QPen pen;
181
 
            QBrush brush;
182
 
            if(mVectorLayer->vectorType() == QGis::Line)
183
 
            {
184
 
                pen.setColor(thecolor);
185
 
                pen.setStyle(Qt::SolidLine);
186
 
                pen.setWidth(1);
187
 
            }
188
 
            else
189
 
            {
190
 
                brush.setColor(thecolor);
191
 
                brush.setStyle(Qt::SolidPattern);
192
 
                pen.setColor(Qt::black);
193
 
                pen.setStyle(Qt::SolidLine);
194
 
                pen.setWidth(1);
195
 
            }
196
 
            sym->setPen(pen);
197
 
            sym->setBrush(brush);
198
 
        }
199
 
    }
200
 
    mClassBreakBox->setCurrentItem(0);
201
 
}
202
 
 
203
 
void QgsUniqueValueDialog::changeCurrentValue()
204
 
{
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())
210
 
    {
211
 
        sydialog.set( it->second);
212
 
        sydialog.setLabel(it->second->label());
213
 
    }
214
 
    else
215
 
    {
216
 
        //no entry found
217
 
    }
218
 
    sydialog.blockSignals(false);
219
 
}
220
 
 
221
 
void QgsUniqueValueDialog::applySymbologyChanges()
222
 
{
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())
227
 
  {
228
 
      it->second->setLabel(sydialog.label());
229
 
      it->second->setLowerValue(value);
230
 
      sydialog.apply( it->second );
231
 
  }
232
 
}