Skip to content

Commit

Permalink
sort libraries by most recent
Browse files Browse the repository at this point in the history
  • Loading branch information
yedpodtrzitko committed May 13, 2024
1 parent cce2965 commit 10a09af
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
6 changes: 6 additions & 0 deletions tagstudio/src/core/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ class SettingItems(str, enum.Enum):
LAST_LIBRARY = "last_library"
LIBS_LIST = "libs_list"
WINDOW_SHOW_LIBS = "window_show_libs"


class Theme(str, enum.Enum):
COLOR_BG = "#65000000"
COLOR_HOVER = "#65AAAAAA"
COLOR_PRESSED = "#65EEEEEE"
31 changes: 25 additions & 6 deletions tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
FoldersToTagsModal,
)

# this import has side-effect of import PySide resources
import src.qt.resources_rc # pylint: disable=unused-import

# SIGQUIT is not defined on Windows
if sys.platform == "win32":
Expand Down Expand Up @@ -431,7 +433,7 @@ def start(self):
macros_menu.addAction(self.sort_fields_action)

folders_to_tags_action = QAction("Create Tags From Folders", menu_bar)
show_libs_list_action = QAction("Show Libraries List", menu_bar)
show_libs_list_action = QAction("Show Recent Libraries", menu_bar)
show_libs_list_action.setCheckable(True)
show_libs_list_action.setChecked(
self.settings.value(SettingItems.WINDOW_SHOW_LIBS, True, type=bool)
Expand Down Expand Up @@ -1341,12 +1343,29 @@ def filter_items(self, query=""):

def update_libs_list(self, path: str):
# add library to list in SettingItems.LIBS_LIST
ITEMS_LIMIT = 5

self.settings.beginGroup(SettingItems.LIBS_LIST)
# check if item is already present in libs_list
path_hash = sha1(path.encode("utf-8")).hexdigest()
libs_list = self.settings.value(SettingItems.LIBS_LIST) or []
if path_hash not in libs_list:
self.settings.setValue(path_hash, path)

current_time = str(time.time())
all_libs = {current_time: path}

for item_key in self.settings.allKeys():
item_path = self.settings.value(item_key)
if item_path != path:
all_libs[item_key] = item_path

# sort items by the most recent first
all_libs = sorted(all_libs.items(), key=lambda item: item[0], reverse=True)

# remove previously saved items
self.settings.clear()

for idx, (item_key, item_value) in enumerate(all_libs, start=1):
if idx > ITEMS_LIMIT:
break
self.settings.setValue(item_key, item_value)

self.settings.endGroup()
self.settings.sync()

Expand Down
55 changes: 37 additions & 18 deletions tagstudio/src/qt/widgets/preview_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)
from humanfriendly import format_size

from src.core.enums import SettingItems
from src.core.enums import SettingItems, Theme
from src.core.library import Entry, ItemType, Library
from src.core.ts_core import VIDEO_TYPES, IMAGE_TYPES
from src.qt.helpers import FileOpenerLabel, FileOpenerHelper, open_file
Expand Down Expand Up @@ -123,7 +123,7 @@ def __init__(self, library: Library, driver: "QtDriver"):
# Qt.TextInteractionFlag.TextSelectableByMouse)

properties_style = (
f"background-color:#65000000;"
f"background-color:{Theme.COLOR_BG};"
f"font-family:Oxanium;"
f"font-weight:bold;"
f"font-size:12px;"
Expand Down Expand Up @@ -163,10 +163,10 @@ def __init__(self, library: Library, driver: "QtDriver"):
# rounded corners are maintained when scrolling. I was unable to
# find the right trick to only select that particular element.
scroll_area.setStyleSheet(
f"QWidget#entryScrollContainer{{"
"background:#65000000;"
"QWidget#entryScrollContainer{"
f"background: {Theme.COLOR_BG};"
"border-radius:6px;"
f"}}"
"}"
)
scroll_area.setWidget(scroll_container)

Expand Down Expand Up @@ -229,43 +229,62 @@ def __init__(self, library: Library, driver: "QtDriver"):
def fill_libs_widget(self, layout: QVBoxLayout):
settings = self.driver.settings
settings.beginGroup(SettingItems.LIBS_LIST)
lib_items = {}
for key in settings.allKeys():
val = settings.value(key)
lib_items: dict[str, tuple[str, str]] = {}
for item_tstamp in settings.allKeys():
val = settings.value(item_tstamp)
cut_val = val
if len(val) > 45:
val = f"{val[0:10]} ... {val[-10:]}"
lib_items[key] = val
cut_val = f"{val[0:10]} ... {val[-10:]}"
lib_items[item_tstamp] = (val, cut_val)

settings.endGroup()

new_keys = set(lib_items.keys())
if new_keys == self.render_libs:
# no need to re-render
return

# sort lib_items by the key
libs_sorted = sorted(lib_items.items(), key=lambda item: item[0], reverse=True)

self.render_libs = new_keys
return self._fill_libs_widget(lib_items, layout)
return self._fill_libs_widget(libs_sorted, layout)

def _fill_libs_widget(self, libraries: dict[str, str], layout: QVBoxLayout):
def _fill_libs_widget(
self, libraries: list[tuple[str, tuple[str, str]]], layout: QVBoxLayout
):
# remove any potential previous items
for idx in reversed(range(layout.count())):
widget = layout.itemAt(idx).widget()
layout.removeWidget(widget)
# remove from GUI
widget.setParent(None)

label = QLabel("Select a library")
label = QLabel("Recent Libraries")
label.setAlignment(Qt.AlignCenter)
layout.addWidget(label)

for lib_key, lib_path in libraries.items():
button = QPushButton(text=lib_path)
button.setObjectName(lib_key)
for tstamp, (full_val, cut_val) in libraries:
button = QPushButton(text=cut_val)
button.setObjectName(f"path{tstamp}")

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

button.clicked.connect(open_library_button_clicked(lib_path))
button.clicked.connect(open_library_button_clicked(full_val))
button.setStyleSheet(
"background-color: #ffffff; color: #000000; text-align: left; padding-left: 10px;"
"QPushButton{"
f"background-color:{Theme.COLOR_BG};"
"border-radius:6px;"
"text-align: left;"
"padding-top: 3px;"
"padding-left: 6px;"
"padding-bottom: 4px;"
"}"
f"QPushButton::hover{{background-color:{Theme.COLOR_HOVER};}}"
f"QPushButton::pressed{{background-color:{Theme.COLOR_PRESSED};}}"
)
button.setCursor(Qt.CursorShape.PointingHandCursor)
layout.addWidget(button)

def resizeEvent(self, event: QResizeEvent) -> None:
Expand Down

0 comments on commit 10a09af

Please sign in to comment.