From 6831a8939277d211386c43189c26d69d0c7f39d5 Mon Sep 17 00:00:00 2001 From: Matheus Cirillo Date: Fri, 26 Apr 2024 23:19:24 -0300 Subject: [PATCH] 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