source: sasview/src/sas/qtgui/MainWindow/DroppableDataLoadWidget.py @ ba400d1

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since ba400d1 was ba400d1, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 6 months ago

Allow directories (and mixed file/dir) to be dropped onto Data Explorer.
SASVIEW-1102

  • Property mode set to 100644
File size: 2.4 KB
Line 
1# global
2import os
3from PyQt5 import QtCore
4from PyQt5 import QtGui
5from PyQt5 import QtWidgets
6
7# UI
8from sas.qtgui.UI import main_resources_rc
9from sas.qtgui.MainWindow.UI.DataExplorerUI import Ui_DataLoadWidget
10
11class DroppableDataLoadWidget(QtWidgets.QTabWidget, Ui_DataLoadWidget):
12    """
13    Overwrite drag and drop methods in the base class
14    so users can drop files directly onto the Data Explorer
15    """
16    def __init__(self, parent=None, guimanager=None):
17        super(DroppableDataLoadWidget, self).__init__(parent)
18        self.setupUi(self)
19
20        # Enable file drag-drop on treeView
21        self.setAcceptDrops(True)
22        self.communicator = guimanager.communicator()
23        flags = QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowSystemMenuHint
24        self.setWindowFlags(flags)
25
26    def dragIsOK(self, event):
27        """
28        Return True if the event contain URLs
29        """
30        # Analyze mime data
31        return bool(event.mimeData().hasUrls() and self.currentIndex() == 0)
32
33    def dragEnterEvent(self, event):
34        """
35        Called automatically on a drag into the treeview
36        """
37        if self.dragIsOK(event):
38            event.setDropAction(QtCore.Qt.CopyAction)
39            event.accept()
40        else:
41            event.ignore()
42
43    def dragMoveEvent(self, event):
44        """
45        Called automatically when a drag is
46        moved inside the treeview
47        """
48        if self.dragIsOK(event):
49            event.accept()
50        else:
51            event.ignore()
52
53    def dropEvent(self, event):
54        """
55        Called automatically when a drop
56        is added to the treeview.
57        """
58        if self.dragIsOK(event):
59            filenames=[]
60            for url in event.mimeData().urls():
61                files = url.toLocalFile()
62                if os.path.isdir(files):
63                # get content of dir into a list
64                    content = [os.path.join(os.path.abspath(files), filename)
65                                for filename in os.listdir(files)]
66                    filenames += content
67                else:
68                    filenames.append(files)
69            self.communicator.fileReadSignal.emit(filenames)
70            event.accept()
71        else:
72            event.ignore()
73
74    def closeEvent(self, event):
75        """
76        Overwrite the close event - no close!
77        """
78        event.ignore()
Note: See TracBrowser for help on using the repository browser.