Skip to content

feat(qt): Add hotkey to add tags to selection #302

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

Closed
wants to merge 2 commits into from
Closed
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
35 changes: 34 additions & 1 deletion tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
)
from humanfriendly import format_timespan

from src.core.enums import SettingItems, SearchMode
from src.core.enums import FieldID, SettingItems, SearchMode
from src.core.library import ItemType
from src.core.ts_core import TagStudioCore
from src.core.constants import (
Expand Down Expand Up @@ -85,6 +85,7 @@
from src.qt.modals.fix_unlinked import FixUnlinkedEntriesModal
from src.qt.modals.fix_dupes import FixDupeFilesModal
from src.qt.modals.folders_to_tags import FoldersToTagsModal
from src.qt.modals.tag_search import TagSearchPanel

# this import has side-effect of import PySide resources
import src.qt.resources_rc # pylint: disable=unused-import
Expand Down Expand Up @@ -384,6 +385,20 @@ def start(self) -> None:
new_tag_action.setToolTip("Ctrl+T")
edit_menu.addAction(new_tag_action)

add_tag_action = QAction("Add Tag To File", menu_bar)
add_tag_action.triggered.connect(lambda: self.attach_tag_action_callback())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could remove the lambda and just use add_tag_action.triggered.connect(self.attach_tag_action_callback), because you don't provide any input to the method. (I know that this is also present in the actions above yours and i think these changes should also be made there, but i don't know if that would be out of scope for this pr)

add_tag_action.setShortcut(
QtCore.QKeyCombination(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could remove the QtCore. by importing QKeyCombination directly, which makes it more readable i think.

QtCore.Qt.KeyboardModifier(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here (and in the following lines) you could remove the QtCore. and just use for example Qt.KeyboardModifier (Qt is already imported in line 28)

QtCore.Qt.KeyboardModifier.ControlModifier
| QtCore.Qt.KeyboardModifier.ShiftModifier
),
QtCore.Qt.Key.Key_T,
)
)
new_tag_action.setToolTip("Ctrl+Shift+T")
edit_menu.addAction(add_tag_action)

edit_menu.addSeparator()

select_all_action = QAction("Select All", menu_bar)
Expand Down Expand Up @@ -724,6 +739,24 @@ def add_tag_action_callback(self):
# panel.tag_updated.connect(lambda tag: self.lib.update_tag(tag))
self.modal.show()

def attach_tag_action_callback(self):
selected_files = [x[1] for x in self.selected if x[0] == ItemType.ENTRY]
tsp = TagSearchPanel(self.lib)
tsp.tag_chosen.connect(lambda x: self.bulk_add_tags(x, selected_files))
self.modal = PanelModal(tsp, "Bulk add tags (Tag Box)", "Add Tags")
tsp.update_tags()
self.modal.show()

def bulk_add_tags(self, tag_id: int, files: list[int]):
for x in files:
# Hardcoded because the shortcut works for just the normal tags
self.lib.get_entry(x).add_tag(self.lib, tag_id, field_id=FieldID.TAGS)

self.preview_panel.update_widgets()

if tag_id in (TAG_FAVORITE, TAG_ARCHIVED):
self.update_badges()

def select_all_action_callback(self):
for item in self.item_thumbs:
if item.mode and (item.mode, item.item_id) not in self.selected:
Expand Down