00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import rospy
00034 import tf
00035 from tf.transformations import quaternion_from_euler
00036
00037 import numpy
00038 import random
00039 from math import sqrt, atan, pi, degrees
00040
00041 import nav_msgs.msg as nav_msgs
00042
00043 from python_qt_binding.QtCore import Signal, Slot, pyqtSlot, Qt, SIGNAL
00044 from python_qt_binding.QtGui import QPixmap, QImage, QGraphicsView, QGraphicsScene, qRgb
00045
00046 from rqt_py_common.topic_helpers import get_field_type
00047
00048
00049 class QMapView(QGraphicsView):
00050 map_changed = Signal()
00051
00052 def __init__(self, parent=None):
00053 super(QMapView, self).__init__()
00054 self._parent = parent
00055 self._scene = QGraphicsScene()
00056
00057 self.map_changed.connect(self._update_map)
00058 self.destroyed.connect(self.close)
00059
00060
00061 self.setDragMode(QGraphicsView.ScrollHandDrag)
00062
00063 self._map = None
00064 self.w = 0
00065 self.h = 0
00066 self._map_item = {}
00067
00068 self._polygons = {}
00069 self._colors = [(238, 34, 116), (68, 134, 252), (236, 228, 46), (102, 224, 18), (242, 156, 6), (240, 64, 10), (196, 30, 250)]
00070
00071 self.setScene(self._scene)
00072
00073 def add_dragdrop(self, item):
00074
00075 def c(x, e):
00076 self.dragEnterEvent(e)
00077
00078 def d(x, e):
00079 self.dropEvent(e)
00080 item.setAcceptDrops(True)
00081 item.dragEnterEvent = c
00082 item.dropEvent = d
00083
00084 def dragEnterEvent(self, e):
00085 if self._parent:
00086 self._parent.dragEnterEvent(e)
00087
00088 def dropEvent(self, e):
00089 if self._parent:
00090 self._parent.dropEvent(e)
00091
00092 def wheelEvent(self, event):
00093 event.ignore()
00094 if event.delta() > 0:
00095 self.scale(1.15, 1.15)
00096 else:
00097 self.scale(0.85, 0.85)
00098
00099 @pyqtSlot(nav_msgs.OccupancyGrid, name='map_received')
00100 def map_cb(self, msg):
00101 self.resolution = msg.info.resolution
00102 self.w = msg.info.width
00103 self.h = msg.info.height
00104
00105 a = numpy.array(msg.data, dtype=numpy.uint8, copy=False, order='C')
00106 a = a.reshape((self.h, self.w))
00107 if self.w % 4:
00108 e = numpy.empty((self.h, 4 - self.w % 4), dtype=a.dtype, order='C')
00109 a = numpy.append(a, e, axis=1)
00110 image = QImage(a.reshape((a.shape[0] * a.shape[1])), self.w, self.h, QImage.Format_Indexed8)
00111
00112 for i in reversed(range(101)):
00113 image.setColor(100 - i, qRgb(i * 2.55, i * 2.55, i * 2.55))
00114 image.setColor(101, qRgb(255, 0, 0))
00115 image.setColor(255, qRgb(200, 200, 200))
00116 self._map = image
00117 self.setSceneRect(0, 0, self.w, self.h)
00118 self.map_changed.emit()
00119
00120 def _update_map(self):
00121 if self._map_item:
00122 self._scene.removeItem(self._map_item)
00123
00124 pixmap = QPixmap.fromImage(self._map)
00125 self._map_item = self._scene.addPixmap(pixmap)
00126
00127
00128 self._mirror(self._map_item)
00129
00130
00131 self.add_dragdrop(self._map_item)
00132
00133 self.centerOn(self._map_item)
00134 self.show()
00135
00136 def _mirror(self, item):
00137 item.scale(-1, 1)
00138 item.translate(-self.w, 0)
00139
00140 def save_settings(self, plugin_settings, instance_settings):
00141
00142 pass
00143
00144 def restore_settings(self, plugin_settings, instance_settings):
00145
00146 pass