Fix open_explorer on Mac, fix right click on FileOpenerLabel

This commit is contained in:
gabrieljreed 2024-05-01 21:41:11 -07:00
parent a10478bc8b
commit 81f550a543

View file

@ -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