Skip to content

Commit 037ebde

Browse files
committed
feat: settings menu MVP
1 parent 11d0553 commit 037ebde

File tree

12 files changed

+184
-114
lines changed

12 files changed

+184
-114
lines changed

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12.8

requirements.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ PySide6==6.8.0.1
1414
rawpy==0.22.0
1515
SQLAlchemy==2.0.34
1616
structlog==24.4.0
17-
typing_extensions>=3.10.0.0,<=4.11.0
17+
typing_extensions
1818
ujson>=5.8.0,<=5.9.0
1919
vtf2img==0.1.0
20-
toml==0.10.2
20+
toml==0.10.2
21+
appdirs==1.4.4
22+
pydantic==2.10.4

tagstudio/src/core/driver.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from pathlib import Path
22

33
import structlog
4-
from PySide6.QtCore import QSettings
54
from src.core.constants import TS_FOLDER_NAME
6-
from src.core.enums import SettingItems
75
from src.core.library.alchemy.library import LibraryStatus
6+
from src.core.settings import TSSettings
7+
from src.core.tscacheddata import TSCachedData
88

99
logger = structlog.get_logger(__name__)
1010

1111

1212
class DriverMixin:
13-
settings: QSettings
13+
settings: TSSettings
14+
cache: TSCachedData
1415

1516
def evaluate_path(self, open_path: str | None) -> LibraryStatus:
1617
"""Check if the path of library is valid."""
@@ -20,17 +21,15 @@ def evaluate_path(self, open_path: str | None) -> LibraryStatus:
2021
if not library_path.exists():
2122
logger.error("Path does not exist.", open_path=open_path)
2223
return LibraryStatus(success=False, message="Path does not exist.")
23-
elif self.settings.value(
24-
SettingItems.START_LOAD_LAST, defaultValue=True, type=bool
25-
) and self.settings.value(SettingItems.LAST_LIBRARY):
26-
library_path = Path(str(self.settings.value(SettingItems.LAST_LIBRARY)))
24+
elif self.settings.open_last_loaded_on_startup and self.cache.last_lib:
25+
library_path = Path(str(self.cache.last_library))
2726
if not (library_path / TS_FOLDER_NAME).exists():
2827
logger.error(
2928
"TagStudio folder does not exist.",
3029
library_path=library_path,
3130
ts_folder=TS_FOLDER_NAME,
3231
)
33-
self.settings.setValue(SettingItems.LAST_LIBRARY, "")
32+
self.cache.last_library = ""
3433
# dont consider this a fatal error, just skip opening the library
3534
library_path = None
3635

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
__all__ = ["tssettings"]
1+
from .tssettings import TSSettings
2+
3+
__all__ = ["TSSettings"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pydantic import BaseModel
2+
3+
4+
class LibSettings(BaseModel):
5+
# Cant think of any library-specific properties lol
6+
test_prop: bool = False

tagstudio/src/core/settings/tssettings.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@ class TSSettings(BaseModel):
1111
dark_mode: bool = Field(default=False)
1212
language: str = Field(default="en-US")
1313

14+
# settings from the old SettingItem enum
15+
open_last_loaded_on_startup: bool = Field(default=False)
16+
show_library_list: bool = Field(default=True)
17+
autoplay: bool = Field(default=False)
18+
19+
filename: str = Field()
20+
1421
@staticmethod
1522
def read_settings(path: Path | str, **kwargs) -> "TSSettings":
16-
# library = kwargs.get("library")
1723
settings_data: dict[str, any] = dict()
1824
if path.exists():
19-
with open(path, "rb").read() as filecontents:
25+
with open(path, "rb") as file:
26+
filecontents = file.read()
2027
if len(filecontents.strip()) != 0:
2128
settings_data = toml.loads(filecontents.decode("utf-8"))
2229

23-
# if library: #TODO: add library-specific settings
24-
# lib_settings_path = Path(library.folder / "settings.toml")
25-
# lib_settings_data: dict[str, any]
26-
# if lib_settings_path.exists:
27-
# with open(lib_settings_path, "rb") as filedata:
28-
# lib_settings_data = tomllib.load(filedata)
29-
# lib_settings = TSSettings(**lib_settings_data)
30-
31-
return TSSettings(**settings_data)
30+
settings_data["filename"] = str(path)
31+
settings = TSSettings(**settings_data)
32+
return settings
3233

3334
def to_dict(self) -> dict[str, any]:
3435
d = dict[str, any]()
@@ -37,6 +38,14 @@ def to_dict(self) -> dict[str, any]:
3738

3839
return d
3940

40-
def save(self, path: Path | str) -> None:
41+
def save(self, path: Path | str | None) -> None:
42+
if isinstance(path, str):
43+
path = Path(path)
44+
45+
if path is None:
46+
path = self.filename
47+
if not path.parent.exists():
48+
path.parent.mkdir(parents=True, exist_ok=True)
49+
4150
with open(path, "w") as f:
4251
toml.dump(self.to_dict(), f)

tagstudio/src/core/tscacheddata.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from datetime import datetime
2+
from pathlib import Path
3+
4+
import structlog
5+
import toml
6+
from appdirs import user_cache_dir
7+
from pydantic import BaseModel, ConfigDict, Field
8+
from src.core.library.alchemy.library import Library
9+
10+
logger = structlog.get_logger(__name__)
11+
12+
cache_dir = Path(user_cache_dir()) / ".TagStudio"
13+
cache_location = cache_dir / "cache.toml"
14+
15+
16+
class TSCachedData(BaseModel):
17+
model_config = ConfigDict(arbitrary_types_allowed=True)
18+
last_library: Library | None = Field(default=None)
19+
library_history: dict[datetime, str] = Field(default_factory=dict[datetime, str])
20+
21+
path: str = Field()
22+
23+
@staticmethod
24+
def open(path: str | None = None) -> "TSCachedData":
25+
file: str | None = None
26+
27+
if path is None:
28+
if not Path(cache_dir).exists():
29+
logger.info("Cache directory does not exist - creating", path=cache_dir)
30+
Path.mkdir(cache_dir)
31+
if not Path(cache_location).exists():
32+
logger.info("Cache file does not exist - creating", path=cache_location)
33+
open(cache_location, "w").close()
34+
file = str(cache_location)
35+
else:
36+
file = path
37+
38+
data = toml.load(file)
39+
data["path"] = str(path) if path is not None else str(cache_location)
40+
cached_data = TSCachedData(**data)
41+
return cached_data
42+
43+
def save(self):
44+
with open(self.path, "wb") as f:
45+
f.writelines(toml.dumps(self))

tagstudio/src/qt/modals/settings_modal.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
QLabel,
88
QVBoxLayout,
99
)
10-
from src.core.settings import TSSettings
10+
from src.core.settings import tssettings
1111
from src.qt.widgets.panel import PanelWidget
1212

1313

1414
class SettingsModal(PanelWidget):
15-
def __init__(self, settings: TSSettings):
15+
def __init__(self, settings: tssettings):
1616
super().__init__()
1717
self.tempSettings = copy.deepcopy(settings)
1818

@@ -29,7 +29,7 @@ def __init__(self, settings: TSSettings):
2929
self.darkMode_Value.setChecked(self.tempSettings.dark_mode)
3030

3131
self.darkMode_Value.stateChanged.connect(
32-
lambda state: self.set_property("dark_mode", bool(state))
32+
lambda state: setattr(self.tempSettings, "dark_mode", bool(state))
3333
)
3434

3535
# ---
@@ -49,15 +49,29 @@ def __init__(self, settings: TSSettings):
4949
self.language_Value.addItems(language_list)
5050
self.language_Value.setCurrentIndex(language_list.index(self.tempSettings.language))
5151
self.language_Value.currentTextChanged.connect(
52-
lambda text: self.set_property("language", text)
52+
lambda text: setattr(self.tempSettings, "language", text)
53+
)
54+
55+
# ---
56+
self.show_library_list_Label = QLabel()
57+
self.show_library_list_Value = QCheckBox()
58+
self.show_library_list_Row = QHBoxLayout()
59+
self.show_library_list_Row.addWidget(self.show_library_list_Label)
60+
self.show_library_list_Row.addWidget(self.show_library_list_Value)
61+
self.show_library_list_Label.setText("Load library list on startup:")
62+
self.show_library_list_Value.setChecked(self.tempSettings.show_library_list)
63+
64+
self.show_library_list_Value.stateChanged.connect(
65+
lambda state: setattr(self.tempSettings, "show_library_list", bool(state))
5366
)
5467

5568
# ---
5669
self.main.addLayout(self.darkMode_Row)
5770
self.main.addLayout(self.language_Row)
71+
self.main.addLayout(self.show_library_list_Row)
5872

5973
def set_property(self, prop_name: str, value: any) -> None:
6074
setattr(self.tempSettings, prop_name, value)
6175

62-
def get_content(self) -> TSSettings:
76+
def get_content(self) -> tssettings:
6377
return self.tempSettings

0 commit comments

Comments
 (0)