Commit 7b491c2e authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][assetwidget] Use a debounce timer to update

This avoids reloading the json_widget content immediately on
each keyboard input or mouse event (i.e. combobox mouse wheel
update)
parent 8867456d
......@@ -89,13 +89,13 @@ class TestAssetWidget:
def test_dirty(self, qtbot, monkeypatch, test_prefix, asset_type_prefix_entry_map):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_prefix_root(test_prefix)
description = "dummy description"
# Question about saving editor content after modification before a new json is loaded
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.No)
asset_widget.set_prefix_root(test_prefix)
for (
asset_type,
(asset_name, editor_type),
......@@ -106,7 +106,7 @@ class TestAssetWidget:
)
assert not asset_widget.current_editor.is_dirty()
with qtbot.waitSignal(asset_widget.current_editor.dataChanged):
with qtbot.waitSignal(asset_widget.json_widget.textChanged):
qtbot.keyClicks(
asset_widget.current_editor.description_lineedit, description
)
......@@ -120,10 +120,10 @@ class TestAssetWidget:
def test_save(self, qtbot, monkeypatch, test_prefix, asset_type_prefix_entry_map):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
description = "dummy description"
asset_widget.set_prefix_root(test_prefix)
description = "dummy description"
for (
asset_type,
(asset_name, editor_type),
......@@ -132,7 +132,7 @@ class TestAssetWidget:
asset_path = self.get_asset_path(test_prefix, asset_type, asset_name)
asset_widget.load_json(asset_path)
with qtbot.waitSignal(asset_widget.current_editor.dataChanged):
with qtbot.waitSignal(asset_widget.json_widget.textChanged):
qtbot.keyClicks(
asset_widget.current_editor.description_lineedit, description
)
......
......@@ -27,6 +27,7 @@ import simplejson as json
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import QFileSystemWatcher
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTabWidget
......@@ -118,6 +119,9 @@ class AssetWidget(QWidget):
self.current_json = None
self.watcher = QFileSystemWatcher()
self.update_timer = QTimer()
self.update_timer.setSingleShot(True)
self.update_timer.setInterval(200)
self.json_widget = QTextEdit()
self.json_widget.setReadOnly(True)
......@@ -147,6 +151,8 @@ class AssetWidget(QWidget):
layout.addLayout(button_layout)
self.watcher.fileChanged.connect(self.__reload_from_harddrive)
self.update_timer.timeout.connect(self.__enable_save)
self.update_timer.timeout.connect(self.__update_json_widget)
self.save_button.clicked.connect(self.save_json)
@property
......@@ -170,7 +176,7 @@ class AssetWidget(QWidget):
"""Update the content of this widget"""
try:
self.current_editor.dataChanged.disconnect(self)
self.current_editor.dataChanged.disconnect(self.update_timer)
except TypeError:
# Nothing was connected yet
pass
......@@ -181,8 +187,7 @@ class AssetWidget(QWidget):
asset_type = self.__asset_type_for_path(self.current_json)
editor = self.editors_type[asset_type]
editor.load_json(json.loads(json_data))
editor.dataChanged.connect(self.__enable_save)
editor.dataChanged.connect(self.__update_json_widget)
editor.dataChanged.connect(self.update_timer.start)
self.editors.setCurrentWidget(editor)
self.save_button.setEnabled(False)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment