def paintEvent(self, event): super().paintEvent(event) text = self.text() if not text or text.endswith('%'): return self.ensurePolished() layout = QTextLayout('%', self.font()) painter = QPainter(self) cr = self.cursorRect() pos = painter.boundingRect(self.rect(), Qt.AlignVCenter, text).topRight() + QPoint( cr.width() // 2, cr.top() // 4) layout.beginLayout() line = layout.createLine() line.setLineWidth(self.width() - pos.x()) line.setPosition(pos) layout.endLayout() painter.setPen(QPen(Qt.gray, 1)) layout.draw(painter, QPoint(0, 0))
def paintEvent(self, event): painter = QPainter(self) painter.setFont(QFont("Arial", 18)) painter.drawText(0, 30, self.__text) # draw preedit text with diffrent color if self.driver.preeditVisible(): rect = painter.boundingRect(self.rect(), self.__text) painter.setPen(Qt.red) painter.drawText(rect.width(), 30, self.driver.preedit())
def paintCell( self, painter: QtGui.QPainter, rect: QtCore.QRect, date: QtCore.QDate ): # Paint background if self.selectedDate() == date: painter.fillRect(rect, self.Color.selected_background) # Paint date text if self.selectedDate() != date: if self.monthShown() != date.month(): painter.setPen(self.Color.unselected_month_date_text) elif qdate_is_weekend(date): painter.setPen(self.Color.unselected_weekend_date_text) else: painter.setPen(self.Color.unselected_date_text) else: painter.setPen(self.Color.selected_date_text) painter.setFont(QtGui.QFont("Helvetica", 20)) painter.drawText(rect, QtCore.Qt.AlignTop, str(date.day())) # Get todo items to paint items = [] try: items = self.todo_item_dict[date.year()][date.month()][date.day()] except KeyError: pass if len(items) != 0: # Paint number of todo items # Text color is same is date's completed_items_num = len([item for item in items if item["completed"]]) x = 40 y = 5 width = rect.width() - x painter.setFont(QtGui.QFont("Helvetica", 14)) item_number_str = f"{completed_items_num}/{len(items)} items" painter.drawText( rect.x() + x, rect.y() + y, width, rect.height(), 0, item_number_str ) # Paint todo items x = 5 y = 32 width = rect.width() - x painter.setFont(QtGui.QFont("Helvetica", 10)) for item in items: if y > rect.height(): break if item["completed"]: painter.setPen(self.Color.completed_todo_item) else: painter.setPen(self.Color.uncompleted_todo_item) height = rect.height() - y bounding_rect = painter.boundingRect( rect.x() + x, rect.y() + y, width, height, QtCore.Qt.TextWordWrap, item["name"], ) painter.drawText( rect.x() + x, rect.y() + y, width, height, QtCore.Qt.TextWordWrap, item["name"], ) y += bounding_rect.height()
def paintCell( self, painter: QtGui.QPainter, rect: QtCore.QRect, qdate: QtCore.QDate ): date = qdate_to_date(qdate) # Paint date text if self.monthShown() != qdate.month(): painter.setPen(self.Color.unselected_month_date_text) else: painter.setPen(self.Color.unselected_date_text) painter.setFont(QtGui.QFont("Helvetica", 20)) painter.drawText(rect, QtCore.Qt.AlignTop, str(qdate.day())) # Get all of the chains for the current week for date_range, chains in self.month_chains.items(): if date_range[0] <= date and date <= date_range[1]: display_chains = chains break else: display_chains = self.generate_week_info(qdate_to_date(qdate)) # Paint completed chains completed_chains_num = 0 x = 5 y = 32 width = rect.width() - x painter.setFont(QtGui.QFont("Helvetica", 10)) for chain in display_chains: if y > rect.height(): break height = rect.height() - y bounding_rect = painter.boundingRect( rect.x() + x, rect.y() + y, width, height, QtCore.Qt.TextWordWrap, chain, ) if chain_handler.get_chain(chain, qdate.year(), qdate.month(), qdate.day()): # Draw colored rect painter.fillRect( rect.x(), rect.y() + y, rect.width(), min(bounding_rect.height(), rect.height() - y), self.chain_color_dict[chain], ) # Draw text painter.setPen(self.Color.chain_name_color) completed_chains_num += 1 painter.drawText( rect.x() + x, rect.y() + y, width, height, QtCore.Qt.TextWordWrap, chain, ) y += bounding_rect.height() # Paint number of completed todo items if completed_chains_num > 0: if self.monthShown() != qdate.month(): painter.setPen(self.Color.unselected_month_date_text) else: painter.setPen(self.Color.unselected_date_text) x = 40 y = 5 width = rect.width() - x painter.setFont(QtGui.QFont("Helvetica", 14)) item_number_str = f"{completed_chains_num}" painter.drawText( rect.x() + x, rect.y() + y, width, rect.height(), 0, item_number_str ) # Surround current date in a blue border if datetime.date.today() == date: painter.setPen(self.Color.selected_background) painter.drawRect(rect.x(), rect.y(), rect.width(), rect.height())
def drawDetections(processed_image: ProcessedImage, pre_loaded_image: ndarray = None, width=2048) -> QPixmap: cv_mat = None if pre_loaded_image is None: cv_mat = cv2.imdecode( fromfile(processed_image.filePath(), dtype=uint8), cv2.IMREAD_UNCHANGED) #cv_mat = cv2.imread(processed_image.filePath()) else: cv_mat = pre_loaded_image pixmap = ImageConverter.CVToQPixmap(cv_mat) original_width = pixmap.width() original_height = pixmap.height() pixmap = pixmap.scaledToWidth(width) x_ratio = pixmap.width() / original_width y_ratio = pixmap.height() / original_height painter = QPainter(pixmap) painter.setFont(QFont(Loader.QSSVariable("@font"), 20)) pen = QPen() pen.setWidth(8) pen.setJoinStyle(Qt.MiterJoin) pen.setColor(QColor("red")) painter.setPen(pen) fill_brush = QBrush() fill_brush.setStyle(Qt.SolidPattern) for d in processed_image.detections(): x, y, w, h = d.boundingBox() x *= x_ratio w *= x_ratio y *= y_ratio h *= y_ratio label = "%s : %.2f" % (d.className(), d.confidence()) morphotype_color = Loader.SpongesMorphotypes()[d.classId()].color() bounding_rect = painter.boundingRect(x, y - 44, 200, 40, Qt.AlignLeft, label) pen.setColor(morphotype_color) painter.setPen(pen) painter.setBrush(Qt.NoBrush) painter.drawRect(x, y, w, h) fill_brush.setColor(morphotype_color) painter.setBrush(fill_brush) painter.drawRect(bounding_rect) pen.setColor(QColor("white")) painter.setPen(pen) painter.drawText(bounding_rect, Qt.AlignLeft, label) painter.end() return pixmap
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.HighQualityAntialiasing) # determine max width/height of all labels if self._maxLabelWidth is None or self._maxLabelHeight is None: self._maxLabelWidth = 0 self._maxLabelHeight = 0 for k in self.labels.values(): b = painter.boundingRect( QRectF(0, 0, 0, 0), int(Qt.AlignLeft) | int(Qt.AlignVCenter), str(k)) self._maxLabelWidth = max(self._maxLabelWidth, b.width()) self._maxLabelHeight = max(self._maxLabelHeight, b.height()) barRect = self.rect() if self._orientation == 'Vertical': # translate Y axis matrix = QMatrix() matrix.translate(0, self.height() - self.margin) matrix.scale(1, -1) painter.setMatrix(matrix) self.gradient.setFinalStop(0, self.height() - self.margin) barRect.setWidth(self._barThickness) barRect.setHeight(barRect.height() - 2 * self.margin) painter.fillRect(barRect, self.gradient) # restore normal coordinates painter.scale(1, -1) self.setMinimumWidth(self._barThickness + self._labelMargin + self._maxLabelWidth) elif self._orientation == 'Horizontal': self.margin = self._maxLabelWidth / 2 + self._labelMargin barRect.setHeight(self._barThickness) barRect.setLeft( self.margin ) # reduces width by margin (as opposed to shifting) # Reduce the width by another margin to pull off the right hand side barRect.setWidth(barRect.width() - self.margin) painter.fillRect(barRect, self.gradient) line_step = barRect.width() / 20 pos = barRect.left() while pos <= barRect.right(): painter.drawLine(pos, 2 * (barRect.bottom() - barRect.top()) / 3, pos, barRect.bottom()) pos += line_step self.setMinimumHeight(self._maxLabelHeight + self._barThickness + self._labelMargin) for pos, label in self.labels.items(): # Figure coordinate position. 1=height-margin for vertical, or width-margin for horizontal if self._orientation == 'Vertical': lpos = -1 * ((self.height() - (2 * self.margin)) * pos) painter.drawText(self._barThickness + self._labelMargin, (.5 * self._maxLabelHeight) + lpos, label) elif self._orientation == 'Horizontal': text_rect = painter.boundingRect(QRectF(0, 0, 0, 0), int(Qt.AlignLeft), str(label)) lpos = ((self.width() - (2 * self.margin)) * pos ) # Center position lleft = lpos - text_rect.width() / 2 painter.drawText(lleft + self.margin, self.height() - 1, label)