Skip to content

Commit

Permalink
Merge pull request #135 from Victor-IX/sending2trash
Browse files Browse the repository at this point in the history
Allow sending files to trash instead of deleting
  • Loading branch information
Victor-IX authored Sep 12, 2024
2 parents 4b24e57 + 558c9ff commit e1e0ddd
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 9 deletions.
12 changes: 11 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
"distro>=1.8.0",
"semver>=3.0.2",
"zstandard>=0.23.0",
"send2trash>=1.8.3",
]
requires-python = ">=3.9, <3.11"
readme = "README.md"
Expand Down
11 changes: 8 additions & 3 deletions source/threads/remover.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@

from modules.task import Task
from PyQt5.QtCore import pyqtSignal
from send2trash import send2trash


@dataclass
class RemovalTask(Task):
path: Path
trash: bool = True
finished = pyqtSignal(bool)

def run(self):
try:
if self.path.is_dir():
rmtree(self.path.as_posix())
if self.trash:
send2trash(str(self.path))
else:
self.path.unlink()
if self.path.is_dir():
rmtree(self.path.as_posix())
else:
self.path.unlink()

self.finished.emit(0)
except OSError:
Expand Down
18 changes: 15 additions & 3 deletions source/widgets/base_menu_widget.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QCursor, QKeyEvent
from PyQt5.QtWidgets import QDesktopWidget, QMenu


class BaseMenuWidget(QMenu):
action_height = 30
holding_shift = pyqtSignal(bool)

def __init__(self, title="", parent=None):
super().__init__(title=title, parent=parent)
self.setWindowFlags(self.windowFlags() | Qt.NoDropShadowWindowHint)
self.setWindowFlags(self.windowFlags() | Qt.WindowType.NoDropShadowWindowHint)
self.action_height = BaseMenuWidget.action_height
self.screen_size = QDesktopWidget().screenGeometry()
self.setToolTipsVisible(True)
Expand Down Expand Up @@ -47,3 +48,14 @@ def trigger(self):
i = i + 1

self.exec_(cursor)

def enable_shifting(self):
""" This is an optional feature because it can be very expensive to do this all the time. """
self.installEventFilter(self)


def eventFilter(self, obj, event):
if isinstance(event, QKeyEvent):
self.holding_shift.emit(event.modifiers() in (Qt.KeyboardModifier.ShiftModifier, Qt.KeyboardModifier.ControlModifier))

return super().eventFilter(obj, event)
52 changes: 50 additions & 2 deletions source/widgets/library_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ def draw(self, build_info: BuildInfo):
self.menu_extended = BaseMenuWidget(parent=self)
self.menu_extended.setFont(self.parent.font_10)

# For checking if shift is held on menus
self.menu.enable_shifting()
self.menu_extended.enable_shifting()
self.menu.holding_shift.connect(self.update_delete_action)
self.menu_extended.holding_shift.connect(self.update_delete_action)

self.deleteAction = QAction("Delete From Drive", self)
self.deleteAction.setIcon(self.parent.icons.delete)
self.deleteAction.triggered.connect(self.ask_remove_from_drive)
Expand Down Expand Up @@ -314,6 +320,8 @@ def context_menu(self):
if self.is_damaged:
return

self.update_delete_action(self.hovering_and_shifting)

if len(self.list_widget.selectedItems()) > 1:
self.menu_extended.trigger()
return
Expand All @@ -327,6 +335,13 @@ def context_menu(self):

self.menu.trigger()

@pyqtSlot(bool)
def update_delete_action(self, shifting: bool):
if shifting:
self.deleteAction.setText("Delete from Drive")
else:
self.deleteAction.setText("Send to Trash")

def mouseDoubleClickEvent(self, _event):
if self.build_info is not None and self.hovering_and_shifting:
self.launch(launch_mode=LaunchOpenLast())
Expand Down Expand Up @@ -512,6 +527,13 @@ def build_info_writer_finished(self):

@QtCore.pyqtSlot()
def ask_remove_from_drive(self):

# if not shift clicked, ask to send to trash instead of deleting
mod = QApplication.keyboardModifiers()
if mod not in (Qt.KeyboardModifier.ShiftModifier, Qt.KeyboardModifier.ControlModifier):
self.ask_send_to_trash()
return

self.item.setSelected(True)
self.dlg = DialogWindow(
parent=self.parent,
Expand All @@ -533,17 +555,43 @@ def remove_from_drive_extended(self):
self.list_widget.itemWidget(item).remove_from_drive()

@QtCore.pyqtSlot()
def remove_from_drive(self):
def remove_from_drive(self, trash=False):
if self.parent_widget is not None:
self.parent_widget.remove_from_drive()
return

path = Path(get_library_folder()) / self.link
a = RemovalTask(path)
a = RemovalTask(path, trash=trash)
a.finished.connect(self.remover_completed)
self.parent.task_queue.append(a)
self.remover_started()

@QtCore.pyqtSlot()
def ask_send_to_trash(self):
self.item.setSelected(True)
self.dlg = DialogWindow(
parent=self.parent,
title="Warning",
text="Are you sure you want to<br> \
send selected builds to trash?",
accept_text="Yes",
cancel_text="No",
)

if len(self.list_widget.selectedItems()) > 1:
self.dlg.accepted.connect(self.send_to_trash_extended)
else:
self.dlg.accepted.connect(self.send_to_trash)

@QtCore.pyqtSlot()
def send_to_trash_extended(self):
for item in self.list_widget.selectedItems():
self.list_widget.itemWidget(item).remove_from_drive(trash=True)

@QtCore.pyqtSlot()
def send_to_trash(self):
self.remove_from_drive(trash=True)

# TODO Clear icon if build in quick launch
def remover_started(self):
self.launchButton.set_text("Deleting")
Expand Down

0 comments on commit e1e0ddd

Please sign in to comment.