Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error on closing library #484

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
28 changes: 13 additions & 15 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,29 @@ def shutdown(self):

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

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)
Expand Down Expand Up @@ -841,15 +845,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 +861,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
12 changes: 9 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,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)