/
qgsselectbyformdialog.cpp
116 lines (97 loc) · 4 KB
/
qgsselectbyformdialog.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
/***************************************************************************
qgsselectbyformdialog.cpp
------------------------
Date : June 2016
Copyright : (C) 2016 nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* 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 "qgsselectbyformdialog.h"
#include "qgsattributeform.h"
#include "qgsmapcanvas.h"
#include "qgssettings.h"
#include <QLayout>
QgsSelectByFormDialog::QgsSelectByFormDialog( QgsVectorLayer *layer, const QgsAttributeEditorContext &context, QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
, mLayer( layer )
, mMessageBar( nullptr )
{
QgsAttributeEditorContext dlgContext = context;
dlgContext.setFormMode( QgsAttributeEditorContext::StandaloneDialog );
dlgContext.setAllowCustomUi( false );
mForm = new QgsAttributeForm( layer, QgsFeature(), dlgContext, this );
mForm->setMode( QgsAttributeForm::SearchMode );
QVBoxLayout *vLayout = new QVBoxLayout();
vLayout->setMargin( 0 );
vLayout->setContentsMargins( 0, 0, 0, 0 );
setLayout( vLayout );
vLayout->addWidget( mForm );
connect( mForm, &QgsAttributeForm::closed, this, &QWidget::close );
QgsSettings settings;
restoreGeometry( settings.value( QStringLiteral( "Windows/SelectByForm/geometry" ) ).toByteArray() );
setWindowTitle( tr( "Select Features by Value" ) );
}
QgsSelectByFormDialog::~QgsSelectByFormDialog()
{
QgsSettings settings;
settings.setValue( QStringLiteral( "Windows/SelectByForm/geometry" ), saveGeometry() );
}
void QgsSelectByFormDialog::setMessageBar( QgsMessageBar *messageBar )
{
mMessageBar = messageBar;
mForm->setMessageBar( messageBar );
}
void QgsSelectByFormDialog::setMapCanvas( QgsMapCanvas *canvas )
{
mMapCanvas = canvas;
connect( mForm, &QgsAttributeForm::zoomToFeatures, this, &QgsSelectByFormDialog::zoomToFeatures );
}
void QgsSelectByFormDialog::zoomToFeatures( const QString &filter )
{
QgsFeatureIds ids;
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( filter )
.setExpressionContext( context )
.setSubsetOfAttributes( QgsAttributeList() );
QgsFeatureIterator features = mLayer->getFeatures( request );
QgsRectangle bbox;
bbox.setMinimal();
QgsFeature feat;
int featureCount = 0;
while ( features.nextFeature( feat ) )
{
QgsGeometry geom = feat.geometry();
if ( geom.isNull() || geom.geometry()->isEmpty() )
continue;
QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
bbox.combineExtentWith( r );
featureCount++;
}
features.close();
QgsSettings settings;
int timeout = settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
if ( featureCount > 0 )
{
mMapCanvas->zoomToFeatureExtent( bbox );
if ( mMessageBar )
{
mMessageBar->pushMessage( QString(),
tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ),
QgsMessageBar::INFO,
timeout );
}
}
else if ( mMessageBar )
{
mMessageBar->pushMessage( QString(),
tr( "No matching features found" ),
QgsMessageBar::INFO,
timeout );
}
}