Skip to content

Commit

Permalink
Add filter option to source select widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 4, 2024
1 parent eb778dd commit b17714c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
32 changes: 27 additions & 5 deletions src/gui/providers/sensorthings/qgssensorthingssourceselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgssensorthingsconnectiondialog.h"
#include "qgssensorthingssourcewidget.h"
#include "qgssensorthingsprovider.h"
#include "qgssensorthingssubseteditor.h"

#include <QFileDialog>
#include <QMessageBox>
Expand Down Expand Up @@ -51,6 +52,9 @@ QgsSensorThingsSourceSelect::QgsSensorThingsSourceSelect( QWidget *parent, Qt::W
vlayout->addWidget( mSourceWidget );
mLayerSettingsContainerWidget->setLayout( vlayout );

txtSubsetSQL->setWrapMode( QgsCodeEditor::WrapWord );
connect( pbnQueryBuilder, &QPushButton::clicked, this, &QgsSensorThingsSourceSelect::buildFilter );

connect( mSourceWidget, &QgsProviderSourceWidget::validChanged, this, &QgsSensorThingsSourceSelect::validate );
connect( mConnectionWidget, &QgsSensorThingsConnectionWidget::validChanged, this, &QgsSensorThingsSourceSelect::validate );

Expand Down Expand Up @@ -158,13 +162,20 @@ void QgsSensorThingsSourceSelect::addButtonClicked()
const bool isCustom = cmbConnections->currentData().toString() == QLatin1String( "~~custom~~" );

const QString providerUri = mConnectionWidget->sourceUri();
const QString layerUri = mSourceWidget->updateUriFromGui( providerUri );
QString layerUri = mSourceWidget->updateUriFromGui( providerUri );

QVariantMap uriParts = QgsProviderRegistry::instance()->decodeUri(
QgsSensorThingsProvider::SENSORTHINGS_PROVIDER_KEY,
layerUri
);

if ( !txtSubsetSQL->text().isEmpty() )
uriParts.insert( QStringLiteral( "sql" ), txtSubsetSQL->text() );

const QVariantMap uriParts = QgsProviderRegistry::instance()->decodeUri(
QgsSensorThingsProvider::SENSORTHINGS_PROVIDER_KEY,
layerUri
);
layerUri = QgsProviderRegistry::instance()->encodeUri(
QgsSensorThingsProvider::SENSORTHINGS_PROVIDER_KEY,
uriParts
);

const Qgis::SensorThingsEntity type = QgsSensorThingsUtils::stringToEntity( uriParts.value( QStringLiteral( "entity" ) ).toString() );

Expand Down Expand Up @@ -274,6 +285,17 @@ void QgsSensorThingsSourceSelect::cmbConnections_currentTextChanged( const QStri
}
}

void QgsSensorThingsSourceSelect::buildFilter()
{
const QgsFields fields = QgsSensorThingsUtils::fieldsForEntityType( mSourceWidget->currentEntityType() );
QgsSensorThingsSubsetEditor subsetEditor( nullptr, fields );
subsetEditor.setSubsetString( txtSubsetSQL->text( ) );
if ( subsetEditor.exec() )
{
txtSubsetSQL->setText( subsetEditor.subsetString() );
}
}

void QgsSensorThingsSourceSelect::validate()
{
const bool isValid = mConnectionWidget->isValid() && mSourceWidget->isValid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class QgsSensorThingsSourceSelect : public QgsAbstractDataSourceWidget, private
void btnLoad_clicked();
//! Stores the selected datasource whenerver it is changed
void cmbConnections_currentTextChanged( const QString &text );
void buildFilter();

void validate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ void QgsSensorThingsSourceWidget::setMapCanvas( QgsMapCanvas *mapCanvas )
mExtentWidget->setMapCanvas( mapCanvas, false );
}

Qgis::SensorThingsEntity QgsSensorThingsSourceWidget::currentEntityType() const
{
return mComboEntityType->currentData().value< Qgis::SensorThingsEntity >();
}

QString QgsSensorThingsSourceWidget::updateUriFromGui( const QString &connectionUri ) const
{
QVariantMap parts = QgsProviderRegistry::instance()->decodeUri(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class QgsSensorThingsSourceWidget : public QgsProviderSourceWidget, protected Ui
QString sourceUri() const override;
QString groupTitle() const override;
void setMapCanvas( QgsMapCanvas *mapCanvas ) override;
Qgis::SensorThingsEntity currentEntityType() const;

/**
* Updates a connection uri with the layer specific URI settings defined in the widget.
Expand Down
57 changes: 55 additions & 2 deletions src/ui/qgssensorthingssourceselectbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>678</width>
<height>561</height>
<height>501</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
Expand Down Expand Up @@ -35,7 +35,7 @@
<x>0</x>
<y>0</y>
<width>658</width>
<height>380</height>
<height>320</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
Expand Down Expand Up @@ -75,6 +75,47 @@
</layout>
</widget>
</item>
<item>
<widget class="QgsCollapsibleGroupBox" name="mSubsetGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>3</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Filter</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="1" column="1">
<widget class="QPushButton" name="pbnQueryBuilder">
<property name="text">
<string>Query Builder</string>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>21</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" colspan="2">
<widget class="QgsCodeEditor" name="txtSubsetSQL" native="true"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
Expand Down Expand Up @@ -166,12 +207,24 @@
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsScrollArea</class>
<extends>QScrollArea</extends>
<header>qgsscrollarea.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCodeEditor</class>
<extends>QWidget</extends>
<header>qgscodeeditor.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>cmbConnections</tabstop>
Expand Down

0 comments on commit b17714c

Please sign in to comment.