Skip to content

Commit

Permalink
Merge pull request #6704 from slarosa/filter_values_qb
Browse files Browse the repository at this point in the history
[FEATURE][needs-docs] add filter line edit to query builder
  • Loading branch information
slarosa committed Apr 2, 2018
2 parents 3408e02 + 5810236 commit ca4bfb1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 46 deletions.
28 changes: 15 additions & 13 deletions src/gui/qgsquerybuilder.cpp
Expand Up @@ -73,6 +73,10 @@ QgsQueryBuilder::QgsQueryBuilder( QgsVectorLayer *layer,
lblDataUri->setText( tr( "Set provider filter on %1" ).arg( layer->name() ) );
txtSQL->setText( mOrigSubsetString );

mFilterLineEdit->setShowSearchIcon( true );
mFilterLineEdit->setPlaceholderText( tr( "Search" ) );
connect( mFilterLineEdit, &QgsFilterLineEdit::textChanged, this, &QgsQueryBuilder::onTextChanged );

populateFields();
}

Expand Down Expand Up @@ -118,6 +122,8 @@ void QgsQueryBuilder::setupGuiViews()
//Initialize the models
mModelFields = new QStandardItemModel();
mModelValues = new QStandardItemModel();
mProxyValues = new QSortFilterProxyModel();
mProxyValues->setSourceModel( mModelValues );
// Modes
lstFields->setViewMode( QListView::ListMode );
lstValues->setViewMode( QListView::ListMode );
Expand All @@ -129,6 +135,7 @@ void QgsQueryBuilder::setupGuiViews()
// Colored rows
lstFields->setAlternatingRowColors( true );
lstValues->setAlternatingRowColors( true );
lstValues->setModel( mProxyValues );
}

void QgsQueryBuilder::fillValues( int idx, int limit )
Expand Down Expand Up @@ -172,14 +179,8 @@ void QgsQueryBuilder::btnSampleValues_clicked()
mLayer->setSubsetString( QLatin1String( "" ) );
}

//delete connection mModelValues and lstValues
QStandardItemModel *tmp = new QStandardItemModel();
lstValues->setModel( tmp );
//Clear and fill the mModelValues
fillValues( mModelFields->data( lstFields->currentIndex(), Qt::UserRole + 1 ).toInt(), 25 );
lstValues->setModel( mModelValues );
//delete the tmp
delete tmp;

if ( prevSubsetString != mLayer->subsetString() )
{
Expand All @@ -199,14 +200,8 @@ void QgsQueryBuilder::btnGetAllValues_clicked()
mLayer->setSubsetString( QLatin1String( "" ) );
}

//delete connection mModelValues and lstValues
QStandardItemModel *tmp = new QStandardItemModel();
lstValues->setModel( tmp );
//Clear and fill the mModelValues
fillValues( mModelFields->data( lstFields->currentIndex(), Qt::UserRole + 1 ).toInt(), -1 );
lstValues->setModel( mModelValues );
//delete the tmp
delete tmp;

if ( prevSubsetString != mLayer->subsetString() )
{
Expand Down Expand Up @@ -340,6 +335,7 @@ void QgsQueryBuilder::lstFields_clicked( const QModelIndex &index )
btnGetAllValues->setEnabled( true );

mModelValues->clear();
mFilterLineEdit->clear();
}
}

Expand All @@ -351,7 +347,7 @@ void QgsQueryBuilder::lstFields_doubleClicked( const QModelIndex &index )

void QgsQueryBuilder::lstValues_doubleClicked( const QModelIndex &index )
{
QVariant value = mModelValues->data( index, Qt::UserRole + 1 );
QVariant value = index.data( Qt::DisplayRole );
if ( value.isNull() )
txtSQL->insertText( QStringLiteral( "NULL" ) );
else if ( value.type() == QVariant::Date && mLayer->providerType() == QLatin1String( "ogr" ) && mLayer->storageType() == QLatin1String( "ESRI Shapefile" ) )
Expand Down Expand Up @@ -400,6 +396,12 @@ void QgsQueryBuilder::btnOr_clicked()
txtSQL->setFocus();
}

void QgsQueryBuilder::onTextChanged( const QString &text )
{
mProxyValues->setFilterCaseSensitivity( Qt::CaseInsensitive );
mProxyValues->setFilterWildcard( text );
}

void QgsQueryBuilder::clear()
{
txtSQL->clear();
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgsquerybuilder.h
Expand Up @@ -18,6 +18,7 @@
#include "qgis.h"
#include <vector>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include <QStandardItem>
#include <QModelIndex>
#include "ui_qgsquerybuilderbase.h"
Expand Down Expand Up @@ -94,6 +95,7 @@ class GUI_EXPORT QgsQueryBuilder : public QDialog, private Ui::QgsQueryBuilderBa
void btnAnd_clicked();
void btnNot_clicked();
void btnOr_clicked();
void onTextChanged( const QString &text );

/**
* Get all distinct values for the field. Values are inserted
Expand Down Expand Up @@ -129,6 +131,8 @@ class GUI_EXPORT QgsQueryBuilder : public QDialog, private Ui::QgsQueryBuilderBa
QStandardItemModel *mModelFields = nullptr;
//! Model for values ListView
QStandardItemModel *mModelValues = nullptr;
//! Filter proxy Model for values ListView
QSortFilterProxyModel *mProxyValues = nullptr;
//! Previous field row to delete model
int mPreviousFieldRow;

Expand Down
109 changes: 76 additions & 33 deletions src/ui/qgsquerybuilderbase.ui
Expand Up @@ -23,8 +23,7 @@
</property>
<property name="windowIcon">
<iconset>
<normaloff/>
</iconset>
<normaloff>.</normaloff>.</iconset>
</property>
<property name="modal">
<bool>true</bool>
Expand Down Expand Up @@ -57,7 +56,16 @@
<string>Fields</string>
</property>
<layout class="QGridLayout">
<property name="margin">
<property name="leftMargin">
<number>11</number>
</property>
<property name="topMargin">
<number>11</number>
</property>
<property name="rightMargin">
<number>11</number>
</property>
<property name="bottomMargin">
<number>11</number>
</property>
<item row="0" column="0">
Expand All @@ -83,29 +91,42 @@ p, li { white-space: pre-wrap; }
<string>Values</string>
</property>
<layout class="QGridLayout">
<property name="margin">
<property name="leftMargin">
<number>11</number>
</property>
<item row="0" column="0" colspan="2">
<widget class="QListView" name="lstValues">
<property name="whatsThis">
<property name="topMargin">
<number>11</number>
</property>
<property name="rightMargin">
<number>11</number>
</property>
<property name="bottomMargin">
<number>11</number>
</property>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="mUseUnfilteredLayer">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Use unfiltered layer</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="btnGetAllValues">
<property name="toolTip">
<string>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;List of values for the current field.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Retrieve &lt;span style=&quot; font-weight:600;&quot;&gt;all&lt;/span&gt; the record in the vector file (&lt;span style=&quot; font-style:italic;&quot;&gt;if the table is big, the operation can consume some time&lt;/span&gt;)&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
<property name="text">
<string>All</string>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QPushButton" name="btnSampleValues">
<property name="toolTip">
<string>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
Expand All @@ -118,29 +139,28 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="btnGetAllValues">
<property name="toolTip">
<item row="1" column="0" colspan="2">
<widget class="QListView" name="lstValues">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Retrieve &lt;span style=&quot; font-weight:600;&quot;&gt;all&lt;/span&gt; the record in the vector file (&lt;span style=&quot; font-style:italic;&quot;&gt;if the table is big, the operation can consume some time&lt;/span&gt;)&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;List of values for the current field.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>All</string>
<property name="autoFillBackground">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="mUseUnfilteredLayer">
<property name="enabled">
<bool>false</bool>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="text">
<string>Use unfiltered layer</string>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QgsFilterLineEdit" name="mFilterLineEdit"/>
</item>
</layout>
</widget>
</widget>
Expand All @@ -152,7 +172,16 @@ p, li { white-space: pre-wrap; }
<string>Operators</string>
</property>
<layout class="QGridLayout">
<property name="margin">
<property name="leftMargin">
<number>11</number>
</property>
<property name="topMargin">
<number>11</number>
</property>
<property name="rightMargin">
<number>11</number>
</property>
<property name="bottomMargin">
<number>11</number>
</property>
<item row="0" column="3">
Expand Down Expand Up @@ -281,7 +310,16 @@ p, li { white-space: pre-wrap; }
<string>Provider specific filter expression</string>
</property>
<layout class="QGridLayout">
<property name="margin">
<property name="leftMargin">
<number>11</number>
</property>
<property name="topMargin">
<number>11</number>
</property>
<property name="rightMargin">
<number>11</number>
</property>
<property name="bottomMargin">
<number>11</number>
</property>
<item row="0" column="0">
Expand All @@ -308,6 +346,11 @@ p, li { white-space: pre-wrap; }
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsFilterLineEdit</class>
<extends>QLineEdit</extends>
<header>qgsfilterlineedit.h</header>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
Expand Down

0 comments on commit ca4bfb1

Please sign in to comment.