Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import rospy
00020
00021 from python_qt_binding.QtGui import *
00022 from python_qt_binding.QtCore import *
00023
00024 from airbus_pyqt_extend.QtAgiCore import loadRsc
00025
00026 rsc = loadRsc("airbus_pyqt_extend")
00027
00028
00029
00030
00031
00032
00033
00034
00035 class QAgiSilderButton(QLabel):
00036
00037 TRIGGER = 'statusChanged'
00038
00039 BACKGROUND_CSS = rsc.styles["QAgiSilderButton.background"]
00040 ON_CSS = rsc.styles["QAgiSilderButton.on"]
00041 OFF_CSS = rsc.styles["QAgiSilderButton.off"]
00042
00043 def __init__(self,parent=None, initial_state=False, on_label='ON', off_label='OFF'):
00044 """! The constructor.
00045 @param parent: object parent.
00046 @type parent: QObject.
00047
00048 @param initial_state: button state (False = off/ True = on).
00049 @type initial_state: bool.
00050 """
00051
00052 QLabel.__init__(self, parent)
00053
00054 self._on_label = on_label
00055 self._off_label = off_label
00056 self._th_off = None
00057 self._th_on = None
00058 self._on = None
00059 self._off = None
00060 self._stick_off = None
00061 self._stick_on = None
00062 self._pressed = False
00063
00064 self.setStyleSheet(self.BACKGROUND_CSS)
00065
00066 self._trigger = QLabel(self)
00067 self._trigger.setMouseTracking(True)
00068
00069 self._trigger.resize(self.width()/2+1,self.height())
00070 self._th_off = self._trigger.width()/2
00071 self._th_on = self._th_off+1
00072 self._on = self.width()-self._trigger.width()
00073 self._off = 0
00074 self._stick_off = int(0.3*self._on)
00075 self._stick_on = int(2*self._stick_off)
00076
00077 if initial_state == True:
00078 self._trigger.setStyleSheet(self.ON_CSS)
00079 self._trigger.setText(self._on_label)
00080 self._trigger.move(self._on,self._trigger.y())
00081 else:
00082 self._trigger.setStyleSheet(self.OFF_CSS)
00083 self._trigger.setText(self._off_label)
00084
00085 def setBackgroundStyle(self, style):
00086 self.BACKGROUND_CSS = style
00087
00088 def setOnText(self, text):
00089 self._on_label = text
00090 self.setState(self.getState(),True)
00091
00092 def setOnStyle(self, style):
00093 self.ON_CSS = style
00094
00095 def setOffText(self, text):
00096 self._off_label = text
00097 self.setState(self.getState(),True)
00098
00099 def setOffStyle(self, style):
00100 self.OFF_CSS = style
00101
00102 def getState(self):
00103 """! Get button state.
00104 @param state: button state (True/False).
00105 @type state: bool.
00106 """
00107 if self._trigger.x() < self._th_off:
00108 return False
00109 else:
00110 return True
00111
00112 def setState(self, state , anonymous=False):
00113 """! Set button state.
00114 @return state: button state (False = off/ True = on).
00115 @type state: bool.
00116
00117 @return anonymous: Option to enabled/desibled QT SIGNAL('statusChanged').
00118 @type anonymous: bool.
00119 """
00120 if state:
00121 self._trigger.move(self._on,self._trigger.y())
00122 self._trigger.setStyleSheet(self.ON_CSS)
00123 self._trigger.setText(self._on_label)
00124
00125 if anonymous is False:
00126 self.emit(SIGNAL('statusChanged'), True)
00127 else:
00128 self._trigger.move(self._off,self._trigger.y())
00129 self._trigger.setStyleSheet(self.OFF_CSS)
00130 self._trigger.setText(self._off_label)
00131
00132 if anonymous is False:
00133 self.emit(SIGNAL('statusChanged'), False)
00134
00135 def mousePressEvent(self, event):
00136 """! Detect button pressed.
00137 @param event: event.
00138 @type event: QEvent.
00139 """
00140 self._pressed = True
00141
00142 def mouseReleaseEvent(self, event):
00143 """! Detect button released.
00144 @param event: event.
00145 @type event: QEvent
00146 """
00147 self._pressed = False
00148
00149 x = event.pos().x() - self._trigger.width()/2
00150
00151 if x >= self._th_on:
00152 self._trigger.move(self._on,self._trigger.y())
00153 self._trigger.setStyleSheet(self.ON_CSS)
00154 self._trigger.setText(self._on_label)
00155 self.emit(SIGNAL('statusChanged'),True)
00156 elif x <= self._th_off:
00157 self._trigger.move(self._off,self._trigger.y())
00158 self._trigger.setStyleSheet(self.OFF_CSS)
00159 self._trigger.setText(self._off_label)
00160 self.emit(SIGNAL('statusChanged'),False)
00161
00162 def mouseMoveEvent(self, event):
00163 """! Detect button moved.
00164 @param event: event.
00165 @type event: QEvent
00166 """
00167
00168 x = event.pos().x() - self._trigger.width()/2
00169 if self._pressed and x >= self._off and x <= self._on:
00170 if x > self._stick_on:
00171 self._trigger.move(self._on,self._trigger.y())
00172 self._trigger.setStyleSheet(self.ON_CSS)
00173 self._trigger.setText(self._on_label)
00174 elif x < self._stick_off:
00175 self._trigger.move(self._off,self._trigger.y())
00176 self._trigger.setStyleSheet(self.OFF_CSS)
00177 self._trigger.setText(self._off_label)
00178 else:
00179 self._trigger.move(x,self._trigger.y())
00180
00181 def resizeEvent(self,event):
00182 """! Resize button.
00183 @param event: event.
00184 @type event: QEvent
00185 """
00186
00187 if self.width() % 2 == 0:
00188 self.resize(self.width()+1,self.height())
00189 else:
00190 self.resize(self.width(),self.height())
00191
00192 if self.getState():
00193 self._trigger.setStyleSheet(self.ON_CSS)
00194 else:
00195 self._trigger.setStyleSheet(self.OFF_CSS)
00196
00197 self._trigger.resize(self.width()/2+1,self.height())
00198 self._th_off = self._trigger.width()/2
00199 self._th_on = self._th_off+1
00200 self._on = self.width()-self._trigger.width()
00201 self._off = 0
00202 self._stick_off = int(0.3*self._on)
00203 self._stick_on = int(2*self._stick_off)
00204
00205 if self._trigger.x() != 0:
00206 self._trigger.move(self._on,self._trigger.y())
00207
00208 if __name__ == "__main__":
00209
00210 from python_qt_binding.QtGui import QApplication
00211 import sys
00212
00213 rospy.init_node('utt_agi_silderbutton')
00214
00215 app = QApplication(sys.argv)
00216 MainWindow = QAgiSilderButton()
00217
00218 def trigger(state):
00219 print state
00220
00221 QObject.connect(MainWindow, SIGNAL('statusChanged'), trigger)
00222
00223 MainWindow.show()
00224 app.exec_()
00225
00226