mini_maxwell.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 from rqt_gui_py.plugin import Plugin
00003 from python_qt_binding.QtGui import (QAction, QIcon, QMenu, QWidget,
00004                                      QPainter, QColor, QFont, QBrush, 
00005                                      QPen)
00006 from python_qt_binding.QtCore import Qt, QTimer, qWarning, Slot, QEvent
00007 from threading import Lock
00008 import rospy
00009 import python_qt_binding.QtCore as QtCore
00010 from std_msgs.msg import Bool, Time
00011 import math
00012 
00013 class DRCEnvironmentViewer(Plugin):
00014     def __init__(self, context):
00015         super(DRCEnvironmentViewer, self).__init__(context)
00016         self.setObjectName("DRCEnvironmentViewer")
00017         self._widget = DRCEnvironmentViewerWidget()
00018         context.add_widget(self._widget)
00019         
00020 class DRCEnvironmentViewerWidget(QWidget):
00021     _SMILEY = ":)"
00022     _FROWN = ":("
00023     _OK_COLOR = QColor("#18FFFF")
00024     _DISABLED_COLOR = QColor("#BDBDBD")
00025     _BLACKOUT_COLOR = QColor("#F44336")
00026     def __init__(self):
00027         self.lock = Lock()
00028         super(DRCEnvironmentViewerWidget, self).__init__()
00029         self.is_disabled = False
00030         self.is_blackout = False
00031         self.next_whiteout_time = rospy.Time.now()
00032         self.blackout_time = rospy.Time.now()
00033         self.event = None
00034         self.sub_is_disabled = rospy.Subscriber("/drc_2015_environment/is_disabled",
00035                                                 Bool, self.isDisabledCallback)
00036         self.sub_is_blackout = rospy.Subscriber("/drc_2015_environment/is_blackout",
00037                                                 Bool, self.isBlackoutCallback)
00038         self.sub_next_whiteout_time = rospy.Subscriber("/drc_2015_environment/next_whiteout_time",
00039                                                        Time, self.nextWhiteoutTimeCallback)
00040         self._update_plot_timer = QTimer(self)
00041         self._update_plot_timer.timeout.connect(self.redraw)
00042         self._update_plot_timer.start(1000 / 15)
00043     def isDisabledCallback(self, msg):
00044         with self.lock:
00045             self.is_disabled = msg.data
00046     def isBlackoutCallback(self, msg):
00047         with self.lock:
00048             if not self.is_blackout and msg.data:
00049                 self.blackout_time = rospy.Time.now()
00050             self.is_blackout = msg.data
00051     def nextWhiteoutTimeCallback(self, msg):
00052         with self.lock:
00053             self.next_whiteout_time = msg.data
00054     def redraw(self):
00055         self.update()
00056         # if self.event:
00057         #     self.paintEvent(self.event)
00058     def paintEvent(self, event):
00059         with self.lock:
00060             self.event = event
00061             rect = event.rect()
00062             qp = QPainter()
00063             qp.begin(self)
00064             radius = min(rect.width(), rect.height()) - 50
00065             qp.setFont(QFont('Helvetica', 100))
00066             qp.setPen(QPen(QBrush(QColor(255, 255, 255)), 20))
00067             
00068             if self.is_disabled:
00069                 qp.fillRect(rect, self._DISABLED_COLOR)
00070                 qp.drawText(rect, QtCore.Qt.AlignCenter, self._FROWN)
00071             elif self.is_blackout:
00072                 qp.fillRect(rect, self._BLACKOUT_COLOR)
00073                 qp.drawText(rect, QtCore.Qt.AlignCenter, self._FROWN)
00074                 time_diff = (self.next_whiteout_time - rospy.Time.now()).to_sec()
00075                 if time_diff < 0:
00076                     time_diff = 0
00077                 time_ratio = time_diff / (self.next_whiteout_time - self.blackout_time).to_sec()
00078                 qp.setFont(QFont('Helvetica', 30))
00079                 qp.drawText(0, rect.height() - 150, rect.width(), 150, QtCore.Qt.AlignCenter, "%.1f sec" % time_diff)
00080                 # 0-360
00081                 if time_ratio > 0:
00082                     rad = int(math.fmod(time_ratio * 360 + 90*16, 360) * 16)
00083                     qp.drawArc((rect.width() - radius) / 2, (rect.height() - radius) / 2, radius, radius, 90*16, rad)
00084             else:
00085                 qp.fillRect(rect, self._OK_COLOR)
00086                 qp.drawText(rect, QtCore.Qt.AlignCenter, self._SMILEY)
00087             qp.end()


jsk_rqt_plugins
Author(s):
autogenerated on Sun Sep 13 2015 22:29:48