1 | # global |
---|
2 | import os |
---|
3 | from PyQt5 import QtCore |
---|
4 | from PyQt5 import QtGui |
---|
5 | from PyQt5 import QtWidgets |
---|
6 | |
---|
7 | # UI |
---|
8 | from sas.qtgui.UI import main_resources_rc |
---|
9 | from sas.qtgui.MainWindow.UI.DataExplorerUI import Ui_DataLoadWidget |
---|
10 | |
---|
11 | class 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() |
---|