From 81f550a5436f6fa20b7a8ef14ca789e4c56668da Mon Sep 17 00:00:00 2001 From: gabrieljreed Date: Wed, 1 May 2024 21:41:11 -0700 Subject: [PATCH] Fix open_explorer on Mac, fix right click on FileOpenerLabel --- tagstudio/src/qt/helpers/file_opener.py | 45 ++++++++++++++++++------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/tagstudio/src/qt/helpers/file_opener.py b/tagstudio/src/qt/helpers/file_opener.py index e6703f11..631892e7 100644 --- a/tagstudio/src/qt/helpers/file_opener.py +++ b/tagstudio/src/qt/helpers/file_opener.py @@ -5,8 +5,10 @@ import logging import os import subprocess +import sys from PySide6.QtWidgets import QLabel +from PySide6.QtCore import Qt ERROR = f'[ERROR]' WARNING = f'[WARNING]' @@ -31,16 +33,23 @@ class FileOpenerHelper(): 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) + logging.info(f'Opening file: {self.filepath}') + if os.name == 'nt': # Windows + command = f'explorer /select,"{self.filepath}"' + subprocess.run(command, shell=True) + elif sys.platform == '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) + elif sys.platform == 'darwin': + command = f'open -R "{self.filepath}"' + result = subprocess.run(command, shell=True) + if result.returncode == 0: + print('Opening file in Finder') + else: + logging.error(f'Failed to open file in Finder: {self.filepath}') else: logging.error(f'File not found: {self.filepath}') @@ -53,6 +62,18 @@ class FileOpenerLabel(QLabel): self.filepath = filepath def mousePressEvent(self, event): + """Handle mouse press events. + + On a left click, open the file in the default file explorer. On a right click, show a context menu. + + Args: + event (QMouseEvent): The mouse press event. + """ super().mousePressEvent(event) - opener = FileOpenerHelper(self.filepath) - opener.open_explorer() + + if event.button() == Qt.LeftButton: + opener = FileOpenerHelper(self.filepath) + opener.open_explorer() + elif event.button() == Qt.RightButton: + # Show context menu + pass