QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsfeatureselectiondlg.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeatureselectiondlg.cpp
3  --------------------------------------
4  Date : 11.6.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsfeatureselectiondlg.h"
17 
19 #include "qgsdistancearea.h"
20 #include "qgsfeaturerequest.h"
22 #include "qgsapplication.h"
24 #include "qgsmapcanvas.h"
25 
26 #include <QWindow>
27 
29  : QDialog( parent, Qt::Window )
30  , mVectorLayer( vl )
31  , mContext( context )
32 {
33  setupUi( this );
34 
35  mFeatureSelection = new QgsVectorLayerSelectionManager( vl, mDualView );
36 
37  mDualView->setFeatureSelectionManager( mFeatureSelection );
38 
39  mDualView->init( mVectorLayer, context.mapCanvas(), QgsFeatureRequest(), context );
40  mDualView->setView( QgsDualView::AttributeEditor );
41 
42  mFeatureFilterWidget->init( vl, context, mDualView, mMessageBar, 5 );
43  mFeatureFilterWidget->filterVisible();
44 
45  connect( mActionExpressionSelect, &QAction::triggered, this, &QgsFeatureSelectionDlg::mActionExpressionSelect_triggered );
46  connect( mActionSelectAll, &QAction::triggered, this, &QgsFeatureSelectionDlg::mActionSelectAll_triggered );
47  connect( mActionInvertSelection, &QAction::triggered, this, &QgsFeatureSelectionDlg::mActionInvertSelection_triggered );
48  connect( mActionRemoveSelection, &QAction::triggered, this, &QgsFeatureSelectionDlg::mActionRemoveSelection_triggered );
49  connect( mActionSearchForm, &QAction::toggled, mDualView, &QgsDualView::toggleSearchMode );
50  connect( mActionSelectedToTop, &QAction::toggled, this, [this]( bool checked ) { mDualView->setSelectedOnTop( checked ); } );
51  connect( mActionZoomMapToSelectedRows, &QAction::triggered, this, &QgsFeatureSelectionDlg::mActionZoomMapToSelectedRows_triggered );
52  connect( mActionPanMapToSelectedRows, &QAction::triggered, this, &QgsFeatureSelectionDlg::mActionPanMapToSelectedRows_triggered );
53 
54  connect( mDualView, &QgsDualView::filterExpressionSet, this, &QgsFeatureSelectionDlg::setFilterExpression );
55  connect( mDualView, &QgsDualView::formModeChanged, this, &QgsFeatureSelectionDlg::viewModeChanged );
56 }
57 
59 {
60  // don't accept Enter key to accept the dialog, user could be editing a text field
61  if ( evt->key() == Qt::Key_Enter || evt->key() == Qt::Key_Return )
62  return;
63  QDialog::keyPressEvent( evt );
64 }
65 
67 {
68  return mFeatureSelection->selectedFeatureIds();
69 }
70 
72 {
73  mFeatureSelection->setSelectedFeatures( ids );
74 }
75 
76 void QgsFeatureSelectionDlg::showEvent( QShowEvent *event )
77 {
78 
79  QWindow *mainWindow = nullptr;
80  for ( const auto &w : QgsApplication::instance()->topLevelWindows() )
81  {
82  if ( w->objectName() == QLatin1String( "QgisAppWindow" ) )
83  {
84  mainWindow = w;
85  break;
86  }
87  }
88 
89  if ( mainWindow )
90  {
91  QSize margins( size() - scrollAreaWidgetContents->size() );
92  QSize innerWinSize( mainWindow->width(), mainWindow->height() );
93  setMaximumSize( innerWinSize );
94  QSize minSize( scrollAreaWidgetContents->sizeHint() );
95  setMinimumSize( std::min( minSize.width() + margins.width( ), innerWinSize.width() ),
96  std::min( minSize.height() + margins.width( ), innerWinSize.height() ) );
97  }
98 
99  QDialog::showEvent( event );
100 }
101 
102 void QgsFeatureSelectionDlg::mActionExpressionSelect_triggered()
103 {
105  dlg->setMessageBar( mMessageBar );
106  dlg->setAttribute( Qt::WA_DeleteOnClose );
107  dlg->show();
108 }
109 
110 void QgsFeatureSelectionDlg::mActionInvertSelection_triggered()
111 {
112  mVectorLayer->invertSelection();
113 }
114 
115 void QgsFeatureSelectionDlg::mActionRemoveSelection_triggered()
116 {
117  mVectorLayer->removeSelection();
118 }
119 
120 void QgsFeatureSelectionDlg::mActionSelectAll_triggered()
121 {
122  mVectorLayer->selectAll();
123 }
124 
125 void QgsFeatureSelectionDlg::mActionZoomMapToSelectedRows_triggered()
126 {
127  mContext.mapCanvas()->zoomToSelected( mVectorLayer );
128 }
129 
130 void QgsFeatureSelectionDlg::mActionPanMapToSelectedRows_triggered()
131 {
132  mContext.mapCanvas()->panToSelected( mVectorLayer );
133 }
134 
135 void QgsFeatureSelectionDlg::setFilterExpression( const QString &filter, QgsAttributeForm::FilterType type )
136 {
137  mFeatureFilterWidget->setFilterExpression( filter, type, true );
138 }
139 
140 void QgsFeatureSelectionDlg::viewModeChanged( QgsAttributeEditorContext::Mode mode )
141 {
142  mActionSearchForm->setChecked( mode == QgsAttributeEditorContext::SearchMode );
143 }
QgsAttributeEditorContext::mapCanvas
QgsMapCanvas * mapCanvas() const
Returns the associated map canvas (e.g.
Definition: qgsattributeeditorcontext.h:140
qgsfeaturerequest.h
qgsmapcanvas.h
QgsDualView::formModeChanged
void formModeChanged(QgsAttributeEditorContext::Mode mode)
Emitted when the form changes mode.
QgsFeatureSelectionDlg::QgsFeatureSelectionDlg
QgsFeatureSelectionDlg(QgsVectorLayer *vl, const QgsAttributeEditorContext &context, QWidget *parent=nullptr)
Constructor for QgsFeatureSelectionDlg.
Definition: qgsfeatureselectiondlg.cpp:28
QgsAttributeEditorContext::SearchMode
@ SearchMode
Form values are used for searching/filtering the layer.
Definition: qgsattributeeditorcontext.h:54
QgsExpressionSelectionDialog
This class offers a dialog to change feature selections.
Definition: qgsexpressionselectiondialog.h:37
QgsApplication::instance
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
Definition: qgsapplication.cpp:411
QgsDualView::toggleSearchMode
void toggleSearchMode(bool enabled)
Toggles whether search mode should be enabled in the form.
Definition: qgsdualview.cpp:660
qgsapplication.h
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsAttributeEditorContext::Mode
Mode
modes
Definition: qgsattributeeditorcontext.h:48
qgsvectorlayerselectionmanager.h
QgsDualView::AttributeEditor
@ AttributeEditor
Show a list of the features, where one can be chosen and the according attribute dialog will be prese...
Definition: qgsdualview.h:65
qgsattributeeditorcontext.h
QgsVectorLayerSelectionManager
Definition: qgsvectorlayerselectionmanager.h:33
qgsexpressionselectiondialog.h
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsFeatureSelectionDlg::showEvent
void showEvent(QShowEvent *event) override
Make sure the dialog does not grow too much.
Definition: qgsfeatureselectiondlg.cpp:76
QgsAttributeForm::FilterType
FilterType
Filter types.
Definition: qgsattributeform.h:64
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsExpressionSelectionDialog::setMessageBar
void setMessageBar(QgsMessageBar *messageBar)
Sets the message bar to display feedback from the dialog.
Definition: qgsexpressionselectiondialog.cpp:89
QgsFeatureSelectionDlg::selectedFeatures
const QgsFeatureIds & selectedFeatures()
Gets the selected features.
Definition: qgsfeatureselectiondlg.cpp:66
qgsdistancearea.h
QgsDualView::filterExpressionSet
void filterExpressionSet(const QString &expression, QgsAttributeForm::FilterType type)
Emitted when a filter expression is set using the view.
qgsfeatureselectiondlg.h
QgsAttributeEditorContext
This class contains context information for attribute editor widgets.
Definition: qgsattributeeditorcontext.h:41
QgsFeatureSelectionDlg::setSelectedFeatures
void setSelectedFeatures(const QgsFeatureIds &ids)
Set the selected features.
Definition: qgsfeatureselectiondlg.cpp:71
QgsFeatureSelectionDlg::keyPressEvent
void keyPressEvent(QKeyEvent *evt) override
Definition: qgsfeatureselectiondlg.cpp:58