Skip to content

Commit

Permalink
fix error on closing library
Browse files Browse the repository at this point in the history
  • Loading branch information
yedpodtrzitko committed Sep 10, 2024
1 parent 2e8efa2 commit 899a26f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
9 changes: 8 additions & 1 deletion tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,20 @@ class Library:
library_dir: Path
storage_path: Path | str
engine: Engine | None
folder: Folder | None
folder: Folder | None = None

ignored_extensions: list[str]

missing_tracker: "MissingRegistry"
dupe_tracker: "DupeRegistry"

def close(self):
if self.engine:
self.engine.dispose()
self.folder = None
del self.library_dir
del self.storage_path

def open_library(
self, library_dir: Path | str, storage_path: str | None = None
) -> None:
Expand Down
29 changes: 13 additions & 16 deletions tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def start(self) -> None:
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 ============================================================
Expand Down Expand Up @@ -553,9 +553,7 @@ def handleSIGTERM(self):

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)
Expand All @@ -567,23 +565,28 @@ def shutdown(self):

QApplication.quit()

def close_library(self):
if not self.lib.library_dir:
def close_library(self, is_shutdown: bool = False):
if not self.lib.folder:
logger.info("No Library to Close")
return

logger.info("Closing Library...")
self.main_window.statusbar.showMessage("Closing Library...")
start_time = time.time()

self.lib.close()

self.settings.setValue(SettingItems.LAST_LIBRARY, self.lib.library_dir)
self.settings.sync()
if is_shutdown:
# no need to do other things on shutdown
return

title_text = f"{self.base_title}"
self.main_window.setWindowTitle(title_text)
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)
Expand Down Expand Up @@ -841,15 +844,10 @@ def remove_grid_item(self, grid_idx: int):
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):
Expand All @@ -862,7 +860,6 @@ def _init_thumb_grid(self):
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)
Expand Down
2 changes: 0 additions & 2 deletions tagstudio/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ class Args:
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())

Expand Down
11 changes: 8 additions & 3 deletions tagstudio/tests/qt/test_driver.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -106,6 +107,10 @@ 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.folder is None
assert not qt_driver.frame_content
assert not qt_driver.selected

# close library again to see there's no error
qt_driver.close_library()
qt_driver.close_library(is_shutdown=True)

0 comments on commit 899a26f

Please sign in to comment.