def paintEvent(self, event): painter = QPainter(self) # Hello world # ブラシ(塗りつぶし)の色を黒に painter.setBrush(Qt.black) # ペン(線描画)の色も黒に painter.setPen(Qt.black) # 背景を描く # self.rect()はwidgetのサイズを返すので、widget全体を埋める四角形を描画する # ペンとブラシの色が黒なので背景色は真っ黒 painter.drawRect(self.rect()) # ペン(線描画)の色を白にする painter.setPen(Qt.white) # フォントサイズを変更する font = painter.font() # つらさはできるだけ大きく表現したほうが良い # 周りが気づくように font.setPointSize(80) painter.setFont(font) # テキストを描画する x = 0 # 左端 y = self.rect().height() * 0.5 # 上下の中心 painter.drawText(x, y, "TSU☆RA☆I")
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): 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): 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): 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 paintEvent(self, event): painter = QPainter(self) # Hello world painter.setBrush(Qt.green) painter.setPen(Qt.black) painter.drawRect(self.rect()) painter.drawText(self.rect().width() * 0.5, self.rect().height() * 0.5, "Hello world!")
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 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): 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 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 paintEvent(self, event): painter = QPainter(self) #painter.begin(self) # puts the arrow in the middle painter.translate(self.width() / 2, self.height() / 2) painter.rotate(self.angleRobo + self.angleVel) line = QLineF(0, 0, self.width() / 2 - 3, 0) headSize = min(self.width() / 20, 4) points = QPolygonF() points.append(QPointF(self.width() / 2 - headSize * 2, headSize)) points.append(QPointF(self.width() / 2 - headSize * 2, -headSize)) points.append(QPointF(self.width() / 2 - 3, 0)) pen = QPen(self.color, 2) painter.setPen(pen) brush = QBrush(self.color) painter.setBrush(brush) painter.drawLine(line) painter.drawConvexPolygon(points)
def paintEvent(self, event): QSlider.paintEvent(self, event) curr_value = str(self.value()) round_value = round(float(curr_value), 4) painter = QPainter(self) painter.setPen(QPen(QtCore.Qt.white)) font_metrics = QFontMetrics(self.font()) font_width = font_metrics.boundingRect(str(round_value)).width() font_height = font_metrics.boundingRect(str(round_value)).height() rect = self.geometry() if self.orientation() == QtCore.Qt.Horizontal: horizontal_x_pos = rect.width() - font_width - 5 horizontal_y_pos = rect.height() * 0.75 painter.drawText(QtCore.QPoint(horizontal_x_pos, horizontal_y_pos), str(round_value)) self.x = self.value() else: pass painter.drawRect(rect)
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): 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 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 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()
def drawCircle(self, pnt, color, fill=True): p = QPainter(self) p.setPen(color) if (fill): p.setBrush(QBrush(color)) p.drawEllipse(pnt, 3, 3)