def make_icon(self, image_list, mode=QIcon.Normal, state=QIcon.On): """ Helper function to create QIcons from lists of image files Warning: svg files interleaved with other files will not render correctly :param image_list: list of image paths to layer into an icon. :type image: list of str :param mode: The mode of the QIcon to be created. :type mode: int :param state: the state of the QIcon to be created. :type state: int """ if type(image_list) is not list: image_list = [image_list] if len(image_list) <= 0: raise TypeError('The list of images is empty.') num_svg = 0 for item in image_list: if item[-4:].lower() == '.svg': num_svg = num_svg + 1 if num_svg != len(image_list): # Legacy support for non-svg images icon_pixmap = QPixmap() icon_pixmap.load(image_list[0]) painter = QPainter(icon_pixmap) for item in image_list[1:]: painter.drawPixmap(0, 0, QPixmap(item)) icon = QIcon() icon.addPixmap(icon_pixmap, mode, state) painter.end() return icon else: # rendering SVG files into a QImage renderer = QSvgRenderer(image_list[0]) icon_image = QImage(renderer.defaultSize(), QImage.Format_ARGB32) icon_image.fill(0) painter = QPainter(icon_image) renderer.render(painter) if len(image_list) > 1: for item in image_list[1:]: renderer.load(item) renderer.render(painter) painter.end() # Convert QImage into a pixmap to create the icon icon_pixmap = QPixmap() icon_pixmap.convertFromImage(icon_image) icon = QIcon(icon_pixmap) return icon
def writeLocationsToFile(self): out_dict = {} out_dict["locations"] = self.locations.keys() out_dict["locations"] = sorted(out_dict["locations"]) out_dict["polygons"] = [] for index, key in enumerate(sorted(self.locations)): out_dict["polygons"].append([]) polygon = self.locations[key] for i in range(polygon.size()): pt = polygon.point(i) scaled_pt = scalePoint(pt, self.image_size, self.map_size) out_dict["polygons"][index].append(scaled_pt.x()) out_dict["polygons"][index].append(scaled_pt.y()) data_directory = os.path.dirname(os.path.realpath(self.location_file)) image_file = getLocationsImageFileLocationFromDataDirectory( data_directory) # Create an image with the location data, so that C++ programs don't need to rely on determining regions using polygons. out_dict["data"] = 'locations.pgm' location_image = QImage(self.map_size, QImage.Format_RGB32) location_image.fill(Qt.white) painter = QPainter(location_image) for index, key in enumerate(self.locations): if index > 254: rospy.logerr( "You have more than 254 locations, which is unsupported by the bwi_planning_common C++ code!" ) painter.setPen(Qt.NoPen) painter.setBrush(QColor(index, index, index)) scaled_polygon = scalePolygon(self.locations[key], self.image_size, self.map_size) painter.drawPolygon(scaled_polygon) painter.end() location_image.save(image_file) stream = open(self.location_file, 'w') yaml.dump(out_dict, stream) stream.close() self.is_modified = False
class MapImage(QLabel): def __init__(self, map_image_location, parent=None): super(MapImage, self).__init__(parent) # Image self.setFixedHeight(700) self.setFixedWidth(1300) self.setObjectName("map_image") # Set defaults to handle mouse events, as if these are not setup and a user clicks on the image, then all future # mouse events are ignored. self.enableDefaultMouseHooks() map_image = QImage(map_image_location) self.map_image = map_image.scaled(1300, 700, Qt.KeepAspectRatio) # Create a pixmap for the overlay. This will be modified by functions to change what is being displayed # on the screen. self.overlay_image = QImage(self.map_image.size(), QImage.Format_ARGB32_Premultiplied) self.overlay_image.fill(Qt.transparent) self.update() def defaultMouseHandler(self, event): # Do nothing. pass def enableDefaultMouseHooks(self): self.mousePressEvent = self.defaultMouseHandler self.mouseMoveEvent = self.defaultMouseHandler self.mouseReleaseEvent = self.defaultMouseHandler def paintEvent(self, event): painter = QPainter(self) painter.drawImage(event.rect(), self.map_image, event.rect()) painter.drawImage(event.rect(), self.overlay_image, event.rect()) painter.end()
def writeLocationsToFile(self): out_dict = {} out_dict["locations"] = self.locations.keys() out_dict["polygons"] = [] for index, location in enumerate(self.locations): out_dict["polygons"].append([]) for i in range(self.locations[location].size()): pt = self.locations[location].point(i) scaled_pt = scalePoint(pt, self.image_size, self.map_size) out_dict["polygons"][index].append(scaled_pt.x()) out_dict["polygons"][index].append(scaled_pt.y()) data_directory = os.path.dirname(os.path.realpath(self.location_file)) image_file = getLocationsImageFileLocationFromDataDirectory(data_directory) # Create an image with the location data, so that C++ programs don't need to rely on determining regions using polygons. out_dict["data"] = 'locations.pgm' location_image = QImage(self.map_size, QImage.Format_RGB32) location_image.fill(Qt.white) painter = QPainter(location_image) for index, location in enumerate(self.locations): if index > 254: rospy.logerr("You have more than 254 locations, which is unsupported by the bwi_planning_common C++ code!") painter.setPen(Qt.NoPen) painter.setBrush(QColor(index, index, index)) scaled_polygon = scalePolygon(self.locations[location], self.image_size, self.map_size) painter.drawPolygon(scaled_polygon) painter.end() location_image.save(image_file) stream = open(self.location_file, 'w') yaml.dump(out_dict, stream) stream.close() self.is_modified = False