Skip to content

Commit 588c21f

Browse files
author
jef
committed
select value for classification in attribute dialog from unique value renderer classes
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8776 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ba4948a commit 588c21f

File tree

3 files changed

+103
-28
lines changed

3 files changed

+103
-28
lines changed

src/app/qgsattributedialog.cpp

Lines changed: 98 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,33 @@
1919
#include "qgsfield.h"
2020
#include "qgslogger.h"
2121

22+
#include "qgsvectorlayer.h"
23+
#include "qgsvectordataprovider.h"
24+
#include "qgsuniquevaluerenderer.h"
25+
#include "qgssymbol.h"
26+
2227
#include <QTableWidgetItem>
2328
#include <QSettings>
2429
#include <QLineEdit>
25-
#include <QSpinBox>
30+
#include <QComboBox>
2631
#include <QLabel>
27-
#include <QDoubleSpinBox>
2832
#include <QFrame>
2933
#include <QScrollArea>
3034

31-
QgsAttributeDialog::QgsAttributeDialog(
32-
QgsFieldMap theFieldMap, QgsFeature * thepFeature)
35+
QgsAttributeDialog::QgsAttributeDialog(QgsVectorLayer *vl, QgsFeature * thepFeature)
3336
: QDialog(),
3437
mSettingsPath("/Windows/AttributeDialog/"),
35-
mFieldMap(theFieldMap),
36-
mpFeature(thepFeature)
38+
mpFeature(thepFeature),
39+
mLayer(vl)
3740
{
38-
3941
setupUi(this);
40-
if (mpFeature==NULL) return;
41-
if (mFieldMap.isEmpty()) return;
42+
if (mpFeature==NULL || vl->getDataProvider()==NULL )
43+
return;
44+
45+
const QgsFieldMap &theFieldMap = vl->getDataProvider()->fields();
46+
47+
if (theFieldMap.isEmpty()) return;
48+
4249
QgsAttributeMap myAttributes = mpFeature->attributeMap();
4350
//
4451
//Set up dynamic inside a scroll box
@@ -58,35 +65,89 @@ QgsAttributeDialog::QgsAttributeDialog(
5865
mypScrollArea->setWidgetResizable( true );
5966
QGridLayout * mypInnerLayout = new QGridLayout(mypInnerFrame);
6067

68+
69+
int classificationField = -1;
70+
QStringList values;
71+
72+
const QgsUniqueValueRenderer *uvr = dynamic_cast<const QgsUniqueValueRenderer *>( mLayer->renderer() );
73+
if( uvr )
74+
{
75+
classificationField = uvr->classificationField();
76+
77+
const QList<QgsSymbol *> symbols = uvr->symbols();
78+
79+
for(int i=0; i<symbols.size(); i++)
80+
{
81+
values.append( symbols[i]->lowerValue() );
82+
}
83+
}
84+
6185
int index=0;
6286
for (QgsAttributeMap::const_iterator it = myAttributes.begin();
6387
it != myAttributes.end();
6488
++it)
6589
{
66-
QString myFieldName = mFieldMap[it.key()].name();
90+
QString myFieldName = theFieldMap[it.key()].name();
91+
int myFieldType = theFieldMap[it.key()].type();
6792
QLabel * mypLabel = new QLabel();
6893
mypInnerLayout->addWidget(mypLabel,index,0);
6994
QVariant myFieldValue = it.value();
70-
QLineEdit * mypLineEdit = new QLineEdit();
71-
//the provider may have provided a default value so use it
72-
mypLineEdit->setText(myFieldValue.toString());
73-
if( mFieldMap[it.key()].type()==QVariant::Int )
95+
96+
QWidget *myWidget;
97+
if(classificationField!=it.key())
98+
{
99+
QLineEdit *le = new QLineEdit();
100+
101+
//the provider may have provided a default value so use it
102+
le->setText(myFieldValue.toString());
103+
104+
if( myFieldType==QVariant::Int )
105+
{
106+
le->setValidator( new QIntValidator(le) );
107+
}
108+
else if( myFieldType==QVariant::Double )
109+
{
110+
le->setValidator( new QIntValidator(le) );
111+
}
112+
113+
myWidget = le;
114+
}
115+
else
116+
{
117+
QComboBox *cb = new QComboBox();
118+
cb->addItems(values);
119+
cb->setEditable(true);
120+
121+
//the provider may have provided a default value so use it
122+
cb->setEditText(myFieldValue.toString());
123+
124+
if( myFieldType==QVariant::Int ) {
125+
cb->setValidator( new QIntValidator(cb) );
126+
}
127+
else if( myFieldType==QVariant::Double )
128+
{
129+
cb->setValidator( new QIntValidator(cb) );
130+
}
131+
132+
myWidget = cb;
133+
}
134+
135+
if( myFieldType==QVariant::Int )
74136
{
75-
mypLineEdit->setValidator( new QIntValidator(mypLineEdit) );
76137
mypLabel->setText(myFieldName + tr(" (int)"));
77138
}
78-
else if( mFieldMap[it.key()].type()==QVariant::Double )
139+
else if( myFieldType==QVariant::Double )
79140
{
80-
mypLineEdit->setValidator( new QDoubleValidator(mypLineEdit) );
81141
mypLabel->setText(myFieldName + tr(" (dbl)"));
82142
}
83143
else //string
84144
{
85145
//any special behaviour for string goes here
86146
mypLabel->setText(myFieldName + tr(" (txt)"));
87147
}
88-
mypInnerLayout->addWidget(mypLineEdit,index,1);
89-
mpWidgets << mypLineEdit;
148+
149+
mypInnerLayout->addWidget(myWidget, index, 1);
150+
mpWidgets << myWidget;
90151
++index;
91152
}
92153
restoreGeometry();
@@ -107,12 +168,26 @@ void QgsAttributeDialog::accept()
107168
it != myAttributes.end();
108169
++it)
109170
{
171+
const QgsFieldMap &theFieldMap = mLayer->getDataProvider()->fields();
172+
110173
//Q_ASSERT(myIndex <= mpWidgets.size());
111-
QString myFieldName = mFieldMap[it.key()].name();
174+
QString myFieldName = theFieldMap[it.key()].name();
112175
bool myFlag=false;
113-
QString myFieldValue =
114-
dynamic_cast<QLineEdit *>(mpWidgets.value(myIndex))->text();
115-
switch( mFieldMap[it.key()].type() )
176+
QString myFieldValue;
177+
178+
QLineEdit *le = dynamic_cast<QLineEdit *>(mpWidgets.value(myIndex));
179+
if(le)
180+
{
181+
myFieldValue = le->text();
182+
}
183+
184+
QComboBox *cb = dynamic_cast<QComboBox *>(mpWidgets.value(myIndex));
185+
if(cb)
186+
{
187+
myFieldValue = cb->currentText();
188+
}
189+
190+
switch( theFieldMap[it.key()].type() )
116191
{
117192
case QVariant::Int:
118193
{

src/app/qgsattributedialog.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ class QDialog;
2727
class QgsFeature;
2828
class QLayout;
2929
class QgsField;
30-
typedef QMap<int, QgsField> QgsFieldMap;
30+
class QgsVectorLayer;
3131

3232
class QgsAttributeDialog: public QDialog, private Ui::QgsAttributeDialogBase
3333
{
3434
Q_OBJECT;
3535

3636
public:
37-
QgsAttributeDialog(const QgsFieldMap thepFieldMap, QgsFeature * thepFeature);
37+
QgsAttributeDialog(QgsVectorLayer *vl, QgsFeature * thepFeature);
3838
~QgsAttributeDialog();
3939

4040
/** Overloaded accept method which will write the feature field
@@ -53,7 +53,7 @@ class QgsAttributeDialog: public QDialog, private Ui::QgsAttributeDialogBase
5353
private:
5454
QString mSettingsPath;
5555
QList<QWidget *> mpWidgets;
56-
QgsFieldMap mFieldMap;
56+
QgsVectorLayer *mLayer;
5757
QgsFeature * mpFeature;
5858
};
5959

src/app/qgsmaptooladdfeature.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent(QMouseEvent * e)
175175
}
176176

177177
// show the dialog to enter attribute values
178-
QgsAttributeDialog * mypDialog = new QgsAttributeDialog(fields,f );
178+
QgsAttributeDialog * mypDialog = new QgsAttributeDialog( vlayer, f );
179179
if (mypDialog->exec())
180180
{
181181
qDebug("Adding feature to layer");
@@ -436,7 +436,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent(QMouseEvent * e)
436436
f->addAttribute(it.key(), provider->getDefaultValue(it.key()));
437437
}
438438

439-
QgsAttributeDialog * mypDialog = new QgsAttributeDialog(fields,f );
439+
QgsAttributeDialog * mypDialog = new QgsAttributeDialog( vlayer, f );
440440
if (mypDialog->exec())
441441
{
442442
if(vlayer->addFeature(*f))

0 commit comments

Comments
 (0)