1 /***************************************************************************
2   qgsmeshselectbyexpressiondialog.cpp - QgsMeshSelectByExpressionDialog
3 
4  ---------------------
5  begin                : 23.8.2021
6  copyright            : (C) 2021 by Vincent Cloarec
7  email                : vcloarec at gmail dot com
8  ***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 #include "qgsmeshselectbyexpressiondialog.h"
17 
18 #include <QAction>
19 
20 #include "qgsapplication.h"
21 #include "qgsexpressioncontextutils.h"
22 #include "qgshelp.h"
23 #include "qgsgui.h"
24 
QgsMeshSelectByExpressionDialog(QWidget * parent)25 QgsMeshSelectByExpressionDialog::QgsMeshSelectByExpressionDialog( QWidget *parent ):
26   QDialog( parent )
27 {
28   setupUi( this );
29 
30   QgsGui::enableAutoGeometryRestore( this );
31 
32   setWindowTitle( tr( "Select Mesh Elements by Expression" ) );
33 
34   QString elementText = tr( "Vertices" );
35   mActionSelect = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpressionSelect.svg" ) ),  tr( "Select" ), this );
36   mActionAddToSelection = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectAdd.svg" ) ), tr( "Add to current selection" ), this );
37   mActionRemoveFromSelection = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectRemove.svg" ) ), tr( "Remove from current selection" ), this );
38 
39   mButtonSelect->addAction( mActionSelect );
40   mButtonSelect->setDefaultAction( mActionSelect );
41   mButtonSelect->addAction( mActionAddToSelection );
42   mButtonSelect->addAction( mActionRemoveFromSelection );
43 
44   mComboBoxElementType->addItem( tr( "Select by Vertices" ), QgsMesh::Vertex );
45   mComboBoxElementType->addItem( tr( "Select by Faces" ), QgsMesh::Face );
46   QgsSettings settings;
47   QgsMesh::ElementType elementType = QgsMesh::Vertex;
48   if ( settings.contains( QStringLiteral( "/meshSelection/elementType" ) ) )
49     elementType = static_cast<QgsMesh::ElementType>( settings.value( QStringLiteral( "/meshSelection/elementType" ) ).toInt() );
50 
51   int comboIndex = mComboBoxElementType->findData( elementType );
52 
53   if ( comboIndex >= 0 )
54     mComboBoxElementType->setCurrentIndex( comboIndex );
55 
56   onElementTypeChanged();
57 
58   connect( mActionSelect, &QAction::triggered, this, [this]
59   {
60     emit select( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::SetSelection, currentElementType() );
61   } );
62   connect( mActionAddToSelection, &QAction::triggered, this, [this]
63   {
64     emit select( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::AddToSelection, currentElementType() );
65   } );
66   connect( mActionRemoveFromSelection, &QAction::triggered, this, [this]
67   {
68     emit select( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::RemoveFromSelection, currentElementType() );
69   } );
70 
71   connect( mActionSelect, &QAction::triggered, this, &QgsMeshSelectByExpressionDialog::saveRecent );
72   connect( mActionAddToSelection, &QAction::triggered, this,  &QgsMeshSelectByExpressionDialog::saveRecent );
73   connect( mActionRemoveFromSelection, &QAction::triggered, this,  &QgsMeshSelectByExpressionDialog::saveRecent );
74 
75   connect( mButtonClose, &QPushButton::clicked, this, &QgsMeshSelectByExpressionDialog::close );
76   connect( mButtonZoomToSelected, &QToolButton::clicked, this, &QgsMeshSelectByExpressionDialog::zoomToSelected );
77 
78   connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsMeshSelectByExpressionDialog::showHelp );
79 
80   connect( mComboBoxElementType, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsMeshSelectByExpressionDialog::onElementTypeChanged );
81 
82   mExpressionBuilder->setExpressionPreviewVisible( false );
83 }
84 
expression() const85 QString QgsMeshSelectByExpressionDialog::expression() const
86 {
87   return mExpressionBuilder->expressionText();
88 }
89 
showHelp() const90 void QgsMeshSelectByExpressionDialog::showHelp() const
91 {
92   QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html" ) );
93 }
94 
saveRecent() const95 void QgsMeshSelectByExpressionDialog::saveRecent() const
96 {
97   mExpressionBuilder->expressionTree()->saveToRecent( mExpressionBuilder->expressionText(), QStringLiteral( "mesh_vertex_selection" ) );
98 }
99 
onElementTypeChanged() const100 void QgsMeshSelectByExpressionDialog::onElementTypeChanged() const
101 {
102   QgsMesh::ElementType elementType = currentElementType() ;
103   QgsSettings settings;
104   settings.setValue( QStringLiteral( "/meshSelection/elementType" ), elementType );
105 
106   QgsExpressionContext expressionContext( {QgsExpressionContextUtils::meshExpressionScope( elementType )} );
107   mExpressionBuilder->init( expressionContext, QStringLiteral( "mesh_vertex_selection" ), QgsExpressionBuilderWidget::LoadAll );
108 }
109 
currentElementType() const110 QgsMesh::ElementType QgsMeshSelectByExpressionDialog::currentElementType() const
111 {
112   return static_cast<QgsMesh::ElementType>( mComboBoxElementType->currentData().toInt() );
113 }
114