-
Notifications
You must be signed in to change notification settings - Fork 238
/
graphback.py
325 lines (254 loc) · 9.85 KB
/
graphback.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
from Ui_MainWindow import Ui_mainWindow
from InputDialog import *
from Ui_InputDialog import Ui_inputDialog_dalvik
class GraphicsCall(QGraphicsView):
scene = None
view = None
pageSize = [100, 100]
root = None
nodeList = []
def __init__(self):
super(GraphicsCall, self).__init__()
# set the drag mode
self.setDragMode(QGraphicsView.ScrollHandDrag)
def wheelEvent(self, event):
factor = 1.41 ** (event.delta()/240.0)
self.scale(factor, factor)
def setPageSize(self, pagesize):
self.pageSize[0] = pagesize[0]
self.pageSize[1] = pagesize[1]
# def initShow(self, tab_cfg, gridLayout, tabWidget, dalvikEdit):
def initShow(self, tab, gridLayout, tabWidget, dalvikEdit):
self.view = GraphicsCall()
self.scene = QGraphicsScene()
self.scene.setSceneRect(0, 0, self.pageSize[0], self.pageSize[1])
self.view.setScene(self.scene)
gridLayout.addWidget(self.view)
import Global
Global.TABWIDGET = tabWidget
Global.DALVIKEDIT = dalvikEdit
def show(self, nodeList, linkList,gridLayout):
self.view = GraphicsCall()
self.scene = QGraphicsScene()
self.scene.setSceneRect(0, 0, self.pageSize[0]*2, self.pageSize[1]*2)
del self.nodeList[:]
import copy
self.nodeList = copy.copy(nodeList)
#for node in nodeList:
# self.scene.addItem(node)
#for link in linkList:
# self.scene.addItem(link)
#self.root = QGraphicsItemGroup(None, self.scene)
# I just want to crate a root QGraphicsItem object, which is None. So it creates a Link object for convenience.
# Qt has a bug in translate function. I crate a root item to implement translate function.
self.root = Link()
for node in nodeList:
node.setParentItem(self.root)
for link in linkList:
link.setParentItem(self.root)
self.scene.addItem(self.root)
self.root.translate(0, 0)
self.show
class Link(QGraphicsLineItem):
painterPath = None
color = None
def __init__(self):
QGraphicsLineItem.__init__(self)
self.painterPath = QPainterPath()
self.myColor = Qt.black
self.setFlags(QGraphicsLineItem.ItemIsMovable | QGraphicsLineItem.ItemIsSelectable)
def drawLine(self, path):
for i in range(0, len(path), 2):
x = path[i]
y = path[i+1]
if i == 0:
self.painterPath.moveTo(x, y)
else:
self.painterPath.lineTo(x, y)
def drawArrow(self, points):
point1_x = points[0]
point1_y = points[1]
point2_x = points[2]
point2_y = points[3]
point3_x = points[4]
point3_y = points[5]
self.painterPath.moveTo(point1_x, point1_y)
self.painterPath.lineTo(point2_x, point2_y)
self.painterPath.lineTo(point3_x, point3_y)
self.painterPath.lineTo(point1_x, point1_y)
def color(self):
return self.myColor
def setColor(self, color):
colorDir = {"red":Qt.red, "blue":Qt.blue, "green":Qt.green}
self.myColor = colorDir[color]
def paint(self, painter, option, widget = None):
pen = QPen(self.myColor)
painter.setPen(pen)
painter.drawPath(self.painterPath)
def boundingRect(self):
rect = QRectF(0, 0, 99999999, 99999999)
return rect
class Node(QGraphicsItem):
myText = None
myTextColor = None
myBackgroundColor = None
myOutlineColor = None
myLinks = []
left = 0.0
top = 0.0
width = 0.0
height = 0.0
font = None
hint = None
minOffset = 0
maxOffset = 0
def __init__(self, aleft = 0.0, atop = 0.0, awidth = 0.0, aheight = 0.0):
QGraphicsItem.__init__(self)
self.myTextColor = QColor(Qt.darkGreen)
self.myOutlineColor = QColor(Qt.darkGray)
self.myBackgroundColor = QColor(Qt.white)
# self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
self.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsFocusable)
self.left = aleft
self.top = atop
self.width = awidth
self.height = aheight
self.font = QFont("Courier", 5, -1, 0)
self.setAcceptHoverEvents(1)
def setText(self, text):
self.prepareGeometryChange()
self.myText = str(text)
self.update()
# after getting the text, it can get the min and max offset
self.getMinMaxOffset()
def getMinMaxOffset(self):
text = self.myText.split("\n")
firstLine = text[0]
endLine = text[-2]
print "getoffset#########\n\n"
self.minOffset = firstLine[:firstLine.index(" ")]
self.maxOffset = endLine[:endLine.index(" ")]
print self.minOffset
print self.maxOffset
# change the string type to hex type
self.minOffset = int(self.minOffset, 16)
self.maxOffset = int(self.maxOffset, 16)
print self.minOffset
print self.maxOffset
def text(self):
return self.myText
def setTextColor(self, color):
self.myTextColor = color
self.update()
def textColor(self):
return self.myTextColor
def setOutlineColor(self, color):
self.myOutlineColor = color
self.update()
def outlineColor(self):
return self.myOutlineColor
def setBackgroundColor(self, color):
self.myBackgroundColor = color
self.update()
def backgroundColor(self):
return self.myBackgroundColor
def addLink(self, link):
self.myLinks.append(link)
def removeLink(self, link):
self.myLinks.remove(link)
def outlineRect(self):
rect = QRectF(self.left, self.top, self.width, self.height)
return rect
def updateOutlineRect(self):
Padding = 8
metrics = QFontMetrics(self.font)
rect = metrics.boundingRect(self.myText)
rect.adjust(-Padding, -Padding, +Padding, +Padding)
rect.translate(-rect.center())
return rect
def boundingRect(self):
Margin = 1
return self.outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin)
def paint(self, painter, option, widget = None):
pen = QPen(self.myOutlineColor)
if option.state & QStyle.State_Selected:
pen.setStyle(Qt.DotLine)
pen.setWidth(2)
painter.setPen(pen)
painter.setBrush(self.myBackgroundColor)
rect = self.outlineRect()
painter.drawRect(rect)
painter.setPen(self.myTextColor)
painter.setFont(self.font)
painter.drawText(rect, Qt.AlignLeft | Qt.AlignVCenter, self.myText)
def mouseDoubleClickEvent(self, event):
# inputDialog = QInputDialog()
# ok = None
# text = QString
# (text, ok) = inputDialog.getText(event.widget(), str("Edit Text"), str("Edit new Text:"), QLineEdit.Normal, self.myText)
# if ok and not text.isEmpty():
# self.setText(text)
inputDialog_dalvik = inputDialog(self, self.myText)
inputDialog_dalvik.exec_()
def hoverEnterEvent(self, event):
scene = self.scene()
pos = event.scenePos()
self.hint = Hint(pos.x(), pos.y(), self.width, self.height, self.myText)
scene.addItem(self.hint)
def hoverLeaveEvent(self, event):
scene = self.scene()
scene.removeItem(self.hint)
del self.hint
def keyPressEvent(self, event):
# If the user press the space key, the view will change to Dalvik from CFG.
if event.key() == Qt.Key_Space:
startLineNumber = "0x" + self.myText[:self.myText.index(" ")]
lineNumber = len(self.myText.split("\n")) - 1
import Global
Global.TABWIDGET.setCurrentIndex(1)
# set the cursor's background color
colorFormat = QTextCharFormat()
colorFormat.setBackground(QColor(Qt.gray).lighter(145))
document = Global.DALVIKEDIT.document()
cursor = document.find(startLineNumber + " ")
for i in range(0, lineNumber - 1):
cursor.movePosition(QTextCursor.Down, QTextCursor.KeepAnchor)
cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
cursor.mergeCharFormat(colorFormat)
Global.DALVIKEDIT.setTextCursor(cursor)
Global.DALVIKEDIT.ensureCursorVisible()
class Hint(QGraphicsItem):
text = None
left = 0.0
top = 0.0
width = 0.0
height = 0.0
def __init__(self, aleft = 0.0, atop = 0.0, awidth = 0.0, aheight = 0.0, text = None):
QGraphicsItem.__init__(self)
self.text = text
self.left = aleft
self.top = atop
self.width = awidth
self.height = aheight
def outlineRect(self):
rect = QRectF(self.left, self.top, self.width, self.height)
return rect
def boundingRect(self):
Margin = 1
return self.outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin)
def paint(self, painter, option, widget = None):
pen = QPen(Qt.white)
if option.state & QStyle.State_Selected:
pen.setStyle(Qt.DotLine)
pen.setWidth(2)
painter.setPen(pen)
brushColor = QColor(Qt.gray).lighter(140)
painter.setBrush(brushColor)
rect = self.outlineRect()
painter.drawRect(rect)
painter.setPen(Qt.blue)
painter.setFont(QFont("Courier", 5, -1, 0))
painter.drawText(rect, Qt.AlignLeft | Qt.AlignVCenter, self.text)