error_status_widget.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import vigir_footstep_planning_msgs.msg
00005 
00006 from python_qt_binding.QtCore import Qt, Signal, Slot
00007 from python_qt_binding.QtGui import QWidget, QVBoxLayout, QHBoxLayout, QTextEdit, QPushButton, QCheckBox, QSizePolicy, QColor
00008 
00009 from vigir_footstep_planning_msgs.msg import ErrorStatus
00010 from vigir_footstep_planning_lib.logging import *
00011 
00012 
00013 # widget for printing error status msgs to a text box
00014 class QErrorStatusTextBox(QTextEdit):
00015 
00016     def __init__(self):
00017         QTextEdit.__init__(self)
00018         self.setReadOnly(True)
00019 
00020     @Slot(str, QColor)
00021     def out_log(self, msg, color=Qt.black):
00022         self.setTextColor(color)
00023         self.append(msg)
00024 
00025     def set_error_status(self, error_status):
00026         self.clear()
00027         self.append_error_status(error_status)
00028 
00029     def append_error_status(self, error_status):
00030         if error_status.error != 0:
00031             self.out_log(error_status.error_msg, Qt.red)
00032 
00033         if error_status.warning != 0:
00034             self.out_log(error_status.warning_msg, QColor(255, 165, 0))
00035 
00036 
00037 # complex widget for printing error status msgs to a text box
00038 class QErrorStatusWidget(QWidget):
00039 
00040     error_status_signal = Signal(ErrorStatus)
00041 
00042     def __init__(self, parent=None, subscribe=False):
00043         QWidget.__init__(self, parent)
00044 
00045         # start widget
00046         vbox = QVBoxLayout()
00047         vbox.setMargin(0)
00048         vbox.setContentsMargins(0, 0, 0, 0)
00049 
00050         # add error status text edit
00051         self.error_status_text_box = QErrorStatusTextBox()
00052         self.error_status_text_box_layout = QHBoxLayout()
00053         self.error_status_text_box_layout.addWidget(self.error_status_text_box)
00054         vbox.addLayout(self.error_status_text_box_layout)
00055 
00056         # add panel
00057         hbox = QHBoxLayout()
00058 
00059         # clear push button
00060         self.execute_command = QPushButton("Clear")
00061         self.execute_command.clicked.connect(self.error_status_text_box.clear)
00062         hbox.addWidget(self.execute_command)
00063 
00064         hbox.addStretch()
00065 
00066         # hide window checkbox
00067         hide_window_check_box = QCheckBox("Hide")
00068         hide_window_check_box.stateChanged.connect(self.state_changed)
00069         hbox.addWidget(hide_window_check_box)
00070 
00071         # end panel
00072         vbox.addLayout(hbox)
00073 
00074         # end widget
00075         self.setLayout(vbox)
00076         #self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
00077 
00078         # subscriber
00079         if subscribe:
00080             self.error_status_sub = rospy.Subscriber("error_status", ErrorStatus, self.error_status_callback)
00081         self.subscribed = subscribe
00082 
00083         # connect signal slot internally to prevent crash by subscriber
00084         self.error_status_signal.connect(self.append_error_status)
00085 
00086     def __del__(self):
00087         if self.subscribed:
00088             self.error_status_sub.unregister()
00089 
00090     def error_status_callback(self, error_status):
00091         self.error_status_signal.emit(error_status)
00092 
00093     def get_text_box(self):
00094         return self.error_status_text_box
00095 
00096     @Slot(str, QColor)
00097     def set_text(self, msg, color):
00098         self.error_status_text_box.clear()
00099         self.append(msg, color)
00100 
00101     @Slot(str, QColor)
00102     def append(self, msg, color):
00103         self.error_status_text_box.out_log(msg, color)
00104 
00105     @Slot(ErrorStatus)
00106     def set_error_status(self, error_status):
00107         self.error_status_text_box.set_error_status(error_status)
00108 
00109     @Slot(ErrorStatus)
00110     def append_error_status(self, error_status):
00111         self.error_status_text_box.append_error_status(error_status)
00112 
00113     @Slot(int)
00114     def state_changed(self, state):
00115         if state == Qt.Unchecked:
00116             self.error_status_text_box_layout.addWidget(self.error_status_text_box)
00117             self.error_status_text_box.setVisible(True)
00118         elif state == Qt.Checked:
00119             self.error_status_text_box_layout.removeWidget(self.error_status_text_box)
00120             self.error_status_text_box.setVisible(False)


vigir_footstep_planning_lib
Author(s): Alexander Stumpf
autogenerated on Sat Jul 15 2017 02:47:56