drag+drop support

This commit is contained in:
2026-09-10 11:18:00 -07:00
parent cd0b55df34
commit 613d2b9103
4 changed files with 162 additions and 15 deletions
+50 -1
View File
@@ -22,6 +22,10 @@ from datetime import datetime
from functools import partial
from multiprocessing import Process, current_process, freeze_support, Queue, set_start_method
# Fix for plotting on linux
if sys.platform.startswith("linux"):
os.environ["QT_QPA_PLATFORM"] = "xcb"
# External library imports
import psutil
@@ -535,6 +539,49 @@ class ThemeChangeWatcher(QObject):
self.main_window.update_theme()
class DropScrollArea(QScrollArea):
"""QScrollArea that accepts .snirf file and folder drops."""
def __init__(self, on_files_dropped_callback, parent=None):
super().__init__(parent)
self.on_files_dropped_callback = on_files_dropped_callback
self.setAcceptDrops(True)
# ScrollAreas require setting acceptDrops on their viewport as well
self.viewport().setAcceptDrops(True)
def dragEnterEvent(self, event) -> None:
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event) -> None:
if not event.mimeData().hasUrls():
return
dropped_paths = [
url.toLocalFile() for url in event.mimeData().urls() if url.isLocalFile()
]
files_to_load = []
for path_str in dropped_paths:
p = Path(path_str)
if p.is_file() and p.suffix.lower() == ".snirf":
files_to_load.append(os.path.normpath(str(p)))
elif p.is_dir():
files_to_load.extend(
os.path.normpath(str(f)) for f in p.rglob("*.snirf")
)
if files_to_load:
self.on_files_dropped_callback(files_to_load)
else:
QMessageBox.information(
self,
"No Valid Files",
"None of the dropped items were .snirf files or directories containing .snirf files.",
)
class MainApplication(QMainWindow):
"""
Main application window that creates and sets up the UI.
@@ -715,7 +762,9 @@ class MainApplication(QMainWindow):
self.bubble_layout = QGridLayout(self.bubble_container)
self.bubble_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.scroll_area = QScrollArea()
self.scroll_area = DropScrollArea(
on_files_dropped_callback=self.project_manager.load_dropped_files
)
self.scroll_area.setWidgetResizable(True)
self.scroll_area.setWidget(self.bubble_container)
self.scroll_area.setMinimumHeight(200)