hytos / HYTOS / HYTOS / Commands / DefaultCommand.py @ b06d26b5
이력 | 보기 | 이력해설 | 다운로드 (11.4 KB)
1 |
import sys |
---|---|
2 |
import os.path |
3 |
import AbstractCommand |
4 |
try:
|
5 |
from PyQt5.QtCore import * |
6 |
from PyQt5.QtGui import * |
7 |
from PyQt5.QtWidgets import * |
8 |
except ImportError: |
9 |
try:
|
10 |
from PyQt4.QtCore import Qt, QRectF, pyqtSignal, QT_VERSION_STR, QEvent |
11 |
from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImge, QPixmap, QPainterPath, QFileDialog, QCursor, QMouseEvent |
12 |
except ImportError: |
13 |
raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.") |
14 |
|
15 |
'''
|
16 |
@brief QtImageViewer Default Command
|
17 |
@author Jeongwoo
|
18 |
@date 18.04.10
|
19 |
@history 2018.05.02 Jeongwoo Change mouse cursor
|
20 |
'''
|
21 |
class DefaultCommand(AbstractCommand.AbstractCommand): |
22 |
onSuccess = pyqtSignal(float, float, float, float) |
23 |
|
24 |
def __init__(self, imageViewer): |
25 |
super(DefaultCommand, self).__init__(imageViewer) |
26 |
self.name = 'Default' |
27 |
self.imageViewer.setCursor(QCursor(Qt.ArrowCursor))
|
28 |
|
29 |
self._vertices = []
|
30 |
self.startX = 0 |
31 |
self.startY = 0 |
32 |
self.isClicked = False |
33 |
|
34 |
self.symbol = None |
35 |
self.isCopy = False |
36 |
self.isSpecBreak = False |
37 |
self.specBreak_startPoint = []
|
38 |
self.tempAngle = 0 |
39 |
|
40 |
'''
|
41 |
@brief Scroll / Pan / Zoom with Wheel Button
|
42 |
@author Jeongwoo
|
43 |
@date 18.04.10
|
44 |
@history kyouho 2018.07.31 add copy symbol logic
|
45 |
humkyung 2018.08.11 cancel paste when user press escape key
|
46 |
'''
|
47 |
def execute(self, param): |
48 |
from SymbolSvgItem import SymbolSvgItem |
49 |
|
50 |
event = param[1]
|
51 |
scenePos = param[2]
|
52 |
|
53 |
self.isTreated = False |
54 |
|
55 |
try:
|
56 |
if not self.isCopy: |
57 |
if 'mousePressEvent' == param[0] and event.button() == Qt.MiddleButton: |
58 |
self.imageViewer.setDragMode(QGraphicsView.ScrollHandDrag)
|
59 |
QGraphicsView.mousePressEvent(self.imageViewer, QMouseEvent(QEvent.MouseButtonPress, event.pos(), Qt.LeftButton, event.buttons(), Qt.NoModifier))
|
60 |
elif 'mouseReleaseEvent' == param[0] and event.button() == Qt.MiddleButton: |
61 |
try:
|
62 |
self.imageViewer.setDragMode(QGraphicsView.NoDrag)
|
63 |
finally:
|
64 |
QGraphicsView.mouseReleaseEvent(self.imageViewer, QMouseEvent(QEvent.MouseButtonRelease, event.pos(), Qt.LeftButton, event.buttons(), Qt.NoModifier))
|
65 |
pass
|
66 |
elif 'mousePressEvent' == param[0] and event.button() == Qt.LeftButton: |
67 |
if not self.imageViewer.scene.items(scenePos): |
68 |
self.imageViewer.scene.clearSelection()
|
69 |
self.imageViewer.setDragMode(QGraphicsView.RubberBandDrag)
|
70 |
self.startX = scenePos.x()
|
71 |
self.startY = scenePos.y()
|
72 |
self._vertices.clear()
|
73 |
self._vertices.append(param[2]) |
74 |
self.isClicked = True |
75 |
elif self.isClicked and 'mouseReleaseEvent' == param[0] and event.button() == Qt.LeftButton: |
76 |
try:
|
77 |
QGraphicsView.mouseReleaseEvent(self.imageViewer, event)
|
78 |
self._vertices.append(param[2]) |
79 |
finally:
|
80 |
self.imageViewer.setDragMode(QGraphicsView.NoDrag)
|
81 |
x = min(self._vertices[0].x(), self._vertices[1].x()) |
82 |
y = min(self._vertices[0].y(), self._vertices[1].y()) |
83 |
width = abs(self._vertices[0].x() - self._vertices[1].x()) |
84 |
height = abs(self._vertices[0].y() - self._vertices[1].y()) |
85 |
self.onSuccess.emit(x, y, width, height)
|
86 |
self.isClicked = False |
87 |
elif self.isClicked and 'mouseMoveEvent' == param[0]: |
88 |
endX = scenePos.x() |
89 |
endY = scenePos.y() |
90 |
|
91 |
if self.startX > endX and self.startY > endY: |
92 |
self.imageViewer.setRubberBandSelectionMode(Qt.ContainsItemShape)
|
93 |
else:
|
94 |
self.imageViewer.setRubberBandSelectionMode(Qt.IntersectsItemShape)
|
95 |
elif 'keyPressEvent' == param[0] and (event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_C): |
96 |
selectedItems = self.imageViewer.scene.selectedItems()
|
97 |
if len(selectedItems) == 1 and issubclass(type(selectedItems[0]), SymbolSvgItem): |
98 |
from xml.etree.ElementTree import tostring |
99 |
xmlStr = str(tostring(selectedItems[0].toXml())) |
100 |
xmlStr = xmlStr[xmlStr.find('<SYMBOL>'):xmlStr.find('</SYMBOL>') + 9] |
101 |
QApplication.clipboard().setText(xmlStr) |
102 |
elif 'keyPressEvent' == param[0] and (event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_V): |
103 |
xmlStr = QApplication.clipboard().text() |
104 |
if xmlStr.find('<SYMBOL>') > -1 and xmlStr.find('</SYMBOL>') > -1: |
105 |
self.copySymbol(QApplication.clipboard().text())
|
106 |
self.isTreated = False |
107 |
elif self.isCopy and self.isSpecBreak: |
108 |
if 'mouseReleaseEvent' == param[0] and event.button() == Qt.LeftButton: |
109 |
from EngineeringConnectorItem import QEngineeringConnectorItem |
110 |
|
111 |
offsetX = 0
|
112 |
offsetY = 0
|
113 |
transform = QTransform() |
114 |
currentPt = self.symbol.getCurrentPoint()
|
115 |
if 0 == self.symbol.angle or 3.14 == self.symbol.angle: |
116 |
transform.translate(self.specBreak_startPoint[0], scenePos.y()) |
117 |
offsetY = self.specBreak_startPoint[1] - scenePos.y() |
118 |
|
119 |
else:
|
120 |
transform.translate(scenePos.x(), self.specBreak_startPoint[1]) |
121 |
offsetX = self.specBreak_startPoint[0] - scenePos.x() |
122 |
|
123 |
transform.rotateRadians(-self.symbol.angle)
|
124 |
transform.translate(-currentPt[0], -currentPt[1]) |
125 |
self.symbol.setTransform(transform)
|
126 |
|
127 |
self.symbol.specBreak_offsetX = offsetX
|
128 |
self.symbol.specBreak_offsetY = offsetY
|
129 |
|
130 |
self.symbol.connectors[0].connectPoint = (self.symbol.connectors[0].connectPoint[0] + offsetX, self.symbol.connectors[0].connectPoint[1] + offsetY) |
131 |
self.symbol.connectors[0].setPos((self.symbol.connectors[0].connectPoint[0], self.symbol.connectors[0].connectPoint[1])) |
132 |
|
133 |
self.symbol = None |
134 |
self.isCopy = False |
135 |
self.isSpecBreak = False |
136 |
|
137 |
QApplication.instance().setOverrideCursor(QCursor(Qt.ArrowCursor)) |
138 |
elif 'mouseMoveEvent' == param[0]: |
139 |
from EngineeringConnectorItem import QEngineeringConnectorItem |
140 |
|
141 |
transform = QTransform() |
142 |
currentPt = self.symbol.getCurrentPoint()
|
143 |
if 0 == self.symbol.angle or 3.14 == self.symbol.angle: |
144 |
transform.translate(self.specBreak_startPoint[0], scenePos.y()) |
145 |
else:
|
146 |
transform.translate(scenePos.x(), self.specBreak_startPoint[1]) |
147 |
transform.rotateRadians(-self.symbol.angle)
|
148 |
transform.translate(-currentPt[0], -currentPt[1]) |
149 |
self.symbol.setTransform(transform)
|
150 |
elif 'keyPressEvent' == param[0] and event.key() == Qt.Key_Escape: |
151 |
self.imageViewer.scene.removeItem(self.symbol) |
152 |
self.symbol = None |
153 |
self.isCopy = False |
154 |
self.isSpecBreak = False |
155 |
|
156 |
else:
|
157 |
if 'mouseReleaseEvent' == param[0] and event.button() == Qt.LeftButton: |
158 |
from EngineeringConnectorItem import QEngineeringConnectorItem |
159 |
|
160 |
item = self.imageViewer.scene.itemAt(scenePos, QTransform())
|
161 |
if item is not None and type(item) is QEngineeringConnectorItem: |
162 |
scenePos = QPointF(item.center()[0], item.center()[1]) |
163 |
|
164 |
transform = QTransform() |
165 |
currentPt = self.symbol.getCurrentPoint()
|
166 |
transform.translate(scenePos.x() - currentPt[0], scenePos.y() - currentPt[1]) |
167 |
self.symbol.setTransform(transform)
|
168 |
|
169 |
self.imageViewer.scene.removeItem(self.symbol) |
170 |
self.imageViewer.matchSymbolToLine(self.symbol, scenePos) |
171 |
self.symbol = None |
172 |
self.isCopy = False |
173 |
|
174 |
QApplication.instance().setOverrideCursor(QCursor(Qt.ArrowCursor)) |
175 |
elif 'mouseMoveEvent' == param[0]: |
176 |
from EngineeringConnectorItem import QEngineeringConnectorItem |
177 |
|
178 |
item = self.imageViewer.scene.itemAt(scenePos, QTransform())
|
179 |
if item is not None and type(item) is QEngineeringConnectorItem: |
180 |
scenePos = QPointF(item.center()[0], item.center()[1]) |
181 |
|
182 |
transform = QTransform() |
183 |
currentPt = self.symbol.getCurrentPoint()
|
184 |
transform.translate(scenePos.x() - currentPt[0], scenePos.y() - currentPt[1]) |
185 |
self.symbol.setTransform(transform)
|
186 |
elif 'keyPressEvent' == param[0] and event.key() == Qt.Key_Escape: |
187 |
self.imageViewer.scene.removeItem(self.symbol) |
188 |
self.symbol = None |
189 |
self.isCopy = False |
190 |
pass
|
191 |
except Exception as ex: |
192 |
from App import App |
193 |
from AppDocData import MessageType |
194 |
|
195 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
196 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
197 |
|
198 |
def undo(self): |
199 |
pass
|
200 |
|
201 |
def redo(self): |
202 |
pass
|
203 |
|
204 |
def reset(self): |
205 |
pass
|
206 |
|
207 |
'''
|
208 |
@brief xml to ElementTree and ElementTree to Symbol
|
209 |
@author kyouho
|
210 |
@date 18.07.31
|
211 |
'''
|
212 |
def copySymbol(self, xmlStr): |
213 |
from xml.etree.ElementTree import Element, SubElement, dump, ElementTree, parse, fromstring |
214 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '\\Shapes') |
215 |
from SymbolSvgItem import SymbolSvgItem |
216 |
import uuid |
217 |
|
218 |
try:
|
219 |
# string to ElementTree
|
220 |
et = fromstring(xmlStr) |
221 |
item = SymbolSvgItem.fromXml(et) |
222 |
self.symbol = item
|
223 |
for connector in self.symbol.connectors: |
224 |
connector.connectedItem = None
|
225 |
# uid 새로 할당
|
226 |
self.symbol.uid = uuid.uuid4()
|
227 |
self.imageViewer.scene.addItem(self.symbol) |
228 |
self.imageViewer.scene.clearFocus()
|
229 |
self.imageViewer.scene.setFocusItem(self.symbol) |
230 |
self.symbol.setSelected(True) |
231 |
self.isCopy = True |
232 |
|
233 |
QApplication.instance().setOverrideCursor(QCursor(Qt.DragCopyCursor)) |
234 |
except Exception as ex: |
235 |
from App import App |
236 |
from AppDocData import MessageType |
237 |
|
238 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
239 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
240 |
|