17
17
#include < QLineEdit>
18
18
#include < QComboBox>
19
19
#include < QPainter>
20
- #include < QCompleter>
21
- #include < QSpinBox>
22
- #include < QDoubleSpinBox>
23
20
24
21
#include " qgsattributetableview.h"
25
22
#include " qgsattributetablemodel.h"
26
23
#include " qgsattributetablefiltermodel.h"
27
24
#include " qgsattributetabledelegate.h"
28
25
#include " qgsvectordataprovider.h"
29
- #include " qgsvectorlayer.h"
30
- #include " qgsuniquevaluerenderer.h"
31
- #include " qgssymbol.h"
26
+ #include " qgsattributeeditor.h"
27
+ #include " qgslogger.h"
32
28
29
+ QgsVectorLayer *QgsAttributeTableDelegate::layer ( const QAbstractItemModel *model ) const
30
+ {
31
+ const QgsAttributeTableModel *tm = dynamic_cast <const QgsAttributeTableModel*>( model );
32
+ if ( tm )
33
+ return tm ->layer ();
34
+
35
+ const QgsAttributeTableFilterModel *fm = dynamic_cast <const QgsAttributeTableFilterModel*>( model );
36
+ if ( fm )
37
+ return fm->layer ();
33
38
34
- QWidget * QgsAttributeTableDelegate::createEditor (
39
+ return NULL ;
40
+ }
41
+
42
+ QWidget *QgsAttributeTableDelegate::createEditor (
35
43
QWidget *parent,
36
44
const QStyleOptionViewItem &option,
37
45
const QModelIndex &index ) const
38
46
{
39
- QWidget *editor;
40
-
41
- const QgsAttributeTableFilterModel* fm = dynamic_cast <const QgsAttributeTableFilterModel*>( index .model () );
42
- if ( !fm )
43
- {
44
- return editor;
45
- }
46
- const QgsAttributeTableModel* m = dynamic_cast <const QgsAttributeTableModel*>( fm->sourceModel () );
47
- if ( !m )
48
- {
49
- return editor;
50
- }
51
-
52
- int col = index .column ();
53
- QVariant::Type type = m->layer ()->dataProvider ()->fields ()[col].type ();
54
- QgsVectorLayer::EditType editType = m->layer ()->editType (col);
55
-
56
- // need to created correct edit widget according to edit type of value
57
- // and fill with data from correct source
58
- if (editType == QgsVectorLayer::LineEdit ||
59
- editType == QgsVectorLayer::UniqueValuesEditable ||
60
- editType == QgsVectorLayer::FileName ||
61
- editType == QgsVectorLayer::Immutable)
62
- { // these are all siple edits
63
- editor = new QLineEdit (parent);
64
- QLineEdit* le = dynamic_cast <QLineEdit*> ( editor );
65
- le-> setReadOnly ( false );
66
-
67
- if ( editType == QgsVectorLayer::UniqueValuesEditable )
68
- { // just this value has completer
69
- QList<QVariant> values;
70
- m->layer ()->dataProvider ()->uniqueValues ( col, values );
71
-
72
- QStringList svalues;
73
- for ( QList<QVariant>::const_iterator it = values.begin (); it != values.end (); it++ )
74
- svalues << it->toString ();
75
-
76
- QCompleter *c = new QCompleter ( svalues );
77
- c->setCompletionMode ( QCompleter::PopupCompletion );
78
- le->setCompleter ( c );
79
- }
80
- if (editType == QgsVectorLayer::Immutable)
81
- {
82
- le->setReadOnly (true );
83
- }
84
- // validators if value needs it
85
- if ( type == QVariant::Int )
86
- {
87
- le->setValidator ( new QIntValidator ( le ) );
88
- }
89
- else if ( type == QVariant::Double )
90
- {
91
- le->setValidator ( new QDoubleValidator ( le ) );
92
- }
93
- }
94
- else if (editType == QgsVectorLayer::ValueMap)
95
- { // simple combobox from data from vector layer
96
- editor = new QComboBox (parent);
97
- QComboBox* cb = dynamic_cast <QComboBox*>( editor );
98
- QMap<QString, QVariant> &map = m->layer ()->valueMap (col);
99
- QMap<QString, QVariant>::iterator it = map.begin ();
100
- for ( ; it != map.end (); it ++)
101
- {
102
- cb->addItem ( it.key () ,it.value ());
103
- }
104
- }
105
- else if (editType == QgsVectorLayer::SliderRange)
106
- { // horizontal slider
107
- editor = new QSlider (Qt::Horizontal, parent);
108
- QSlider* s = dynamic_cast <QSlider*>(editor );
109
- QgsVectorLayer::RangeData &range = m->layer ()->range (col);
110
- s->setMinimum ( range.mMin .toInt () );
111
- s->setMaximum ( range.mMax .toInt () );
112
- s->setPageStep ( range.mStep .toInt () );
113
- }
114
- else if (editType == QgsVectorLayer::Classification)
115
- {
116
- // should be prepared probably to not change it always
117
- int classificationField = -1 ;
118
- QMap<QString, QString> classes;
119
- const QgsUniqueValueRenderer *uvr = dynamic_cast <const QgsUniqueValueRenderer *>( m->layer ()->renderer () );
120
- if ( uvr )
121
- {
122
- classificationField = uvr->classificationField ();
123
- const QList<QgsSymbol *> symbols = uvr->symbols ();
124
-
125
- for ( int i = 0 ; i < symbols.size (); i++ )
126
- {
127
- QString label = symbols[i]->label ();
128
- QString name = symbols[i]->lowerValue ();
129
- if ( label == " " )
130
- label = name;
131
- classes.insert ( name, label );
132
- }
133
- }
134
- editor = new QComboBox (parent);
135
- QComboBox* cb = dynamic_cast <QComboBox*>( editor );
136
- for ( QMap<QString, QString>::const_iterator it = classes.begin (); it != classes.end (); it++ )
137
- {
138
- cb->addItem ( it.value (), it.key () );
139
- }
140
- }
141
- else if (editType == QgsVectorLayer::UniqueValues)
142
- {
143
- QList<QVariant> values;
144
- m->layer ()->dataProvider ()->uniqueValues ( col, values );
145
-
146
- editor = new QComboBox (parent);
147
- QComboBox* cb = dynamic_cast <QComboBox*>( editor );
148
- cb->setEditable ( true );
149
-
150
- for ( QList<QVariant>::iterator it = values.begin (); it != values.end (); it++ )
151
- cb->addItem ( it->toString () );
152
-
153
- }
154
- else if (editType == QgsVectorLayer::Enumeration)
155
- {
156
- QStringList enumValues;
157
- m->layer ()->dataProvider ()->enumValues ( col, enumValues );
47
+ QgsVectorLayer *vl = layer ( index .model () );
48
+ if ( vl == NULL )
49
+ return NULL ;
158
50
159
- editor = new QComboBox (parent);
160
- QComboBox* cb = dynamic_cast <QComboBox*>( editor );
161
- QStringList::const_iterator s_it = enumValues.constBegin ();
162
- for ( ; s_it != enumValues.constEnd (); ++s_it )
163
- {
164
- cb->addItem ( *s_it );
165
- }
166
- }
167
- else if (editType == QgsVectorLayer::EditRange)
168
- {
169
- if ( type == QVariant::Int )
170
- {
171
- int min = m->layer ()->range ( col ).mMin .toInt ();
172
- int max = m->layer ()->range ( col ).mMax .toInt ();
173
- int step = m->layer ()->range ( col ).mStep .toInt ();
174
- editor = new QSpinBox (parent);
175
- QSpinBox* sb = dynamic_cast <QSpinBox*>( editor );
51
+ QWidget *widget = QgsAttributeEditor::createAttributeEditor ( parent, vl, index .column (), index .model ()->data ( index ) );
52
+ widget->adjustSize ();
176
53
177
- sb->setRange ( min, max );
178
- sb->setSingleStep ( step );
54
+ QgsAttributeTableView *tv = dynamic_cast <QgsAttributeTableView *>( parent->parentWidget () );
55
+ tv->setRowHeight ( index .row (), widget->height () );
56
+ tv->setColumnWidth ( index .column (), widget->width () );
179
57
180
- }
181
- else if ( type == QVariant::Double )
182
- {
183
- double min = m->layer ()->range ( col ).mMin .toDouble ();
184
- double max = m->layer ()->range ( col ).mMax .toDouble ();
185
- double step = m->layer ()->range ( col ).mStep .toDouble ();
186
- editor = new QDoubleSpinBox (parent);
187
- QDoubleSpinBox* dsb = dynamic_cast <QDoubleSpinBox*>( editor );
58
+ return widget;
59
+ }
188
60
189
- dsb->setRange ( min, max );
190
- dsb->setSingleStep ( step );
191
- }
192
- }
61
+ void QgsAttributeTableDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
62
+ {
63
+ QgsVectorLayer *vl = layer ( index .model () );
64
+ if ( vl == NULL )
65
+ return ;
193
66
67
+ QVariant value;
68
+ if ( !QgsAttributeEditor::retrieveValue ( editor, vl, index .column (), value ) )
69
+ return ;
194
70
195
- return editor ;
71
+ model-> setData ( index , value ) ;
196
72
}
197
73
198
-
199
74
void QgsAttributeTableDelegate::paint ( QPainter * painter,
200
75
const QStyleOptionViewItem & option,
201
76
const QModelIndex & index ) const
@@ -212,130 +87,3 @@ void QgsAttributeTableDelegate::paint( QPainter * painter,
212
87
painter->restore ();
213
88
}
214
89
}
215
-
216
-
217
- void QgsAttributeTableDelegate::setEditorData (QWidget *editor, const QModelIndex &index) const
218
- {
219
-
220
-
221
- const QgsAttributeTableFilterModel* fm = dynamic_cast <const QgsAttributeTableFilterModel*>( index .model () );
222
- if ( !fm )
223
- {
224
- return ;
225
- }
226
- const QgsAttributeTableModel* m = dynamic_cast <const QgsAttributeTableModel*>( fm->sourceModel () );
227
- if ( !m )
228
- {
229
- return ;
230
- }
231
- int col = index .column ();
232
- QVariant::Type type = m->layer ()->dataProvider ()->fields ()[col].type ();
233
- QgsVectorLayer::EditType editType = m->layer ()->editType (col);
234
- if (editType == QgsVectorLayer::LineEdit ||
235
- editType == QgsVectorLayer::UniqueValuesEditable ||
236
- editType == QgsVectorLayer::FileName ||
237
- editType == QgsVectorLayer::Immutable)
238
- {
239
- QString qs = index .model ()->data (index , Qt::DisplayRole).toString ();
240
-
241
- QLineEdit* le = dynamic_cast <QLineEdit*> ( editor );
242
- le->setText ( qs );
243
- }
244
- else if (editType == QgsVectorLayer::ValueMap ||
245
- editType == QgsVectorLayer::Classification ||
246
- editType == QgsVectorLayer::UniqueValues ||
247
- editType == QgsVectorLayer::Enumeration)
248
- {
249
- QComboBox* cb = dynamic_cast <QComboBox*>( editor );
250
- QVariant qs = index .model ()->data (index , Qt::EditRole);
251
- int cbIndex = cb->findData (qs);
252
- if (cbIndex > -1 )
253
- {
254
- cb->setCurrentIndex (cbIndex);
255
- }
256
- }
257
- else if (editType == QgsVectorLayer::SliderRange)
258
- {
259
- int value = index .model ()->data (index , Qt::EditRole).toInt ();
260
- QSlider* s = dynamic_cast <QSlider*>( editor );
261
- s->setValue ( value );
262
- }
263
- else if (editType == QgsVectorLayer::EditRange)
264
- {
265
- if ( type == QVariant::Int )
266
- {
267
- QSpinBox* sb = dynamic_cast <QSpinBox*>( editor );
268
- int value = index .model ()->data (index , Qt::EditRole).toInt ();
269
- sb->setValue ( value );
270
- }
271
- else if ( type == QVariant::Double )
272
- {
273
- QDoubleSpinBox* sb = dynamic_cast <QDoubleSpinBox*>( editor );
274
- double value = index .model ()->data (index , Qt::EditRole).toDouble ();
275
- sb->setValue ( value );
276
- }
277
- }
278
- }
279
-
280
-
281
-
282
- void QgsAttributeTableDelegate::setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
283
- {
284
- const QgsAttributeTableFilterModel* fm = dynamic_cast <const QgsAttributeTableFilterModel*>( index .model () );
285
- if ( !fm )
286
- {
287
- return ;
288
- }
289
- const QgsAttributeTableModel* m = dynamic_cast <const QgsAttributeTableModel*>( fm->sourceModel () );
290
- if ( !m )
291
- {
292
- return ;
293
- }
294
-
295
- int col = index .column ();
296
- QVariant::Type type = m->layer ()->dataProvider ()->fields ()[col].type ();
297
- QgsVectorLayer::EditType editType = m->layer ()->editType (col);
298
- if (editType == QgsVectorLayer::LineEdit ||
299
- editType == QgsVectorLayer::UniqueValuesEditable ||
300
- editType == QgsVectorLayer::FileName ||
301
- editType == QgsVectorLayer::Immutable)
302
- {
303
- QLineEdit* le = dynamic_cast <QLineEdit*> ( editor );
304
- QString text = le->text ();
305
- QVariant value = QVariant (text);
306
- model->setData ( index , value );
307
- }
308
- else if (editType == QgsVectorLayer::ValueMap ||
309
- editType == QgsVectorLayer::Classification ||
310
- editType == QgsVectorLayer::UniqueValues ||
311
- editType == QgsVectorLayer::Enumeration)
312
- {
313
- QComboBox* cb = dynamic_cast <QComboBox*>( editor );
314
- model->setData (index , cb->itemData (cb->currentIndex ()));
315
- }
316
- else if (editType == QgsVectorLayer::SliderRange)
317
- {
318
- QSlider* s = dynamic_cast <QSlider*>( editor );
319
- model->setData ( index , s->value () );
320
- }
321
- else if (editType == QgsVectorLayer::EditRange)
322
- {
323
- if ( type == QVariant::Int )
324
- {
325
- QSpinBox* sb = dynamic_cast <QSpinBox*>( editor );
326
- model->setData ( index , sb->value () );
327
- }
328
- else if ( type == QVariant::Double )
329
- {
330
- QDoubleSpinBox* sb = dynamic_cast <QDoubleSpinBox*>( editor );
331
- model->setData ( index , sb->value () );
332
- }
333
- }
334
- }
335
-
336
-
337
-
338
-
339
-
340
-
341
-
0 commit comments