Skip to content

Commit

Permalink
use list of libraries as a starting window
Browse files Browse the repository at this point in the history
  • Loading branch information
yedpodtrzitko committed May 3, 2024
1 parent 75cfbe2 commit a5441ab
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 30 deletions.
1 change: 0 additions & 1 deletion tagstudio/src/core/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,6 @@ def refresh_dir(self):
if (end_time - start_time) > 0.034:
yield self.dir_file_count
start_time = time.time()
# print('')

# Sorts the files by date modified, descending.
if len(self.files_not_in_library) <= 100000:
Expand Down
1 change: 1 addition & 0 deletions tagstudio/src/core/ts_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
BACKUP_FOLDER_NAME: str = "backups"
COLLAGE_FOLDER_NAME: str = "collages"
LIBRARY_FILENAME: str = "ts_library.json"
SETTINGS_LIBS_LIST: str = "libraries"

# TODO: Turn this whitelist into a user-configurable blacklist.
IMAGE_TYPES: list[str] = [
Expand Down
113 changes: 84 additions & 29 deletions tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@
import sys
import time
from datetime import datetime as dt
from hashlib import sha1
from pathlib import Path
from queue import Empty, Queue
from typing import Optional

from PIL import Image
from PySide6 import QtCore
from PySide6.QtCore import QObject, QThread, Signal, Qt, QThreadPool, QTimer, QSettings
from PySide6.QtCore import QObject, QThread, Signal, Qt, QThreadPool, QSettings
from PySide6.QtGui import (
QGuiApplication,
QPixmap,
QMouseEvent,
QColor,
QAction,
QFontDatabase,
QIcon,
QColor,
)
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import (
Expand All @@ -41,6 +42,8 @@
QFileDialog,
QSplashScreen,
QMenu,
QLabel,
QVBoxLayout,
)
from humanfriendly import format_timespan

Expand Down Expand Up @@ -68,6 +71,7 @@
TS_FOLDER_NAME,
VERSION_BRANCH,
VERSION,
SETTINGS_LIBS_LIST,
)
from src.core.utils.web import strip_web_protocol
from src.qt.flowlayout import FlowLayout
Expand Down Expand Up @@ -171,9 +175,6 @@ def __init__(self, core, args):
self.lib = self.core.lib
self.args = args

# self.main_window = None
# self.main_window = Ui_MainWindow()

self.branch: str = (" (" + VERSION_BRANCH + ")") if VERSION_BRANCH else ""
self.base_title: str = f"TagStudio {VERSION}{self.branch}"
# self.title_text: str = self.base_title
Expand All @@ -190,6 +191,9 @@ def __init__(self, core, args):
QSettings.IniFormat, QSettings.UserScope, "tagstudio", "TagStudio"
)

# flag for whether the library window has been initialized already
self._lib_window_init = False

max_threads = os.cpu_count()
for i in range(max_threads):
# thread = threading.Thread(target=self.consumer, name=f'ThumbRenderer_{i}',args=(), daemon=True)
Expand Down Expand Up @@ -236,9 +240,6 @@ def start(self):

# Handle OS signals
self.setup_signals()
timer = QTimer()
timer.start(500)
timer.timeout.connect(lambda: None)

# self.main_window = loader.load(home_path)
self.main_window = Ui_MainWindow()
Expand Down Expand Up @@ -456,6 +457,69 @@ def start(self):
self.collation_thumb_size = math.ceil(self.thumb_size * 2)
# self.filtered_items: list[tuple[SearchItemType, int]] = []

if self.args.open:
self.open_library(self.args.open)
else:
self.settings.beginGroup(SETTINGS_LIBS_LIST)
lib_items = {x: self.settings.value(x) for x in self.settings.allKeys()}
if lib_items:
self.settings.endGroup()
self.list_libraries(lib_items)
elif self.settings.value("last_library"):
lib = self.settings.value("last_library")
self.splash.showMessage(
f'Opening Library "{lib}"...',
int(Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignHCenter),
QColor("#9782ff"),
)
self.open_library(lib)

app.exec_()

self.shutdown()

def list_libraries(self, libraries: dict[str, str]):
layout = QVBoxLayout()

label = QLabel("Select a library")
label.setFixedHeight(50)
label.setAlignment(Qt.AlignCenter)
layout.addWidget(label)

for lib_key, lib_path in libraries.items():
button = QPushButton(text=lib_path)
button.setObjectName(lib_key)

def open_library_button_clicked(path):
return lambda: self.open_library(path)

button.clicked.connect(open_library_button_clicked(lib_path))
button.setStyleSheet(
"background-color: #ffffff; color: #000000; text-align: left; padding-left: 30px;"
)
button.setFixedHeight(50)
layout.addWidget(button)

# add empty label to fill remaining space
spacer = QLabel()
layout.addWidget(spacer)

flow_container: QWidget = QWidget()
flow_container.setObjectName("flowContainer")
flow_container.setLayout(layout)

sa: QScrollArea = self.main_window.scrollArea
sa.setWidget(flow_container)

self.main_window.show()
self.main_window.activateWindow()
self.splash.finish(self.main_window)

def init_library_window(self):
if self._lib_window_init:
return

self._lib_window_init = True
self._init_thumb_grid()

# TODO: Put this into its own method that copies the font file(s) into memory
Expand Down Expand Up @@ -509,22 +573,6 @@ def start(self):
self.preview_panel.update_widgets()

# Check if a library should be opened on startup, args should override last_library
# TODO: check for behavior (open last, open default, start empty)
if self.args.open or self.settings.contains("last_library"):
if self.args.open:
lib = self.args.open
elif self.settings.value("last_library"):
lib = self.settings.value("last_library")
self.splash.showMessage(
f'Opening Library "{lib}"...',
int(Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignHCenter),
QColor("#9782ff"),
)
self.open_library(lib)

app.exec_()

self.shutdown()

def callback_library_needed_check(self, func):
"""Check if loaded library has valid path before executing the button function"""
Expand Down Expand Up @@ -676,7 +724,7 @@ def add_new_files_callback(self):
iterator.value.connect(lambda x: pw.update_progress(x + 1))
iterator.value.connect(
lambda x: pw.update_label(
f'Scanning Directories for New Files...\n{x+1} File{"s" if x+1 != 1 else ""} Searched, {len(self.lib.files_not_in_library)} New Files Found'
f'Scanning Directories for New Files...\n{x + 1} File{"s" if x + 1 != 1 else ""} Searched, {len(self.lib.files_not_in_library)} New Files Found'
)
)
r = CustomRunnable(lambda: iterator.run())
Expand Down Expand Up @@ -727,7 +775,7 @@ def add_new_files_runnable(self):
iterator.value.connect(lambda x: pw.update_progress(x + 1))
iterator.value.connect(
lambda x: pw.update_label(
f"Running Configured Macros on {x+1}/{len(new_ids)} New Entries"
f"Running Configured Macros on {x + 1}/{len(new_ids)} New Entries"
)
)
r = CustomRunnable(lambda: iterator.run())
Expand Down Expand Up @@ -1260,12 +1308,17 @@ def filter_items(self, query=""):
self.main_window.statusbar.showMessage(
f"{len(all_items)} Results ({format_timespan(end_time - start_time)})"
)
# logging.info(f'Done Filtering! ({(end_time - start_time):.3f}) seconds')

# self.update_thumbs()
def update_libs_list(self, library_path: str):
self.settings.beginGroup(SETTINGS_LIBS_LIST)
path_hash = sha1(library_path.encode("utf-8")).hexdigest()
self.settings.setValue(path_hash, library_path)
self.settings.endGroup()

def open_library(self, path):
def open_library(self, path: str):
"""Opens a TagStudio library."""
self.init_library_window()

if self.lib.library_dir:
self.save_library()
self.lib.clear_internal_vars()
Expand All @@ -1290,6 +1343,8 @@ def open_library(self, path):
print(f"Library Creation Return Code: {self.lib.create_library(path)}")
self.add_new_files_callback()

self.update_libs_list(path)

title_text = f"{self.base_title} - Library '{self.lib.library_dir}'"
self.main_window.setWindowTitle(title_text)

Expand Down

0 comments on commit a5441ab

Please sign in to comment.