Added the option to delete files in the right click context menu

This commit is contained in:
peterbousaada
2024-06-12 19:14:36 -04:00
committed by Travis Abendshien
parent a037a3b1e2
commit e0cc0dd5a7
2 changed files with 53 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
import logging
import os.path
import subprocess
import sys
import traceback
from pathlib import Path
from typing import Callable
ERROR = f"[ERROR]"
WARNING = f"[WARNING]"
INFO = f"[INFO]"
logging.basicConfig(format="%(message)s", level=logging.INFO)
def delete_file(path: str | Path, callback: Callable):
_path = str(path)
logging.info(f"Deleting file: {_path}")
if not os.path.exists(_path):
logging.error(f"File not found: {_path}")
return
try:
os.remove(path)
callback()
except:
traceback.print_exc()
class FileDeleterHelper:
def __init__(self, filepath: str | Path):
self.filepath = str(filepath)
def set_filepath(self, filepath: str | Path):
self.filepath = str(filepath)
def set_delete_callback(self, callback: Callable):
self.delete_callback = callback
def delete_file(self):
delete_file(self.filepath, self.delete_callback)

View File

@@ -30,6 +30,7 @@ from src.core.constants import (
from src.core.media_types import MediaCategories, MediaType
from src.qt.flowlayout import FlowWidget
from src.qt.helpers.file_opener import FileOpenerHelper
from src.qt.helpers.file_deleter import FileDeleterHelper
from src.qt.widgets.thumb_renderer import ThumbRenderer
from src.qt.widgets.thumb_button import ThumbButton
@@ -193,12 +194,16 @@ class ItemThumb(FlowWidget):
self.thumb_button.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu)
self.opener = FileOpenerHelper("")
self.deleter = FileDeleterHelper("")
open_file_action = QAction("Open file", self)
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)
delete_action = QAction("Delete", self)
delete_action.triggered.connect(self.deleter.delete_file)
self.thumb_button.addAction(open_file_action)
self.thumb_button.addAction(open_explorer_action)
self.thumb_button.addAction(delete_action)
# Static Badges ========================================================
@@ -440,6 +445,8 @@ class ItemThumb(FlowWidget):
entry = self.lib.get_entry(self.item_id)
filepath = self.lib.library_dir / entry.path / entry.filename
self.opener.set_filepath(filepath)
self.deleter.set_filepath(filepath)
self.deleter.set_delete_callback(self._on_delete)
def assign_favorite(self, value: bool):
# Switching mode to None to bypass mode-specific operations when the
@@ -540,3 +547,9 @@ class ItemThumb(FlowWidget):
mimedata.setUrls(paths)
drag.setMimeData(mimedata)
drag.exec(Qt.DropAction.CopyAction)
def _on_delete(self):
entry = self.lib.get_entry(self.item_id)
self.lib.remove_entry(self.item_id)
self.panel.driver.purge_item_from_navigation(entry.type, self.item_id)
self.panel.driver.filter_items()