Source

APKinspector / Graph.py

Full commit
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_smali



class GraphicsView(QGraphicsView):
    scene = None
    view = None
    pageSize = [100, 100]
    
    def __init__(self):
        super(GraphicsView, 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):
        self.view = GraphicsView()
        self.scene = QGraphicsScene()
        self.scene.setSceneRect(0, 0, self.pageSize[0], self.pageSize[1])
        self.view.setScene(self.scene)
        gridLayout.addWidget(self.view)

    
    def show(self, nodeList, linkList):
                
        self.scene.clear()
        self.scene.setSceneRect(0, 0, self.pageSize[0], self.pageSize[1])
        
        for node in nodeList:
            self.scene.addItem(node)
        
        for link in linkList:
            self.scene.addItem(link)
        


class Link(QGraphicsLineItem):
    myFromNode = None
    myToNode = None
    painterPath = None
    color = None
    
    def __init__(self, fromNode = None, toNode = None):
        QGraphicsLineItem.__init__(self)        
        self.myFromNode = fromNode
        self.myToNode = toNode
        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
    
    
    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)
        self.left = aleft
        self.top = atop
        self.width = awidth
        self.height = aheight
        
        self.font = QFont("Courier", 5, -1, 0)


    
    def setText(self, text):
        self.prepareGeometryChange()
        self.myText = text
        self.update()
    
    def text(self):
        return text
    
    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_smali = inputDialog(self, self.myText)
        inputDialog_smali.exec_()