mini_maxwell.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from distutils.version import LooseVersion
4 import math
5 from threading import Lock
6 
7 import python_qt_binding
8 import python_qt_binding.QtCore as QtCore
9 from python_qt_binding.QtCore import QEvent
10 from python_qt_binding.QtCore import Qt
11 from python_qt_binding.QtCore import QTimer
12 from python_qt_binding.QtCore import qWarning
13 from python_qt_binding.QtCore import Slot
14 from python_qt_binding.QtGui import QBrush
15 from python_qt_binding.QtGui import QColor
16 from python_qt_binding.QtGui import QFont
17 from python_qt_binding.QtGui import QIcon
18 from python_qt_binding.QtGui import QPainter
19 from python_qt_binding.QtGui import QPen
20 
21 import rospy
22 from rqt_gui_py.plugin import Plugin
23 from std_msgs.msg import Bool, Time
24 
25 if LooseVersion(python_qt_binding.QT_BINDING_VERSION).version[0] >= 5:
26  from python_qt_binding.QtWidgets import QAction
27  from python_qt_binding.QtWidgets import QMenu
28  from python_qt_binding.QtWidgets import QWidget
29 
30 else:
31  from python_qt_binding.QtGui import QAction
32  from python_qt_binding.QtGui import QMenu
33  from python_qt_binding.QtGui import QWidget
34 
35 
37  def __init__(self, context):
38  super(DRCEnvironmentViewer, self).__init__(context)
39  self.setObjectName("DRCEnvironmentViewer")
41  context.add_widget(self._widget)
42 
43 
45  _SMILEY = ":)"
46  _FROWN = ":("
47  _OK_COLOR = QColor("#18FFFF")
48  _DISABLED_COLOR = QColor("#BDBDBD")
49  _BLACKOUT_COLOR = QColor("#F44336")
50 
51  def __init__(self):
52  self.lock = Lock()
53  super(DRCEnvironmentViewerWidget, self).__init__()
54  self.is_disabled = False
55  self.is_blackout = False
56  self.next_whiteout_time = rospy.Time.now()
57  self.blackout_time = rospy.Time.now()
58  self.event = None
59  self.sub_is_disabled = rospy.Subscriber(
60  "/drc_2015_environment/is_disabled",
61  Bool, self.isDisabledCallback)
62  self.sub_is_blackout = rospy.Subscriber(
63  "/drc_2015_environment/is_blackout",
64  Bool, self.isBlackoutCallback)
65  self.sub_next_whiteout_time = rospy.Subscriber(
66  "/drc_2015_environment/next_whiteout_time",
67  Time, self.nextWhiteoutTimeCallback)
68  self._update_plot_timer = QTimer(self)
69  self._update_plot_timer.timeout.connect(self.redraw)
70  self._update_plot_timer.start(1000 / 15)
71 
72  def isDisabledCallback(self, msg):
73  with self.lock:
74  self.is_disabled = msg.data
75 
76  def isBlackoutCallback(self, msg):
77  with self.lock:
78  if not self.is_blackout and msg.data:
79  self.blackout_time = rospy.Time.now()
80  self.is_blackout = msg.data
81 
82  def nextWhiteoutTimeCallback(self, msg):
83  with self.lock:
84  self.next_whiteout_time = msg.data
85 
86  def redraw(self):
87  self.update()
88  # if self.event:
89  # self.paintEvent(self.event)
90 
91  def paintEvent(self, event):
92  with self.lock:
93  self.event = event
94  rect = event.rect()
95  qp = QPainter()
96  qp.begin(self)
97  radius = min(rect.width(), rect.height()) - 50
98  qp.setFont(QFont('Helvetica', 100))
99  qp.setPen(QPen(QBrush(QColor(255, 255, 255)), 20))
100 
101  if self.is_disabled:
102  qp.fillRect(rect, self._DISABLED_COLOR)
103  qp.drawText(rect, QtCore.Qt.AlignCenter, self._FROWN)
104  elif self.is_blackout:
105  qp.fillRect(rect, self._BLACKOUT_COLOR)
106  qp.drawText(rect, QtCore.Qt.AlignCenter, self._FROWN)
107  time_diff = (
108  self.next_whiteout_time - rospy.Time.now()).to_sec()
109  if time_diff < 0:
110  time_diff = 0
111  time_ratio = time_diff / (
112  self.next_whiteout_time - self.blackout_time).to_sec()
113  qp.setFont(QFont('Helvetica', 30))
114  qp.drawText(
115  0, rect.height() - 150, rect.width(), 150,
116  QtCore.Qt.AlignCenter, "%.1f sec" % time_diff)
117  # 0-360
118  if time_ratio > 0:
119  rad = int(math.fmod(time_ratio * 360 + 90*16, 360) * 16)
120  qp.drawArc(
121  (rect.width() - radius) / 2,
122  (rect.height() - radius) / 2, radius, radius, 90*16,
123  rad)
124  else:
125  qp.fillRect(rect, self._OK_COLOR)
126  qp.drawText(rect, QtCore.Qt.AlignCenter, self._SMILEY)
127  qp.end()


jsk_rqt_plugins
Author(s):
autogenerated on Sat Mar 20 2021 03:03:13