fix: prevent error on closing library (#484)

This commit is contained in:
yed
2024-09-10 15:31:33 +07:00
committed by GitHub
parent 2e8efa288d
commit af642a7d29
4 changed files with 31 additions and 22 deletions

View File

@@ -95,8 +95,8 @@ def get_default_tags() -> tuple[Tag, ...]:
class Library:
"""Class for the Library object, and all CRUD operations made upon it."""
library_dir: Path
storage_path: Path | str
library_dir: Path | None = None
storage_path: Path | str | None
engine: Engine | None
folder: Folder | None
@@ -105,6 +105,13 @@ class Library:
missing_tracker: "MissingRegistry"
dupe_tracker: "DupeRegistry"
def close(self):
if self.engine:
self.engine.dispose()
self.library_dir = None
self.storage_path = None
self.folder = None
def open_library(
self, library_dir: Path | str, storage_path: str | None = None
) -> None:

View File

@@ -323,7 +323,7 @@ class QtDriver(QObject):
file_menu.addSeparator()
close_library_action = QAction("&Close Library", menu_bar)
close_library_action.triggered.connect(lambda: self.close_library())
close_library_action.triggered.connect(self.close_library)
file_menu.addAction(close_library_action)
# Edit Menu ============================================================
@@ -553,9 +553,7 @@ class QtDriver(QObject):
def shutdown(self):
"""Save Library on Application Exit"""
if self.lib and self.lib.library_dir:
self.settings.setValue(SettingItems.LAST_LIBRARY, self.lib.library_dir)
self.settings.sync()
self.close_library(is_shutdown=True)
logger.info("[SHUTDOWN] Ending Thumbnail Threads...")
for _ in self.thumb_threads:
self.thumb_job_queue.put(Consumer.MARKER_QUIT)
@@ -567,7 +565,7 @@ class QtDriver(QObject):
QApplication.quit()
def close_library(self):
def close_library(self, is_shutdown: bool = False):
if not self.lib.library_dir:
logger.info("No Library to Close")
return
@@ -575,15 +573,21 @@ class QtDriver(QObject):
logger.info("Closing Library...")
self.main_window.statusbar.showMessage("Closing Library...")
start_time = time.time()
self.settings.setValue(SettingItems.LAST_LIBRARY, self.lib.library_dir)
self.settings.sync()
title_text = f"{self.base_title}"
self.main_window.setWindowTitle(title_text)
self.lib.close()
if is_shutdown:
# no need to do other things on shutdown
return
self.main_window.setWindowTitle(self.base_title)
self.selected = []
self.frame_content = []
self.item_thumbs = []
[x.set_mode(None) for x in self.item_thumbs]
self.preview_panel.update_widgets()
self.main_window.toggle_landing_page(True)
@@ -841,15 +845,10 @@ class QtDriver(QObject):
self.item_thumbs[grid_idx].hide()
def _init_thumb_grid(self):
# logger.info('Initializing Thumbnail Grid...')
layout = FlowLayout()
layout.setGridEfficiency(True)
# layout.setContentsMargins(0,0,0,0)
layout.setSpacing(min(self.thumb_size // 10, 12))
# layout = QHBoxLayout()
# layout.setSizeConstraint(QLayout.SizeConstraint.SetMaximumSize)
# layout = QListView()
# layout.setViewMode(QListView.ViewMode.IconMode)
layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
# TODO - init after library is loaded, it can have different page_size
for grid_idx in range(self.filter.page_size):
@@ -862,7 +861,6 @@ class QtDriver(QObject):
self.flow_container: QWidget = QWidget()
self.flow_container.setObjectName("flowContainer")
self.flow_container.setLayout(layout)
layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
sa: QScrollArea = self.main_window.scrollArea
sa.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
sa.setWidgetResizable(True)

View File

@@ -105,8 +105,6 @@ def qt_driver(qtbot, library):
open = pathlib.Path(tmp_dir)
ci = True
# patch CustomRunnable
with patch("src.qt.ts_qt.Consumer"), patch("src.qt.ts_qt.CustomRunnable"):
driver = QtDriver(backend, Args())

View File

@@ -1,6 +1,7 @@
from pathlib import Path
from unittest.mock import Mock
from src.core.library import Entry
from src.core.library.alchemy.enums import FilterState
from src.core.library.json.library import ItemType
@@ -106,6 +107,11 @@ def test_close_library(qt_driver):
qt_driver.close_library()
# Then
assert len(qt_driver.frame_content) == 0
assert len(qt_driver.item_thumbs) == 0
assert qt_driver.selected == []
assert qt_driver.lib.library_dir is None
assert not qt_driver.frame_content
assert not qt_driver.selected
assert not any(x.mode for x in qt_driver.item_thumbs)
# close library again to see there's no error
qt_driver.close_library()
qt_driver.close_library(is_shutdown=True)