From 81f550a5436f6fa20b7a8ef14ca789e4c56668da Mon Sep 17 00:00:00 2001 From: gabrieljreed Date: Wed, 1 May 2024 21:41:11 -0700 Subject: [PATCH 1/4] 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 From 08c0704926b119e41498368c5f74cfa69666f765 Mon Sep 17 00:00:00 2001 From: gabrieljreed Date: Wed, 1 May 2024 21:42:58 -0700 Subject: [PATCH 2/4] Fix open_file for platforms other than Windows --- tagstudio/src/qt/helpers/file_opener.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tagstudio/src/qt/helpers/file_opener.py b/tagstudio/src/qt/helpers/file_opener.py index 631892e7..ca3f9514 100644 --- a/tagstudio/src/qt/helpers/file_opener.py +++ b/tagstudio/src/qt/helpers/file_opener.py @@ -25,11 +25,16 @@ class FileOpenerHelper(): 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: + if not os.path.exists(self.filepath): logging.error(f'File not found: {self.filepath}') + return + + if sys.platform == 'win32': + os.startfile(self.filepath) + elif sys.platform == 'linux': + subprocess.run(['xdg-open', self.filepath]) + elif sys.platform == 'darwin': + subprocess.run(['open', self.filepath]) def open_explorer(self): if os.path.exists(self.filepath): @@ -47,7 +52,7 @@ class FileOpenerHelper(): command = f'open -R "{self.filepath}"' result = subprocess.run(command, shell=True) if result.returncode == 0: - print('Opening file in Finder') + logging.info('Opening file in Finder') else: logging.error(f'Failed to open file in Finder: {self.filepath}') else: From 85b4be85250681045684222eeac072e8a05b511b Mon Sep 17 00:00:00 2001 From: gabrieljreed Date: Wed, 1 May 2024 21:45:43 -0700 Subject: [PATCH 3/4] Add docstrings --- tagstudio/src/qt/helpers/file_opener.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tagstudio/src/qt/helpers/file_opener.py b/tagstudio/src/qt/helpers/file_opener.py index ca3f9514..ef2b6697 100644 --- a/tagstudio/src/qt/helpers/file_opener.py +++ b/tagstudio/src/qt/helpers/file_opener.py @@ -25,6 +25,7 @@ class FileOpenerHelper(): self.filepath = filepath def open_file(self): + """Open the file in the default program.""" if not os.path.exists(self.filepath): logging.error(f'File not found: {self.filepath}') return @@ -37,6 +38,7 @@ class FileOpenerHelper(): subprocess.run(['open', self.filepath]) def open_explorer(self): + """Open the file in the default file explorer.""" if os.path.exists(self.filepath): logging.info(f'Opening file: {self.filepath}') if os.name == 'nt': # Windows From eb5124bd0a7bc37a1031ee3ee52fbedb48fc7cbc Mon Sep 17 00:00:00 2001 From: gabrieljreed Date: Wed, 1 May 2024 22:01:08 -0700 Subject: [PATCH 4/4] Update docstrings --- tagstudio/src/qt/helpers/file_opener.py | 36 ++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/tagstudio/src/qt/helpers/file_opener.py b/tagstudio/src/qt/helpers/file_opener.py index e30bedb0..82461242 100644 --- a/tagstudio/src/qt/helpers/file_opener.py +++ b/tagstudio/src/qt/helpers/file_opener.py @@ -20,6 +20,13 @@ logging.basicConfig(format="%(message)s", level=logging.INFO) def open_file(path: str, file_manager: bool = False): + """Open a file in the default application or file explorer. + + Args: + path (str): The path to the file to open. + file_manager (bool, optional): Whether to open the file in the file manager (e.g. Finder on macOS). + Defaults to False. + """ logging.info(f'Opening file: {path}') if not os.path.exists(path): logging.error(f'File not found: {path}') @@ -62,24 +69,47 @@ def open_file(path: str, file_manager: bool = False): class FileOpenerHelper: - def __init__(self, filepath:str): + def __init__(self, filepath: str): + """Initialize the FileOpenerHelper. + + Args: + filepath (str): The path to the file to open. + """ self.filepath = filepath - def set_filepath(self, filepath:str): + def set_filepath(self, filepath: str): + """Set the filepath to open. + + Args: + filepath (str): The path to the file to open. + """ self.filepath = filepath def open_file(self): + """Open the file in the default application.""" open_file(self.filepath) def open_explorer(self): - open_file(self.filepath, True) + """Open the file in the default file explorer.""" + open_file(self.filepath, file_manager=True) class FileOpenerLabel(QLabel): def __init__(self, text, parent=None): + """Initialize the FileOpenerLabel. + + Args: + text (str): The text to display. + parent (QWidget, optional): The parent widget. Defaults to None. + """ super().__init__(text, parent) def setFilePath(self, filepath): + """Set the filepath to open. + + Args: + filepath (str): The path to the file to open. + """ self.filepath = filepath def mousePressEvent(self, event):