Skip to content

Commit

Permalink
split FilterState name and id
Browse files Browse the repository at this point in the history
  • Loading branch information
yedpodtrzitko committed Aug 2, 2024
1 parent 5c8885a commit b112089
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 171 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ ruff==0.4.2
pre-commit==3.7.0
pytest==8.2.0
Pyinstaller==6.6.0
mypy==1.10.0
mypy==1.11.0
syrupy==4.6.1
40 changes: 0 additions & 40 deletions tagstudio/src/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,45 +124,5 @@
TEXT_FIELDS = ["text_line", "text_box"]
DATE_FIELDS = ["datetime"]

TAG_COLORS = [
"",
"black",
"dark gray",
"gray",
"light gray",
"white",
"light pink",
"pink",
"red",
"red orange",
"orange",
"yellow orange",
"yellow",
"lime",
"light green",
"mint",
"green",
"teal",
"cyan",
"light blue",
"blue",
"blue violet",
"violet",
"purple",
"lavender",
"berry",
"magenta",
"salmon",
"auburn",
"dark brown",
"brown",
"light brown",
"blonde",
"peach",
"warm gray",
"cool gray",
"olive",
]

TAG_FAVORITE = 1
TAG_ARCHIVED = 0
57 changes: 49 additions & 8 deletions tagstudio/src/core/library/alchemy/enums.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import enum
from dataclasses import dataclass, field
from enum import Enum
from enum import Enum, auto


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


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

page_index: int = 0
page_size: int = 100
query: str | None = None
name: str | None = None
id: int | None = None

# default_search: str = "name"

def __post_init__(self):
# strip query automatically
self.query = self.query and self.query.strip()
self.name = self.name and self.name.strip()
self.id = self.id and int(self.id)

@property
def summary(self) -> str | int | None:
"""Show query summary"""
return self.name or self.id or None

@property
def limit(self):
Expand Down
6 changes: 3 additions & 3 deletions tagstudio/src/core/library/alchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ def tag_ids(self) -> list[int]:
def __init__(
self,
name: str,
tags: set[Tag] = set(),
entry: "Entry" | None = None,
tags: set[int] | None = None,
entry: Entry | None = None,
entry_id=entry_id,
type: TagBoxTypes = TagBoxTypes.tag_box,
):
self.name = name
self.tags = tags
self.tags = tags or set()

Check failure on line 105 in tagstudio/src/core/library/alchemy/fields.py

View workflow job for this annotation

GitHub Actions / Run MyPy

[mypy] reported by reviewdog 🐶 Incompatible types in assignment (expression has type "set[int]", variable has type "SQLCoreOperations[set[Tag]] | set[Tag]") [assignment] Raw Output: /home/runner/work/TagStudio/TagStudio/tagstudio/src/core/library/alchemy/fields.py:105:21: error: Incompatible types in assignment (expression has type "set[int]", variable has type "SQLCoreOperations[set[Tag]] | set[Tag]") [assignment]
self.type = type
self.entry_id = entry_id

Expand Down
57 changes: 27 additions & 30 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,39 +312,26 @@ def search_library(

lookup_strategy = None

if query := search.query:
if "entry_id:" in search.query:
lookup_strategy = "entry_id"
_, _, entry_id = query.partition(":")
statement = statement.where(Entry.id == int(entry_id))

elif "tag_id:" in query:
lookup_strategy = "tag_id"

potential_tag_id = query.split(":")[-1].strip()
if potential_tag_id.isdigit():
tag_id = int(potential_tag_id)
statement = statement.where(Tag.id == tag_id)

elif ":" not in query:
lookup_strategy = "tag_name"

# for now assume plain string is tag
tag_value = query.strip()
# check if Tag.name or Tag.shorthand matches the tag_value
statement = statement.where(
or_(Tag.name == tag_value, Tag.shorthand == tag_value)
).distinct()
if search.name:
lookup_strategy = "tag_name"
statement = statement.where(
or_(
Tag.name == search.name,
Tag.shorthand == search.name,
)
).distinct()

else:
lookup_strategy = "path"
statement = statement.where(Entry.path.like(f"%{query}%"))
elif search.id:
lookup_strategy = "tag_id"
statement = statement.where(Tag.id == search.id)

# TODO - add other lookups

logger.info(
"searching library",
filter=search,
lookup_strategy=lookup_strategy,
# query_full=statement.compile(compile_kwargs={"literal_binds": True}),
query_full=statement.compile(compile_kwargs={"literal_binds": True}),
)

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

with Session(self.engine) as session, session.begin():
query = select(Tag)
query = query.options(
selectinload(Tag.subtags),
selectinload(Tag.aliases),
)

if search.query:
if search.name:
query = query.where(
or_(
Tag.name == search.query,
Tag.shorthand == search.query,
Tag.name == search.name,
Tag.shorthand == search.name,
# Tag.id == search.query,
)
)
Expand Down Expand Up @@ -674,3 +665,9 @@ def entry_archived_favorited_status(self, entry: int | Entry) -> tuple[bool, boo

def save_library_backup_to_disk(self, *args, **kwargs):
logger.error("save_library_backup_to_disk to be implemented")

def get_tag(self, tag_id: int) -> Tag:
with Session(self.engine) as session, session.begin():
tag = session.scalars(select(Tag).where(Tag.id == tag_id)).one()
session.expunge(tag)
return tag
36 changes: 18 additions & 18 deletions tagstudio/src/core/palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ColorType(int, Enum):
DARK_ACCENT = 4


_TAG_COLORS = {
TAG_COLORS = {
"": {
ColorType.PRIMARY: "#1e1e1e",
ColorType.TEXT: ColorType.LIGHT_ACCENT,
Expand All @@ -30,7 +30,7 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#b7b6be",
ColorType.DARK_ACCENT: "#03020a",
},
"dark gray": {
"dark_gray": {
ColorType.PRIMARY: "#24232a",
ColorType.TEXT: ColorType.LIGHT_ACCENT,
ColorType.BORDER: "#2a2930",
Expand All @@ -44,7 +44,7 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#cbcad2",
ColorType.DARK_ACCENT: "#191820",
},
"light gray": {
"light_gray": {
ColorType.PRIMARY: "#aaa9b0",
ColorType.TEXT: ColorType.DARK_ACCENT,
ColorType.BORDER: "#b6b4bc",
Expand All @@ -58,7 +58,7 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#ffffff",
ColorType.DARK_ACCENT: "#302f36",
},
"light pink": {
"light_pink": {
ColorType.PRIMARY: "#ff99c4",
ColorType.TEXT: ColorType.DARK_ACCENT,
ColorType.BORDER: "#ffaad0",
Expand Down Expand Up @@ -87,7 +87,7 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#f39caa",
ColorType.DARK_ACCENT: "#440d12",
},
"red orange": {
"red_orange": {
ColorType.PRIMARY: "#e83726",
ColorType.TEXT: ColorType.DARK_ACCENT,
ColorType.BORDER: "#ea4b3b",
Expand All @@ -108,7 +108,7 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#f7b79b",
ColorType.DARK_ACCENT: "#551e0a",
},
"yellow orange": {
"yellow_orange": {
ColorType.PRIMARY: "#fa9a2c",
ColorType.TEXT: ColorType.DARK_ACCENT,
ColorType.BORDER: "#fba94b",
Expand Down Expand Up @@ -137,7 +137,7 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#e9f9b7",
ColorType.DARK_ACCENT: "#405516",
},
"light green": {
"light_green": {
ColorType.PRIMARY: "#85ec76",
ColorType.TEXT: ColorType.DARK_ACCENT,
ColorType.BORDER: "#a3f198",
Expand Down Expand Up @@ -165,7 +165,7 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#bff5f0",
ColorType.DARK_ACCENT: "#0f4246",
},
"light blue": {
"light_blue": {
ColorType.PRIMARY: "#55bbf6",
ColorType.TEXT: ColorType.DARK_ACCENT,
ColorType.BORDER: "#70c6f7",
Expand All @@ -179,7 +179,7 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#aedbfa",
ColorType.DARK_ACCENT: "#122948",
},
"blue violet": {
"blue_violet": {
ColorType.PRIMARY: "#5948f2",
ColorType.TEXT: ColorType.LIGHT_ACCENT,
ColorType.BORDER: "#6258f3",
Expand Down Expand Up @@ -235,28 +235,28 @@ class ColorType(int, Enum):
ColorType.LIGHT_ACCENT: "#d98a7f",
ColorType.DARK_ACCENT: "#3d100a",
},
"light brown": {
"light_brown": {
ColorType.PRIMARY: "#be5b2d",
ColorType.TEXT: ColorType.DARK_ACCENT,
ColorType.BORDER: "#c4693d",
ColorType.LIGHT_ACCENT: "#e5b38c",
ColorType.DARK_ACCENT: "#4c290e",
},
"dark brown": {
"dark_brown": {
ColorType.PRIMARY: "#4c2315",
ColorType.TEXT: ColorType.LIGHT_ACCENT,
ColorType.BORDER: "#542a1c",
ColorType.LIGHT_ACCENT: "#b78171",
ColorType.DARK_ACCENT: "#211006",
},
"cool gray": {
"cool_gray": {
ColorType.PRIMARY: "#515768",
ColorType.TEXT: ColorType.LIGHT_ACCENT,
ColorType.BORDER: "#5b6174",
ColorType.LIGHT_ACCENT: "#9ea1c3",
ColorType.DARK_ACCENT: "#181a37",
},
"warm gray": {
"warm_gray": {
ColorType.PRIMARY: "#625550",
ColorType.TEXT: ColorType.LIGHT_ACCENT,
ColorType.BORDER: "#6c5e57",
Expand All @@ -280,11 +280,11 @@ class ColorType(int, Enum):
}


def get_tag_color(type, color: TagColor):
def get_tag_color(type, color_id: str):
try:
if type == ColorType.TEXT:
return get_tag_color(_TAG_COLORS[color][type], color)
else:
return _TAG_COLORS[color][type]
return TAG_COLORS[color_id][type]
# if type == ColorType.TEXT:
# return get_tag_color([color][type], color)

except KeyError:
return "#FF00FF"
19 changes: 11 additions & 8 deletions tagstudio/src/qt/modals/build_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from src.core.library import Tag, Library
from src.core.library.alchemy.enums import TagColor
from src.core.palette import ColorType, get_tag_color
from src.core.constants import TAG_COLORS

from src.qt.widgets.panel import PanelWidget, PanelModal
from src.qt.widgets.tag import TagWidget
from src.qt.modals.tag_search import TagSearchPanel
Expand Down Expand Up @@ -139,15 +139,18 @@ def __init__(self, library: Library, tag_id: int = -1):
self.color_field.setEditable(False)
self.color_field.setMaxVisibleItems(10)
self.color_field.setStyleSheet("combobox-popup:0;")
for color in TAG_COLORS:
self.color_field.addItem(color.title())
for color in TagColor:
self.color_field.addItem(color.name)
# self.color_field.setProperty("appearance", "flat")
self.color_field.currentTextChanged.connect(
lambda c: self.color_field.setStyleSheet(f"""combobox-popup:0;
font-weight:600;
color:{get_tag_color(ColorType.TEXT, c.lower())};
background-color:{get_tag_color(ColorType.PRIMARY, c.lower())};
""")
lambda c: self.color_field.setStyleSheet(
(
"combobox-popup:0;"
"font-weight:600;"
f"color:{get_tag_color(ColorType.TEXT, c)};"
f"background-color:{get_tag_color(ColorType.PRIMARY, c)};"
)
)
)
self.color_layout.addWidget(self.color_field)

Expand Down
Loading

0 comments on commit b112089

Please sign in to comment.