-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsidentifyresultsdialog.cpp
1983 lines (1672 loc) · 64.1 KB
/
qgsidentifyresultsdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsidentifyresults.cpp - description
-------------------
begin : Fri Oct 25 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman at mrcc dot com
Romans 3:23=>Romans 6:23=>Romans 5:8=>Romans 10:9,10=>Romans 12
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsactionmanager.h"
#include "qgsattributedialog.h"
#include "qgsdockwidget.h"
#include "qgseditorwidgetregistry.h"
#include "qgsfeatureaction.h"
#include "qgsfeatureiterator.h"
#include "qgsfeaturestore.h"
#include "qgsgeometry.h"
#include "qgshighlight.h"
#include "qgsidentifyresultsdialog.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayeractionregistry.h"
#include "qgsmaplayer.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsproject.h"
#include "qgsrasterdataprovider.h"
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgswebview.h"
#include "qgswebframe.h"
#include "qgsstringutils.h"
#include "qgstreewidgetitem.h"
#include "qgsfiledownloader.h"
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgssettings.h"
#include <QCloseEvent>
#include <QLabel>
#include <QAction>
#include <QTreeWidgetItem>
#include <QPixmap>
#include <QMenu>
#include <QClipboard>
#include <QMenuBar>
#include <QPushButton>
#include <QPrinter>
#include <QPrintDialog>
#include <QDesktopServices>
#include <QMessageBox>
#include <QComboBox>
#include <QTextDocument>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFileDialog>
#include <QFileInfo>
#include <QRegExp>
//graph
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>
#include "qgscolorramp.h" // for random colors
QgsIdentifyResultsWebView::QgsIdentifyResultsWebView( QWidget *parent ) : QgsWebView( parent )
{
setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
#ifdef WITH_QTWEBKIT
page()->setForwardUnsupportedContent( true );
#endif
page()->setLinkDelegationPolicy( QWebPage::DontDelegateLinks );
settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
settings()->setAttribute( QWebSettings::PluginsEnabled, true );
#ifdef QGISDEBUG
settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
#endif
connect( page(), SIGNAL( downloadRequested( QNetworkRequest ) ), this, SLOT( downloadRequested( QNetworkRequest ) ) );
connect( page(), SIGNAL( unsupportedContent( QNetworkReply * ) ), this, SLOT( unsupportedContent( QNetworkReply * ) ) );
}
void QgsIdentifyResultsWebView::downloadRequested( const QNetworkRequest &request )
{
handleDownload( request.url() );
}
void QgsIdentifyResultsWebView::unsupportedContent( QNetworkReply *reply )
{
handleDownload( reply->url() );
}
void QgsIdentifyResultsWebView::handleDownload( QUrl url )
{
if ( ! url.isValid() )
{
QMessageBox::warning( this, tr( "Invalid URL" ), tr( "The download URL is not valid: %1" ).arg( url.toString( ) ) );
}
else
{
const QString DOWNLOADER_LAST_DIR_KEY( "Qgis/fileDownloaderLastDir" );
QgsSettings settings;
// Try to get some information from the URL
QFileInfo info( url.toString( ) );
QString savePath = settings.value( DOWNLOADER_LAST_DIR_KEY ).toString( );
QString fileName = info.fileName().replace( QRegExp( "[^A-z0-9\\-_\\.]" ), "_" );
if ( ! savePath.isEmpty() && ! fileName.isEmpty( ) )
{
savePath = QDir::cleanPath( savePath + QDir::separator() + fileName );
}
QString targetFile = QFileDialog::getSaveFileName( this,
tr( "Save as" ),
savePath,
info.suffix( ).isEmpty() ? QString( ) : "*." + info.suffix( )
);
if ( ! targetFile.isEmpty() )
{
settings.setValue( DOWNLOADER_LAST_DIR_KEY, QFileInfo( targetFile ).dir().absolutePath( ) );
// Start the download
new QgsFileDownloader( url, targetFile );
}
}
}
void QgsIdentifyResultsWebView::print()
{
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog( &printer );
if ( dialog->exec() == QDialog::Accepted )
{
QgsWebView::print( &printer );
}
}
void QgsIdentifyResultsWebView::contextMenuEvent( QContextMenuEvent *e )
{
QMenu *menu = page()->createStandardContextMenu();
if ( !menu )
return;
QAction *action = new QAction( tr( "Print" ), this );
connect( action, SIGNAL( triggered() ), this, SLOT( print() ) );
menu->addAction( action );
menu->exec( e->globalPos() );
delete menu;
}
QgsWebView *QgsIdentifyResultsWebView::createWindow( QWebPage::WebWindowType type )
{
QDialog *d = new QDialog( this );
QLayout *l = new QVBoxLayout( d );
QgsWebView *wv = new QgsWebView( d );
l->addWidget( wv );
wv->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
wv->page()->setNetworkAccessManager( QgsNetworkAccessManager::instance() );
wv->settings()->setAttribute( QWebSettings::LocalContentCanAccessRemoteUrls, true );
wv->settings()->setAttribute( QWebSettings::JavascriptCanOpenWindows, true );
wv->settings()->setAttribute( QWebSettings::PluginsEnabled, true );
#ifdef QGISDEBUG
wv->settings()->setAttribute( QWebSettings::DeveloperExtrasEnabled, true );
#endif
d->setModal( type != QWebPage::WebBrowserWindow );
d->show();
return wv;
}
// QgsIdentifyResultsWebView size:
// ---------------
//
// 1) QTreeWidget is not able to scroll continuously through the content of large widgets
// inserted into items via setItemWidget, it always jumps to the top of next
// item and it is not able to scroll to the bottom of an inserted large
// widget (until the QTreeWidget itself is large enough to show the whole
// inserted widget). => We have to keep the height of QgsIdentifyResultsWebView smaller
// than the height of QTreeWidget so that a user can see it entire, even if
// this height is smaller than QgsIdentifyResultsWebView content (i.e. QgsIdentifyResultsWebView scroolbar
// is added). We make it even a bit smaller so that a user can see a bit of
// context (items above/below) when scrolling which is more pleasant.
//
// 2) contentsSize() is 0,0 until a page is loaded. If there are no external
// resources (like images) used, contentsSize() is available immediately
// after setHtml(), otherwise the contentSize() is 0,0 until the page is
// loaded and contentsSizeChanged () is emitted.
//
// 3) If QgsIdentifyResultsWebView is resized (on page load) after it was inserted into
// QTreeWidget, the row does not reflect that change automatically and
// consecutive resize of QTreeWidget will cause to shrink QgsIdentifyResultsWebView to the
// original row height. That is expected, Qt: "setItemWidget() should only
// be used to display static content... => we must not change QgsIdentifyResultsWebView
// size after it was inserted to QTreeWidget
// TODO(?): Sometimes it may happen that if multiple QgsIdentifyResultsWebView are inserted to
// QTreeWidget for the first time, and both share the same external source
// (image) the layout gets somehow confused - wrong positions, overlapped (Qt
// bug?) until next QTreeWidget resize.
// TODO(?): if the results dialog is resized to smaller height, existing
// QgsIdentifyResultsWebView are not (and must not be!) resized and scrolling becomes a bit
// unpleasant until next identify. AFAIK it could only be solved using
// QItemDelegate.
// size hint according to content
QSize QgsIdentifyResultsWebView::sizeHint() const
{
QSize s = page()->mainFrame()->contentsSize();
QgsDebugMsg( QString( "content size: %1 x %2" ).arg( s.width() ).arg( s.height() ) );
int height = s.height();
// parent is qt_scrollarea_viewport
// parent is not available the first time - before results dialog was shown
QWidget *widget = qobject_cast<QWidget *>( parent() );
if ( widget )
{
// It can probably happen that parent is available but it does not have yet
// correct size, see #9377.
int max = widget->size().height() * 0.9;
QgsDebugMsg( QString( "parent widget height = %1 max height = %2" ).arg( widget->size().height() ).arg( max ) );
height = qMin( height, max );
}
else
{
QgsDebugMsg( "parent not available" );
}
// Always keep some minimum size, e.g. if page is not yet loaded
// or parent has wrong size
height = qMax( height, 100 );
s = QSize( size().width(), height );
QgsDebugMsg( QString( "size: %1 x %2" ).arg( s.width() ).arg( s.height() ) );
return s;
}
QgsIdentifyResultsFeatureItem::QgsIdentifyResultsFeatureItem( const QgsFields &fields, const QgsFeature &feature, const QgsCoordinateReferenceSystem &crs, const QStringList &strings )
: QTreeWidgetItem( strings )
, mFields( fields )
, mFeature( feature )
, mCrs( crs )
{
}
void QgsIdentifyResultsWebViewItem::setHtml( const QString &html )
{
mWebView->setHtml( html );
}
void QgsIdentifyResultsWebViewItem::setContent( const QByteArray &data, const QString &mimeType, const QUrl &baseUrl )
{
mWebView->setContent( data, mimeType, baseUrl );
}
QgsIdentifyResultsWebViewItem::QgsIdentifyResultsWebViewItem( QTreeWidget *treeWidget )
{
mWebView = new QgsIdentifyResultsWebView( treeWidget );
mWebView->hide();
setText( 0, tr( "Loading..." ) );
connect( mWebView->page(), SIGNAL( loadFinished( bool ) ), this, SLOT( loadFinished( bool ) ) );
}
void QgsIdentifyResultsWebViewItem::loadFinished( bool ok )
{
Q_UNUSED( ok );
mWebView->show();
treeWidget()->setItemWidget( this, 0, mWebView );
// Span columns to save some space, must be after setItemWidget() to take effect.
setFirstColumnSpanned( true );
disconnect( mWebView->page(), SIGNAL( loadFinished( bool ) ), this, SLOT( loadFinished( bool ) ) );
}
// Tree hierarchy
//
// layer [userrole: QgsMapLayer]
// feature: displayfield|displayvalue [userrole: fid, index in feature list]
// derived attributes (if any) [userrole: "derived"]
// name value
// actions (if any) [userrole: "actions"]
// edit [userrole: "edit"]
// action [userrole: "action", idx]
// action [userrole: "map_layer_action", QgsMapLayerAction]
// displayname [userroles: fieldIdx, original name] displayvalue [userrole: original value]
// displayname [userroles: fieldIdx, original name] displayvalue [userrole: original value]
// displayname [userroles: fieldIdx, original name] displayvalue [userrole: original value]
// feature
// derived attributes (if any)
// name value
// actions (if any)
// action
// name value
QgsIdentifyResultsDialog::QgsIdentifyResultsDialog( QgsMapCanvas *canvas, QWidget *parent, Qt::WindowFlags f )
: QDialog( parent, f )
, mActionPopup( nullptr )
, mCanvas( canvas )
, mDock( nullptr )
{
setupUi( this );
mOpenFormAction->setDisabled( true );
QgsSettings mySettings;
mDock = new QgsDockWidget( tr( "Identify Results" ), QgisApp::instance() );
mDock->setObjectName( QStringLiteral( "IdentifyResultsDock" ) );
mDock->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );
mDock->setWidget( this );
if ( !QgisApp::instance()->restoreDockWidget( mDock ) )
QgisApp::instance()->addDockWidget( Qt::RightDockWidgetArea, mDock );
else
QgisApp::instance()->panelMenu()->addAction( mDock->toggleViewAction() );
int size = mySettings.value( QStringLiteral( "IconSize" ), 16 ).toInt();
if ( size > 32 )
{
size -= 16;
}
else if ( size == 32 )
{
size = 24;
}
else
{
size = 16;
}
mIdentifyToolbar->setIconSize( QSize( size, size ) );
mExpandNewAction->setChecked( mySettings.value( QStringLiteral( "Map/identifyExpand" ), false ).toBool() );
mActionCopy->setEnabled( false );
lstResults->setColumnCount( 2 );
lstResults->sortByColumn( -1 );
setColumnText( 0, tr( "Feature" ) );
setColumnText( 1, tr( "Value" ) );
int width = mySettings.value( QStringLiteral( "Windows/Identify/columnWidth" ), "0" ).toInt();
if ( width > 0 )
{
lstResults->setColumnWidth( 0, width );
}
width = mySettings.value( QStringLiteral( "Windows/Identify/columnWidthTable" ), "0" ).toInt();
if ( width > 0 )
{
tblResults->setColumnWidth( 0, width );
}
// retrieve mode before on_cmbIdentifyMode_currentIndexChanged resets it on addItem
int identifyMode = mySettings.value( QStringLiteral( "Map/identifyMode" ), 0 ).toInt();
cmbIdentifyMode->addItem( tr( "Current layer" ), 0 );
cmbIdentifyMode->addItem( tr( "Top down, stop at first" ), 1 );
cmbIdentifyMode->addItem( tr( "Top down" ), 2 );
cmbIdentifyMode->addItem( tr( "Layer selection" ), 3 );
cmbIdentifyMode->setCurrentIndex( cmbIdentifyMode->findData( identifyMode ) );
cbxAutoFeatureForm->setChecked( mySettings.value( QStringLiteral( "Map/identifyAutoFeatureForm" ), false ).toBool() );
// view modes
cmbViewMode->addItem( tr( "Tree" ), 0 );
cmbViewMode->addItem( tr( "Table" ), 0 );
cmbViewMode->addItem( tr( "Graph" ), 0 );
// graph
mPlot->setVisible( false );
mPlot->setAutoFillBackground( false );
mPlot->setAutoDelete( true );
mPlot->insertLegend( new QwtLegend(), QwtPlot::TopLegend );
QSizePolicy sizePolicy = QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
sizePolicy.setHorizontalStretch( 0 );
sizePolicy.setVerticalStretch( 0 );
sizePolicy.setHeightForWidth( mPlot->sizePolicy().hasHeightForWidth() );
mPlot->setSizePolicy( sizePolicy );
mPlot->updateGeometry();
connect( lstResults, SIGNAL( itemExpanded( QTreeWidgetItem * ) ),
this, SLOT( itemExpanded( QTreeWidgetItem * ) ) );
connect( lstResults, SIGNAL( currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ) ),
this, SLOT( handleCurrentItemChanged( QTreeWidgetItem *, QTreeWidgetItem * ) ) );
connect( lstResults, SIGNAL( itemClicked( QTreeWidgetItem *, int ) ),
this, SLOT( itemClicked( QTreeWidgetItem *, int ) ) );
connect( mActionPrint, SIGNAL( triggered( bool ) ), this, SLOT( printCurrentItem() ) );
connect( mOpenFormAction, SIGNAL( triggered( bool ) ), this, SLOT( featureForm() ) );
connect( mClearResultsAction, SIGNAL( triggered( bool ) ), this, SLOT( clear() ) );
connect( mHelpToolButton, SIGNAL( clicked() ), this, SLOT( helpRequested() ) );
}
QgsIdentifyResultsDialog::~QgsIdentifyResultsDialog()
{
clearHighlights();
QgsSettings settings;
settings.setValue( QStringLiteral( "Windows/Identify/columnWidth" ), lstResults->columnWidth( 0 ) );
if ( mActionPopup )
delete mActionPopup;
Q_FOREACH ( QgsIdentifyPlotCurve *curve, mPlotCurves )
delete curve;
mPlotCurves.clear();
}
QTreeWidgetItem *QgsIdentifyResultsDialog::layerItem( QObject *object )
{
for ( int i = 0; i < lstResults->topLevelItemCount(); i++ )
{
QTreeWidgetItem *item = lstResults->topLevelItem( i );
if ( item->data( 0, Qt::UserRole ).value<QObject *>() == object )
return item;
}
return nullptr;
}
void QgsIdentifyResultsDialog::addFeature( const QgsMapToolIdentify::IdentifyResult &result )
{
if ( result.mLayer->type() == QgsMapLayer::VectorLayer )
{
addFeature( qobject_cast<QgsVectorLayer *>( result.mLayer ), result.mFeature, result.mDerivedAttributes );
}
else if ( result.mLayer->type() == QgsMapLayer::RasterLayer )
{
addFeature( qobject_cast<QgsRasterLayer *>( result.mLayer ), result.mLabel, result.mAttributes, result.mDerivedAttributes, result.mFields, result.mFeature, result.mParams );
}
}
void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeature &f, const QMap<QString, QString> &derivedAttributes )
{
QTreeWidgetItem *layItem = layerItem( vlayer );
if ( !layItem )
{
layItem = new QTreeWidgetItem( QStringList() << vlayer->name() );
layItem->setData( 0, Qt::UserRole, QVariant::fromValue( qobject_cast<QObject *>( vlayer ) ) );
lstResults->addTopLevelItem( layItem );
connect( vlayer, SIGNAL( destroyed() ), this, SLOT( layerDestroyed() ) );
connect( vlayer, SIGNAL( crsChanged() ), this, SLOT( layerDestroyed() ) );
connect( vlayer, SIGNAL( featureDeleted( QgsFeatureId ) ), this, SLOT( featureDeleted( QgsFeatureId ) ) );
connect( vlayer, SIGNAL( attributeValueChanged( QgsFeatureId, int, const QVariant & ) ),
this, SLOT( attributeValueChanged( QgsFeatureId, int, const QVariant & ) ) );
connect( vlayer, SIGNAL( editingStarted() ), this, SLOT( editingToggled() ) );
connect( vlayer, SIGNAL( editingStopped() ), this, SLOT( editingToggled() ) );
}
QgsIdentifyResultsFeatureItem *featItem = new QgsIdentifyResultsFeatureItem( vlayer->fields(), f, vlayer->crs() );
featItem->setData( 0, Qt::UserRole, FID_TO_STRING( f.id() ) );
featItem->setData( 0, Qt::UserRole + 1, mFeatures.size() );
mFeatures << f;
layItem->addChild( featItem );
if ( derivedAttributes.size() >= 0 )
{
QgsTreeWidgetItem *derivedItem = new QgsTreeWidgetItem( QStringList() << tr( "(Derived)" ) );
derivedItem->setData( 0, Qt::UserRole, "derived" );
derivedItem->setAlwaysOnTopPriority( 0 );
featItem->addChild( derivedItem );
for ( QMap< QString, QString>::const_iterator it = derivedAttributes.begin(); it != derivedAttributes.end(); ++it )
{
derivedItem->addChild( new QTreeWidgetItem( QStringList() << it.key() << it.value() ) );
}
}
//get valid QgsMapLayerActions for this layer
QList< QgsMapLayerAction * > registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer );
QList<QgsAction> actions = vlayer->actions()->actions( QStringLiteral( "Feature" ) );
if ( !vlayer->fields().isEmpty() || !actions.isEmpty() || !registeredActions.isEmpty() )
{
QgsTreeWidgetItem *actionItem = new QgsTreeWidgetItem( QStringList() << tr( "(Actions)" ) );
actionItem->setData( 0, Qt::UserRole, "actions" );
actionItem->setAlwaysOnTopPriority( 1 );
featItem->addChild( actionItem );
if ( vlayer->fields().size() > 0 )
{
QTreeWidgetItem *editItem = new QTreeWidgetItem( QStringList() << QLatin1String( "" ) << ( vlayer->isEditable() ? tr( "Edit feature form" ) : tr( "View feature form" ) ) );
editItem->setIcon( 0, QgsApplication::getThemeIcon( QStringLiteral( "/mActionFormView.svg" ) ) );
editItem->setData( 0, Qt::UserRole, "edit" );
actionItem->addChild( editItem );
}
Q_FOREACH ( const QgsAction &action, actions )
{
if ( !action.runable() )
continue;
QTreeWidgetItem *twi = new QTreeWidgetItem( QStringList() << QLatin1String( "" ) << action.name() );
twi->setIcon( 0, QgsApplication::getThemeIcon( QStringLiteral( "/mAction.svg" ) ) );
twi->setData( 0, Qt::UserRole, "action" );
twi->setData( 0, Qt::UserRole + 1, action.id() );
actionItem->addChild( twi );
}
//add actions from QgsMapLayerActionRegistry
for ( int i = 0; i < registeredActions.size(); i++ )
{
QgsMapLayerAction *action = registeredActions.at( i );
QTreeWidgetItem *twi = new QTreeWidgetItem( QStringList() << QLatin1String( "" ) << action->text() );
twi->setIcon( 0, QgsApplication::getThemeIcon( QStringLiteral( "/mAction.svg" ) ) );
twi->setData( 0, Qt::UserRole, "map_layer_action" );
twi->setData( 0, Qt::UserRole + 1, qVariantFromValue( qobject_cast<QObject *>( action ) ) );
actionItem->addChild( twi );
connect( action, SIGNAL( destroyed() ), this, SLOT( mapLayerActionDestroyed() ) );
}
}
const QgsFields &fields = vlayer->fields();
QgsAttributes attrs = f.attributes();
bool featureLabeled = false;
for ( int i = 0; i < attrs.count(); ++i )
{
if ( i >= fields.count() )
break;
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( vlayer, fields[i].name() );
if ( setup.type() == QLatin1String( "Hidden" ) )
{
continue;
}
QString defVal;
if ( fields.fieldOrigin( i ) == QgsFields::OriginProvider && vlayer->dataProvider() )
defVal = vlayer->dataProvider()->defaultValueClause( fields.fieldOriginIndex( i ) );
QString value = defVal == attrs.at( i ) ? defVal : fields.at( i ).displayString( attrs.at( i ) );
QgsTreeWidgetItem *attrItem = new QgsTreeWidgetItem( QStringList() << QString::number( i ) << value );
featItem->addChild( attrItem );
attrItem->setData( 0, Qt::DisplayRole, vlayer->attributeDisplayName( i ) );
attrItem->setToolTip( 0, vlayer->attributeDisplayName( i ) );
attrItem->setData( 0, Qt::UserRole, fields.at( i ).name() );
attrItem->setData( 0, Qt::UserRole + 1, i );
attrItem->setData( 1, Qt::UserRole, value );
value = representValue( vlayer, setup, fields.at( i ).name(), attrs.at( i ) );
attrItem->setSortData( 1, value );
attrItem->setToolTip( 1, value );
bool foundLinks = false;
QString links = QgsStringUtils::insertLinks( value, &foundLinks );
if ( foundLinks )
{
QLabel *valueLabel = new QLabel( links );
valueLabel->setOpenExternalLinks( true );
attrItem->setData( 1, Qt::DisplayRole, QString() );
attrItem->treeWidget()->setItemWidget( attrItem, 1, valueLabel );
}
else
{
attrItem->setData( 1, Qt::DisplayRole, value );
attrItem->treeWidget()->setItemWidget( attrItem, 1, nullptr );
}
if ( fields.at( i ).name() == vlayer->displayField() )
{
featItem->setText( 0, attrItem->text( 0 ) );
featItem->setToolTip( 0, attrItem->text( 0 ) );
featItem->setText( 1, attrItem->text( 1 ) );
featItem->setToolTip( 1, attrItem->text( 1 ) );
featureLabeled = true;
}
}
if ( !featureLabeled )
{
featItem->setText( 0, tr( "Title" ) );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vlayer ) );
context.setFeature( f );
QString value = QgsExpression( vlayer->displayExpression() ).evaluate( &context ).toString();
featItem->setText( 1, value );
featItem->setToolTip( 1, value );
}
// table
int j = tblResults->rowCount();
for ( int i = 0; i < attrs.count(); ++i )
{
if ( i >= fields.count() )
continue;
QString value = fields.at( i ).displayString( attrs.at( i ) );
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( vlayer, fields.at( i ).name() );
QString value2 = representValue( vlayer, setup, fields.at( i ).name(), value );
tblResults->setRowCount( j + 1 );
QgsDebugMsg( QString( "adding item #%1 / %2 / %3 / %4" ).arg( j ).arg( vlayer->name(), vlayer->attributeDisplayName( i ), value2 ) );
QTableWidgetItem *item = new QTableWidgetItem( vlayer->name() );
item->setData( Qt::UserRole, QVariant::fromValue( qobject_cast<QObject *>( vlayer ) ) );
item->setData( Qt::UserRole + 1, vlayer->id() );
tblResults->setItem( j, 0, item );
item = new QTableWidgetItem( FID_TO_STRING( f.id() ) );
item->setData( Qt::UserRole, FID_TO_STRING( f.id() ) );
item->setData( Qt::UserRole + 1, mFeatures.size() );
tblResults->setItem( j, 1, item );
item = new QTableWidgetItem( QString::number( i ) );
if ( fields.at( i ).name() == vlayer->displayField() )
item->setData( Qt::DisplayRole, vlayer->attributeDisplayName( i ) + " *" );
else
item->setData( Qt::DisplayRole, vlayer->attributeDisplayName( i ) );
item->setData( Qt::UserRole, fields.at( i ).name() );
item->setData( Qt::UserRole + 1, i );
tblResults->setItem( j, 2, item );
item = new QTableWidgetItem( value );
item->setData( Qt::UserRole, value );
item->setData( Qt::DisplayRole, value2 );
tblResults->setItem( j, 3, item );
// highlight first item
// if ( i==0 )
// {
// QBrush b = tblResults->palette().brush( QPalette::AlternateBase );
// for ( int k = 0; k <= 3; k++)
// tblResults->item( j, k )->setBackground( b );
// }
tblResults->resizeRowToContents( j );
j++;
}
//tblResults->resizeColumnToContents( 1 );
highlightFeature( featItem );
}
void QgsIdentifyResultsDialog::mapLayerActionDestroyed()
{
QTreeWidgetItemIterator it( lstResults );
while ( *it )
{
if ( ( *it )->data( 0, Qt::UserRole ) == "map_layer_action" &&
( *it )->data( 0, Qt::UserRole + 1 ).value< QObject *>() == sender() )
delete *it;
else
++it;
}
}
QgsIdentifyPlotCurve::QgsIdentifyPlotCurve( const QMap<QString, QString> &attributes,
QwtPlot *plot, const QString &title, QColor color )
{
mPlotCurve = new QwtPlotCurve( title );
if ( color == QColor() )
{
color = QgsLimitedRandomColorRamp::randomColors( 1 ).at( 0 );
}
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mPlotCurve->setSymbol( new QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::white ),
QPen( color, 2 ), QSize( 9, 9 ) ) );
mPlotCurve->setPen( QPen( color, 2 ) ); // needed for legend
#else
mPlotCurve->setSymbol( QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::white ),
QPen( color, 2 ), QSize( 9, 9 ) ) );
mPlotCurve->setPen( QPen( color, 2 ) );
#endif
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
QVector<QPointF> myData;
#else
QVector<double> myX2Data;
QVector<double> myY2Data;
#endif
int i = 1;
for ( QMap<QString, QString>::const_iterator it = attributes.begin();
it != attributes.end(); ++it )
{
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
myData << QPointF( double( i++ ), it.value().toDouble() );
#else
myX2Data.append( double( i++ ) );
myY2Data.append( it.value().toDouble() );
#endif
}
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mPlotCurve->setSamples( myData );
#else
mPlotCurve->setData( myX2Data, myY2Data );
#endif
mPlotCurve->attach( plot );
plot->setAxisMaxMinor( QwtPlot::xBottom, 0 );
//mPlot->setAxisScale( QwtPlot::xBottom, 1, mPlotCurve->dataSize());
//mPlot->setAxisScale( QwtPlot::yLeft, ymin, ymax );
plot->replot();
plot->setVisible( true );
}
QgsIdentifyPlotCurve::~QgsIdentifyPlotCurve()
{
if ( mPlotCurve )
{
mPlotCurve->detach();
delete mPlotCurve;
}
}
QString QgsIdentifyResultsDialog::representValue( QgsVectorLayer *vlayer, const QgsEditorWidgetSetup &setup, const QString &fieldName, const QVariant &value )
{
QVariant cache;
QMap<QString, QVariant> &layerCaches = mWidgetCaches[vlayer->id()];
QgsEditorWidgetFactory *factory = QgsEditorWidgetRegistry::instance()->factory( setup.type() );
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
int idx = vlayer->fields().lookupField( fieldName );
if ( !factory )
return value.toString();
if ( layerCaches.contains( fieldName ) )
{
cache = layerCaches[ fieldName ];
}
else
{
cache = fieldFormatter->createCache( vlayer, idx, setup.config() );
layerCaches.insert( fieldName, cache );
}
return fieldFormatter->representValue( vlayer, idx, setup.config(), cache, value );
}
void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
const QString &label,
const QMap<QString, QString> &attributes,
const QMap<QString, QString> &derivedAttributes,
const QgsFields &fields,
const QgsFeature &feature,
const QMap<QString, QVariant> ¶ms )
{
QgsDebugMsg( QString( "feature.isValid() = %1" ).arg( feature.isValid() ) );
QTreeWidgetItem *layItem = layerItem( layer );
QgsRaster::IdentifyFormat currentFormat = QgsRasterDataProvider::identifyFormatFromName( layer->customProperty( QStringLiteral( "identify/format" ) ).toString() );
if ( !layItem )
{
layItem = new QTreeWidgetItem( QStringList() << QString::number( lstResults->topLevelItemCount() ) << layer->name() );
layItem->setData( 0, Qt::UserRole, QVariant::fromValue( qobject_cast<QObject *>( layer ) ) );
lstResults->addTopLevelItem( layItem );
QComboBox *formatCombo = new QComboBox();
// Add all supported formats, best first. HTML is considered the best because
// it usually holds most information.
int capabilities = layer->dataProvider()->capabilities();
QList<QgsRaster::IdentifyFormat> formats;
formats << QgsRaster::IdentifyFormatHtml
<< QgsRaster::IdentifyFormatFeature
<< QgsRaster::IdentifyFormatText
<< QgsRaster::IdentifyFormatValue;
Q_FOREACH ( QgsRaster::IdentifyFormat f, formats )
{
if ( !( QgsRasterDataProvider::identifyFormatToCapability( f ) & capabilities ) )
continue;
formatCombo->addItem( QgsRasterDataProvider::identifyFormatLabel( f ), f );
formatCombo->setItemData( formatCombo->count() - 1, qVariantFromValue( qobject_cast<QObject *>( layer ) ), Qt::UserRole + 1 );
if ( currentFormat == f )
formatCombo->setCurrentIndex( formatCombo->count() - 1 );
}
if ( formatCombo->count() > 1 )
{
// Add format combo box item
// Space added before format to keep it first in ordered list: TODO better (user data)
QTreeWidgetItem *formatItem = new QTreeWidgetItem( QStringList() << ' ' + tr( "Format" ) );
layItem->addChild( formatItem );
lstResults->setItemWidget( formatItem, 1, formatCombo );
connect( formatCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( formatChanged( int ) ) );
}
else
{
delete formatCombo;
}
connect( layer, SIGNAL( destroyed() ), this, SLOT( layerDestroyed() ) );
connect( layer, SIGNAL( crsChanged() ), this, SLOT( layerDestroyed() ) );
}
// Set/reset getFeatureInfoUrl (currently only available for Feature, so it may change if format changes)
layItem->setData( 0, GetFeatureInfoUrlRole, params.value( QStringLiteral( "getFeatureInfoUrl" ) ) );
QgsIdentifyResultsFeatureItem *featItem = new QgsIdentifyResultsFeatureItem( fields, feature, layer->crs(), QStringList() << label << QLatin1String( "" ) );
layItem->addChild( featItem );
// add feature attributes
if ( feature.isValid() )
{
QgsDebugMsg( QString( "fields size = %1 attributes size = %2" ).arg( fields.size() ).arg( feature.attributes().size() ) );
QgsAttributes attrs = feature.attributes();
for ( int i = 0; i < attrs.count(); ++i )
{
if ( i >= fields.count() )
continue;
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << attrs.at( i ).toString() );
attrItem->setData( 0, Qt::DisplayRole, fields.at( i ).name() );
QVariant value = attrs.at( i );
attrItem->setData( 1, Qt::DisplayRole, value );
featItem->addChild( attrItem );
}
}
if ( currentFormat == QgsRaster::IdentifyFormatHtml || currentFormat == QgsRaster::IdentifyFormatText )
{
QgsIdentifyResultsWebViewItem *attrItem = new QgsIdentifyResultsWebViewItem( lstResults );
featItem->addChild( attrItem ); // before setHtml()!
if ( !attributes.isEmpty() )
{
attrItem->setContent( attributes.begin().value().toUtf8(), currentFormat == QgsRaster::IdentifyFormatHtml ? "text/html" : "text/plain; charset=utf-8" );
}
else
{
attrItem->setContent( tr( "No attributes." ).toUtf8(), QStringLiteral( "text/plain; charset=utf-8" ) );
}
}
else
{
for ( QMap<QString, QString>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
{
featItem->addChild( new QTreeWidgetItem( QStringList() << it.key() << it.value() ) );
}
}
if ( derivedAttributes.size() >= 0 )
{
QgsTreeWidgetItem *derivedItem = new QgsTreeWidgetItem( QStringList() << tr( "(Derived)" ) );
derivedItem->setData( 0, Qt::UserRole, "derived" );
derivedItem->setAlwaysOnTopPriority( 0 );
featItem->addChild( derivedItem );
for ( QMap< QString, QString>::const_iterator it = derivedAttributes.begin(); it != derivedAttributes.end(); ++it )
{
derivedItem->addChild( new QTreeWidgetItem( QStringList() << it.key() << it.value() ) );
}
}
// table
int i = 0;
int j = tblResults->rowCount();
tblResults->setRowCount( j + attributes.count() );
for ( QMap<QString, QString>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
{
QgsDebugMsg( QString( "adding item #%1 / %2 / %3 / %4" ).arg( j ).arg( layer->name(), it.key(), it.value() ) );
QTableWidgetItem *item = new QTableWidgetItem( layer->name() );
item->setData( Qt::UserRole, QVariant::fromValue( qobject_cast<QObject *>( layer ) ) );
item->setData( Qt::UserRole + 1, layer->id() );
tblResults->setItem( j, 0, item );
tblResults->setItem( j, 1, new QTableWidgetItem( QString::number( i + 1 ) ) );
tblResults->setItem( j, 2, new QTableWidgetItem( it.key() ) );
tblResults->setItem( j, 3, new QTableWidgetItem( it.value() ) );
tblResults->resizeRowToContents( j );
j++;
i++;
}
//tblResults->resizeColumnToContents( 1 );
// graph
if ( !attributes.isEmpty() )
{
mPlotCurves.append( new QgsIdentifyPlotCurve( attributes, mPlot, layer->name() ) );
}
}
void QgsIdentifyResultsDialog::editingToggled()
{
QTreeWidgetItem *layItem = layerItem( sender() );
QgsVectorLayer *vlayer = vectorLayer( layItem );
if ( !layItem || !vlayer )
return;
// iterate features
int i;
for ( i = 0; i < layItem->childCount(); i++ )
{
QTreeWidgetItem *featItem = layItem->child( i );
int j;
for ( j = 0; j < featItem->childCount() && featItem->child( j )->data( 0, Qt::UserRole ).toString() != QLatin1String( "actions" ); j++ )
QgsDebugMsg( QString( "%1: skipped %2" ).arg( featItem->child( j )->data( 0, Qt::UserRole ).toString() ) );
if ( j == featItem->childCount() || featItem->child( j )->childCount() < 1 )
continue;
QTreeWidgetItem *actions = featItem->child( j );
for ( j = 0; i < actions->childCount() && actions->child( j )->data( 0, Qt::UserRole ).toString() != QLatin1String( "edit" ); j++ )
;
if ( j == actions->childCount() )
continue;
QTreeWidgetItem *editItem = actions->child( j );
mOpenFormAction->setToolTip( vlayer->isEditable() ? tr( "Edit feature form" ) : tr( "View feature form" ) );
editItem->setText( 1, mOpenFormAction->toolTip() );
}
}
// Call to show the dialog box.
void QgsIdentifyResultsDialog::show()
{
bool showFeatureForm = false;
if ( lstResults->topLevelItemCount() > 0 )
{
QTreeWidgetItem *layItem = lstResults->topLevelItem( 0 );
QTreeWidgetItem *featItem = layItem->child( 0 );
if ( lstResults->topLevelItemCount() == 1 && layItem->childCount() == 1 )
{
lstResults->setCurrentItem( featItem );
if ( QgsSettings().value( QStringLiteral( "/Map/identifyAutoFeatureForm" ), false ).toBool() )
{
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( layItem->data( 0, Qt::UserRole ).value<QObject *>() );
if ( layer )
{
// if this is the only feature and it's on a vector layer
// show the form dialog instead of the results window
showFeatureForm = true;
}
}
}
// expand first layer and feature
featItem->setExpanded( true );
layItem->setExpanded( true );
}
// expand all if enabled
if ( mExpandNewAction->isChecked() )
{
lstResults->expandAll();
}
QDialog::show();
// when the feature form is opened don't show and raise the identify result.
// If it's not docked, the results would open after or possibly on top of the
// feature form and stay open (on top the canvas) after the feature form is
// closed.
if ( showFeatureForm )
{
featureForm();
}
else
{
mDock->show();
mDock->raise();
}
}
void QgsIdentifyResultsDialog::itemClicked( QTreeWidgetItem *item, int column )
{
Q_UNUSED( column );
if ( item->data( 0, Qt::UserRole ).toString() == QLatin1String( "edit" ) )
{
lstResults->setCurrentItem( item );
featureForm();
}
else if ( item->data( 0, Qt::UserRole ).toString() == QLatin1String( "action" ) )
{
doAction( item, item->data( 0, Qt::UserRole + 1 ).toString() );
}