Skip to content

Parity: Port create and add from v9.4 to v 9.5 #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tagstudio/resources/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
"tag.color": "Color",
"tag.confirm_delete": "Are you sure you want to delete the tag \"{tag_name}\"?",
"tag.create": "Create Tag",
"tag.create_add": "Create && Add \"{query}\"",
"tag.edit": "Edit Tag",
"tag.name": "Name",
"tag.new": "New Tag",
Expand Down
1 change: 1 addition & 0 deletions tagstudio/resources/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"tag.add_to_search": "Ajouter à la Recherche",
"tag.aliases": "Alias",
"tag.color": "Couleur",
"tag.create_add": "Créer && Adjouter \"{query}\""
"tag.name": "Nom",
"tag.new": "Nouveau Label",
"tag.parent_tags": "Labels Parent",
Expand Down
92 changes: 82 additions & 10 deletions tagstudio/src/qt/modals/tag_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import math

import src.qt.modals.build_tag as build_tag
import structlog
from PySide6.QtCore import QSize, Qt, Signal
from PySide6.QtGui import QShowEvent
Expand All @@ -19,6 +20,7 @@
)
from src.core.constants import RESERVED_TAG_END, RESERVED_TAG_START
from src.core.library import Library, Tag
from src.core.library.alchemy.enums import TagColor
from src.core.palette import ColorType, get_tag_color
from src.qt.translations import Translations
from src.qt.widgets.panel import PanelModal, PanelWidget
Expand Down Expand Up @@ -117,6 +119,71 @@ def __build_row_item_widget(self, tag: Tag):
row.addWidget(add_button)
return container

def construct_tag_button(self, query: str | None):
"""Constructs a Create Tag Button."""
container = QWidget()
row = QHBoxLayout(container)
row.setContentsMargins(0, 0, 0, 0)
row.setSpacing(3)

create_button = QPushButton(self)
Translations.translate_qobject(create_button, "tag.create_add", query=query)
create_button.setFlat(True)

inner_layout = QHBoxLayout()
inner_layout.setObjectName("innerLayout")
inner_layout.setContentsMargins(2, 2, 2, 2)
create_button.setLayout(inner_layout)
create_button.setMinimumSize(math.ceil(22 * 1.5), 22)

create_button.setStyleSheet(
f"QPushButton{{"
f"background: {get_tag_color(ColorType.PRIMARY, TagColor.DEFAULT)};"
f"color: {get_tag_color(ColorType.TEXT, TagColor.DEFAULT)};"
f"font-weight: 600;"
f"border-color:{get_tag_color(ColorType.BORDER, TagColor.DEFAULT)};"
f"border-radius: 6px;"
f"border-style:solid;"
f"border-width: {math.ceil(self.devicePixelRatio())}px;"
f"padding-right: 4px;"
f"padding-bottom: 1px;"
f"padding-left: 4px;"
f"font-size: 13px"
f"}}"
f"QPushButton::hover{{"
f"border-color:{get_tag_color(ColorType.LIGHT_ACCENT, TagColor.DEFAULT)};"
f"}}"
)

create_button.clicked.connect(lambda: self.create_and_add_tag(query))
row.addWidget(create_button)

return container

def create_and_add_tag(self, name: str):
"""Opens "Create Tag" panel to create and add a new tag with given name."""
logger.info("Create and Add Tag", name=name)

def on_tag_modal_saved():
"""Callback for actions to perform when a new tag is confirmed created."""
tag: Tag = self.build_tag_modal.build_tag()
self.lib.add_tag(tag)
self.add_tag_modal.hide()

self.tag_chosen.emit(tag.id)
self.search_field.setText("")
self.update_tags()

self.build_tag_modal: build_tag.BuildTagPanel = build_tag.BuildTagPanel(self.lib)
self.add_tag_modal: PanelModal = PanelModal(self.build_tag_modal, has_save=True)
Translations.translate_with_setter(self.add_tag_modal.setTitle, "tag.new")
Translations.translate_with_setter(self.add_tag_modal.setWindowTitle, "tag.add")

self.build_tag_modal.name_field.setText(name)
self.add_tag_modal.saved.connect(on_tag_modal_saved)
self.add_tag_modal.save_button.setFocus()
self.add_tag_modal.show()

def update_tags(self, query: str | None = None):
logger.info("[Tag Search Super Class] Updating Tags")

Expand All @@ -134,14 +201,22 @@ def update_tags(self, query: str | None = None):
if tag.id not in self.exclude:
self.scroll_layout.addWidget(self.__build_row_item_widget(tag))

# If query doesnt exist add create button
if len(tag_results) == 0:
c = self.construct_tag_button(query)
self.scroll_layout.addWidget(c)

self.search_field.setFocus()

def on_return(self, text: str):
if text and self.first_tag_id is not None:
if self.is_tag_chooser:
self.tag_chosen.emit(self.first_tag_id)
self.search_field.setText("")
self.update_tags()
if text:
if self.first_tag_id is not None:
if self.is_tag_chooser:
self.tag_chosen.emit(self.first_tag_id)
self.search_field.setText("")
self.update_tags()
else:
self.create_and_add_tag(text)
else:
self.search_field.setFocus()
self.parentWidget().hide()
Expand All @@ -156,16 +231,13 @@ def remove_tag(self, tag: Tag):
pass

def edit_tag(self, tag: Tag):
# only import here because of circular imports
from src.qt.modals.build_tag import BuildTagPanel

def callback(btp: BuildTagPanel):
def callback(btp: build_tag.BuildTagPanel):
self.lib.update_tag(
btp.build_tag(), set(btp.parent_ids), set(btp.alias_names), set(btp.alias_ids)
)
self.update_tags(self.search_field.text())

build_tag_panel = BuildTagPanel(self.lib, tag=tag)
build_tag_panel = build_tag.BuildTagPanel(self.lib, tag=tag)

self.edit_modal = PanelModal(
build_tag_panel,
Expand Down
Loading