28
28
#include " qgsattributedialog.h"
29
29
#include " qgsmapcanvas.h"
30
30
#include " qgsattributeaction.h"
31
+ #include " qgsfeatureaction.h"
31
32
32
33
#include < QCloseEvent>
33
34
#include < QLabel>
44
45
45
46
#include " qgslogger.h"
46
47
47
- QgsFeatureAction::QgsFeatureAction ( const QString &name, QgsIdentifyResults *results, QgsVectorLayer *vl, int action, QTreeWidgetItem *featItem )
48
- : QAction( name, results )
49
- , mLayer( vl )
50
- , mAction( action )
51
- {
52
- QList< QPair<QString, QString> > attributes;
53
- results->retrieveAttributes ( featItem, mAttributes , mIdx );
54
- }
55
-
56
- void QgsFeatureAction::execute ()
57
- {
58
- mLayer ->actions ()->doAction ( mAction , mAttributes , mIdx );
59
- }
60
-
61
48
class QgsIdentifyResultsDock : public QDockWidget
62
49
{
63
50
public:
@@ -82,9 +69,9 @@ class QgsIdentifyResultsDock : public QDockWidget
82
69
// actions (if any) [userrole: "actions"]
83
70
// edit [userrole: "edit"]
84
71
// action [userrole: "action", idx]
85
- // name value
86
- // name value
87
- // name value
72
+ // displayname [userroles: fieldIdx, original name] displayvalue [userrole: original value]
73
+ // displayname [userroles: fieldIdx, original name] displayvalue [userrole: original value]
74
+ // displayname [userroles: fieldIdx, original name] displayvalue [userrole: original value]
88
75
// feature
89
76
// derived attributes (if any)
90
77
// name value
@@ -145,61 +132,74 @@ QTreeWidgetItem *QgsIdentifyResults::layerItem( QObject *layer )
145
132
return 0 ;
146
133
}
147
134
148
- void QgsIdentifyResults::addFeature ( QgsMapLayer *layer, int fid,
149
- QString displayField, QString displayValue,
150
- const QMap<QString, QString> &attributes,
135
+ void QgsIdentifyResults::addFeature ( QgsVectorLayer *vlayer, int fid,
136
+ const QgsAttributeMap &attributes,
151
137
const QMap<QString, QString> &derivedAttributes )
152
138
{
153
- QTreeWidgetItem *layItem = layerItem ( layer );
154
- QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
155
- QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
139
+ QTreeWidgetItem *layItem = layerItem ( vlayer );
156
140
157
141
if ( layItem == 0 )
158
142
{
159
- layItem = new QTreeWidgetItem ( QStringList () << QString::number ( lstResults->topLevelItemCount () ) << layer ->name () );
160
- layItem->setData ( 0 , Qt::UserRole, QVariant::fromValue ( qobject_cast<QObject *>( layer ) ) );
143
+ layItem = new QTreeWidgetItem ( QStringList () << QString::number ( lstResults->topLevelItemCount () ) << vlayer ->name () );
144
+ layItem->setData ( 0 , Qt::UserRole, QVariant::fromValue ( qobject_cast<QObject *>( vlayer ) ) );
161
145
lstResults->addTopLevelItem ( layItem );
162
146
163
- if ( vlayer )
164
- {
165
- connect ( vlayer, SIGNAL ( layerDeleted () ), this , SLOT ( layerDestroyed () ) );
166
- connect ( vlayer, SIGNAL ( layerCrsChanged () ), this , SLOT ( layerDestroyed () ) );
167
- connect ( vlayer, SIGNAL ( featureDeleted ( int ) ), this , SLOT ( featureDeleted ( int ) ) );
168
- connect ( vlayer, SIGNAL ( attributeValueChanged ( int , int , const QVariant & ) ), this , SLOT ( attributeValueChanged ( int , int , const QVariant & ) ) );
169
- connect ( vlayer, SIGNAL ( editingStarted () ), this , SLOT ( editingToggled () ) );
170
- connect ( vlayer, SIGNAL ( editingStopped () ), this , SLOT ( editingToggled () ) );
171
- }
172
- else
173
- {
174
- connect ( layer, SIGNAL ( destroyed () ), this , SLOT ( layerDestroyed () ) );
175
- connect ( layer, SIGNAL ( layerCrsChanged () ), this , SLOT ( layerDestroyed () ) );
176
- }
147
+ connect ( vlayer, SIGNAL ( layerDeleted () ), this , SLOT ( layerDestroyed () ) );
148
+ connect ( vlayer, SIGNAL ( layerCrsChanged () ), this , SLOT ( layerDestroyed () ) );
149
+ connect ( vlayer, SIGNAL ( featureDeleted ( int ) ), this , SLOT ( featureDeleted ( int ) ) );
150
+ connect ( vlayer, SIGNAL ( attributeValueChanged ( int , int , const QVariant & ) ), this , SLOT ( attributeValueChanged ( int , int , const QVariant & ) ) );
151
+ connect ( vlayer, SIGNAL ( editingStarted () ), this , SLOT ( editingToggled () ) );
152
+ connect ( vlayer, SIGNAL ( editingStopped () ), this , SLOT ( editingToggled () ) );
177
153
}
178
154
179
- QTreeWidgetItem *featItem = new QTreeWidgetItem ( QStringList () << displayField << displayValue ) ;
155
+ QTreeWidgetItem *featItem = new QTreeWidgetItem;
180
156
featItem->setData ( 0 , Qt::UserRole, fid );
181
157
layItem->addChild ( featItem );
182
158
183
- if ( !rlayer || rlayer-> providerKey () != " wms " )
159
+ for ( QgsAttributeMap::const_iterator it = attributes. begin (); it != attributes. end (); it++ )
184
160
{
185
- for ( QMap<QString, QString>::const_iterator it = attributes.begin (); it != attributes.end (); it++ )
161
+ QTreeWidgetItem *attrItem = new QTreeWidgetItem ( QStringList () << QString::number ( it.key () ) << it.value ().toString () );
162
+
163
+ const QgsFieldMap &fields = vlayer->pendingFields ();
164
+
165
+ QgsFieldMap::const_iterator fit = fields.find ( it.key () );
166
+ if ( fit == fields.constEnd () )
186
167
{
187
- QTreeWidgetItem *attrItem = new QTreeWidgetItem ( QStringList () << it.key () << it.value () );
188
- if ( vlayer )
189
- {
190
- attrItem->setData ( 0 , Qt::UserRole, vlayer->fieldNameIndex ( it.key () ) );
191
- }
192
- featItem->addChild ( attrItem );
168
+ delete attrItem;
169
+ continue ;
193
170
}
194
- }
195
- else
196
- {
197
- QTreeWidgetItem *attrItem = new QTreeWidgetItem ( QStringList () << attributes.begin ().key () << " " );
198
- featItem->addChild ( attrItem );
199
171
200
- QTextBrowser *tb = new QTextBrowser ( attrItem->treeWidget () );
201
- tb->setHtml ( attributes.begin ().value () );
202
- attrItem->treeWidget ()->setItemWidget ( attrItem, 1 , tb );
172
+ attrItem->setData ( 0 , Qt::DisplayRole, vlayer->attributeDisplayName ( it.key () ) );
173
+ attrItem->setData ( 0 , Qt::UserRole, fit->name () );
174
+ attrItem->setData ( 0 , Qt::UserRole + 1 , it.key () );
175
+
176
+ QVariant value = it.value ();
177
+ attrItem->setData ( 1 , Qt::UserRole, value );
178
+
179
+ switch ( vlayer->editType ( it.key () ) )
180
+ {
181
+ case QgsVectorLayer::Hidden:
182
+ // skip the item
183
+ delete attrItem;
184
+ continue ;
185
+
186
+ case QgsVectorLayer::ValueMap:
187
+ value = vlayer->valueMap ( it.key () ).key ( it->toString (), QString ( " (%1)" ).arg ( it->toString () ) );
188
+ break ;
189
+
190
+ default :
191
+ break ;
192
+ }
193
+
194
+ attrItem->setData ( 1 , Qt::DisplayRole, value );
195
+
196
+ if ( fit->name () == vlayer->displayField () )
197
+ {
198
+ featItem->setText ( 0 , attrItem->text ( 0 ) );
199
+ featItem->setText ( 1 , attrItem->text ( 1 ) );
200
+ }
201
+
202
+ featItem->addChild ( attrItem );
203
203
}
204
204
205
205
if ( derivedAttributes.size () >= 0 )
@@ -214,33 +214,81 @@ void QgsIdentifyResults::addFeature( QgsMapLayer *layer, int fid,
214
214
}
215
215
}
216
216
217
- if ( vlayer )
217
+ QTreeWidgetItem *actionItem = new QTreeWidgetItem ( QStringList () << tr ( " (Actions)" ) );
218
+ actionItem->setData ( 0 , Qt::UserRole, " actions" );
219
+ featItem->addChild ( actionItem );
220
+
221
+ QTreeWidgetItem *editItem = new QTreeWidgetItem ( QStringList () << " " << ( vlayer->isEditable () ? tr ( " Edit feature form" ) : tr ( " View feature form" ) ) );
222
+ editItem->setIcon ( 0 , QgisApp::getThemeIcon ( vlayer->isEditable () ? " /mIconEditable.png" : " /mIconEditable.png" ) );
223
+ editItem->setData ( 0 , Qt::UserRole, " edit" );
224
+ actionItem->addChild ( editItem );
225
+
226
+ for ( int i = 0 ; i < vlayer->actions ()->size (); i++ )
218
227
{
219
- QTreeWidgetItem *actionItem = new QTreeWidgetItem ( QStringList () << tr ( " (Actions)" ) );
220
- actionItem->setData ( 0 , Qt::UserRole, " actions" );
221
- featItem->addChild ( actionItem );
228
+ const QgsAction &action = vlayer->actions ()->at ( i );
222
229
223
- QTreeWidgetItem *editItem = new QTreeWidgetItem ( QStringList () << " " << ( vlayer->isEditable () ? tr ( " Edit feature form" ) : tr ( " View feature form" ) ) );
224
- editItem->setIcon ( 0 , QgisApp::getThemeIcon ( vlayer->isEditable () ? " /mIconEditable.png" : " /mIconEditable.png" ) );
225
- editItem->setData ( 0 , Qt::UserRole, " edit" );
226
- actionItem->addChild ( editItem );
230
+ if ( !action.runable () )
231
+ continue ;
227
232
228
- for ( int i = 0 ; i < vlayer->actions ()->size (); i++ )
229
- {
230
- const QgsAction &action = vlayer->actions ()->at ( i );
233
+ QTreeWidgetItem *twi = new QTreeWidgetItem ( QStringList () << " " << action.name () );
234
+ twi->setIcon ( 0 , QgisApp::getThemeIcon ( " /mAction.png" ) );
235
+ twi->setData ( 0 , Qt::UserRole, " action" );
236
+ twi->setData ( 0 , Qt::UserRole + 1 , QVariant::fromValue ( i ) );
237
+ actionItem->addChild ( twi );
238
+ }
231
239
232
- if ( !action.runable () )
233
- continue ;
240
+ highlightFeature ( featItem );
241
+ }
242
+
243
+ void QgsIdentifyResults::addFeature ( QgsRasterLayer *layer,
244
+ QString label,
245
+ const QMap<QString, QString> &attributes,
246
+ const QMap<QString, QString> &derivedAttributes )
247
+ {
248
+ QTreeWidgetItem *layItem = layerItem ( layer );
249
+
250
+ if ( layItem == 0 )
251
+ {
252
+ layItem = new QTreeWidgetItem ( QStringList () << QString::number ( lstResults->topLevelItemCount () ) << layer->name () );
253
+ layItem->setData ( 0 , Qt::UserRole, QVariant::fromValue ( qobject_cast<QObject *>( layer ) ) );
254
+ lstResults->addTopLevelItem ( layItem );
255
+
256
+ connect ( layer, SIGNAL ( destroyed () ), this , SLOT ( layerDestroyed () ) );
257
+ connect ( layer, SIGNAL ( layerCrsChanged () ), this , SLOT ( layerDestroyed () ) );
258
+ }
259
+
260
+ QTreeWidgetItem *featItem = new QTreeWidgetItem ( QStringList () << label << " " );
261
+ featItem->setData ( 0 , Qt::UserRole, -1 );
262
+ layItem->addChild ( featItem );
263
+
264
+ if ( layer && layer->providerKey () == " wms" )
265
+ {
266
+ QTreeWidgetItem *attrItem = new QTreeWidgetItem ( QStringList () << attributes.begin ().key () << " " );
267
+ featItem->addChild ( attrItem );
234
268
235
- QTreeWidgetItem *twi = new QTreeWidgetItem ( QStringList () << " " << action.name () );
236
- twi->setIcon ( 0 , QgisApp::getThemeIcon ( " /mAction.png" ) );
237
- twi->setData ( 0 , Qt::UserRole, " action" );
238
- twi->setData ( 0 , Qt::UserRole + 1 , QVariant::fromValue ( i ) );
239
- actionItem->addChild ( twi );
269
+ QTextBrowser *tb = new QTextBrowser ( attrItem->treeWidget () );
270
+ tb->setHtml ( attributes.begin ().value () );
271
+ attrItem->treeWidget ()->setItemWidget ( attrItem, 1 , tb );
272
+ }
273
+ else
274
+ {
275
+ for ( QMap<QString, QString>::const_iterator it = attributes.begin (); it != attributes.end (); it++ )
276
+ {
277
+ featItem->addChild ( new QTreeWidgetItem ( QStringList () << it.key () << it.value () ) );
240
278
}
241
279
}
242
280
243
- highlightFeature ( featItem );
281
+ if ( derivedAttributes.size () >= 0 )
282
+ {
283
+ QTreeWidgetItem *derivedItem = new QTreeWidgetItem ( QStringList () << tr ( " (Derived)" ) );
284
+ derivedItem->setData ( 0 , Qt::UserRole, " derived" );
285
+ featItem->addChild ( derivedItem );
286
+
287
+ for ( QMap< QString, QString>::const_iterator it = derivedAttributes.begin (); it != derivedAttributes.end (); it++ )
288
+ {
289
+ derivedItem->addChild ( new QTreeWidgetItem ( QStringList () << it.key () << it.value () ) );
290
+ }
291
+ }
244
292
}
245
293
246
294
void QgsIdentifyResults::editingToggled ()
@@ -484,7 +532,7 @@ void QgsIdentifyResults::deactivate()
484
532
void QgsIdentifyResults::doAction ( QTreeWidgetItem *item, int action )
485
533
{
486
534
int idx;
487
- QList< QPair<QString, QString> > attributes;
535
+ QgsAttributeMap attributes;
488
536
QTreeWidgetItem *featItem = retrieveAttributes ( item, attributes, idx );
489
537
if ( !featItem )
490
538
return ;
@@ -570,7 +618,7 @@ QgsVectorLayer *QgsIdentifyResults::vectorLayer( QTreeWidgetItem *item )
570
618
}
571
619
572
620
573
- QTreeWidgetItem *QgsIdentifyResults::retrieveAttributes ( QTreeWidgetItem *item, QList< QPair<QString, QString> > &attributes, int &idx )
621
+ QTreeWidgetItem *QgsIdentifyResults::retrieveAttributes ( QTreeWidgetItem *item, QgsAttributeMap &attributes, int &idx )
574
622
{
575
623
QTreeWidgetItem *featItem = featureItem ( item );
576
624
if ( !featItem )
@@ -585,8 +633,8 @@ QTreeWidgetItem *QgsIdentifyResults::retrieveAttributes( QTreeWidgetItem *item,
585
633
if ( item->childCount () > 0 )
586
634
continue ;
587
635
if ( item == lstResults->currentItem () )
588
- idx = attributes. size ();
589
- attributes << QPair<QString, QString> ( item->data ( 0 , Qt::DisplayRole ). toString (), item->data ( 1 , Qt::DisplayRole ). toString ( ) );
636
+ idx = item-> data ( 0 , Qt::UserRole + 1 ). toInt ();
637
+ attributes. insert ( item->data ( 0 , Qt::UserRole + 1 ). toInt (), item->data ( 1 , Qt::DisplayRole ) );
590
638
}
591
639
592
640
return featItem;
@@ -939,13 +987,23 @@ void QgsIdentifyResults::copyFeatureAttributes()
939
987
QClipboard *clipboard = QApplication::clipboard ();
940
988
QString text;
941
989
990
+ QgsVectorLayer *vlayer = vectorLayer ( lstResults->currentItem () );
991
+ if ( !vlayer )
992
+ return ;
993
+
942
994
int idx;
943
- QList< QPair<QString, QString> > attributes;
995
+ QgsAttributeMap attributes;
944
996
retrieveAttributes ( lstResults->currentItem (), attributes, idx );
945
997
946
- for ( QList< QPair<QString, QString> >::iterator it = attributes.begin (); it != attributes.end (); it++ )
998
+ const QgsFieldMap &fields = vlayer->pendingFields ();
999
+
1000
+ for ( QgsAttributeMap::const_iterator it = attributes.begin (); it != attributes.end (); it++ )
947
1001
{
948
- text += QString ( " %1: %2\n " ).arg ( it->first ).arg ( it->second );
1002
+ QgsFieldMap::const_iterator fit = fields.find ( it.key () );
1003
+ if ( fit == fields.constEnd () )
1004
+ continue ;
1005
+
1006
+ text += QString ( " %1: %2\n " ).arg ( fit->name () ).arg ( it.value ().toString () );
949
1007
}
950
1008
951
1009
QgsDebugMsg ( QString ( " set clipboard: %1" ).arg ( text ) );
0 commit comments