Skip to content

[Refactor] type fixes and minor improvements to file_opener.py #876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/tagstudio/qt/helpers/file_opener.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import sys
import traceback
from pathlib import Path
from typing import override

import structlog
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QLabel
from PySide6.QtGui import QMouseEvent
from PySide6.QtWidgets import QLabel, QWidget

from tagstudio.qt.helpers.silent_popen import silent_Popen

Expand Down Expand Up @@ -115,36 +117,40 @@ def open_explorer(self):


class FileOpenerLabel(QLabel):
def __init__(self, parent=None):
def __init__(self, parent: QWidget | None = None) -> None:
"""Initialize the FileOpenerLabel.

Args:
parent (QWidget, optional): The parent widget. Defaults to None.
"""
self.filepath: str | Path | None = None

super().__init__(parent)

def set_file_path(self, filepath):
def set_file_path(self, filepath: str | Path) -> None:
"""Set the filepath to open.

Args:
filepath (str): The path to the file to open.
"""
self.filepath = filepath

def mousePressEvent(self, event): # noqa: N802
@override
def mousePressEvent(self, ev: QMouseEvent) -> None:
"""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.
ev (QMouseEvent): The mouse press event.
"""
super().mousePressEvent(event)

if event.button() == Qt.MouseButton.LeftButton:
if ev.button() == Qt.MouseButton.LeftButton:
assert self.filepath is not None, "File path is not set"
opener = FileOpenerHelper(self.filepath)
opener.open_explorer()
elif event.button() == Qt.MouseButton.RightButton:
elif ev.button() == Qt.MouseButton.RightButton:
# Show context menu
pass
else:
super().mousePressEvent(ev)