class StartSelectView(QGraphicsView):

    def __init__(self, *args):
        QGraphicsView.__init__(self, *args)
        self.move(2, 250)
        self.btnSize = 28
        self.setMaximumHeight(self.btnSize * 2)
        self.setMaximumWidth(334)
        self.setMinimumHeight(self.btnSize * 2)
        self.setMinimumWidth(334)
        self.adjustSize()
        self.scene = QGraphicsScene(self)
        self.setStyleSheet('background-color:transparent; border-width: 0px; border: 0px;')
        self.psButtons = QPixmap(os.getcwd() + '/../icons/controller-sprite.png')
        self.select = self.psButtons.copy(696, 120, 45, 30)
        self.select = self.select.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.selectItem = QGraphicsPixmapItem(self.select)
        self.selectItem.setOffset(QPointF(0, 0))
        self.scene.addItem(self.selectItem)
        self.start = self.psButtons.copy(754, 120, 45, 30)
        self.start = self.start.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.startItem = QGraphicsPixmapItem(self.start)
        self.startItem.setOffset(QPointF(86, 0))
        self.scene.addItem(self.startItem)
        self.setScene(self.scene)
Exemple #2
0
    def __mouseReleaseEvent(self,event):
        global PIX
        self.drag_stop = event.pos()
        im = PIX.pixmap().toImage()
        
        start, stop = self.mapToScene(self.drag_start),self.mapToScene(self.drag_stop)
        logging.debug((start,stop))

        augmented = QGraphicsPixmapItem(QPixmap(tempfile))
        
        augmented.setOffset(rect.topLeft())
        augmented.setMatrix(QMatrix())
        if None != self.selectbox: self.scene().removeItem(self.selectbox)
        self.selectbox = augmented
        self.scene().addItem(self.selectbox)
class FaceButtonsView(QGraphicsView):

    def __init__(self, *args):
        QGraphicsView.__init__(self, *args)
        self.move(170, 90)
        self.btnSize = 40
        self.padding = 5
        self.setMaximumHeight(self.btnSize * 4)
        self.setMaximumWidth(self.btnSize * 4)
        self.setMinimumHeight(self.btnSize * 4)
        self.setMinimumWidth(self.btnSize * 4)
        self.adjustSize()
        self.setStyleSheet('background-color:transparent; border-width: 0px; border: 0px;')
        self.scene = QGraphicsScene(self)
        self.psButtons = QPixmap(os.getcwd() + '/../icons/PS3_Buttons.png')
        self.triangle = self.psButtons.copy(0, 0, 220, 225)
        self.triangle = self.triangle.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.square = self.psButtons.copy(220, 0, 220, 225)
        self.square = self.square.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.circle = self.psButtons.copy(440, 0, 220, 225)
        self.circle = self.circle.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.cross = self.psButtons.copy(660, 0, 220, 225)
        self.cross = self.cross.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.triangleItem = QGraphicsPixmapItem(self.triangle)
        self.triangleItem.setOffset(QPointF(self.btnSize + self.padding, 0))
        self.scene.addItem(self.triangleItem)
        self.squareItem = QGraphicsPixmapItem(self.square)
        self.squareItem.setOffset(QPointF(0, self.btnSize + self.padding))
        self.scene.addItem(self.squareItem)
        self.circleItem = QGraphicsPixmapItem(self.circle)
        self.circleItem.setOffset(QPointF(self.btnSize * 2 + self.padding * 2, self.btnSize + self.padding))
        self.scene.addItem(self.circleItem)
        self.crossItem = QGraphicsPixmapItem(self.cross)
        self.crossItem.setOffset(QPointF(self.btnSize + self.padding, self.btnSize * 2 + self.padding * 2))
        self.scene.addItem(self.crossItem)
        self.effect = QGraphicsDropShadowEffect()
        self.effect.setOffset(0, 0)
        self.effect.setBlurRadius(20)
        self.effect.setColor(Qt.green)
        self.triangleItem.setGraphicsEffect(self.effect)
        self.setScene(self.scene)
        self.tl2 = QTimeLine(10000)
        self.tl2.setFrameRange(0, 10000)
        self.t = QGraphicsItemAnimation()
        self.t.setItem(self.triangleItem)
        self.t.setTimeLine(self.tl2)
        self.tl2.connect(self.tl2, SIGNAL('frameChanged(int)'), self.updateEffect)
        self.effectd = 3
        self.tl2.start()

    def updateEffect(self):
        if self.effect.blurRadius() > 50:
            self.effectd = -3
        elif self.effect.blurRadius() < 5:
            self.effectd = 3
        self.effect.setBlurRadius(self.effect.blurRadius() + self.effectd)
class DpadView(QGraphicsView):

    def __init__(self, *args):
        QGraphicsView.__init__(self, *args)
        self.move(2, 90)
        self.btnSize = 75
        self.padding = -35
        self.setMaximumHeight(self.btnSize * 2 + 20)
        self.setMaximumWidth(self.btnSize * 2 + 20)
        self.setMinimumHeight(self.btnSize * 2 + 20)
        self.setMinimumWidth(self.btnSize * 2 + 20)
        self.adjustSize()
        self.setStyleSheet('background-color:transparent; border-width: 0px; border: 0px;')
        self.scene = QGraphicsScene(self)
        self.left = QPixmap(os.getcwd() + '/../icons/left.png')
        self.left = self.left.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.right = QPixmap(os.getcwd() + '/../icons/right.png')
        self.right = self.right.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.up = QPixmap(os.getcwd() + '/../icons/up.png')
        self.up = self.up.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.down = QPixmap(os.getcwd() + '/../icons/down.png')
        self.down = self.down.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.leftItem = QGraphicsPixmapItem(self.left)
        self.leftItem.setOffset(QPointF(0, self.btnSize + self.padding))
        self.scene.addItem(self.leftItem)
        self.rightItem = QGraphicsPixmapItem(self.right)
        self.rightItem.setOffset(QPointF(self.btnSize * 2 + self.padding * 2, self.btnSize + self.padding))
        self.scene.addItem(self.rightItem)
        self.upItem = QGraphicsPixmapItem(self.up)
        self.upItem.setOffset(QPointF(self.btnSize + self.padding, 0))
        self.scene.addItem(self.upItem)
        self.downItem = QGraphicsPixmapItem(self.down)
        self.downItem.setOffset(QPointF(self.btnSize + self.padding, self.btnSize * 2 + self.padding * 2))
        self.scene.addItem(self.downItem)
        self.effect = QGraphicsDropShadowEffect()
        self.effect.setOffset(0, 0)
        self.effect.setBlurRadius(20)
        self.effect.setColor(Qt.green)
        self.downItem.setGraphicsEffect(self.effect)
        self.setScene(self.scene)
        self.tl2 = QTimeLine(10000)
        self.tl2.setFrameRange(0, 10000)
        self.t = QGraphicsItemAnimation()
        self.t.setItem(self.downItem)
        self.t.setTimeLine(self.tl2)
        self.tl2.connect(self.tl2, SIGNAL('frameChanged(int)'), self.updateEffect)
        self.effectd = 3
        self.tl2.start()

    def updateEffect(self):
        if self.effect.blurRadius() > 50:
            self.effectd = -3
        elif self.effect.blurRadius() < 5:
            self.effectd = 3
        self.effect.setBlurRadius(self.effect.blurRadius() + self.effectd)
Exemple #5
0
class AI(object):

    def __init__(self, QMainWindow):
        self.game = QMainWindow
        self.clear_unused_fields()
        ships = 0
        while ships < 10:
            x = randint(0, 9)
            y = randint(0, 8)
            if self.game.enemies_board[x][y] == 0:
                self.game.enemies_board[x][y] = 1
                ships += 1

    def clear_unused_fields(self):
        self.game.connect_btn.hide()
        self.game.ip_addr_field.hide()
        self.game.port_field.hide()

    def make_move(self):
        if not self.game.is_my_turn:
            x = randint(0, 9)
            y = randint(0, 8)
            if self.game.my_board[x][y] not in range(2,4):
                if self.game.my_board[x][y] == 1:
                    self.game.my_board[x][y] = 2
                    picture = 'res/cancel.png'
                else:
                    self.game.my_board[x][y] = 3
                    picture = 'res/ok.png'

                if x % 2 != 0:
                    self.item = QGraphicsPixmapItem(QPixmap(picture))
                    self.item.setOffset(16+y*61, 17+(x*52))
                    self.game.scene.addItem(self.item)

                elif x % 2 == 0:
                    self.item = QGraphicsPixmapItem(QPixmap(picture))
                    self.item.setOffset(47+y*61, 16+x*53)
                    self.game.scene.addItem(self.item)

            print(self.game.enemies_board)
            print(self.game.my_board)
            self.game.is_my_turn = True
class BumpersView(QGraphicsView):

    def __init__(self, *args):
        QGraphicsView.__init__(self, *args)
        self.move(2, 22)
        self.btnSize = 60
        self.setMaximumHeight(60)
        self.setMaximumWidth(332)
        self.setMinimumHeight(60)
        self.setMinimumWidth(332)
        self.adjustSize()
        self.scene = QGraphicsScene(self)
        self.setStyleSheet('background-color:transparent; border-width: 0px; border: 0px;')
        self.l1 = QPixmap(os.getcwd() + '/../icons/l1.png')
        self.l1 = self.l1.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.l1Item = QGraphicsPixmapItem(self.l1)
        self.l1Item.setOffset(QPointF(30, 0))
        self.scene.addItem(self.l1Item)
        self.r1 = QPixmap(os.getcwd() + '/../icons/r1.png')
        self.r1 = self.l1.scaled(self.btnSize, self.btnSize, Qt.KeepAspectRatio)
        self.r1Item = QGraphicsPixmapItem(self.r1)
        self.r1Item.setOffset(QPointF(200, 0))
        self.scene.addItem(self.r1Item)
        self.setScene(self.scene)
Exemple #7
0
class MyWin(QMainWindow):

    STATEK = 'res/ship1.png'

    def __init__(self, address='127.0.0.1', port=5007):
        QMainWindow.__init__(self)
        uic.loadUi('untitled.ui', self)

        self.step_nr = 0
        self.is_my_turn = True
        self.ai_mode = False

        #  List with game states
        self.state = []

        self.ready_to_play = False
        self.send = (-1, -1)

        # 1 - Ship
        # 2 - Hit
        # 3 - Missed 
        self.my_board = np.zeros((10, 9))
        self.enemies_board = np.zeros((10, 9))
        self.build_ships_from_matrix()
        self.tie = 0
        self.my_points = 0
        self.enemies_points = 0

        self.xml_init()
        self.state.append((self.my_board, self.enemies_board, self.my_points))

        self.my_ships = 0

        self.s = Server(self, address, port)
        self.s.start()

        self.is_client_connected = False

        WindowConfig.config(self)
        self.scene = QGraphicsScene()
        self.my_screen.setScene(self.scene)

        self.dm = DrawMap(self.scene)

        # self.my_screen.viewport().installEventFilter(self)

        # buttons

        self.ai_btn.clicked.connect(self.play_with_ai)
        self.move_btn.clicked.connect(self.move_ship)
        self.next_btn.clicked.connect(self.next_step)
        self.back_btn.clicked.connect(self.step_back)
        self.save_btn.clicked.connect(self.save_dialog)
        self.clear_btn.clicked.connect(self.clear_board)
        self.read_file_btn.clicked.connect(self.load_state)
        self.gen_rand_btn.clicked.connect(self.build_random_matrix)
        self.shot_btn.clicked.connect(self.execute_shot)
        self.set_ship_btn.clicked.connect(self.set_ship)
        self.connect_btn.clicked.connect(self.connect_with_enemy)
        self.exit_btn.clicked.connect(self.close_app)
        pass

    def xml_init(self):
        self.root = ET.Element("game")

        doc = ET.SubElement(self.root, "initialization")
        ET.SubElement(doc, "my_board", name="my board").text = str(self.my_board)
        ET.SubElement(doc, "enemies_board", name="enemies board").text = str(self.my_board)

    def play_with_ai(self):
        self.ai_mode = True
        self.ai = AI(self)
        pass

    def move_ship(self):
        x = self.x_field.text()
        y = self.y_field.text()
        x, y = parser(x, y)

        if (x != -1 and y != -1) and \
                (self.my_board[x][y] == 1):
            new_x = randint(0, 9)
            new_y = randint(0, 8)
            if self.my_board[new_x][new_y] not in range(1,4):
                self.my_board[x][y] = 0
                self.my_board[new_x][new_y] = 1
                self.clear_board()
                self.build_shots_from_matrix()
        else:
            QMessageBox.about(self, "Move", "Wybierz statek")
        pass

    def next_step(self):
        self.my_board = self.state[self.step_nr][0]
        self.enemies_board = self.state[self.step_nr][1]
        self.my_points = self.state[self.step_nr][2]

        self.clear_board()
        self.build_shots_from_matrix()
        self.step_nr += 1

    def step_back(self):
        self.step_nr -= 1

        self.my_board = self.state[self.step_nr][0]
        self.enemies_board = self.state[self.step_nr][1]
        self.my_points = self.state[self.step_nr][2]

        self.clear_board()
        self.build_shots_from_matrix()

    def save_dialog(self):

        fname = QFileDialog.getOpenFileName(self, 'Open file',
                '/home')

        pickle.dump( self.state, open( fname, "wb" ) )

        self.tree = ET.ElementTree(self.root)

        xmlstr = minidom.parseString(ET.tostring(self.root)).toprettyxml(indent="   ")
        with open("newState.xml", "w") as f:
            f.write(xmlstr)

        # self.tree.write("states.xml")

    def load_state(self):
        '''
        Reads game state from binary file.
        '''

        fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
        self.state = pickle.load(open(fname, "rb"))

        # TODO
        tree = ET.parse('New_Database.xml')
        root = tree.getroot()

    def eventFilter(self, source, event):
        if (event.type() == QEvent.MouseButtonPress and
                source is self.my_screen.viewport()):
                    QCursor.pos()
                    QMessageBox.about(self, "Debug", "debug {}".format(QCursor.pos()))

    def connect_with_enemy(self):
        '''
        Connects our game instance with our enemy
        '''
        ip_addr = self.ip_addr_field.text()
        port = self.port_field.text()
        port = int(port)

        pattern = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
        test = pattern.match(ip_addr)

        if test:
            QMessageBox.about(self, "Info", "{}".format(ip_addr, port))
            self.connect_btn.hide()
            self.ip_addr_field.hide()
            self.port_field.hide()
            self.client = jsonSocket.JsonClient(address=ip_addr, port=port)
            self.client.connect()
        else:
            QMessageBox.about(self, "Blad", "Bledny format adresu ip")

    def execute_shot(self):
        '''
        Shot execution.
        '''
        is_hit = False

        if not self.ai_mode:

            if not self.is_my_turn:
                QMessageBox.about(self, "Czekaj", "Czekaj na twoją turę!")
                return

            if not self.is_client_connected:
                x = self.x_field.text()
                y = self.y_field.text()
                x, y = parser(x, y)

                if x != -1 and y != -1:

                    if self.enemies_board[x][y] == 1:
                        self.enemies_board[x][y] = 2
                        is_hit = True
                        picture = 'res/ok.png'
                    else:
                        self.enemies_board[x][y] = 3
                        is_hit = False
                        picture = 'res/cancel.png'

                    # if x % 2 != 0:
                    #     self.item = QGraphicsPixmapItem(QPixmap(picture))
                    #     self.item.setOffset(16+y*61, 17+(x*52))
                    #     self.scene.addItem(self.item)
                    #
                    #
                    # elif x % 2 == 0:
                    #     self.item = QGraphicsPixmapItem(QPixmap(picture))
                    #     self.item.setOffset(47+y*61, 16+x*53)
                    #     self.scene.addItem(self.item)

                    # QMessageBox.about(self, "Strzal", "({}, {})".format(x, y))
                    self.send = (x, y)
                    self.client.sendObj({"x": x, "y": y})
                else:
                    QMessageBox.about(self, "Blad", "Niepoprawne pola")
            else:
                QMessageBox.about(self, "Info", "Nie polaczyles sie z przeciwnikiem")

            if is_hit:
                pass
            else:
                pass

        else:
            x = self.x_field.text()
            y = self.y_field.text()
            x, y = parser(x, y)

            if x != -1 and y != -1:

                if self.enemies_board[x][y] == 1:
                    self.enemies_board[x][y] = 2
                    is_hit = True
                    picture = 'res/ok.png'
                else:
                    self.enemies_board[x][y] = 3
                    is_hit = False
                    picture = 'res/cancel.png'

                self.is_my_turn = False
                self.ai.make_move()

    def get_shot(self, x, y):
        '''
        Shot handler.
        :param x: x coord of shot
        :param y: y coord of shot
        '''
        is_Hit = False

        if self.my_board[x][y] == 1:
            self.my_board[x][y] = 2
            is_hit = True
            picture = 'res/ok.png'
        else:
            self.my_board[x][y] = -1
            is_hit = False
            picture = 'res/cancel.png'

        if x % 2 != 0:
            self.item = QGraphicsPixmapItem(QPixmap(picture))
            self.item.setOffset(16+y*61, 17+(x*52))
            self.scene.addItem(self.item)

        elif x % 2 == 0:
            self.item = QGraphicsPixmapItem(QPixmap(picture))
            self.item.setOffset(47+y*61, 16+x*53)
            self.scene.addItem(self.item)

    def set_ship(self):
        if not self.is_client_connected:
            x = self.set_X.text()
            y = self.set_Y.text()
            x, y = parser(x, y)

            if self.my_ships < 10:
                if x != -1 and y != -1:

                    # Dodanie statku do pola
                    self.my_board[x][y] = 1
                    self.my_ships += 1

                    self.enemies_board[x][y] = 1

                    QMessageBox.about(self, "Statek na polu", "({}, {})".format(x, y))
                    if x % 2 != 0:
                        self.item = QGraphicsPixmapItem(QPixmap(self.STATEK))
                        self.item.setOffset(18+y*61, 21+(x*52))
                        self.scene.addItem(self.item)

                    elif x % 2 == 0:
                        self.item = QGraphicsPixmapItem(QPixmap(self.STATEK))
                        self.item.setOffset(49+y*61, 21+x*53)
                        self.scene.addItem(self.item)

                else:
                    QMessageBox.about(self, "Blad", "Niepoprawne pola")
            else:
                    QMessageBox.about(self, "Blad", "Wykorzystales juz cala flote")
        else:
            QMessageBox.about(self, "Info", "Nie polaczyles sie z przeciwnikiem")

        if self.my_ships == 5:
            self.ready_to_play = True
            self.set_X.hide()
            self.set_Y.hide()
            self.set_ship_btn.hide()
            QMessageBox.about(self, "Info", "Jestes gotowy aby rozpoczac gre")

    def build_ships_from_matrix(self):
        for x in range(10):
            for y in range(9):
                if self.my_board[x][y] == 1:

                    if x % 2 != 0:
                        self.item = QGraphicsPixmapItem(QPixmap(self.STATEK))
                        self.item.setOffset(18+y*61, 21+(x*52))
                        self.scene.addItem(self.item)

                    elif x % 2 == 0:
                        self.item = QGraphicsPixmapItem(QPixmap(self.STATEK))
                        self.item.setOffset(49+y*61, 21+x*53)
                        self.scene.addItem(self.item)

    def build_shots_from_matrix(self):
        picture = 'res/cancel.png'
        for x in range(10):
            for y in range(9):

                if self.my_board[x][y] == 2 or self.my_board[x][y] == 3:

                    if x % 2 != 0:
                        self.item = QGraphicsPixmapItem(QPixmap(picture))
                        self.item.setOffset(16+y*61, 17+(x*52))
                        self.scene.addItem(self.item)

                    elif x % 2 == 0:
                        self.item = QGraphicsPixmapItem(QPixmap(picture))
                        self.item.setOffset(47+y*61, 16+x*53)
                        self.scene.addItem(self.item)

                self.build_ships_from_matrix()

    def build_random_matrix(self):
        self.dm.clear()
        self.ready_to_play = True
        self.set_X.hide()
        self.set_Y.hide()
        self.set_ship_btn.hide()
        self.my_board = np.zeros((10, 9))
        ships = 0
        while ships < 10:
            x = randint(0, 9)
            y = randint(0, 8)
            if self.my_board[x][y] == 0:
                self.my_board[x][y] = 1
                ships += 1
        self.build_ships_from_matrix()
        # self.state.append((self.my_board, self.enemies_board, self.my_points))

    def clear_board(self):
        self.dm.clear()

    def close_app(self):
        if self.is_client_connected:
            self.client.close()
        self.s.stop()
        self.close()

    @staticmethod
    def _parser(x, y):
        try:
            x = int(x)
            y = int(y)
            if x not in range(11):
                x, y = 0, 0
            if y not in range(11):
                x, y = 0, 0
        except ValueError:
            x, y = 0, 0
        return x, y
class Button(QWidget):
    LEFT = "left"
    RIGHT = "right"
    TOP = "top"
    BOTTOM = "bottom"
    
    def __init__(self, filename=None, width=None, height=None, x=0, y=0, pixmap=None, group=None, pos=None, size=None, padding=None):
        self.effects = {}
        self.animations = {}
        self.filename = filename if isinstance(filename, str) else None
        if pixmap :
            self.pixmap = pixmap
        elif isinstance(filename, QPixmap) :
            self.pixmap = filename
        elif isinstance(filename, str) :
            self.pixmap = QPixmap(os.path.dirname(__file__) + '/../buttons/' + filename)
        else :
            self.pixmap = None
        if (width != None or height != None) and self.pixmap != None :
            self.pixmap = self.pixmap.scaled(width if width != None else height,
                                     height if height != None else width, Qt.KeepAspectRatio)
        self.group = group
        self.pos = pos
        self.size = size
        self.padding = padding
        self.x = x
        self.y = y
        
        if isinstance(self.pixmap, QPixmap) :
            self.item = QGraphicsPixmapItem(self.pixmap)
            self.item.setOffset(QPointF(self.getX(), self.getY()))
            self.effect = QGraphicsDropShadowEffect()
            self.effect.setOffset(0, 0)
            self.effect.setBlurRadius(0)
            self.effect.setColor(Qt.green)
            self.addEffect('shadow', self.effect, True)
            self.addAnimation('glow', Glow(15, 300, self, maxRadius=80, minRadius=5))
#             self.item.setGraphicsEffect(effect)
#             self.tl = QTimeLine(10000)
#             self.t = QGraphicsItemAnimation()
#             self.t.setItem(self.item)
#             self.t.setTimeLine(self.tl)
#             self.tl.connect(self.tl, SIGNAL('frameChanged(int)'), self.test)
#             self.tl.start()
#             print 'added'
    def test(self):
        print 1
    def getX(self):
        x = self.x
        if self.group != None :
            x += self.group.x
        if self.pos != None :
            x += self.getPositionX()
        return x
    
    def getY(self):
        y = self.y
        if self.group != None :
            y += self.group.getY()
        if self.pos != None :
            y += self.getPositionY()
        return y
    
    def getPositionX(self):
        if self.pos == Button.TOP or self.pos == Button.BOTTOM:
            return self.getSize() + self.getPadding() if self.getCols() > 1 else 0
        elif self.pos == Button.RIGHT :
            return self.getSize() * (self.getCols() - 1) + self.getPadding() * (self.getCols() - 1)
        elif self.pos == Button.LEFT :
            return 0
    
    def getPositionY(self):
        if self.pos == Button.LEFT or self.pos == Button.RIGHT:
            return self.getSize() + self.getPadding() if self.getRows() > 1 else 0
        elif self.pos == Button.BOTTOM :
            return self.getSize() * (self.getRows() - 1) + self.getPadding() * (self.getRows() - 1)
        elif self.pos == Button.TOP :
            return 0
    
    def getSize(self):
        if self.size != None : 
            return self.size
        if isinstance(self.group, ButtonGroup) :
            return self.group.getSize()
        return 0
    
    def getPadding(self):
        if self.padding != None :
            return self.padding
        if isinstance(self.group, ButtonGroup) :
            return self.group.getPadding()
        return 0
    
    def getRows(self):
        if isinstance(self.group, ButtonGroup) :
            return self.group.getRows()
        return 1;
    def getCols(self):
        if isinstance(self.group, ButtonGroup) :
            return self.group.getCols()
        return 1;
    
    def addEffect(self, name, effect, show=True):
        self.effects[name] = effect
        if show :
            self.setEffect(name)
    
    def setEffect(self, name):
        self.item.setGraphicsEffect(self.effects[name])
    
    def setScene(self, scene):
        self.scene = scene
        self.scene.addItem(self.item)
    
    def addAnimation(self, name, animation):
        self.animations[name] = animation
    
    def play(self, name):
        print 'start'
        self.animations[name].start()
    
    def update(self, pressed):
        if pressed :
            print 'pressed'
            self.play('glow')
class SixAxisView(QGraphicsView):

    def __init__(self, *args):
        QGraphicsView.__init__(self, *args)
        self.move(20, 240)
        self.outerD = 125
        self.innerD = 35
        self.innerRange = 48
        self.inputRange = 256
        self.thresh = 3
        self.padding = 40
        self.marginTop = 10
        self.worker = QSixAxisThread()
        self.worker.valueUpdated.connect(self.moveJoysticks)
        self.worker.start()
        self.setContentsMargins(0, 0, 0, 0)
        self.setMaximumHeight(180)
        self.setMaximumWidth(420)
        self.setMinimumHeight(240)
        self.setMinimumWidth(300)
        self.adjustSize()
        self.scene = QGraphicsScene(self)
        self.outerCircle1 = QGraphicsEllipseItem(0, self.marginTop, self.outerD, self.outerD)
        self.outerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine))
        self.outerCircle1.setBrush(Qt.gray)
        self.innerCircle1 = QGraphicsEllipseItem(self.outerD / 2 - self.innerD / 2, self.outerD / 2 - self.innerD / 2 + self.marginTop, self.innerD, self.innerD)
        self.innerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine))
        self.innerCircle1.setBrush(Qt.lightGray)
        self.scene.addItem(self.outerCircle1)
        self.scene.addItem(self.innerCircle1)
        self.outerCircle2 = QGraphicsEllipseItem(self.outerD + self.padding, self.marginTop, self.outerD, self.outerD)
        self.outerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine))
        self.outerCircle2.setBrush(Qt.gray)
        self.innerCircle2 = QGraphicsEllipseItem(self.outerD + self.padding + self.outerD / 2 - self.innerD / 2, self.outerD / 2 - self.innerD / 2 + self.marginTop, self.innerD, self.innerD)
        self.innerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine))
        self.innerCircle2.setBrush(Qt.lightGray)
        self.scene.addItem(self.outerCircle2)
        self.scene.addItem(self.innerCircle2)
        self.setScene(self.scene)
        self.setStyleSheet('background-color:transparent; border-width: 0px; border: 0px;')
        self.currentX = 0
        self.currentY = 0
        self.currentA = 0
        self.currentZ = 0
        self.psBtn = QPixmap(os.getcwd() + '/../icons/bt_PS.png')
        self.psBtn = self.psBtn.scaled(50, 50, Qt.KeepAspectRatio)
        self.psItem = QGraphicsPixmapItem(self.psBtn)
        self.psItem.setOffset(QPointF(self.outerD - 4, 0))
        self.effect = QGraphicsDropShadowEffect()
        self.effect.setOffset(0, 0)
        self.effect.setBlurRadius(20)
        self.effect.setColor(Qt.green)
        self.psItem.setGraphicsEffect(self.effect)
        self.scene.addItem(self.psItem)
        self.tl2 = QTimeLine(10000)
        self.tl2.setFrameRange(0, 10000)
        self.c = QGraphicsItemAnimation()
        self.c.setItem(self.psItem)
        self.c.setTimeLine(self.tl2)
        self.tl2.connect(self.tl2, SIGNAL('frameChanged(int)'), self.updateEffect)
        self.effectd = 3
        self.tl2.start()

    def updateEffect(self):
        if self.effect.blurRadius() > 50:
            self.effectd = -3
        elif self.effect.blurRadius() < 5:
            self.effectd = 3
        self.effect.setBlurRadius(self.effect.blurRadius() + self.effectd)

    def update(self, *args, **kwargs):
        return QGraphicsView.update(self, *args, **kwargs)

    def moveJoysticks(self, x, y, a, z, l3, r3):
        x2 = x * self.innerRange / self.inputRange - self.innerRange / 2
        y2 = y * self.innerRange / self.inputRange - self.innerRange / 2
        a2 = a * self.innerRange / self.inputRange - self.innerRange / 2
        z2 = z * self.innerRange / self.inputRange - self.innerRange / 2
        if -self.thresh <= x2 <= self.thresh:
            x2 = 0
        if -self.thresh <= y2 <= self.thresh:
            y2 = 0
        if -self.thresh <= a2 <= self.thresh:
            a2 = 0
        if -self.thresh <= z2 <= self.thresh:
            z2 = 0
        self.tl = QTimeLine(10)
        self.tl.setFrameRange(0, 10)
        self.a = QGraphicsItemAnimation()
        self.a.setItem(self.innerCircle1)
        self.a.setTimeLine(self.tl)
        self.a.setPosAt(0, QPointF(self.currentX, self.currentY))
        self.a.setTranslationAt(1, x2, y2)
        if l3:
            self.innerCircle1.setPen(QPen(QColor(Qt.white), 1, Qt.SolidLine))
            self.innerCircle1.setBrush(QColor(200, 225, 3))
        else:
            self.innerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine))
            self.innerCircle1.setBrush(Qt.lightGray)
        if r3:
            self.innerCircle2.setPen(QPen(QColor(Qt.white), 1, Qt.SolidLine))
            self.innerCircle2.setBrush(QColor(200, 225, 3))
        else:
            self.innerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine))
            self.innerCircle2.setBrush(Qt.lightGray)
        self.b = QGraphicsItemAnimation()
        self.b.setItem(self.innerCircle2)
        self.b.setTimeLine(self.tl)
        self.b.setPosAt(0, QPointF(self.currentA, self.currentZ))
        self.b.setTranslationAt(1, a2, z2)
        self.currentX = x2
        self.currentY = y2
        self.currentA = a2
        self.currentZ = z2
        self.tl.start()