From 79e0263e972c955a467fd6e527820542208f1756 Mon Sep 17 00:00:00 2001 From: Matheus Cirillo Date: Fri, 26 Apr 2024 20:09:43 -0300 Subject: [PATCH 1/7] file_label opens file path --- tagstudio/src/qt/ts_qt.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index 7e397f90..df53f4da 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -2015,7 +2015,24 @@ class PreviewPanel(QWidget): image_layout.addWidget(self.preview_img) image_layout.setAlignment(self.preview_img, Qt.AlignmentFlag.AlignCenter) - self.file_label = QLabel('Filename') + class ClickableLabel(QLabel): + def __init__(self, text, parent=None): + super().__init__(text, parent) + + def setFilePath(self, filepath): + self.filepath = filepath + + def mousePressEvent(self, event): + super().mousePressEvent(event) + #open file + if hasattr(self, 'filepath'): + if os.path.exists(self.filepath): + os.startfile(self.filepath) + logging.info(f'Opening file: {self.filepath}') + else: + logging.error(f'File not found: {self.filepath}') + + self.file_label = ClickableLabel('Filename') self.file_label.setWordWrap(True) self.file_label.setTextInteractionFlags( Qt.TextInteractionFlag.TextSelectableByMouse) @@ -2245,6 +2262,7 @@ class PreviewPanel(QWidget): if (len(self.selected) == 0 or self.selected != self.driver.selected): filepath = os.path.normpath(f'{self.lib.library_dir}/{item.path}/{item.filename}') + self.file_label.setFilePath(filepath) window_title = filepath ratio: float = self.devicePixelRatio() self.tr.render_big(time.time(), filepath, (512, 512), ratio) From 18dcedd6a0b7f142252301bc1c0afa2f704d2f8b Mon Sep 17 00:00:00 2001 From: Matheus Cirillo Date: Fri, 26 Apr 2024 20:13:58 -0300 Subject: [PATCH 2/7] code cleanup --- tagstudio/src/qt/ts_qt.py | 20 ++------------------ tagstudio/src/qt/utils/clickableLabels.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 tagstudio/src/qt/utils/clickableLabels.py diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index df53f4da..0e352a2a 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -46,6 +46,7 @@ from src.core.ts_core import (TagStudioCore, TAG_COLORS, DATE_FIELDS, TEXT_FIELD from src.core.utils.web import strip_web_protocol from src.qt.flowlayout import FlowLayout, FlowWidget from src.qt.main_window import Ui_MainWindow +from src.qt.utils.clickableLabels import FileOpenerLabel import src.qt.resources_rc # SIGQUIT is not defined on Windows @@ -2015,24 +2016,7 @@ class PreviewPanel(QWidget): image_layout.addWidget(self.preview_img) image_layout.setAlignment(self.preview_img, Qt.AlignmentFlag.AlignCenter) - class ClickableLabel(QLabel): - def __init__(self, text, parent=None): - super().__init__(text, parent) - - def setFilePath(self, filepath): - self.filepath = filepath - - def mousePressEvent(self, event): - super().mousePressEvent(event) - #open file - if hasattr(self, 'filepath'): - if os.path.exists(self.filepath): - os.startfile(self.filepath) - logging.info(f'Opening file: {self.filepath}') - else: - logging.error(f'File not found: {self.filepath}') - - self.file_label = ClickableLabel('Filename') + self.file_label = FileOpenerLabel('Filename') self.file_label.setWordWrap(True) self.file_label.setTextInteractionFlags( Qt.TextInteractionFlag.TextSelectableByMouse) diff --git a/tagstudio/src/qt/utils/clickableLabels.py b/tagstudio/src/qt/utils/clickableLabels.py new file mode 100644 index 00000000..12d4cba9 --- /dev/null +++ b/tagstudio/src/qt/utils/clickableLabels.py @@ -0,0 +1,20 @@ +import os +import logging +from PySide6.QtWidgets import QLabel + +class FileOpenerLabel(QLabel): + def __init__(self, text, parent=None): + super().__init__(text, parent) + + def setFilePath(self, filepath): + self.filepath = filepath + + def mousePressEvent(self, event): + super().mousePressEvent(event) + #open file + if hasattr(self, 'filepath'): + if os.path.exists(self.filepath): + os.startfile(self.filepath) + logging.info(f'Opening file: {self.filepath}') + else: + logging.error(f'File not found: {self.filepath}') \ No newline at end of file From bcf4453c8d3bbe60404b63bff14e62b3c04a2016 Mon Sep 17 00:00:00 2001 From: Matheus Cirillo Date: Fri, 26 Apr 2024 20:17:54 -0300 Subject: [PATCH 3/7] clickable label to the right place --- tagstudio/src/qt/ts_qt.py | 18 +++++++++++++++++- tagstudio/src/qt/utils/clickableLabels.py | 20 -------------------- 2 files changed, 17 insertions(+), 21 deletions(-) delete mode 100644 tagstudio/src/qt/utils/clickableLabels.py diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index 0e352a2a..3d8a593b 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -46,7 +46,6 @@ from src.core.ts_core import (TagStudioCore, TAG_COLORS, DATE_FIELDS, TEXT_FIELD from src.core.utils.web import strip_web_protocol from src.qt.flowlayout import FlowLayout, FlowWidget from src.qt.main_window import Ui_MainWindow -from src.qt.utils.clickableLabels import FileOpenerLabel import src.qt.resources_rc # SIGQUIT is not defined on Windows @@ -1970,6 +1969,23 @@ class AddFieldModal(QWidget): self.root_layout.addStretch(1) self.root_layout.addWidget(self.button_container) +class FileOpenerLabel(QLabel): + def __init__(self, text, parent=None): + super().__init__(text, parent) + + def setFilePath(self, filepath): + self.filepath = filepath + + def mousePressEvent(self, event): + super().mousePressEvent(event) + #open file + if hasattr(self, 'filepath'): + if os.path.exists(self.filepath): + os.startfile(self.filepath) + logging.info(f'Opening file: {self.filepath}') + else: + logging.error(f'File not found: {self.filepath}') + class PreviewPanel(QWidget): """The Preview Panel Widget.""" tags_updated = Signal() diff --git a/tagstudio/src/qt/utils/clickableLabels.py b/tagstudio/src/qt/utils/clickableLabels.py deleted file mode 100644 index 12d4cba9..00000000 --- a/tagstudio/src/qt/utils/clickableLabels.py +++ /dev/null @@ -1,20 +0,0 @@ -import os -import logging -from PySide6.QtWidgets import QLabel - -class FileOpenerLabel(QLabel): - def __init__(self, text, parent=None): - super().__init__(text, parent) - - def setFilePath(self, filepath): - self.filepath = filepath - - def mousePressEvent(self, event): - super().mousePressEvent(event) - #open file - if hasattr(self, 'filepath'): - if os.path.exists(self.filepath): - os.startfile(self.filepath) - logging.info(f'Opening file: {self.filepath}') - else: - logging.error(f'File not found: {self.filepath}') \ No newline at end of file From a9cbab40abbe7f468132f9fae61b481cd25b2e60 Mon Sep 17 00:00:00 2001 From: Matheus Cirillo Date: Fri, 26 Apr 2024 20:40:43 -0300 Subject: [PATCH 4/7] works as expected (open file in explorer) in windows --- tagstudio/src/qt/ts_qt.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index 3d8a593b..0a265e68 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -1970,21 +1970,28 @@ class AddFieldModal(QWidget): self.root_layout.addWidget(self.button_container) class FileOpenerLabel(QLabel): - def __init__(self, text, parent=None): - super().__init__(text, parent) + def __init__(self, text, parent=None): + super().__init__(text, parent) - def setFilePath(self, filepath): - self.filepath = filepath - - def mousePressEvent(self, event): - super().mousePressEvent(event) - #open file - if hasattr(self, 'filepath'): - if os.path.exists(self.filepath): - os.startfile(self.filepath) - logging.info(f'Opening file: {self.filepath}') - else: - logging.error(f'File not found: {self.filepath}') + def setFilePath(self, filepath): + self.filepath = filepath + + def mousePressEvent(self, event): + super().mousePressEvent(event) + #open file + if hasattr(self, 'filepath'): + if os.path.exists(self.filepath): + logging.info(f'Opening file: {self.filepath}') + if os.name == 'nt': # Windows + command = f'explorer /select,"{self.filepath}"' + subprocess.run(command, shell=True) + else: # macOS and Linux + command = f'nautilus --select "{self.filepath}"' # Adjust for your Linux file manager if different + if subprocess.run(command, shell=True).returncode == 0: + file_loc = os.path.dirname(self.filepath) + os.startfile(self.file_loc) + else: + logging.error(f'File not found: {self.filepath}') class PreviewPanel(QWidget): """The Preview Panel Widget.""" From 5bd2aaaf9ec9b570db2a3021bf5024487c07d025 Mon Sep 17 00:00:00 2001 From: Matheus Cirillo Date: Fri, 26 Apr 2024 22:04:40 -0300 Subject: [PATCH 5/7] context menu for thumbs --- tagstudio/src/qt/ts_qt.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index 0a265e68..950d445b 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -2742,6 +2742,8 @@ class ItemThumb(FlowWidget): """ The thumbnail widget for a library item (Entry, Collation, Tag Group, etc.). """ + on_click = Signal() + on_edit = Signal() update_cutoff: float = time.time() @@ -2871,6 +2873,14 @@ class ItemThumb(FlowWidget): # self.bg_button.setMinimumSize(*thumb_size) # self.bg_button.setMaximumSize(*thumb_size) + self.thumb_button.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu) + open_file_action = QAction('Open file', self) + open_file_action.triggered.connect(self.open_file) + open_explorer_action = QAction('Open file in explorer', self) + open_explorer_action.triggered.connect(self.open_explorer) + self.thumb_button.addAction(open_file_action) + self.thumb_button.addAction(open_explorer_action) + # Static Badges ======================================================== # Item Type Badge ------------------------------------------------------ @@ -2970,6 +2980,31 @@ class ItemThumb(FlowWidget): self.set_mode(mode) + def open_file(self): + entry = self.lib.get_entry(self.item_id) + filepath = os.path.normpath(f'{self.lib.library_dir}/{entry.path}/{entry.filename}') + if os.path.exists(filepath): + os.startfile(filepath) + logging.info(f'Opening file: {filepath}') + else: + logging.error(f'File not found: {filepath}') + + def open_explorer(self): + entry = self.lib.get_entry(self.item_id) + filepath = os.path.normpath(f'{self.lib.library_dir}/{entry.path}/{entry.filename}') + if os.path.exists(filepath): + logging.info(f'Opening file: {filepath}') + if os.name == 'nt': # Windows + command = f'explorer /select,"{filepath}"' + subprocess.run(command, shell=True) + else: # macOS and Linux + command = f'nautilus --select "{filepath}"' # Adjust for your Linux file manager if different + if subprocess.run(command, shell=True).returncode == 0: + file_loc = os.path.dirname(filepath) + os.startfile(file_loc) + else: + logging.error(f'File not found: {filepath}') + def set_mode(self, mode: Optional[ItemType]) -> None: if mode is None: self.unsetCursor() From 6831a8939277d211386c43189c26d69d0c7f39d5 Mon Sep 17 00:00:00 2001 From: Matheus Cirillo Date: Fri, 26 Apr 2024 23:19:24 -0300 Subject: [PATCH 6/7] file opening preview panel context menu --- tagstudio/src/qt/ts_qt.py | 98 +++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index 950d445b..69519bd8 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -1969,6 +1969,34 @@ class AddFieldModal(QWidget): self.root_layout.addStretch(1) self.root_layout.addWidget(self.button_container) +class FileOpenerHelper(): + def __init__(self, filepath:str): + self.filepath = filepath + + def set_filepath(self, filepath:str): + self.filepath = filepath + + def open_file(self): + if os.path.exists(self.filepath): + os.startfile(self.filepath) + logging.info(f'Opening file: {self.filepath}') + else: + logging.error(f'File not found: {self.filepath}') + + def open_explorer(self): + if os.path.exists(self.filepath): + logging.info(f'Opening file: {self.filepath}') + if os.name == 'nt': # Windows + command = f'explorer /select,"{self.filepath}"' + subprocess.run(command, shell=True) + else: # macOS and Linux + command = f'nautilus --select "{self.filepath}"' # Adjust for your Linux file manager if different + if subprocess.run(command, shell=True).returncode == 0: + file_loc = os.path.dirname(self.filepath) + file_loc = os.path.normpath(file_loc) + os.startfile(file_loc) + else: + logging.error(f'File not found: {self.filepath}') class FileOpenerLabel(QLabel): def __init__(self, text, parent=None): super().__init__(text, parent) @@ -1978,20 +2006,8 @@ class FileOpenerLabel(QLabel): def mousePressEvent(self, event): super().mousePressEvent(event) - #open file - if hasattr(self, 'filepath'): - if os.path.exists(self.filepath): - logging.info(f'Opening file: {self.filepath}') - if os.name == 'nt': # Windows - command = f'explorer /select,"{self.filepath}"' - subprocess.run(command, shell=True) - else: # macOS and Linux - command = f'nautilus --select "{self.filepath}"' # Adjust for your Linux file manager if different - if subprocess.run(command, shell=True).returncode == 0: - file_loc = os.path.dirname(self.filepath) - os.startfile(self.file_loc) - else: - logging.error(f'File not found: {self.filepath}') + opener = FileOpenerHelper(self.filepath) + opener.open_explorer() class PreviewPanel(QWidget): """The Preview Panel Widget.""" @@ -2028,6 +2044,13 @@ class PreviewPanel(QWidget): self.preview_img = QPushButton() self.preview_img.setMinimumSize(*self.img_button_size) self.preview_img.setFlat(True) + + self.preview_img.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu) + self.open_file_action = QAction('Open file', self) + self.open_explorer_action = QAction('Open file in explorer', self) + + self.preview_img.addAction(self.open_file_action) + self.preview_img.addAction(self.open_explorer_action) self.tr = ThumbRenderer() self.tr.updated.connect(lambda ts, i, s: (self.preview_img.setIcon(i))) self.tr.updated_ratio.connect(lambda ratio: (self.set_image_ratio(ratio), @@ -2246,6 +2269,7 @@ class PreviewPanel(QWidget): if len(self.driver.selected) == 0: if len(self.selected) != 0 or not self.initialized: self.file_label.setText(f"No Items Selected") + self.file_label.setFilePath('') self.dimensions_label.setText("") ratio: float = self.devicePixelRatio() self.tr.render_big(time.time(), '', (512, 512), ratio, True) @@ -2275,6 +2299,10 @@ class PreviewPanel(QWidget): self.tr.render_big(time.time(), filepath, (512, 512), ratio) self.file_label.setText("\u200b".join(filepath)) + opener = FileOpenerHelper(filepath) + self.open_file_action.triggered.connect(opener.open_file) + self.open_explorer_action.triggered.connect(opener.open_explorer) + # TODO: Do this somewhere else, this is just here temporarily. extension = os.path.splitext(filepath)[1][1:].lower() try: @@ -2347,6 +2375,7 @@ class PreviewPanel(QWidget): elif len(self.driver.selected) > 1: if self.selected != self.driver.selected: self.file_label.setText(f"{len(self.driver.selected)} Items Selected") + self.file_label.setFilePath('') self.dimensions_label.setText("") ratio: float = self.devicePixelRatio() self.tr.render_big(time.time(), '', (512, 512), ratio, True) @@ -2742,9 +2771,6 @@ class ItemThumb(FlowWidget): """ The thumbnail widget for a library item (Entry, Collation, Tag Group, etc.). """ - on_click = Signal() - on_edit = Signal() - update_cutoff: float = time.time() collation_icon_128: Image.Image = Image.open(os.path.normpath( @@ -2874,10 +2900,11 @@ class ItemThumb(FlowWidget): # self.bg_button.setMaximumSize(*thumb_size) self.thumb_button.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu) + self.opener = FileOpenerHelper('') open_file_action = QAction('Open file', self) - open_file_action.triggered.connect(self.open_file) + open_file_action.triggered.connect(lambda: print('Open file')) open_explorer_action = QAction('Open file in explorer', self) - open_explorer_action.triggered.connect(self.open_explorer) + open_explorer_action.triggered.connect(self.opener.open_explorer) self.thumb_button.addAction(open_file_action) self.thumb_button.addAction(open_explorer_action) @@ -2980,31 +3007,6 @@ class ItemThumb(FlowWidget): self.set_mode(mode) - def open_file(self): - entry = self.lib.get_entry(self.item_id) - filepath = os.path.normpath(f'{self.lib.library_dir}/{entry.path}/{entry.filename}') - if os.path.exists(filepath): - os.startfile(filepath) - logging.info(f'Opening file: {filepath}') - else: - logging.error(f'File not found: {filepath}') - - def open_explorer(self): - entry = self.lib.get_entry(self.item_id) - filepath = os.path.normpath(f'{self.lib.library_dir}/{entry.path}/{entry.filename}') - if os.path.exists(filepath): - logging.info(f'Opening file: {filepath}') - if os.name == 'nt': # Windows - command = f'explorer /select,"{filepath}"' - subprocess.run(command, shell=True) - else: # macOS and Linux - command = f'nautilus --select "{filepath}"' # Adjust for your Linux file manager if different - if subprocess.run(command, shell=True).returncode == 0: - file_loc = os.path.dirname(filepath) - os.startfile(file_loc) - else: - logging.error(f'File not found: {filepath}') - def set_mode(self, mode: Optional[ItemType]) -> None: if mode is None: self.unsetCursor() @@ -3100,7 +3102,15 @@ class ItemThumb(FlowWidget): def set_item_id(self, id: int): + ''' + also sets the filepath for the file opener + ''' self.item_id = id + if(id == -1): + return + entry = self.lib.get_entry(self.item_id) + filepath = os.path.normpath(f'{self.lib.library_dir}/{entry.path}/{entry.filename}') + self.opener.set_filepath(filepath) def assign_favorite(self, value: bool): # Switching mode to None to bypass mode-specific operations when the From 955d4a0c9f2b2419061b363aced4a2df646a3362 Mon Sep 17 00:00:00 2001 From: Matheus Cirillo Date: Sat, 27 Apr 2024 09:44:04 -0300 Subject: [PATCH 7/7] fixed bug where it wouldn't open outside debug mode --- tagstudio/src/qt/ts_qt.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index 69519bd8..8adaad91 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -2046,6 +2046,7 @@ class PreviewPanel(QWidget): self.preview_img.setFlat(True) self.preview_img.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu) + self.opener = FileOpenerHelper('') self.open_file_action = QAction('Open file', self) self.open_explorer_action = QAction('Open file in explorer', self) @@ -2271,6 +2272,7 @@ class PreviewPanel(QWidget): self.file_label.setText(f"No Items Selected") self.file_label.setFilePath('') self.dimensions_label.setText("") + self.preview_img.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu) ratio: float = self.devicePixelRatio() self.tr.render_big(time.time(), '', (512, 512), ratio, True) try: @@ -2299,9 +2301,10 @@ class PreviewPanel(QWidget): self.tr.render_big(time.time(), filepath, (512, 512), ratio) self.file_label.setText("\u200b".join(filepath)) - opener = FileOpenerHelper(filepath) - self.open_file_action.triggered.connect(opener.open_file) - self.open_explorer_action.triggered.connect(opener.open_explorer) + self.preview_img.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu) + self.opener = FileOpenerHelper(filepath) + self.open_file_action.triggered.connect(self.opener.open_file) + self.open_explorer_action.triggered.connect(self.opener.open_explorer) # TODO: Do this somewhere else, this is just here temporarily. extension = os.path.splitext(filepath)[1][1:].lower() @@ -2377,6 +2380,7 @@ class PreviewPanel(QWidget): self.file_label.setText(f"{len(self.driver.selected)} Items Selected") self.file_label.setFilePath('') self.dimensions_label.setText("") + self.preview_img.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu) ratio: float = self.devicePixelRatio() self.tr.render_big(time.time(), '', (512, 512), ratio, True) try: @@ -2902,7 +2906,7 @@ class ItemThumb(FlowWidget): self.thumb_button.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu) self.opener = FileOpenerHelper('') open_file_action = QAction('Open file', self) - open_file_action.triggered.connect(lambda: print('Open file')) + open_file_action.triggered.connect(self.opener.open_file) open_explorer_action = QAction('Open file in explorer', self) open_explorer_action.triggered.connect(self.opener.open_explorer) self.thumb_button.addAction(open_file_action)