diff --git a/cromulant/config.py b/cromulant/config.py index bb7d00f..3085e43 100644 --- a/cromulant/config.py +++ b/cromulant/config.py @@ -52,6 +52,7 @@ class Config: settings_json: Path countries_json: Path filter_width: int = 150 + filter_debouncer_delay: int = 200 @staticmethod def prepare() -> None: diff --git a/cromulant/filter.py b/cromulant/filter.py index 209ed60..3c55a96 100644 --- a/cromulant/filter.py +++ b/cromulant/filter.py @@ -2,17 +2,33 @@ from __future__ import annotations from PySide6.QtWidgets import QWidget # type: ignore from PySide6.QtGui import QKeyEvent # type: ignore +from PySide6.QtCore import QTimer +from .config import Config from .window import Window class Filter: + debouncer: QTimer + + @staticmethod + def prepare() -> None: + Filter.debouncer = QTimer() + Filter.debouncer.setSingleShot(True) + Filter.debouncer.setInterval(Config.filter_debouncer_delay) + Filter.debouncer.timeout.connect(Filter.do_filter) + @staticmethod def get_value() -> str: return str(Window.filter.text()).lower().strip() @staticmethod def filter(event: QKeyEvent | None = None) -> None: + Filter.debouncer.stop() + Filter.debouncer.start() + + @staticmethod + def do_filter() -> None: value = Filter.get_value() for i in range(Window.view.count()): diff --git a/cromulant/game.py b/cromulant/game.py index e4a66c5..a05ffc5 100644 --- a/cromulant/game.py +++ b/cromulant/game.py @@ -273,7 +273,7 @@ class Game: @staticmethod def restart() -> None: - opts = ["25", "50", "100", "200", "300", "400", "500"] + opts = ["25", "50", "100", "200", "300"] opts = [f"{opt} ants" for opt in opts] size = Window.prompt_combobox("Size of the population", opts, 2) diff --git a/cromulant/main.py b/cromulant/main.py index 3c77a84..70250d9 100644 --- a/cromulant/main.py +++ b/cromulant/main.py @@ -6,6 +6,7 @@ from .ants import Ants from .window import Window from .game import Game from .settings import Settings +from .filter import Filter def main() -> None: @@ -14,6 +15,7 @@ def main() -> None: Window.prepare() Ants.prepare() Settings.prepare() + Filter.prepare() Game.prepare() Game.start_loop() Window.start()