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


jsk_rqt_plugins
Author(s):
autogenerated on Wed May 1 2019 02:40:16