00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import rospy
00020 import os
00021 from roslib.packages import get_pkg_dir
00022 import math
00023
00024 from python_qt_binding.QtGui import *
00025 from python_qt_binding.QtCore import *
00026
00027 from airbus_pyqt_extend.QtAgiCore import loadRsc
00028
00029 rsc = loadRsc("airbus_pyqt_extend")
00030
00031 class QAgiJoystickHandle(QLabel):
00032
00033 def __init__(self, parent=None, w=100, h=100):
00034 QLabel.__init__(self, parent)
00035
00036 self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint)
00037 self.setWindowFlags(Qt.WindowStaysOnTopHint)
00038 self.setAttribute(Qt.WA_NoSystemBackground)
00039 self.setAttribute(Qt.WA_TranslucentBackground)
00040 self.setFixedSize(QSize(w, h))
00041
00042 self.setPixmap(rsc.images.getPixmap("jHand",w, h))
00043
00044 self._x0 = w / 2
00045 self._y0 = h / 2
00046
00047 def follow(self, x, y):
00048 self.move(x-self._x0, y-self._y0)
00049
00050 def stop(self, x0, y0):
00051 self.move(x0-self._x0, y0-self._y0)
00052
00053 def x0(self):
00054 return self._x0
00055
00056 def y0(self):
00057 return self._y0
00058
00059 def resizeEvent(self, event):
00060 self._x0 = self.width() / 2
00061 self._y0 = self.height() / 2
00062 self.setPixmap(rsc.images.getPixmap("jHand",self.width(), self.height()))
00063
00064 class QAgiAbstractJoystick(QLabel):
00065
00066 def __init__(self, parent=None, listener=None):
00067 QLabel.__init__(self, parent)
00068
00069 self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint)
00070 self.setWindowFlags(Qt.WindowStaysOnTopHint)
00071 self.setAttribute(Qt.WA_NoSystemBackground)
00072 self.setAttribute(Qt.WA_TranslucentBackground)
00073 self.setAttribute(Qt.WA_AcceptTouchEvents)
00074 self.setFixedSize(QSize(256,256))
00075
00076 self._listener = listener
00077
00078 self._handle = QAgiJoystickHandle(self, 90, 90)
00079
00080 self._rayon = self.width()/ 4
00081 self._x0 = self.width() / 2
00082 self._y0 = self.height() / 2
00083
00084 self._background = None
00085
00086 self._handle.stop(self._x0, self._y0)
00087
00088 def setBackgroundRsc(self, name):
00089 self._background = name
00090 self.setPixmap(rsc.images.getPixmap( self._background, self.width(), self.height()))
00091
00092 def setHandleSize(self, size):
00093 self._handle.setFixedSize(size)
00094 self._handle.resizeEvent(None)
00095 self._handle.stop(self._x0, self._y0)
00096
00097 def setHandleEventListener(self, listener):
00098 self._listener = listener
00099
00100 def mousePressEvent(self, event):
00101 self.emit(SIGNAL('joystickPressedEvent'))
00102
00103 def mouseMoveEvent(self, event):
00104
00105 try:
00106 if self._listener is not None:
00107 self._listener(event)
00108 except Exception as ex:
00109 rospy.logerr(str(ex))
00110
00111 def mouseReleaseEvent(self, event):
00112 self._handle.stop(self._x0, self._y0)
00113 self.emit(SIGNAL('valuesChanged'),[0, 0])
00114 self.emit(SIGNAL('joystickReleasedEvent'))
00115
00116 def x0(self):
00117 return self._x0
00118
00119 def y0(self):
00120 return self._y0
00121
00122 def getRadius(self):
00123 return self._rayon
00124
00125 def getHandle(self):
00126 return self._handle
00127
00128 def resizeEvent(self, event):
00129 self._rayon = self.width()/ 4
00130 self._x0 = self.width() / 2
00131 self._y0 = self.height() / 2
00132 self._handle.stop(self._x0, self._y0)
00133 self.setPixmap(rsc.images.getPixmap( self._background, self.width(), self.height()))
00134
00135
00136 class QAgiJoystick(QAgiAbstractJoystick):
00137
00138 def __init__(self, parent=None):
00139 QAgiAbstractJoystick.__init__(self, parent)
00140 self.setBackgroundRsc("joyXY")
00141 self.setHandleEventListener(self._handle_event_listner)
00142
00143
00144 def _handle_event_listner(self, event):
00145
00146 x = event.pos().x()
00147 y = event.pos().y()
00148 B = event.pos().x() - self.x0()
00149 A = self._y0 - event.pos().y()
00150
00151 th = math.sqrt((A*A) + (B*B))
00152
00153 if th < self.getRadius():
00154 x = event.pos().x()
00155 y = event.pos().y()
00156 else:
00157
00158 try:
00159 AE = (self.getRadius() / th)*B
00160 DE = (AE / B)*A
00161 x = AE+self.x0()
00162 y = -DE+self.y0()
00163 except ZeroDivisionError:
00164 pass
00165
00166 self.getHandle().follow(x, y)
00167
00168 x = float((x - self.getHandle().x0())-(self.x0()- self.getHandle().x0())) / self.getRadius()
00169 y = float((self.y0()- self.getHandle().y0())-(y - self.getHandle().y0())) / self.getRadius()
00170
00171 self.emit(SIGNAL('valuesChanged'),[x, y])
00172
00173 class QAgiHJoystick(QAgiAbstractJoystick):
00174
00175 def __init__(self, parent=None):
00176 QAgiAbstractJoystick.__init__(self, parent)
00177 self.setBackgroundRsc("joyX")
00178 self.setHandleEventListener(self._handle_event_listner)
00179
00180 def _handle_event_listner(self, event):
00181
00182 x = event.pos().x()
00183
00184 if event.pos().x() > self.x0()-self.getRadius() and \
00185 event.pos().x() < self.x0()+self.getRadius():
00186
00187 self.getHandle().follow(x, self.y0())
00188
00189 x = float((x - self.getHandle().x0())-(self.x0()- self.getHandle().x0())) / self.getRadius()
00190 self.emit(SIGNAL('valuesChanged'),[x, 0.])
00191
00192 class QAgiVJoystick(QAgiAbstractJoystick):
00193
00194 def __init__(self, parent=None):
00195 QAgiAbstractJoystick.__init__(self, parent)
00196 self.setBackgroundRsc("joyY")
00197 self.setHandleEventListener(self._handle_event_listner)
00198
00199 def _handle_event_listner(self, event):
00200
00201 y = event.pos().y()
00202
00203 if y > self.y0()-self.getRadius() and \
00204 y < self.y0()+self.getRadius():
00205
00206 self.getHandle().follow(self.x0(), y)
00207
00208 y = float((self.y0()- self.getHandle().y0())-(y - self.getHandle().y0())) / self.getRadius()
00209 self.emit(SIGNAL('valuesChanged'),[0., y])
00210
00211 def resizeEvent(self, event):
00212 self.setPixmap(rsc.images.getPixmap("joyY", self.width(), self.height()))
00213
00214 class Unitest(QWidget):
00215
00216 def __init__(self):
00217 QWidget.__init__(self)
00218
00219 layout = QHBoxLayout(self)
00220
00221 m_joystick = QAgiJoystick()
00222 m_joystick.setHandleSize(QSize(150,150))
00223 m_joystick.setFixedSize(QSize(512,512))
00224
00225 m_h_joystick = QAgiHJoystick()
00226 m_v_joystick = QAgiVJoystick()
00227
00228 self.connect(m_joystick, SIGNAL("valuesChanged"), self._update_values)
00229 self.connect(m_h_joystick, SIGNAL("valuesChanged"), self._update_values)
00230 self.connect(m_v_joystick, SIGNAL("valuesChanged"), self._update_values)
00231
00232 layout.addWidget(m_joystick)
00233 layout.addWidget(m_h_joystick)
00234 layout.addWidget(m_v_joystick)
00235
00236 def _update_values(self, values):
00237 print str(values)
00238
00239 if __name__ == "__main__":
00240
00241 import sys, os
00242
00243 a = QApplication(sys.argv)
00244
00245 name = 'omni_gui_test_node_%d' % os.getpid()
00246 rospy.init_node(name)
00247 MainWindow = Unitest()
00248 MainWindow.show()
00249 a.exec_()
00250
00251