19
19
#include " qgsfield.h"
20
20
#include " qgslogger.h"
21
21
22
+ #include " qgsvectorlayer.h"
23
+ #include " qgsvectordataprovider.h"
24
+ #include " qgsuniquevaluerenderer.h"
25
+ #include " qgssymbol.h"
26
+
22
27
#include < QTableWidgetItem>
23
28
#include < QSettings>
24
29
#include < QLineEdit>
25
- #include < QSpinBox >
30
+ #include < QComboBox >
26
31
#include < QLabel>
27
- #include < QDoubleSpinBox>
28
32
#include < QFrame>
29
33
#include < QScrollArea>
30
34
31
- QgsAttributeDialog::QgsAttributeDialog (
32
- QgsFieldMap theFieldMap, QgsFeature * thepFeature)
35
+ QgsAttributeDialog::QgsAttributeDialog (QgsVectorLayer *vl, QgsFeature * thepFeature)
33
36
: QDialog(),
34
37
mSettingsPath(" /Windows/AttributeDialog/" ),
35
- mFieldMap(theFieldMap ),
36
- mpFeature(thepFeature )
38
+ mpFeature(thepFeature ),
39
+ mLayer(vl )
37
40
{
38
-
39
41
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
+
42
49
QgsAttributeMap myAttributes = mpFeature->attributeMap ();
43
50
//
44
51
// Set up dynamic inside a scroll box
@@ -58,35 +65,89 @@ QgsAttributeDialog::QgsAttributeDialog(
58
65
mypScrollArea->setWidgetResizable ( true );
59
66
QGridLayout * mypInnerLayout = new QGridLayout (mypInnerFrame);
60
67
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
+
61
85
int index=0 ;
62
86
for (QgsAttributeMap::const_iterator it = myAttributes.begin ();
63
87
it != myAttributes.end ();
64
88
++it)
65
89
{
66
- QString myFieldName = mFieldMap [it.key ()].name ();
90
+ QString myFieldName = theFieldMap[it.key ()].name ();
91
+ int myFieldType = theFieldMap[it.key ()].type ();
67
92
QLabel * mypLabel = new QLabel ();
68
93
mypInnerLayout->addWidget (mypLabel,index,0 );
69
94
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 )
74
136
{
75
- mypLineEdit->setValidator ( new QIntValidator (mypLineEdit) );
76
137
mypLabel->setText (myFieldName + tr (" (int)" ));
77
138
}
78
- else if ( mFieldMap [it. key ()]. type () ==QVariant::Double )
139
+ else if ( myFieldType ==QVariant::Double )
79
140
{
80
- mypLineEdit->setValidator ( new QDoubleValidator (mypLineEdit) );
81
141
mypLabel->setText (myFieldName + tr (" (dbl)" ));
82
142
}
83
143
else // string
84
144
{
85
145
// any special behaviour for string goes here
86
146
mypLabel->setText (myFieldName + tr (" (txt)" ));
87
147
}
88
- mypInnerLayout->addWidget (mypLineEdit,index,1 );
89
- mpWidgets << mypLineEdit;
148
+
149
+ mypInnerLayout->addWidget (myWidget, index, 1 );
150
+ mpWidgets << myWidget;
90
151
++index;
91
152
}
92
153
restoreGeometry ();
@@ -107,12 +168,26 @@ void QgsAttributeDialog::accept()
107
168
it != myAttributes.end ();
108
169
++it)
109
170
{
171
+ const QgsFieldMap &theFieldMap = mLayer ->getDataProvider ()->fields ();
172
+
110
173
// Q_ASSERT(myIndex <= mpWidgets.size());
111
- QString myFieldName = mFieldMap [it.key ()].name ();
174
+ QString myFieldName = theFieldMap [it.key ()].name ();
112
175
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 () )
116
191
{
117
192
case QVariant::Int:
118
193
{
0 commit comments