1 | # global |
---|
2 | from PyQt4 import QtCore |
---|
3 | |
---|
4 | # UI |
---|
5 | from UI.DataExplorerUI import DataLoadWidget |
---|
6 | |
---|
7 | class DroppableDataLoadWidget(DataLoadWidget): |
---|
8 | """ |
---|
9 | Overwrite drag and drop methods in the base class |
---|
10 | so users can drop files directly onto the Data Explorer |
---|
11 | """ |
---|
12 | def __init__(self, parent=None, guimanager=None): |
---|
13 | super(DroppableDataLoadWidget, self).__init__(parent) |
---|
14 | |
---|
15 | # Enable file drag-drop on treeView |
---|
16 | self.setAcceptDrops(True) |
---|
17 | self.communicator = guimanager.communicator() |
---|
18 | flags = QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowSystemMenuHint |
---|
19 | self.setWindowFlags(flags) |
---|
20 | |
---|
21 | def dragIsOK(self, event): |
---|
22 | """ |
---|
23 | Return True if the event contain URLs |
---|
24 | """ |
---|
25 | # Analyze mime data |
---|
26 | return bool(event.mimeData().hasUrls() and self.currentIndex() == 0) |
---|
27 | |
---|
28 | def dragEnterEvent(self, event): |
---|
29 | """ |
---|
30 | Called automatically on a drag into the treeview |
---|
31 | """ |
---|
32 | if self.dragIsOK(event): |
---|
33 | event.setDropAction(QtCore.Qt.CopyAction) |
---|
34 | event.accept() |
---|
35 | else: |
---|
36 | event.ignore() |
---|
37 | |
---|
38 | def dragMoveEvent(self, event): |
---|
39 | """ |
---|
40 | Called automatically when a drag is |
---|
41 | moved inside the treeview |
---|
42 | """ |
---|
43 | if self.dragIsOK(event): |
---|
44 | event.accept() |
---|
45 | else: |
---|
46 | event.ignore() |
---|
47 | |
---|
48 | def dropEvent(self, event): |
---|
49 | """ |
---|
50 | Called automatically when a drop |
---|
51 | is added to the treeview. |
---|
52 | """ |
---|
53 | if self.dragIsOK(event): |
---|
54 | filenames=[] |
---|
55 | for url in event.mimeData().urls(): |
---|
56 | filenames.append(str(url.toLocalFile())) |
---|
57 | self.communicator.fileReadSignal.emit(filenames) |
---|
58 | event.accept() |
---|
59 | else: |
---|
60 | event.ignore() |
---|
61 | |
---|
62 | def closeEvent(self, event): |
---|
63 | """ |
---|
64 | Overwrite the close event - no close! |
---|
65 | """ |
---|
66 | event.ignore() |
---|