Skip to content

Commit 7f1efe3

Browse files
authored
Filtering integration 2 (#4448)
1 parent 66ee587 commit 7f1efe3

6 files changed

Lines changed: 36 additions & 19 deletions

File tree

app/filtercontroller.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ void FilterController::clearLayerFilters( const QString &layerId )
4545

4646
void FilterController::clearAllFilters()
4747
{
48-
mFieldFilters.clear();
48+
for ( FieldFilter &filter : mFieldFilters )
49+
{
50+
filter.value.clear();
51+
}
52+
mFilteringEnabled = false;
53+
emit hasFiltersEnabledChanged();
4954

5055
const QMap<QString, QgsMapLayer *> layers = QgsProject::instance()->mapLayers();
5156
for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
@@ -63,14 +68,15 @@ void FilterController::loadFilterConfig( const QgsProject *project )
6368
mFieldFilters.clear();
6469

6570
bool valueRead = false;
66-
const bool filteringEnabled = project->readBoolEntry( QStringLiteral( "Mergin" ), QStringLiteral( "Filtering/Enabled" ), false, &valueRead );
71+
const bool filteringAvailable = project->readBoolEntry( QStringLiteral( "Mergin" ), QStringLiteral( "Filtering/Enabled" ), false, &valueRead );
6772

6873
//return early if filtering is not setup
6974
if ( !valueRead )
7075
{
7176
return;
7277
}
73-
mFilteringEnabled = filteringEnabled;
78+
mFilteringAvailable = filteringAvailable;
79+
emit hasFiltersAvailableChanged();
7480

7581
const QString filtersDef = project->readEntry( QStringLiteral( "Mergin" ), QStringLiteral( "Filtering/Filters" ) );
7682
QJsonParseError jsonError;
@@ -231,6 +237,12 @@ void FilterController::applyFiltersToLayer( QgsVectorLayer *layer )
231237
const QString filterExpr = generateFilterExpression( layer->id() );
232238
const bool success = layer->setSubsetString( filterExpr );
233239

240+
if ( !filterExpr.isEmpty() && success && !mFilteringEnabled )
241+
{
242+
mFilteringEnabled = true;
243+
emit hasFiltersEnabledChanged();
244+
}
245+
234246
qDebug() << "Applied filter to layer" << layer->name() << ":" << filterExpr << "success:" << success;
235247

236248
// Trigger a layer refresh to ensure the filter takes effect
@@ -242,7 +254,9 @@ void FilterController::applyFiltersToLayer( QgsVectorLayer *layer )
242254

243255
void FilterController::applyFiltersToAllLayers()
244256
{
245-
const bool hadFilters = mFilteringEnabled;
257+
// Change filters enabled to false before enabling filters to find out if any are active
258+
mFilteringEnabled = false;
259+
emit hasFiltersEnabledChanged();
246260

247261
const QgsProject *project = QgsProject::instance();
248262
if ( !project )
@@ -257,14 +271,6 @@ void FilterController::applyFiltersToAllLayers()
257271
applyFiltersToLayer( vectorLayer );
258272
}
259273
}
260-
261-
//TODO: probably can be removed
262-
emit filtersChanged();
263-
264-
if ( hadFilters != mFilteringEnabled )
265-
{
266-
emit hasFiltersEnabledChanged();
267-
}
268274
}
269275

270276
bool FilterController::hasFiltersAvailable() const

app/filtercontroller.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ class FilterController : public QObject
123123

124124

125125
signals:
126-
void filtersChanged();
127126
void hasActiveFiltersChanged();
128127
void layerFilterChanged( const QString &layerId );
129128
void hasFiltersAvailableChanged();

app/qml/filters/MMFiltersPanel.qml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ MMComponents.MMDrawer {
5858
text: qsTr( "Reset" )
5959
fontColor: __style.grapeColor
6060
bgndColor: __style.negativeLightColor
61-
bgndColorHover: __style.negativeLightColor
62-
fontColorHover: __style.grapeColor
63-
iconColorHover: __style.grapeColor
61+
bgndColorHover: __style.grapeColor
62+
fontColorHover: __style.negativeLightColor
6463

6564
anchors {
6665
left: parent.left
@@ -70,6 +69,7 @@ MMComponents.MMDrawer {
7069

7170
onClicked: {
7271
__activeProject.filterController.clearAllFilters()
72+
inputRepeater.model = __activeProject.filterController.getFilters()
7373
}
7474
}
7575

app/qml/filters/components/MMFilterTextEditor.qml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ Column {
4545
id: debounceTimer
4646
interval: 300
4747
repeat: false
48-
onTriggered: root.currentValue = [filterInput.text]
48+
onTriggered: {
49+
if (filterInput.text) {
50+
root.currentValue = [filterInput.text]
51+
} else {
52+
root.currentValue = undefined
53+
}
54+
}
4955
}
5056
}

app/qml/filters/components/MMFilterTextInput.qml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ MMPrivateComponents.MMBaseSingleLineInput {
7373
// keep checked in sync with whether the field has a value
7474
onTextChanged: {
7575
if ( root.type === MMFilterTextInput.InputType.Text || root.type === MMFilterTextInput.InputType.Number ) {
76-
root.checked = ( root.text !== "" )
76+
root.checked = false
7777
}
7878
}
7979

@@ -84,4 +84,10 @@ MMPrivateComponents.MMBaseSingleLineInput {
8484
root.checked = false
8585
}
8686
}
87+
88+
Component.onCompleted: {
89+
if ( root.text ) {
90+
root.checked = true
91+
}
92+
}
8793
}

app/qml/map/MMMapController.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ Item {
860860
MMMapButton {
861861
id: filterIndicatorButton
862862

863-
visible: root.state === "view" && __activeProject.filterController?.filteringEnabled
863+
visible: root.state === "view" && __activeProject.filterController?.filteringAvailable
864864
iconSource: __style.filterIcon
865865
bgndColor: __activeProject.filterController?.filteringEnabled ? __style.positiveColor : __style.polarColor
866866

0 commit comments

Comments
 (0)