Add function to update search filter when the user types

Seems to work fine.
The pyqtSignal is not technically necessary unless we display this filter in the interface anywhere. We don't currently. If the signal is not present it would complain about non-notifyable properties. And making it a slot instead of a signal seems a bit unintuitive to me in how the field is being used.

Contributes to issue CURA-8009.
This commit is contained in:
Ghostkeeper 2021-07-12 17:41:10 +02:00
parent 22826bc4d8
commit 65f7eb5ea8
No known key found for this signature in database
GPG Key ID: D2A8871EE34EC59A
2 changed files with 40 additions and 1 deletions

View File

@ -48,6 +48,8 @@ Item
id: searchBar
Layout.fillWidth: true
height: createNewProjectButton.height
onTextEdited: manager.projectFilter = text
}
Cura.SecondaryButton

View File

@ -1,4 +1,6 @@
# Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
import json
import math
import os
@ -8,7 +10,7 @@ from enum import IntEnum
from pathlib import Path
from typing import Optional, List, Dict, Any, cast
from PyQt5.QtCore import pyqtSignal, QObject, pyqtSlot, pyqtProperty, Q_ENUMS, QUrl
from PyQt5.QtCore import pyqtSignal, QObject, pyqtSlot, pyqtProperty, Q_ENUMS, QTimer, QUrl
from PyQt5.QtNetwork import QNetworkReply
from PyQt5.QtQml import qmlRegisterType, qmlRegisterUncreatableType
@ -116,6 +118,11 @@ class DigitalFactoryController(QObject):
self._project_model = DigitalFactoryProjectModel()
self._selected_project_idx = -1
self._project_creation_error_text = "Something went wrong while creating a new project. Please try again."
self._project_filter = ""
self._project_filter_change_timer = QTimer()
self._project_filter_change_timer.setInterval(1000)
self._project_filter_change_timer.setSingleShot(True)
self._project_filter_change_timer.timeout.connect(self._applyProjectFilter)
# Initialize the file model
self._file_model = DigitalFactoryFileModel()
@ -302,6 +309,36 @@ class DigitalFactoryController(QObject):
self._selected_file_indices = file_indices
self.selectedFileIndicesChanged.emit(file_indices)
def setProjectFilter(self, new_filter) -> None:
"""
Called when the user wants to change the search filter for projects.
The filter is not immediately applied. There is some delay to allow the user to finish typing.
:param new_filter: The new filter that the user wants to apply.
"""
self._project_filter = new_filter
self._project_filter_change_timer.start()
"""
Signal to notify Qt that the applied filter has changed.
"""
projectFilterChanged = pyqtSignal()
@pyqtProperty(str, notify = projectFilterChanged, fset = setProjectFilter)
def projectFilter(self) -> str:
"""
The current search filter being applied to the project list.
:return: The current search filter being applied to the project list.
"""
return self._project_filter
def _applyProjectFilter(self):
"""
Actually apply the current filter to search for projects with the user-defined search string.
:return:
"""
self.projectFilterChanged.emit()
@pyqtProperty(QObject, constant = True)
def digitalFactoryProjectModel(self) -> "DigitalFactoryProjectModel":
return self._project_model