alarm.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2015 Airbus
00004 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 #   http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 
00018 
00019 import rospy
00020 import time
00021 
00022 from python_qt_binding.QtGui import *
00023 from python_qt_binding.QtCore import *
00024 from python_qt_binding import loadUi
00025 
00026 from airbus_cobot_gui.res import R
00027 
00028 ## @class Alarm
00029 ## @brief Class for alarm structure.
00030 class Alarm:
00031     
00032     NONE      = 0
00033     WARNING   = 1
00034     ERROR     = 2
00035     CRITICAL  = 3
00036     FATAL     = 4
00037     
00038     def __init__(self, context, level, what):
00039         self._context = context
00040         self._level = level
00041         self._what  = what
00042     
00043     def level(self):
00044         return self._level
00045     
00046     def what(self):
00047         return self._what
00048     
00049     def acquit(self):
00050         self._context.acquitAlarm()
00051     
00052 class Incremental(QThread):
00053     
00054     def __init__(self):
00055         QThread.__init__(self)
00056         
00057         self._start_pos = 30
00058         self._end_pos   = 300
00059         self._step      = 1
00060         
00061     def init(self, start_pos, end_pos, step, speed = 0.0005):
00062         
00063         self._start_pos = start_pos
00064         self._end_pos   = end_pos
00065         self._step      = step
00066         
00067     def run(self):
00068         
00069         for i in range(self._start_pos, self._end_pos, self._step):
00070             self.emit(SIGNAL('valueChanged(int)'),i)
00071             time.sleep(0.0005)
00072             
00073             
00074 class AlarmWidget(QWidget):
00075     
00076     def __init__(self, parent, alarm):
00077         QWidget.__init__(self)
00078         
00079         self._parent = parent
00080         self._alarm = alarm
00081         self._item  = QListWidgetItem('')
00082         
00083         loadUi(R.layouts.alarm_widget, self)
00084         
00085         self.connect(self.acquit_button, SIGNAL('clicked()'), self.onAcquit)
00086         
00087         self.what.setText(alarm.what())
00088         self._item.setSizeHint(self.sizeHint())
00089         
00090     def getItem(self):
00091         return self._item
00092         
00093     def onAcquit(self):
00094         self._parent.onRemoveAlarm(self.getItem())
00095         
00096 class AlarmManagerWidget(QWidget):
00097     
00098     OPENED = True
00099     CLOSED = False
00100     MINIMUM_HEIGHT = 80
00101     MAXIMUM_HEIGHT = 350
00102     
00103     def __init__(self, context):
00104         QWidget.__init__(self)
00105         
00106         loadUi(R.layouts.alarm_listview, self)
00107         
00108         self.setFixedHeight(self.MINIMUM_HEIGHT)
00109         self.alarm_listview.setSpacing(5)
00110         
00111         self._context = context
00112         self._parent  = context.getParent()
00113         self._status  = self.CLOSED
00114         self._lng     = context.getLanguage()
00115         
00116         self._incremental = Incremental()
00117         self.connect(self._incremental, SIGNAL('valueChanged(int)'), self.setFixedHeight)
00118         self.connect(self.drawable_button, SIGNAL('clicked()'), self.switchDrawable)
00119         
00120         self._parent.alarm_label.setPixmap(R.getPixmapById("ico_alarm").scaled(
00121                            75,75,
00122                            Qt.KeepAspectRatio,Qt.SmoothTransformation))
00123         
00124         self._context.addAlarmEventListner(self.onAppendAlarm)
00125         self._context.addLanguageEventListner(self.onTranslate)
00126         
00127         self._parent.alarm_label.setVisible(False)
00128         self.setVisible(False)
00129     
00130     def getDrawableHeight(self):
00131         
00132         h = self.MINIMUM_HEIGHT+self.drawable_button.height()
00133         
00134         if self.alarm_listview.count() > 1:
00135             h = ((self.alarm_listview.count()+1)*40)+20
00136             if h > self.MAXIMUM_HEIGHT:
00137                 h = self.MAXIMUM_HEIGHT
00138         
00139         return h
00140     
00141     def openDrawable(self):
00142         self._incremental.init(self.MINIMUM_HEIGHT, self.getDrawableHeight(), 1)
00143         self._incremental.start()
00144         self._status = self.OPENED
00145             
00146     def closeDrawable(self):
00147         self._incremental.init(self.getDrawableHeight(), self.MINIMUM_HEIGHT, -1)
00148         self._incremental.start()
00149         self._status = self.CLOSED
00150         
00151     def switchDrawable(self):
00152         
00153         if self._status == self.CLOSED:
00154             self.openDrawable()
00155         else:
00156             self.closeDrawable()
00157             
00158     def updateDrawable(self):
00159         
00160         if  self._status == self.OPENED:
00161             self.setFixedHeight(self.getDrawableHeight())
00162     
00163     def onAppendAlarm(self, alarm):
00164         
00165         widget = AlarmWidget(self, alarm)
00166         self.alarm_listview.addItem(widget.getItem())
00167         self.alarm_listview.setItemWidget(widget.getItem(), widget)
00168         
00169         if self.alarm_listview.count() > 1:
00170             self.updateHandleText(self.alarm_listview.count())
00171             self.drawable_button.setVisible(True)
00172         else:
00173             self.drawable_button.setVisible(False)
00174         
00175         self._parent.alarm_label.setVisible(True)
00176         self.setVisible(True)
00177         
00178         self.updateDrawable()
00179     
00180     def onRemoveAlarm(self, item):
00181         
00182         self.alarm_listview.takeItem(self.alarm_listview.row(item))
00183         
00184         if self.alarm_listview.count() == 1:
00185             self.drawable_button.setVisible(False)
00186             self.closeDrawable()
00187         elif self.alarm_listview.count() == 0:
00188             self._parent.alarm_label.setVisible(False)
00189             self.setVisible(False)
00190         else:
00191             self.updateHandleText(self.alarm_listview.count())
00192             self.updateDrawable()
00193             
00194     def updateHandleText(self, nb_alarm):
00195         self.drawable_button.setText("%s %s"%(nb_alarm,R.values.strings.alarms_waiting(self._lng)))
00196     
00197     def onTranslate(self, lng):
00198         self._lng = lng
00199         self.updateHandleText(self.alarm_listview.count())
00200         
00201     
00202 #End of file


airbus_cobot_gui
Author(s): Martin Matignon
autogenerated on Thu Jun 6 2019 17:59:19