hytos / DTI_PID / DTI_PID / LibraryItem.py @ db2dcb47
이력 | 보기 | 이력해설 | 다운로드 (3.72 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
""" This is library module """
|
3 |
|
4 |
from PyQt5.QtCore import * |
5 |
from PyQt5.QtGui import * |
6 |
from PyQt5.QtWidgets import * |
7 |
from PyQt5.QtSvg import * |
8 |
|
9 |
|
10 |
class LibraryItemWidget(QWidget): |
11 |
COLUMN = 3
|
12 |
|
13 |
def __init__(self, parent=None): |
14 |
super().__init__(parent)
|
15 |
self.setAcceptDrops(True) |
16 |
|
17 |
self.layout = QGridLayout()
|
18 |
self.setLayout(self.layout) |
19 |
|
20 |
def remove_library(self, item): |
21 |
matches = [idx for idx in range(self.layout.count()) if self.layout.itemAt(idx).widget() is item] |
22 |
if matches:
|
23 |
layout_item = self.layout.takeAt(matches[0]) |
24 |
self.layout.removeWidget(item)
|
25 |
item.deleteLater() |
26 |
del item
|
27 |
self.layout.removeItem(layout_item)
|
28 |
del layout_item
|
29 |
|
30 |
self.layout.update()
|
31 |
|
32 |
def dragEnterEvent(self, event: QDragEnterEvent) -> None: |
33 |
if issubclass(type(event.source()), QTreeWidget) and event.mimeData().hasFormat('text/plain'): |
34 |
|
35 |
# check if symbol is already registered
|
36 |
for idx in range(self.layout.count()): |
37 |
widget = self.layout.itemAt(idx).widget()
|
38 |
id = f"{event.mimeData().tag.getType()}.{event.mimeData().tag.getName()}"
|
39 |
if widget.id == id: |
40 |
event.ignore() |
41 |
return
|
42 |
# up to here
|
43 |
event.accept() |
44 |
else:
|
45 |
event.ignore() |
46 |
|
47 |
def dropEvent(self, event: QDropEvent) -> None: |
48 |
if hasattr(event.mimeData(), 'tag'): |
49 |
item = LibraryItem(event.mimeData().tag) |
50 |
row = int((self.layout.count()) / LibraryItemWidget.COLUMN) |
51 |
col = (self.layout.count()) % LibraryItemWidget.COLUMN
|
52 |
self.layout.addWidget(item, row, col)
|
53 |
|
54 |
event.acceptProposedAction() |
55 |
|
56 |
|
57 |
class LibraryItem(QFrame): |
58 |
def __init__(self, symbol): |
59 |
QFrame.__init__(self)
|
60 |
self.id = f"{symbol.getType()}.{symbol.getName()}" |
61 |
self._symbol = symbol
|
62 |
# An Icon and a label below
|
63 |
icon = QLabel() |
64 |
icon.resize(symbol.pixmap.width(), symbol.pixmap.height()) |
65 |
icon.setPixmap(symbol.pixmap) |
66 |
layout = QGridLayout() |
67 |
layout.addWidget(icon, 0, 0, Qt.AlignHCenter) |
68 |
title = QLabel(symbol.getName()) |
69 |
font = title.font() |
70 |
font.setPixelSize(10)
|
71 |
title.setFont(font) |
72 |
layout.addWidget(title, 1, 0, Qt.AlignTop | Qt.AlignHCenter) |
73 |
self.setLayout(layout)
|
74 |
self.setMaximumSize(96, 96) |
75 |
|
76 |
@property
|
77 |
def symbol(self): |
78 |
return self._symbol |
79 |
|
80 |
def keyPressEvent(self, event: QKeyEvent) -> None: |
81 |
if event.key() == Qt.Key_Delete:
|
82 |
self.parentWidget().remove_library(self) |
83 |
return
|
84 |
|
85 |
super(LibraryItem, self).keyPressEvent(event) |
86 |
|
87 |
"""
|
88 |
Drag and drop management
|
89 |
"""
|
90 |
|
91 |
def enterEvent(self, event): |
92 |
self.setFrameStyle(QFrame.Panel | QFrame.Sunken)
|
93 |
self.setCursor(Qt.OpenHandCursor)
|
94 |
self.setFocus()
|
95 |
|
96 |
def leaveEvent(self, event): |
97 |
self.setFrameStyle(QFrame.NoFrame)
|
98 |
self.setCursor(Qt.ArrowCursor)
|
99 |
|
100 |
def mousePressEvent(self, mouseEvent): |
101 |
self.setCursor(Qt.ClosedHandCursor)
|
102 |
|
103 |
def mouseReleaseEvent(self, mouseEvent): |
104 |
self.setCursor(Qt.OpenHandCursor)
|
105 |
|
106 |
def mouseMoveEvent(self, event): |
107 |
"""Drag-n-Drop on left click"""
|
108 |
if event.buttons() != Qt.LeftButton:
|
109 |
return
|
110 |
|
111 |
mime = QMimeData() |
112 |
mime.setText(self._symbol.getName())
|
113 |
|
114 |
drag = QDrag(self)
|
115 |
drag.setMimeData(mime) |
116 |
originalPoint = self._symbol.getOriginalPoint()
|
117 |
drag.setHotSpot(QPoint(int(float(originalPoint.split(",")[0])), int(float(originalPoint.split(",")[1])))) |
118 |
drag.setPixmap(self._symbol.pixmap)
|
119 |
drag.exec(Qt.CopyAction) |