Skip to content

Commit e7983c1

Browse files
committed
drag drop
1 parent d393e48 commit e7983c1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

backgroundremoval.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ def __init__(self, scene, name="View", parent=None):
176176
self.scene().addItem(self.brush_cursor_item)
177177
self.brush_cursor_item.hide()
178178

179+
def dragEnterEvent(self, event):
180+
# Pass the event to the parent to handle
181+
event.ignore()
182+
183+
def dropEvent(self, event):
184+
# Pass the event to the parent to handle
185+
event.ignore()
186+
179187
def set_sibling(self, sibling_view):
180188
self.sibling = sibling_view
181189
self.horizontalScrollBar().valueChanged.connect(self.sync_scroll_h)
@@ -738,6 +746,7 @@ def __init__(self, image_paths):
738746
self.load_blank_image()
739747

740748
def init_ui(self):
749+
self.setAcceptDrops(True)
741750
main = QWidget(); self.setCentralWidget(main)
742751
layout = QHBoxLayout(main)
743752

@@ -896,6 +905,8 @@ def make_slider_row(lbl_text, min_v, max_v, def_v):
896905
self.view_input = SynchronizedGraphicsView(self.scene_input, name="Input View")
897906
self.view_input.set_controller(self)
898907
self.view_input.setBackgroundBrush(QBrush(QColor(220, 220, 220), Qt.BrushStyle.DiagCrossPattern))
908+
self.view_input.setAcceptDrops(True)
909+
899910

900911
self.input_pixmap_item = QGraphicsPixmapItem(); self.scene_input.addItem(self.input_pixmap_item)
901912
self.overlay_pixmap_item = QGraphicsPixmapItem(); self.overlay_pixmap_item.setOpacity(0.5); self.scene_input.addItem(self.overlay_pixmap_item)
@@ -910,6 +921,7 @@ def make_slider_row(lbl_text, min_v, max_v, def_v):
910921
self.view_output = SynchronizedGraphicsView(self.scene_output, name="Output View")
911922
self.view_output.set_controller(self)
912923
self.view_output.setBackgroundBrush(QBrush(QColor(220, 220, 220), Qt.BrushStyle.DiagCrossPattern))
924+
self.view_output.setAcceptDrops(True)
913925

914926
self.output_pixmap_item = QGraphicsPixmapItem()
915927
self.scene_output.addItem(self.output_pixmap_item)
@@ -935,6 +947,30 @@ def make_slider_row(lbl_text, min_v, max_v, def_v):
935947
self.statusBar().addPermanentWidget(self.progress_bar) # Add to right side
936948
self.statusBar().addPermanentWidget(self.zoom_label)
937949

950+
def dragEnterEvent(self, event):
951+
if event.mimeData().hasUrls():
952+
# Check if any of the files are images
953+
supported_exts = ['.png', '.jpg', '.jpeg', '.webp', '.bmp', '.tif', '.tiff']
954+
for url in event.mimeData().urls():
955+
if url.isLocalFile():
956+
path = url.toLocalFile()
957+
if os.path.splitext(path)[1].lower() in supported_exts:
958+
event.acceptProposedAction()
959+
return
960+
super().dragEnterEvent(event)
961+
962+
def dropEvent(self, event):
963+
urls = event.mimeData().urls()
964+
if urls:
965+
supported_exts = ['.png', '.jpg', '.jpeg', '.webp', '.bmp', '.tif', '.tiff']
966+
fnames = [url.toLocalFile() for url in urls if url.isLocalFile() and os.path.splitext(url.toLocalFile())[1].lower() in supported_exts]
967+
968+
if fnames:
969+
self.image_paths = sorted(fnames)
970+
self.current_image_index = 0
971+
self.load_image(self.image_paths[0])
972+
super().dropEvent(event)
973+
938974
def update_window_title(self): # <--- NEW METHOD
939975
base_title = "Interactive Image Background Remover"
940976

0 commit comments

Comments
 (0)