forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragGraphics.py
146 lines (118 loc) · 4.8 KB
/
DragGraphics.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2023/02/09
@author: Irony
@site: https://pyqt.site https://github.com/PyQt5
@email: [email protected]
@file: DragGraphics.py
@description:
"""
import json
import os
try:
from PyQt5.QtCore import QMimeData, Qt
from PyQt5.QtGui import QDrag, QIcon, QPixmap
from PyQt5.QtWidgets import (QApplication, QGraphicsPixmapItem,
QGraphicsScene, QGraphicsView, QHBoxLayout,
QListWidget, QListWidgetItem, QTreeWidget,
QTreeWidgetItem, QWidget)
except ImportError:
from PySide2.QtCore import QMimeData, Qt
from PySide2.QtGui import QDrag, QIcon, QPixmap
from PySide2.QtWidgets import (QApplication, QGraphicsPixmapItem,
QGraphicsScene, QGraphicsView, QHBoxLayout,
QListWidget, QListWidgetItem, QTreeWidget,
QTreeWidgetItem, QWidget)
class ListWidget(QListWidget):
def __init__(self, *args, **kwargs):
super(ListWidget, self).__init__(*args, **kwargs)
self.setDragEnabled(True)
self.setDragDropMode(QListWidget.DragOnly)
self.setDefaultDropAction(Qt.IgnoreAction)
self.setEditTriggers(QListWidget.NoEditTriggers)
self.setResizeMode(QListWidget.Adjust)
self.setViewMode(QListWidget.IconMode)
def startDrag(self, supportedActions):
items = self.selectedItems()
if not items:
return
# 这里就简单的根据名字提示来传递数据了,实际上可以传递任意数据
data = QMimeData()
data.setData('application/node-items',
json.dumps([item.toolTip() for item in items]).encode())
# 这里简单显示第一个缩略图
pixmap = items[0].icon().pixmap(36, 36)
drag = QDrag(self)
drag.setMimeData(data)
drag.setPixmap(pixmap)
drag.setHotSpot(pixmap.rect().center())
drag.exec_(supportedActions)
class GraphicsView(QGraphicsView):
def __init__(self, *args, **kwargs):
super(GraphicsView, self).__init__(*args, **kwargs)
self.setAcceptDrops(True)
self._scene = QGraphicsScene(self) # 场景
self.setScene(self._scene)
def dragEnterEvent(self, event):
"""判断拖入的数据是否支持"""
mimeData = event.mimeData()
if not mimeData.hasFormat('application/node-items'):
event.ignore()
return
event.acceptProposedAction()
dragMoveEvent = dragEnterEvent
def dropEvent(self, event):
"""获取拖拽的数据并绘制对于的图形"""
datas = event.mimeData().data('application/node-items')
datas = json.loads(datas.data().decode())
print('datas:', datas)
path = os.path.join(os.path.dirname(__file__), 'Data/icons')
for name in datas:
item = QGraphicsPixmapItem(QPixmap(os.path.join(path, name)))
item.setFlags(QGraphicsPixmapItem.ItemIsFocusable |
QGraphicsPixmapItem.ItemIsMovable)
self._scene.addItem(item)
pos = self.mapToScene(event.pos())
item.moveBy(pos.x(), pos.y())
class DragGraphics(QWidget):
def __init__(self, *args, **kwargs):
super(DragGraphics, self).__init__(*args, **kwargs)
self.resize(800, 600)
layout = QHBoxLayout(self)
# 左侧树形控制
self.treeWidget = QTreeWidget(self)
self.treeWidget.header().setVisible(False)
self.treeWidget.setMaximumWidth(300)
layout.addWidget(self.treeWidget)
# 右侧图形显示
self.graphicsView = GraphicsView(self)
layout.addWidget(self.graphicsView)
self._init_trees()
def _init_trees(self):
"""初始化树形控件中的图形节点列表"""
# 1. 获取所有图标
path = os.path.join(os.path.dirname(__file__), 'Data/icons')
icons = [os.path.join(path, name) for name in os.listdir(path)]
# 2. 添加根节点
for i in range(2):
item = QTreeWidgetItem(self.treeWidget)
item.setText(0, 'View %d' % i)
# 3. 添加子节点作为容器用于存放图标
itemc = QTreeWidgetItem(item)
child = ListWidget(self.treeWidget)
self.treeWidget.setItemWidget(itemc, 0, child)
# 4. 添加图标
for icon in icons:
item = QListWidgetItem(child)
item.setIcon(QIcon(icon))
item.setToolTip(os.path.basename(icon))
self.treeWidget.expandAll()
if __name__ == '__main__':
import cgitb
import sys
cgitb.enable(format='text')
app = QApplication(sys.argv)
w = DragGraphics()
w.show()
sys.exit(app.exec_())