def paintEvent(self, event): with self.lock: self.event = event rect = event.rect() qp = QPainter() qp.begin(self) radius = min(rect.width(), rect.height()) - 50 qp.setFont(QFont('Helvetica', 100)) qp.setPen(QPen(QBrush(QColor(255, 255, 255)), 20)) if self.is_disabled: qp.fillRect(rect, self._DISABLED_COLOR) qp.drawText(rect, QtCore.Qt.AlignCenter, self._FROWN) elif self.is_blackout: qp.fillRect(rect, self._BLACKOUT_COLOR) qp.drawText(rect, QtCore.Qt.AlignCenter, self._FROWN) time_diff = (self.next_whiteout_time - rospy.Time.now()).to_sec() if time_diff < 0: time_diff = 0 time_ratio = time_diff / (self.next_whiteout_time - self.blackout_time).to_sec() qp.setFont(QFont('Helvetica', 30)) qp.drawText(0, rect.height() - 150, rect.width(), 150, QtCore.Qt.AlignCenter, "%.1f sec" % time_diff) # 0-360 if time_ratio > 0: rad = int(math.fmod(time_ratio * 360 + 90*16, 360) * 16) qp.drawArc((rect.width() - radius) / 2, (rect.height() - radius) / 2, radius, radius, 90*16, rad) else: qp.fillRect(rect, self._OK_COLOR) qp.drawText(rect, QtCore.Qt.AlignCenter, self._SMILEY) qp.end()
def paintEvent(self, event): geom = super(AxisWidget, self).geometry() h = geom.height() w = geom.width() box = QRect(2, 2, w - 4, h - 4) horiz = QLine(2, h / 2, w - 2, h / 2) vert = QLine(w / 2, 2, w / 2, h - 2) targ = QPoint(self._x * (w - 4) / 2 + w / 2, self._y * (h - 4) / 2 + h / 2) plt = super(AxisWidget, self).palette() linebrsh = plt.dark() targetbrsh = plt.highlight() linepen = QPen(linebrsh, 1, Qt.SolidLine, Qt.SquareCap) targetpen = QPen(targetbrsh, 2, Qt.SolidLine, Qt.SquareCap) qp = QPainter() qp.begin(self) qp.setPen(linepen) qp.drawRect(box) qp.drawLine(horiz) qp.drawLine(vert) qp.setPen(targetpen) qp.drawEllipse(targ, 10, 10) qp.end()
def paintEvent(self,event): super(JoystickPointView,self).paintEvent(event) try: if self._initialized: pass except: self._origPos = self.pos() self._initialized = True qp = QPainter() qp.begin(self) borderWidth = 2 radius = self.height()/2 center = QtCore.QPoint(self.height()/2,self.width()/2) # Outer Circle qp.setRenderHint(QPainter.Antialiasing, True) qp.setPen(QPen(QtCore.Qt.darkGray, borderWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,QtCore.Qt.RoundJoin)) qp.setBrush(QBrush(QtCore.Qt.white, QtCore.Qt.SolidPattern)) qp.drawEllipse(center,radius-borderWidth,radius-borderWidth) # Inner Circle qp.setPen(QPen(QtCore.Qt.lightGray, borderWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,QtCore.Qt.RoundJoin)) qp.setBrush(QBrush(QtCore.Qt.white, QtCore.Qt.SolidPattern)) qp.drawEllipse(center,radius-borderWidth-1,radius-borderWidth-1) qp.end()
def paintEvent(self, event): painter = QPainter(self) #painter.setPen(QtCore.Qt.black) #painter.translate(self.x(), self.y()) painter.rotate(self.angle) painter.drawPixmap(0, 0, self.width(), self.height(), self._pixmap) painter.end()
def paintEvent(self,event): if not self._initialized: self.placeStickAtCenter() self._initialized = True borderWidth = 1 joyRange = 80 center = QtCore.QPoint(self.height()/2,self.width()/2) qp = QPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing, True) qp.setPen(QPen(QtCore.Qt.lightGray, borderWidth, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,QtCore.Qt.RoundJoin)) if self._mode == "circle": qp.drawEllipse(center,joyRange,joyRange) if self._mode == "square": x = center.x() - joyRange y = center.y() - joyRange width = joyRange * 2 height = joyRange * 2 qp.drawRect(x,y,width,height) qp.end() super(JoystickView,self).paintEvent(event)
def image_paint_event(self, data): self.image_paint_event_original(data) if self._cv_image is None: return; img = self._cv_image painter = QPainter(self.imageFrame) painter.drawImage(data.rect(), img) painter.end()
def _save_image(self): file_name, _ = QFileDialog.getSaveFileName(self._widget, self.tr('Save as image'), 'rosgraph.png', self.tr('Image (*.bmp *.jpg *.png *.tiff)')) if file_name is None or file_name == '': return img = QImage((self._scene.sceneRect().size() * 2.0).toSize(), QImage.Format_ARGB32_Premultiplied) painter = QPainter(img) painter.setRenderHint(QPainter.Antialiasing) self._scene.render(painter) painter.end() img.save(file_name)
def _save_image(self): file_name, _ = QFileDialog.getSaveFileName(self._widget, self.tr('Save as image'), 'rospackgraph.png', self.tr('Image (*.bmp *.jpg *.png *.tiff)')) if file_name is None or file_name == '': return img = QImage((self._scene.sceneRect().size() * 2.0).toSize(), QImage.Format_ARGB32_Premultiplied) painter = QPainter(img) painter.setRenderHint(QPainter.Antialiasing) self._scene.render(painter) painter.end() img.save(file_name)
def paintEvent(self, event): contents_y = self.edit.verticalScrollBar().value() page_bottom = contents_y + self.edit.viewport().height() font_metrics = self.fontMetrics() current_block = self.edit.document().findBlock( self.edit.textCursor().position()) painter = QPainter(self) painter.setPen(Qt.darkGray) line_count = 0 first_pos = -1 posdiff = 0 # Iterate over all text blocks in the document. block = self.edit.document().begin() while block.isValid(): line_count += 1 # the top left position of the block in the document if posdiff == 0: position_y = self.edit.document().documentLayout( ).blockBoundingRect(block).topLeft().y() if first_pos == -1: first_pos = position_y else: posdiff = position_y - first_pos else: position_y += posdiff # check if the position of the block is out side of visible area if position_y > page_bottom: break # we want the line number for the selected line to be bold. bold = False if block == current_block: bold = True font = painter.font() font.setBold(True) painter.setFont(font) painter.setPen(Qt.black) # Draw the line number right justified at the y position of the # line. 3 is the magic padding number. drawText(x, y, text) painter.drawText( self.width() - font_metrics.width(str(line_count)) - 3, position_y - contents_y + font_metrics.ascent(), str(line_count)) if bold: font = painter.font() font.setBold(False) painter.setFont(font) painter.setPen(Qt.darkGray) block = block.next() self.highest_line = line_count painter.end() QWidget.paintEvent(self, event)
def mouseMoveEvent(self, event): ''' Determine if the current movement is a drag. If it is, convert it into a QDrag. If the drag ends inside the tab bar, emit an move_tab_signal. If the drag ends outside the tab bar, emit an detach_tab_signal. ''' # Determine if the current movement is detected as a drag if not self.drag_start_pos.isNull() and ((event.pos() - self.drag_start_pos).manhattanLength() > QApplication.startDragDistance()): self.drag_initiated = True # If the current movement is a drag initiated by the left button if ((event.buttons() & Qt.LeftButton)) and self.drag_initiated: # Stop the move event finishMoveEvent = QMouseEvent(QEvent.MouseMove, event.pos(), Qt.NoButton, Qt.NoButton, Qt.NoModifier) QTabBar.mouseMoveEvent(self, finishMoveEvent) # Convert the move event into a drag drag = QDrag(self) mime_data = QMimeData() mime_data.setData('action', b'application/tab-detach') drag.setMimeData(mime_data) # Create the appearance of dragging the tab content # tab_index = self.tabAt(self.drag_start_pos) pixmap = self.parentWidget().grab() targetPixmap = QPixmap(pixmap.size()) targetPixmap.fill(Qt.transparent) painter = QPainter(targetPixmap) painter.setOpacity(0.85) painter.drawPixmap(0, 0, pixmap) painter.end() drag.setPixmap(targetPixmap) # Initiate the drag dropAction = drag.exec_(Qt.MoveAction | Qt.CopyAction) # If the drag completed outside of the tab bar, detach the tab and move # the content to the current cursor position if dropAction == Qt.IgnoreAction: event.accept() self.detach_tab_signal.emit(self.tabAt(self.drag_start_pos), self.mouse_cursor.pos(), False) elif dropAction == Qt.MoveAction: # else if the drag completed inside the tab bar, move the selected tab to the new position if not self.drag_droped_pos.isNull(): self.move_tab_signal.emit(self.tabAt(self.drag_start_pos), self.tabAt(self.drag_droped_pos)) else: # else if the drag completed inside the tab bar new TabBar, move the selected tab to the new TabBar self.detach_tab_signal.emit(self.tabAt(self.drag_start_pos), self.mouse_cursor.pos(), False) event.accept() else: QTabBar.mouseMoveEvent(self, event)
def save_svg_to_file(self): file_name, _ = QFileDialog.getSaveFileName(self._widget, self.tr("Save as SVG"), "stackmachine.svg", self.tr("Scalable Vector Graphic (*.svg)")) if file_name is not None and file_name != "": generator = QSvgGenerator() generator.setFileName(file_name) generator.setSize((self._scene.sceneRect().size() * 2.0).toSize()) painter = QPainter(generator) painter.setRenderHint(QPainter.Antialiasing) self._scene.render(painter) painter.end()
def _save_svg(self): file_name, _ = QFileDialog.getSaveFileName(self._widget, self.tr('Save as SVG'), 'rosgraph.svg', self.tr('Scalable Vector Graphic (*.svg)')) if file_name is None or file_name == '': return generator = QSvgGenerator() generator.setFileName(file_name) generator.setSize((self._scene.sceneRect().size() * 2.0).toSize()) painter = QPainter(generator) painter.setRenderHint(QPainter.Antialiasing) self._scene.render(painter) painter.end()
def paintEvent(self, event): p = QPainter() p.begin(self) if self.velocity < 0: p.setBrush(self.brushGreen) p.setPen(self.penGreen) else: p.setBrush(self.brushRed) p.setPen(self.penRed) p.drawPie(0, 0, self.width(), self.height(), self.angle, self.velocity) p.end()
def _save_svg(self): file_name, _ = QFileDialog.getSaveFileName(self._widget, self.tr('Save as SVG'), 'rospackgraph.svg', self.tr('Scalable Vector Graphic (*.svg)')) if file_name is None or file_name == '': return generator = QSvgGenerator() generator.setFileName(file_name) generator.setSize((self._scene.sceneRect().size() * 2.0).toSize()) painter = QPainter(generator) painter.setRenderHint(QPainter.Antialiasing) self._scene.render(painter) painter.end()
def _export(self): file_name, _ = QFileDialog.getSaveFileName( self, self.tr("Save as image"), "graph.png", self.tr("Image (*.bmp *.jpg *.png *.tiff)") ) if file_name is None or file_name == "": return img = QImage((self._scene.sceneRect().size() * 2.0).toSize(), QImage.Format_ARGB32_Premultiplied) painter = QPainter(img) painter.setRenderHint(QPainter.Antialiasing) self._scene.render(painter) painter.end() img.save(file_name)
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 updateOverlay(self, rect=None): # Redraw the overlay image from scratch using the location image and current location. self.image.overlay_image.fill(Qt.transparent) painter = QPainter(self.image.overlay_image) painter.setBackgroundMode(Qt.TransparentMode) painter.setCompositionMode(QPainter.CompositionMode_Source) for location in self.locations: if self.draw_location[location]: color = self.location_colors[location] if self.edit_properties_location == location and self.editing_properties: color = self.edit_area_selection_color lineColor = QColor(color) lineColor.setAlpha(255) brushColor = QColor(color) brushColor.setAlpha(128) painter.setPen(lineColor) painter.setBrush(brushColor) painter.drawPolygon(self.locations[location]) if (self.current_selection is not None) or (self.new_selection is not None): lineColor = QColor(self.edit_area_selection_color) lineColor.setAlpha(255) brushColor = QColor(self.edit_area_selection_color) brushColor.setAlpha(128) painter.setPen(lineColor) painter.setBrush(brushColor) if self.new_selection is not None: # Create a temporary polygon as the new selection is being drawn. if self.current_selection is not None: current_selection = QPolygon(self.current_selection) if self.subtract_new_selection: current_selection = current_selection.subtracted( self.new_selection) else: current_selection = current_selection.united( self.new_selection) painter.drawPolygon(current_selection) elif self.subtract_new_selection == False: painter.drawPolygon(self.new_selection) else: painter.drawPolygon(self.current_selection) painter.end() if rect is None: self.image.update() else: self.image.update(rect)
def updateOverlay(self, rect = None): # Redraw the overlay image from scratch using the location image and current location. self.image.overlay_image.fill(Qt.transparent) painter = QPainter(self.image.overlay_image) painter.setBackgroundMode(Qt.TransparentMode) painter.setCompositionMode(QPainter.CompositionMode_Source) for location in self.locations: if self.draw_location[location]: color = self.location_colors[location] if self.edit_properties_location == location and self.editing_properties: color = self.edit_area_selection_color lineColor = QColor(color) lineColor.setAlpha(255) brushColor = QColor(color) brushColor.setAlpha(128) painter.setPen(lineColor) painter.setBrush(brushColor) painter.drawPolygon(self.locations[location]) if (self.current_selection is not None) or (self.new_selection is not None): lineColor = QColor(self.edit_area_selection_color) lineColor.setAlpha(255) brushColor = QColor(self.edit_area_selection_color) brushColor.setAlpha(128) painter.setPen(lineColor) painter.setBrush(brushColor) if self.new_selection is not None: # Create a temporary polygon as the new selection is being drawn. if self.current_selection is not None: current_selection = QPolygon(self.current_selection) if self.subtract_new_selection: current_selection = current_selection.subtracted(self.new_selection) else: current_selection = current_selection.united(self.new_selection) painter.drawPolygon(current_selection) elif self.subtract_new_selection == False: painter.drawPolygon(self.new_selection) else: painter.drawPolygon(self.current_selection) painter.end() if rect is None: self.image.update() else: self.image.update(rect)
def paintEvent(self,event=None): #global mouse_is_pressed w_height = self.frameGeometry().height() w_width = self.frameGeometry().width() if w_height > w_width: diam = w_width else: diam = w_height radius = int(diam / 2) - 10 start_x = w_width/2 - radius start_y = w_height/2 - radius line1_x1 = w_width/2 + radius*0.707 line1_x2 = w_width/2 - radius*0.707 line1_y1 = w_height/2 - radius*0.707 line1_y2 = w_height/2 + radius*0.707 line2_x1 = w_width/2 - radius*0.707 line2_x2 = w_width/2 + radius*0.707 line2_y1 = line1_y1 line2_y2 = line1_y2 painter=QPainter() painter.begin(self) painter.setPen(QPen(Qt.black)) painter.drawLine(line1_x1, line1_y1, line1_x2, line1_y2) #cross painter.drawLine(line2_x1, line2_y1, line2_x2, line2_y2) painter.drawEllipse(start_x,start_y , (radius*2), (radius*2)) if self.mouse_is_pressed == True: painter.setBrush(Qt.blue) rad = self.get_radius(w_width, w_height, self.cursor_pos) pos = self.get_pos(w_width, w_height, self.cursor_pos) if (radius < rad): rad = radius self.draw_arc(painter, w_width, w_height, pos, rad) self.cur_nradius = rad*1.0/radius self.cur_sector = pos else: self.cur_nradius = 0.0 self.cur_sector = 0.0 painter.end() #print self.mouse_is_pressed pass
def paintEvent(self, event): with self.lock: if self.status == 1: color = self._SUCCESS_COLOR elif self.status == 2: color = self._WARN_COLOR else: color = self._UNKNOWN_COLOR rect = event.rect() qp = QPainter() qp.begin(self) radius = min(rect.width(), rect.height()) - 100 qp.setFont(QFont('Helvetica', 100)) qp.setPen(QPen(QBrush(color), 50)) qp.setBrush(color) qp.drawEllipse((rect.width() - radius) / 2, (rect.height() - radius) / 2, radius, radius) qp.end() return
def paintEvent(self, event): contents_y = self.edit.verticalScrollBar().value() page_bottom = contents_y + self.edit.viewport().height() font_metrics = self.fontMetrics() current_block = self.edit.document().findBlock(self.edit.textCursor().position()) painter = QPainter(self) painter.setPen(Qt.darkGray) line_count = 0 # Iterate over all text blocks in the document. block = self.edit.document().begin() while block.isValid(): line_count += 1 # the top left position of the block in the document position = self.edit.document().documentLayout().blockBoundingRect(block).topLeft() # check if the position of the block is out side of visible area if position.y() > page_bottom: break # we want the line number for the selected line to be bold. bold = False if block == current_block: bold = True font = painter.font() font.setBold(True) painter.setFont(font) painter.setPen(Qt.black) # Draw the line number right justified at the y position of the # line. 3 is the magic padding number. drawText(x, y, text) painter.drawText( self.width() - font_metrics.width(str(line_count)) - 3, round(position.y()) - contents_y + font_metrics.ascent() + self.edit.document().documentMargin(), str(line_count), ) if bold: font = painter.font() font.setBold(False) painter.setFont(font) painter.setPen(Qt.darkGray) block = block.next() self.highest_line = line_count painter.end() QWidget.paintEvent(self, event)
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
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
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 paintEvent(self, event, ctx=None): if ctx is None: ctx = QPainter(self) self.face.paint(ctx) ctx.end()
def redraw(self, event): """ Draw timeline's "cells" (ie. boxes that represent seconds). Taken from robot_monitor.message_timeline.on_paint @deprecated: This way (using QPainter) didn't work as intended. """ #super(TimelineView, self).paintEvent(event) # painter = QPainter(self) ## This yields warning that says: ## "QPainter::begin: Widget painting can only begin as a result of a paintEvent" painter = QPainter() painter.begin(self.viewport()) pen = QPen( Qt.black, 2) #, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin) # Needs modified. painter.setPen(pen) is_enabled = self.isEnabled() #(width_tl, height_tl) = self.size() qsize = self.size() width_tl = qsize.width() height_tl = qsize.height() length_tl = self._max_num_seconds + 1 - self._min_num_seconds rospy.logdebug('paintEvent is_enabled=%s length_tl=%d', is_enabled, length_tl) value_size = width_tl / float(length_tl) for i in xrange(0, length_tl): brush = None color_index = i + self._min_num_seconds if (is_enabled): qcolor = self._color_callback(color_index) else: qcolor = QColor('grey') end_color = QColor(0.5 * QColor('red').value(), 0.5 * QColor('green').value(), 0.5 * QColor('blue').value()) #painter.setBrush(qcolor) painter.setBrush(QBrush(qcolor)) start = i * value_size end = (i + 1) * value_size rect = QRect(start, 0, end, height_tl) # dc.GradientFillLinear(wx.Rect(start, 0, end, height_tl), # qcolor, end_color, wx.SOUTH) rospy.logdebug('paintEvent i=%d start=%s end=%s height_tl=%s', i, start, end, height_tl) #painter.fillRect(QRect(start, end, 100, height_tl), qcolor) painter.fillRect(rect, qcolor) if (i > 0): # dc.SetPen(wx.BLACK_PEN) pen.setColor(Qt.black) # dc.DrawLine(start, 0, start, height_tl) painter.drawLine(start, 0, start, height_tl) size_marker = QSize(20, 20) marker_x = ( (self._xpos_marker - 1) * value_size + (value_size / 2.0) - (self._timeline_marker.actualSize(size_marker).width() / 2.0)) # dc.DrawBitmap(self._timeline_marker, marker_x, 0, True) qrect = QRect(marker_x, 0, size_marker.width(), size_marker.height()) self._timeline_marker.paint(painter, qrect) rospy.logdebug(' paintEvent marker_x=%s', marker_x) painter.end()