-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag_list.py
76 lines (64 loc) · 2.51 KB
/
drag_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
try:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
except ImportError:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
LISTBOX_MIMETYPE = "application/x-item"
NODES_TO_ICONS = {
"Csv Loader": "icons/csv24.png",
"Excel Loader": "icons/excel24.png",
"Xml Loader": "icons/xml24.png",
"Attribute Remover": "icons/preprocessing242.png",
"Filter": "icons/filter24.png",
"Linear Regression": "icons/linear-reg24.png",
"Decision Tree": "icons/decision-tree24.png",
"Knn": "icons/classification24.png",
"Naive Bayes": "icons/classification24.png",
"SVM": "icons/classification24.png",
"K-Means": "icons/clustering24.png",
"Hierarchical": "icons/clustering24.png",
"Csv Saver": "icons/saver24.png",
"Serializer": "icons/serializer24.png",
"Deserializer": "icons/serializer24.png",
"Predictor": "icons/loupe24.png",
"Pie Chart": "icons/pie-chart24.png",
"Scatter plot": "icons/scatter24.png",
"Simple Plot": "icons/plot24.png",
"Text output": "icons/text24.png",
"Histogram": "icons/histogram24.png"
}
class DragList(QListWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setIconSize(QSize(32, 32))
self.setSelectionMode(QAbstractItemView.SingleSelection)
self.setDragEnabled(True)
def add_items(self, items, icon):
for item_name in items:
item = QListWidgetItem(item_name, self)
pixmap = QPixmap(NODES_TO_ICONS.get(item_name, icon))
item.setIcon(QIcon(pixmap))
item.setSizeHint(QSize(32, 32))
item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled)
# setup data
item.setData(Qt.UserRole, pixmap)
item.setData(Qt.UserRole + 1, 1)
def startDrag(self, *args, **kwargs):
item = self.currentItem()
op_code = item.data(Qt.UserRole + 1)
pixmap = QPixmap(item.data(Qt.UserRole))
item_data = QByteArray()
data_stream = QDataStream(item_data, QIODevice.WriteOnly)
data_stream << pixmap
data_stream.writeInt(op_code)
data_stream.writeQString(item.text())
mime_data = QMimeData()
mime_data.setData(LISTBOX_MIMETYPE, item_data)
drag = QDrag(self)
drag.setMimeData(mime_data)
drag.setHotSpot(QPoint(pixmap.width() / 2, pixmap.height() / 2))
drag.setPixmap(pixmap)
drag.exec_(Qt.MoveAction)