Skip to content

Commit b112089

Browse files
committed
split FilterState name and id
1 parent 5c8885a commit b112089

File tree

15 files changed

+166
-171
lines changed

15 files changed

+166
-171
lines changed

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ ruff==0.4.2
22
pre-commit==3.7.0
33
pytest==8.2.0
44
Pyinstaller==6.6.0
5-
mypy==1.10.0
5+
mypy==1.11.0
66
syrupy==4.6.1

tagstudio/src/core/constants.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -124,45 +124,5 @@
124124
TEXT_FIELDS = ["text_line", "text_box"]
125125
DATE_FIELDS = ["datetime"]
126126

127-
TAG_COLORS = [
128-
"",
129-
"black",
130-
"dark gray",
131-
"gray",
132-
"light gray",
133-
"white",
134-
"light pink",
135-
"pink",
136-
"red",
137-
"red orange",
138-
"orange",
139-
"yellow orange",
140-
"yellow",
141-
"lime",
142-
"light green",
143-
"mint",
144-
"green",
145-
"teal",
146-
"cyan",
147-
"light blue",
148-
"blue",
149-
"blue violet",
150-
"violet",
151-
"purple",
152-
"lavender",
153-
"berry",
154-
"magenta",
155-
"salmon",
156-
"auburn",
157-
"dark brown",
158-
"brown",
159-
"light brown",
160-
"blonde",
161-
"peach",
162-
"warm gray",
163-
"cool gray",
164-
"olive",
165-
]
166-
167127
TAG_FAVORITE = 1
168128
TAG_ARCHIVED = 0

tagstudio/src/core/library/alchemy/enums.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
import enum
22
from dataclasses import dataclass, field
3-
from enum import Enum
3+
from enum import Enum, auto
44

55

66
class TagColor(Enum):
7-
default = 0
8-
black = 1
9-
dark_gray = 2
10-
gray = 3
11-
red = 4
7+
default = 1
8+
black = 2
9+
dark_gray = 3
10+
gray = 4
11+
light_gray = 5
12+
white = 6
13+
light_pink = 7
14+
pink = 8
15+
red = 9
16+
red_orange = 10
17+
orange = 11
18+
yellow_orange = 12
19+
yellow = 13
20+
lime = 14
21+
light_green = 15
22+
mint = 16
23+
green = 17
24+
teal = 18
25+
cyan = 19
26+
light_blue = 20
27+
blue = 21
28+
blue_violet = 22
29+
violet = 23
30+
purple = 24
31+
lavender = 25
32+
berry = 26
33+
magenta = 27
34+
salmon = 28
35+
auburn = 29
36+
dark_brown = 30
37+
brown = 31
38+
light_brown = 32
39+
blonde = 33
40+
peach = 34
41+
warm_gray = 35
42+
cool_gray = 36
43+
olive = 37
1244

1345

1446
class SearchMode(enum.IntEnum):
@@ -30,11 +62,20 @@ class FilterState:
3062

3163
page_index: int = 0
3264
page_size: int = 100
33-
query: str | None = None
65+
name: str | None = None
66+
id: int | None = None
67+
68+
# default_search: str = "name"
3469

3570
def __post_init__(self):
3671
# strip query automatically
37-
self.query = self.query and self.query.strip()
72+
self.name = self.name and self.name.strip()
73+
self.id = self.id and int(self.id)
74+
75+
@property
76+
def summary(self) -> str | int | None:
77+
"""Show query summary"""
78+
return self.name or self.id or None
3879

3980
@property
4081
def limit(self):

tagstudio/src/core/library/alchemy/fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ def tag_ids(self) -> list[int]:
9696
def __init__(
9797
self,
9898
name: str,
99-
tags: set[Tag] = set(),
100-
entry: "Entry" | None = None,
99+
tags: set[int] | None = None,
100+
entry: Entry | None = None,
101101
entry_id=entry_id,
102102
type: TagBoxTypes = TagBoxTypes.tag_box,
103103
):
104104
self.name = name
105-
self.tags = tags
105+
self.tags = tags or set()
106106
self.type = type
107107
self.entry_id = entry_id
108108

tagstudio/src/core/library/alchemy/library.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -312,39 +312,26 @@ def search_library(
312312

313313
lookup_strategy = None
314314

315-
if query := search.query:
316-
if "entry_id:" in search.query:
317-
lookup_strategy = "entry_id"
318-
_, _, entry_id = query.partition(":")
319-
statement = statement.where(Entry.id == int(entry_id))
320-
321-
elif "tag_id:" in query:
322-
lookup_strategy = "tag_id"
323-
324-
potential_tag_id = query.split(":")[-1].strip()
325-
if potential_tag_id.isdigit():
326-
tag_id = int(potential_tag_id)
327-
statement = statement.where(Tag.id == tag_id)
328-
329-
elif ":" not in query:
330-
lookup_strategy = "tag_name"
331-
332-
# for now assume plain string is tag
333-
tag_value = query.strip()
334-
# check if Tag.name or Tag.shorthand matches the tag_value
335-
statement = statement.where(
336-
or_(Tag.name == tag_value, Tag.shorthand == tag_value)
337-
).distinct()
315+
if search.name:
316+
lookup_strategy = "tag_name"
317+
statement = statement.where(
318+
or_(
319+
Tag.name == search.name,
320+
Tag.shorthand == search.name,
321+
)
322+
).distinct()
338323

339-
else:
340-
lookup_strategy = "path"
341-
statement = statement.where(Entry.path.like(f"%{query}%"))
324+
elif search.id:
325+
lookup_strategy = "tag_id"
326+
statement = statement.where(Tag.id == search.id)
327+
328+
# TODO - add other lookups
342329

343330
logger.info(
344331
"searching library",
345332
filter=search,
346333
lookup_strategy=lookup_strategy,
347-
# query_full=statement.compile(compile_kwargs={"literal_binds": True}),
334+
query_full=statement.compile(compile_kwargs={"literal_binds": True}),
348335
)
349336

350337
entries_ = list(session.scalars(statement).unique())
@@ -362,12 +349,16 @@ def search_tags(
362349

363350
with Session(self.engine) as session, session.begin():
364351
query = select(Tag)
352+
query = query.options(
353+
selectinload(Tag.subtags),
354+
selectinload(Tag.aliases),
355+
)
365356

366-
if search.query:
357+
if search.name:
367358
query = query.where(
368359
or_(
369-
Tag.name == search.query,
370-
Tag.shorthand == search.query,
360+
Tag.name == search.name,
361+
Tag.shorthand == search.name,
371362
# Tag.id == search.query,
372363
)
373364
)
@@ -674,3 +665,9 @@ def entry_archived_favorited_status(self, entry: int | Entry) -> tuple[bool, boo
674665

675666
def save_library_backup_to_disk(self, *args, **kwargs):
676667
logger.error("save_library_backup_to_disk to be implemented")
668+
669+
def get_tag(self, tag_id: int) -> Tag:
670+
with Session(self.engine) as session, session.begin():
671+
tag = session.scalars(select(Tag).where(Tag.id == tag_id)).one()
672+
session.expunge(tag)
673+
return tag

tagstudio/src/core/palette.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ColorType(int, Enum):
1515
DARK_ACCENT = 4
1616

1717

18-
_TAG_COLORS = {
18+
TAG_COLORS = {
1919
"": {
2020
ColorType.PRIMARY: "#1e1e1e",
2121
ColorType.TEXT: ColorType.LIGHT_ACCENT,
@@ -30,7 +30,7 @@ class ColorType(int, Enum):
3030
ColorType.LIGHT_ACCENT: "#b7b6be",
3131
ColorType.DARK_ACCENT: "#03020a",
3232
},
33-
"dark gray": {
33+
"dark_gray": {
3434
ColorType.PRIMARY: "#24232a",
3535
ColorType.TEXT: ColorType.LIGHT_ACCENT,
3636
ColorType.BORDER: "#2a2930",
@@ -44,7 +44,7 @@ class ColorType(int, Enum):
4444
ColorType.LIGHT_ACCENT: "#cbcad2",
4545
ColorType.DARK_ACCENT: "#191820",
4646
},
47-
"light gray": {
47+
"light_gray": {
4848
ColorType.PRIMARY: "#aaa9b0",
4949
ColorType.TEXT: ColorType.DARK_ACCENT,
5050
ColorType.BORDER: "#b6b4bc",
@@ -58,7 +58,7 @@ class ColorType(int, Enum):
5858
ColorType.LIGHT_ACCENT: "#ffffff",
5959
ColorType.DARK_ACCENT: "#302f36",
6060
},
61-
"light pink": {
61+
"light_pink": {
6262
ColorType.PRIMARY: "#ff99c4",
6363
ColorType.TEXT: ColorType.DARK_ACCENT,
6464
ColorType.BORDER: "#ffaad0",
@@ -87,7 +87,7 @@ class ColorType(int, Enum):
8787
ColorType.LIGHT_ACCENT: "#f39caa",
8888
ColorType.DARK_ACCENT: "#440d12",
8989
},
90-
"red orange": {
90+
"red_orange": {
9191
ColorType.PRIMARY: "#e83726",
9292
ColorType.TEXT: ColorType.DARK_ACCENT,
9393
ColorType.BORDER: "#ea4b3b",
@@ -108,7 +108,7 @@ class ColorType(int, Enum):
108108
ColorType.LIGHT_ACCENT: "#f7b79b",
109109
ColorType.DARK_ACCENT: "#551e0a",
110110
},
111-
"yellow orange": {
111+
"yellow_orange": {
112112
ColorType.PRIMARY: "#fa9a2c",
113113
ColorType.TEXT: ColorType.DARK_ACCENT,
114114
ColorType.BORDER: "#fba94b",
@@ -137,7 +137,7 @@ class ColorType(int, Enum):
137137
ColorType.LIGHT_ACCENT: "#e9f9b7",
138138
ColorType.DARK_ACCENT: "#405516",
139139
},
140-
"light green": {
140+
"light_green": {
141141
ColorType.PRIMARY: "#85ec76",
142142
ColorType.TEXT: ColorType.DARK_ACCENT,
143143
ColorType.BORDER: "#a3f198",
@@ -165,7 +165,7 @@ class ColorType(int, Enum):
165165
ColorType.LIGHT_ACCENT: "#bff5f0",
166166
ColorType.DARK_ACCENT: "#0f4246",
167167
},
168-
"light blue": {
168+
"light_blue": {
169169
ColorType.PRIMARY: "#55bbf6",
170170
ColorType.TEXT: ColorType.DARK_ACCENT,
171171
ColorType.BORDER: "#70c6f7",
@@ -179,7 +179,7 @@ class ColorType(int, Enum):
179179
ColorType.LIGHT_ACCENT: "#aedbfa",
180180
ColorType.DARK_ACCENT: "#122948",
181181
},
182-
"blue violet": {
182+
"blue_violet": {
183183
ColorType.PRIMARY: "#5948f2",
184184
ColorType.TEXT: ColorType.LIGHT_ACCENT,
185185
ColorType.BORDER: "#6258f3",
@@ -235,28 +235,28 @@ class ColorType(int, Enum):
235235
ColorType.LIGHT_ACCENT: "#d98a7f",
236236
ColorType.DARK_ACCENT: "#3d100a",
237237
},
238-
"light brown": {
238+
"light_brown": {
239239
ColorType.PRIMARY: "#be5b2d",
240240
ColorType.TEXT: ColorType.DARK_ACCENT,
241241
ColorType.BORDER: "#c4693d",
242242
ColorType.LIGHT_ACCENT: "#e5b38c",
243243
ColorType.DARK_ACCENT: "#4c290e",
244244
},
245-
"dark brown": {
245+
"dark_brown": {
246246
ColorType.PRIMARY: "#4c2315",
247247
ColorType.TEXT: ColorType.LIGHT_ACCENT,
248248
ColorType.BORDER: "#542a1c",
249249
ColorType.LIGHT_ACCENT: "#b78171",
250250
ColorType.DARK_ACCENT: "#211006",
251251
},
252-
"cool gray": {
252+
"cool_gray": {
253253
ColorType.PRIMARY: "#515768",
254254
ColorType.TEXT: ColorType.LIGHT_ACCENT,
255255
ColorType.BORDER: "#5b6174",
256256
ColorType.LIGHT_ACCENT: "#9ea1c3",
257257
ColorType.DARK_ACCENT: "#181a37",
258258
},
259-
"warm gray": {
259+
"warm_gray": {
260260
ColorType.PRIMARY: "#625550",
261261
ColorType.TEXT: ColorType.LIGHT_ACCENT,
262262
ColorType.BORDER: "#6c5e57",
@@ -280,11 +280,11 @@ class ColorType(int, Enum):
280280
}
281281

282282

283-
def get_tag_color(type, color: TagColor):
283+
def get_tag_color(type, color_id: str):
284284
try:
285-
if type == ColorType.TEXT:
286-
return get_tag_color(_TAG_COLORS[color][type], color)
287-
else:
288-
return _TAG_COLORS[color][type]
285+
return TAG_COLORS[color_id][type]
286+
# if type == ColorType.TEXT:
287+
# return get_tag_color([color][type], color)
288+
289289
except KeyError:
290290
return "#FF00FF"

tagstudio/src/qt/modals/build_tag.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from src.core.library import Tag, Library
2121
from src.core.library.alchemy.enums import TagColor
2222
from src.core.palette import ColorType, get_tag_color
23-
from src.core.constants import TAG_COLORS
23+
2424
from src.qt.widgets.panel import PanelWidget, PanelModal
2525
from src.qt.widgets.tag import TagWidget
2626
from src.qt.modals.tag_search import TagSearchPanel
@@ -139,15 +139,18 @@ def __init__(self, library: Library, tag_id: int = -1):
139139
self.color_field.setEditable(False)
140140
self.color_field.setMaxVisibleItems(10)
141141
self.color_field.setStyleSheet("combobox-popup:0;")
142-
for color in TAG_COLORS:
143-
self.color_field.addItem(color.title())
142+
for color in TagColor:
143+
self.color_field.addItem(color.name)
144144
# self.color_field.setProperty("appearance", "flat")
145145
self.color_field.currentTextChanged.connect(
146-
lambda c: self.color_field.setStyleSheet(f"""combobox-popup:0;
147-
font-weight:600;
148-
color:{get_tag_color(ColorType.TEXT, c.lower())};
149-
background-color:{get_tag_color(ColorType.PRIMARY, c.lower())};
150-
""")
146+
lambda c: self.color_field.setStyleSheet(
147+
(
148+
"combobox-popup:0;"
149+
"font-weight:600;"
150+
f"color:{get_tag_color(ColorType.TEXT, c)};"
151+
f"background-color:{get_tag_color(ColorType.PRIMARY, c)};"
152+
)
153+
)
151154
)
152155
self.color_layout.addWidget(self.color_field)
153156

0 commit comments

Comments
 (0)