Skip to content

Commit 2c128b4

Browse files
committed
fix error on closing library
1 parent 2e8efa2 commit 2c128b4

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

tagstudio/src/core/library/alchemy/library.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,20 @@ class Library:
9898
library_dir: Path
9999
storage_path: Path | str
100100
engine: Engine | None
101-
folder: Folder | None
101+
folder: Folder | None = None
102102

103103
ignored_extensions: list[str]
104104

105105
missing_tracker: "MissingRegistry"
106106
dupe_tracker: "DupeRegistry"
107107

108+
def close(self):
109+
if self.engine:
110+
self.engine.dispose()
111+
self.folder = None
112+
del self.library_dir
113+
del self.storage_path
114+
108115
def open_library(
109116
self, library_dir: Path | str, storage_path: str | None = None
110117
) -> None:

tagstudio/src/qt/ts_qt.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def start(self) -> None:
323323
file_menu.addSeparator()
324324

325325
close_library_action = QAction("&Close Library", menu_bar)
326-
close_library_action.triggered.connect(lambda: self.close_library())
326+
close_library_action.triggered.connect(self.close_library)
327327
file_menu.addAction(close_library_action)
328328

329329
# Edit Menu ============================================================
@@ -553,9 +553,7 @@ def handleSIGTERM(self):
553553

554554
def shutdown(self):
555555
"""Save Library on Application Exit"""
556-
if self.lib and self.lib.library_dir:
557-
self.settings.setValue(SettingItems.LAST_LIBRARY, self.lib.library_dir)
558-
self.settings.sync()
556+
self.close_library(is_shutdown=True)
559557
logger.info("[SHUTDOWN] Ending Thumbnail Threads...")
560558
for _ in self.thumb_threads:
561559
self.thumb_job_queue.put(Consumer.MARKER_QUIT)
@@ -567,23 +565,29 @@ def shutdown(self):
567565

568566
QApplication.quit()
569567

570-
def close_library(self):
571-
if not self.lib.library_dir:
568+
def close_library(self, is_shutdown: bool = False):
569+
if not self.lib.folder:
572570
logger.info("No Library to Close")
573571
return
574572

575573
logger.info("Closing Library...")
576574
self.main_window.statusbar.showMessage("Closing Library...")
577575
start_time = time.time()
576+
578577
self.settings.setValue(SettingItems.LAST_LIBRARY, self.lib.library_dir)
579578
self.settings.sync()
580579

581-
title_text = f"{self.base_title}"
582-
self.main_window.setWindowTitle(title_text)
580+
self.lib.close()
581+
582+
if is_shutdown:
583+
# no need to do other things on shutdown
584+
return
585+
586+
self.main_window.setWindowTitle(self.base_title)
583587

584588
self.selected = []
585589
self.frame_content = []
586-
self.item_thumbs = []
590+
[x.set_mode(None) for x in self.item_thumbs]
587591

588592
self.preview_panel.update_widgets()
589593
self.main_window.toggle_landing_page(True)
@@ -841,15 +845,10 @@ def remove_grid_item(self, grid_idx: int):
841845
self.item_thumbs[grid_idx].hide()
842846

843847
def _init_thumb_grid(self):
844-
# logger.info('Initializing Thumbnail Grid...')
845848
layout = FlowLayout()
846849
layout.setGridEfficiency(True)
847-
# layout.setContentsMargins(0,0,0,0)
848850
layout.setSpacing(min(self.thumb_size // 10, 12))
849-
# layout = QHBoxLayout()
850-
# layout.setSizeConstraint(QLayout.SizeConstraint.SetMaximumSize)
851-
# layout = QListView()
852-
# layout.setViewMode(QListView.ViewMode.IconMode)
851+
layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
853852

854853
# TODO - init after library is loaded, it can have different page_size
855854
for grid_idx in range(self.filter.page_size):
@@ -862,7 +861,6 @@ def _init_thumb_grid(self):
862861
self.flow_container: QWidget = QWidget()
863862
self.flow_container.setObjectName("flowContainer")
864863
self.flow_container.setLayout(layout)
865-
layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
866864
sa: QScrollArea = self.main_window.scrollArea
867865
sa.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
868866
sa.setWidgetResizable(True)

tagstudio/tests/conftest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ class Args:
105105
open = pathlib.Path(tmp_dir)
106106
ci = True
107107

108-
# patch CustomRunnable
109-
110108
with patch("src.qt.ts_qt.Consumer"), patch("src.qt.ts_qt.CustomRunnable"):
111109
driver = QtDriver(backend, Args())
112110

tagstudio/tests/qt/test_driver.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pathlib import Path
22
from unittest.mock import Mock
33

4+
45
from src.core.library import Entry
56
from src.core.library.alchemy.enums import FilterState
67
from src.core.library.json.library import ItemType
@@ -106,6 +107,10 @@ def test_close_library(qt_driver):
106107
qt_driver.close_library()
107108

108109
# Then
109-
assert len(qt_driver.frame_content) == 0
110-
assert len(qt_driver.item_thumbs) == 0
111-
assert qt_driver.selected == []
110+
assert qt_driver.lib.folder is None
111+
assert not qt_driver.frame_content
112+
assert not qt_driver.selected
113+
114+
# close library again to see there's no error
115+
qt_driver.close_library()
116+
qt_driver.close_library(is_shutdown=True)

0 commit comments

Comments
 (0)