1#!/usr/bin/env python 2 3 4############################################################################# 5## 6## Copyright (C) 2013 Riverbank Computing Limited. 7## Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 8## All rights reserved. 9## 10## This file is part of the examples of PyQt. 11## 12## $QT_BEGIN_LICENSE:BSD$ 13## You may use this file under the terms of the BSD license as follows: 14## 15## "Redistribution and use in source and binary forms, with or without 16## modification, are permitted provided that the following conditions are 17## met: 18## * Redistributions of source code must retain the above copyright 19## notice, this list of conditions and the following disclaimer. 20## * Redistributions in binary form must reproduce the above copyright 21## notice, this list of conditions and the following disclaimer in 22## the documentation and/or other materials provided with the 23## distribution. 24## * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor 25## the names of its contributors may be used to endorse or promote 26## products derived from this software without specific prior written 27## permission. 28## 29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 40## $QT_END_LICENSE$ 41## 42############################################################################# 43 44 45from PyQt5.QtWidgets import QApplication, QWidget 46from PyQt5.QtDBus import QDBusAbstractInterface, QDBusConnection 47 48from ui_controller import Ui_Controller 49 50 51class CarInterface(QDBusAbstractInterface): 52 53 def __init__(self, service, path, connection, parent=None): 54 super(CarInterface, self).__init__(service, path, 55 'org.example.Examples.CarInterface', connection, parent) 56 57 def accelerate(self): 58 self.asyncCall('accelerate') 59 60 def decelerate(self): 61 self.asyncCall('decelerate') 62 63 def turnLeft(self): 64 self.asyncCall('turnLeft') 65 66 def turnRight(self): 67 self.asyncCall('turnRight') 68 69 70class Controller(QWidget): 71 72 def __init__(self, parent=None): 73 super(Controller, self).__init__(parent) 74 75 self.ui = Ui_Controller() 76 77 self.ui.setupUi(self) 78 79 self.car = CarInterface('org.example.CarExample', '/Car', 80 QDBusConnection.sessionBus(), self) 81 82 self.startTimer(1000) 83 84 def timerEvent(self, event): 85 if self.car.isValid(): 86 self.ui.label.setText("connected") 87 else: 88 self.ui.label.setText("disconnected") 89 90 def on_accelerate_clicked(self): 91 self.car.accelerate() 92 93 def on_decelerate_clicked(self): 94 self.car.decelerate() 95 96 def on_left_clicked(self): 97 self.car.turnLeft() 98 99 def on_right_clicked(self): 100 self.car.turnRight() 101 102 103if __name__ == '__main__': 104 import sys 105 106 app = QApplication(sys.argv) 107 108 controller = Controller() 109 controller.show() 110 111 sys.exit(app.exec_()) 112