def paint(self, painter, option, widget=None): # title path_title = QPainterPath() path_title.setFillRule(Qt.WindingFill) path_title.addRoundedRect(0, 0, self.width, self.title_height, self.edge_size, self.edge_size) path_title.addRect(0, self.title_height - self.edge_size, self.edge_size, self.edge_size) path_title.addRect(self.width - self.edge_size, self.title_height - self.edge_size, self.edge_size, self.edge_size) painter.setPen(Qt.NoPen) painter.setBrush(self._title_brush) painter.drawPath(path_title.simplified()) # content path_content = QPainterPath() path_content.setFillRule(Qt.WindingFill) path_content.addRoundedRect(0, self.title_height, self.width, self.height - self.title_height, self.edge_size, self.edge_size) path_content.addRect(0, self.title_height, self.edge_size, self.edge_size) path_content.addRect(self.width - self.edge_size, self.title_height, self.edge_size, self.edge_size) painter.setPen(Qt.NoPen) painter.setBrush(self._background_brush) painter.drawPath(path_content.simplified()) # outline path_outline = QPainterPath() path_outline.addRoundedRect(0, 0, self.width, self.height, self.edge_size, self.edge_size) painter.setPen(self._pen_default if not self.isSelected() else self._pen_selected) painter.setBrush(Qt.NoBrush) painter.drawPath(path_outline.simplified())
def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: Optional[QWidget] = ...) -> None: # content rect = self.rect() path_content = QPainterPath() path_content.setFillRule(Qt.WindingFill) path_content.addRoundedRect(rect, self.edge_roundness, self.edge_roundness) painter.setPen(Qt.NoPen) painter.setBrush(self._brush_background) painter.drawPath(path_content.simplified()) # outline path_outline = QPainterPath() path_outline.addRoundedRect(rect, self.edge_roundness, self.edge_roundness) painter.setPen( self._pen_default if not self.isSelected() else self._pen_selected) painter.setBrush(Qt.NoBrush) painter.drawPath(path_outline.simplified()) for handle in self.handles.values(): path_handle = QPainterPath() path_handle.addRect(handle) painter.drawPath(path_handle)
def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: Optional[QWidget] = ...) -> None: x = y = 0 path_title = QPainterPath() path_title.setFillRule(Qt.WindingFill) # Rectangle for title path_title.addRoundedRect(x, y, self.width, self.title_height, self.edge_roundness, self.edge_roundness) path_title.addRect(x, y + self.title_height - self.edge_roundness, self.edge_roundness, self.edge_roundness) path_title.addRect(x + self.width - self.edge_roundness, y + self.title_height - self.edge_roundness, self.edge_roundness, self.edge_roundness) painter.setPen(Qt.NoPen) painter.setBrush(self._brush_title) painter.drawPath(path_title.simplified()) # content path_content = QPainterPath() path_content.setFillRule(Qt.WindingFill) path_content.addRoundedRect(x, y + self.title_height, self.width, self.height - self.title_height, self.edge_roundness, self.edge_roundness) path_content.addRect(x, y + self.title_height, self.edge_roundness, self.edge_roundness) path_content.addRect(x + self.width - self.edge_roundness, y + self.title_height, self.edge_roundness, self.edge_roundness) painter.setPen(Qt.NoPen) painter.setBrush(self._brush_background) painter.drawPath(path_content.simplified()) # outline path_outline = QPainterPath() path_outline.addRoundedRect(x, y, self.width, self.height, self.edge_roundness, self.edge_roundness) painter.setBrush(Qt.NoBrush) if self._hovered: painter.setPen(self._pen_hovered) painter.drawPath(path_outline.simplified()) painter.setPen(self._pen_default) painter.drawPath(path_outline.simplified()) else: painter.setPen(self._pen_default if not self.isSelected() else self._pen_selected) painter.drawPath(path_outline.simplified())
def compute_path(self, moves): # Cardinal directions in clockwise order, starting at up dirs = {(0, -1): 0, (1, 0): 1, (0, 1): 2, (-1, 0): 3} # temp: memorize used x- and y- values xlist, ylist = [self._start[0], ], [self._start[1], ] # Initialize path with start segment x, y = self._start[0], self._start[1] shape = QPainterPath() shape.addRect(x,y,1,1) path = [((x, y), (0, (dirs[moves[0][1]] + 2) % 4)), ] x, y = x + moves[0][1][0], y + moves[0][1][1] # Append other segments for index in range(1, len(moves)): xlist.append(x) # temp ylist.append(y) # temp shape.addRect(x,y,1,1) path.append( ((x, y), (self._get_tile(moves[index-1][1], moves[index][1], dirs)))) x, y = x + moves[index][1][0], y + moves[index][1][1] # End with end segment xlist.append(x) # temp ylist.append(y) # temp shape.addRect(x,y,1,1) path.append(((x, y), (3, dirs[moves[-1][1]]))) self._path = tuple(path) self._shape = shape.simplified() self._brect = QRectF(QPointF(min(xlist), min(ylist)), QPointF(max(xlist)+1.0, max(ylist)+1.0))
def drawRect(self): ''' Piirtää esineen ja lisää sen sceneen. Palauttaa esineen Load ja Undo luokkia varten. ''' path = QPainterPath() rect = self.setRect() path.addRect(rect) path.simplified() #self.rectitem = QGraphicsPathItem(path) self.rectitem = PathItem(path, self.undoStack) self.rectitem.setPen(QPen(self.color, 2)) self.rectitem.setFlag(QGraphicsItem.ItemIsSelectable) self.rectitem.setFlag(QGraphicsItem.ItemIsMovable) self.scene.addItem(self.rectitem) return self.rectitem '''
def paint(self, painter, option, widget=None): # content path_content = QPainterPath() path_content.setFillRule(Qt.WindingFill) path_content.addEllipse(0, 0, self.width, self.height) painter.setPen(Qt.NoPen) brush = self._background_brush if self.has_input( ) or not self.node.is_linked() else self._disconnected_brush painter.setBrush(brush) painter.drawPath(path_content.simplified()) # outline path_outline = QPainterPath() path_outline.addEllipse(0, 0, self.width, self.height) painter.setPen( self._pen_default if not self.isSelected() else self._pen_selected) painter.setBrush(Qt.NoBrush) painter.drawPath(path_outline.simplified())
def paint(self, painter, option, widget=None): path = QPainterPath() path.addRoundedRect(self.m_rect, 5, 5) anchor = self.mapFromParent(self.m_chart.mapToPosition(self.m_anchor)) if not self.m_rect.contains(anchor): point1 = QPointF() point2 = QPointF() # establish the position of the anchor point in relation to m_rect above = anchor.y() <= self.m_rect.top() aboveCenter = (anchor.y() > self.m_rect.top() and anchor.y() <= self.m_rect.center().y()) belowCenter = (anchor.y() > self.m_rect.center().y() and anchor.y() <= self.m_rect.bottom()) below = anchor.y() > self.m_rect.bottom() onLeft = anchor.x() <= self.m_rect.left() leftOfCenter = (anchor.x() > self.m_rect.left() and anchor.x() <= self.m_rect.center().x()) rightOfCenter = (anchor.x() > self.m_rect.center().x() and anchor.x() <= self.m_rect.right()) onRight = anchor.x() > self.m_rect.right() # get the nearest m_rect corner. x = (onRight + rightOfCenter) * self.m_rect.width() y = (below + belowCenter) * self.m_rect.height() cornerCase = ((above and onLeft) or (above and onRight) or (below and onLeft) or (below and onRight)) vertical = abs(anchor.x() - x) > abs(anchor.y() - y) x1 = (x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * int(not vertical) * (onLeft * 10 - onRight * 20)) y1 = (y + aboveCenter * 10 - belowCenter * 20 + cornerCase * int(vertical) * (above * 10 - below * 20)) point1.setX(x1) point1.setY(y1) x2 = (x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * int(not vertical) * (onLeft * 20 - onRight * 10)) y2 = (y + aboveCenter * 20 - belowCenter * 10 + cornerCase * int(vertical) * (above * 20 - below * 10)) point2.setX(x2) point2.setY(y2) path.moveTo(point1) path.lineTo(anchor) path.lineTo(point2) path = path.simplified() painter.setBrush(QColor(255, 255, 255)) painter.drawPath(path) painter.drawText(self.m_textRect, self.m_text)
def drawObject(self, painter: 'QPainter', rect: QtCore.QRectF, doc: 'QTextDocument', pos_in_document: int, format_: 'QTextFormat') -> None: painter.setRenderHint(QPainter.Antialiasing, True) c = QColor(255, 80, 0, 160) painter.setBrush(QBrush(c, Qt.SolidPattern)) painter.setPen(QPen(QtCore.Qt.white, 2, QtCore.Qt.SolidLine)) tag_kind: TagKind = format_.property(TagTextObject.kind_propid) top = rect.top() left = rect.left() width = rect.width() height = rect.height() square_size = rect.height() / 2 if tag_kind == TagKind.START: path = QPainterPath() path.setFillRule(Qt.WindingFill) path.addRoundedRect(rect, 10, 10) # QRectF(aleft: float, atop: float, awidth: float, aheight: float) bottom_left_rect = QRectF(left, top + height - square_size, square_size, square_size) path.addRoundedRect(bottom_left_rect, 2, 2) # Bottom left top_left_rect = QRectF(left, top, square_size, square_size) path.addRoundedRect(top_left_rect, 2, 2) # Top left painter.drawPath(path.simplified()) elif tag_kind == TagKind.END: path = QPainterPath() path.setFillRule(Qt.WindingFill) path.addRoundedRect(rect, 10, 10) top_right_rect = QRectF((left + width) - square_size, top, square_size, square_size) path.addRoundedRect(top_right_rect, 2, 2) # Top right bottom_right_rect = QRectF((left + width) - square_size, top + height - square_size, square_size, square_size) path.addRoundedRect(bottom_right_rect, 2, 2) # Bottom right painter.drawPath(path.simplified()) else: painter.drawRoundedRect(rect, 4, 4) tag_name = format_.property(TagTextObject.name_propid) painter.drawText(rect, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignCenter, tag_name)
def _dbgRingShape(self, p): r, R, = self.boundings() opath = QPainterPath() opath.setFillRule(Qt.WindingFill) opath.addEllipse(-R, -R, 2*R, 2*R) ipath = QPainterPath() ipath.setFillRule(Qt.WindingFill) ipath.addEllipse(-r, -r, 2*r, 2*r) p.fillPath(opath.subtracted(ipath), QColor(255, 255, 0, 50)) p.strokePath(opath.simplified(), QPen(Qt.black, 3)) p.strokePath(ipath, QPen(Qt.black, 1))
def paint(self, painter, option, widget=None): # content path_content = QPainterPath() path_content.setFillRule(Qt.WindingFill) polygon = QPolygonF() polygon.append(QPoint(self.width * 0.5, 2)) polygon.append(QPoint(self.width - 2, self.height * 0.5)) polygon.append(QPoint(self.width * 0.5, self.height - 2)) polygon.append(QPoint(2, self.height * 0.5)) polygon.append(QPoint(self.width * 0.5, 2)) path_content.addPolygon(polygon) painter.setPen(Qt.NoPen) painter.setBrush(self._background_brush) painter.drawPath(path_content.simplified()) # outline path_outline = QPainterPath() path_outline.addPolygon(polygon) painter.setPen( self._pen_default if not self.isSelected() else self._pen_selected) painter.setBrush(Qt.NoBrush) painter.drawPath(path_outline.simplified())
def paint(self, painter, QStyleOptionGraphicsItem, widget=None): """Overloaded QWidget paint method. Draws the GUIFeedbackPopup on the scene. Args: painter: QWidget painter class. QStyleOptionGraphicsItem: ??? (not used). widget: The parent widget if there is one(not used). Todo: * Find out what QStyleOptionGraphicsItem is for. """ # content path_content = QPainterPath() path_content.setFillRule(Qt.WindingFill) path_content.addRoundedRect(0, 0, self.__width, self.__height, self.__edge_roundness, self.__edge_roundness) painter.setPen(Qt.NoPen) painter.setBrush(self.__brush_background) painter.drawPath(path_content.simplified()) # outline path_outline = QPainterPath() path_outline.addRoundedRect(0, 0, self.__width, self.__height, self.__edge_roundness, self.__edge_roundness) # 9pt = 9px # px max of 188 (188 - padding[8] = 180) painter.setBrush(Qt.NoBrush) painter.setPen(self.__pen_default) painter.drawPath(path_outline.simplified()) # paint icon painter.drawPixmap(QRectF(-12.5, -12.5, 24.0, 24.0), self.__icons, QRectF(self.__icon_offset, 0, 24.0, 24.0))
def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: Optional[QWidget] = ...) -> None: """Paint the node as filled circle of radius equal to heigth / 2 Parameters ---------- painter : QPainter option : QStyleOptionGraphicsItem widget : QWidget Returns ------- None """ path_content = QPainterPath() path_content.setFillRule(Qt.WindingFill) painter.setPen(Qt.NoPen) painter.setBrush(self._brush_background) path_content.addEllipse(0, 0, 2 * self.radius, 2 * self.radius) painter.drawPath(path_content) # outline path_outline = QPainterPath() path_outline.addEllipse(0, 0, 2 * self.radius, 2 * self.radius) painter.setBrush(Qt.NoBrush) if self._hovered: painter.setPen(self._pen_hovered) painter.drawPath(path_outline.simplified()) painter.setPen(self._pen_default) painter.drawPath(path_outline.simplified()) else: painter.setPen(self._pen_default if not self.isSelected() else self._pen_selected) painter.drawPath(path_outline.simplified())
def paint(self, painter, option, widget): path = QPainterPath() x, y, w, d = self.m_rect.getRect() self.m_rect = QRectF(x, y, w, d) path.addRoundedRect(self.m_rect, 5, 5) anchor = self.mapFromParent(self.chart.mapToPosition(self.m_anchor)) if not self.m_rect.contains(QPoint(int(anchor.x()), int(anchor.y()))): point1 = QPointF() point2 = QPointF() above = anchor.y() <= self.m_rect.top() aboveCenter = anchor.y() > self.m_rect.top() and anchor.y( ) <= self.m_rect.center().y() belowCenter = anchor.y() > self.m_rect.center().y() and anchor.y( ) <= self.m_rect.bottom() below = anchor.y() > self.m_rect.bottom() onLeft = anchor.x() <= self.m_rect.left() leftOfCenter = anchor.x() > self.m_rect.left() and anchor.x( ) <= self.m_rect.center().x() rightOfCenter = anchor.x() > self.m_rect.center().x() and anchor.x( ) <= self.m_rect.right() onRight = anchor.x() > self.m_rect.right() #get the nearest m_rect corner x = (onRight + rightOfCenter) * self.m_rect.width() y = (below + belowCenter) * self.m_rect.height() cornerCase = (above and onLeft) or (above and onRight) or ( below and onLeft) or (below and onRight) vertical = abs(anchor.x() - x) > abs(anchor.y() - y) x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * ( not vertical) * (onLeft * 10 - onRight * 20) y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * ( above * 10 - below * 20) point1.setX(x1) point1.setY(y1) x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * ( not vertical) * (onLeft * 20 - onRight * 10) y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * ( above * 20 - below * 10) point2.setX(x2) point2.setY(y2) path.moveTo(point1) path.lineTo(anchor) path.lineTo(point2) path = path.simplified() painter.setBrush(QColor(255, 255, 255)) painter.drawPath(path) painter.drawText(self.m_textRect, Qt.AlignLeft, self.m_text)
def handleShapeDescription(self, shape, list, textOnly=False): if (shape.type() != "KoSvgTextShapeID" and textOnly is True): return shapeDesc = {} shapeDesc["name"] = shape.name() rect = shape.boundingBox() listOfPoints = [ rect.topLeft(), rect.topRight(), rect.bottomRight(), rect.bottomLeft() ] shapeDoc = minidom.parseString(shape.toSvg()) docElem = shapeDoc.documentElement svgRegExp = re.compile('[MLCSQHVATmlzcqshva]\d+\.?\d* \d+\.?\d*') transform = docElem.getAttribute("transform") coord = [] adjust = QTransform() # TODO: If we get global transform api, use that instead of parsing manually. if "translate" in transform: transform = transform.replace('translate(', '') for c in transform[:-1].split(" "): coord.append(float(c)) if len(coord) < 2: coord.append(coord[0]) adjust = QTransform(1, 0, 0, 1, coord[0], coord[1]) if "matrix" in transform: transform = transform.replace('matrix(', '') for c in transform[:-1].split(" "): coord.append(float(c)) adjust = QTransform(coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]) path = QPainterPath() if docElem.localName == "path": dVal = docElem.getAttribute("d") listOfSvgStrings = [" "] listOfSvgStrings = svgRegExp.findall(dVal) if listOfSvgStrings: listOfPoints = [] for l in listOfSvgStrings: line = l[1:] coordinates = line.split(" ") if len(coordinates) < 2: coordinates.append(coordinates[0]) x = float(coordinates[-2]) y = float(coordinates[-1]) offset = QPointF() if l.islower(): offset = listOfPoints[0] if l.lower().startswith("m"): path.moveTo(QPointF(x, y) + offset) elif l.lower().startswith("h"): y = listOfPoints[-1].y() path.lineTo(QPointF(x, y) + offset) elif l.lower().startswith("v"): x = listOfPoints[-1].x() path.lineTo(QPointF(x, y) + offset) elif l.lower().startswith("c"): path.cubicTo(coordinates[0], coordinates[1], coordinates[2], coordinates[3], x, y) else: path.lineTo(QPointF(x, y) + offset) path.setFillRule(Qt.WindingFill) for polygon in path.simplified().toSubpathPolygons(adjust): for point in polygon: listOfPoints.append(point) elif docElem.localName == "rect": listOfPoints = [] if (docElem.hasAttribute("x")): x = float(docElem.getAttribute("x")) else: x = 0 if (docElem.hasAttribute("y")): y = float(docElem.getAttribute("y")) else: y = 0 w = float(docElem.getAttribute("width")) h = float(docElem.getAttribute("height")) path.addRect(QRectF(x, y, w, h)) for point in path.toFillPolygon(adjust): listOfPoints.append(point) elif docElem.localName == "ellipse": listOfPoints = [] if (docElem.hasAttribute("cx")): x = float(docElem.getAttribute("cx")) else: x = 0 if (docElem.hasAttribute("cy")): y = float(docElem.getAttribute("cy")) else: y = 0 ry = float(docElem.getAttribute("ry")) rx = float(docElem.getAttribute("rx")) path.addEllipse(QPointF(x, y), rx, ry) for point in path.toFillPolygon(adjust): listOfPoints.append(point) elif docElem.localName == "text": # NOTE: This only works for horizontal preformated text. Vertical text needs a different # ordering of the rects, and wraparound should try to take the shape it is wraped in. family = "sans-serif" if docElem.hasAttribute("font-family"): family = docElem.getAttribute("font-family") size = "11" if docElem.hasAttribute("font-size"): size = docElem.getAttribute("font-size") multilineText = True for el in docElem.childNodes: if el.nodeType == minidom.Node.TEXT_NODE: multilineText = False if multilineText: listOfPoints = [] listOfRects = [] # First we collect all the possible line-rects. for el in docElem.childNodes: if docElem.hasAttribute("font-family"): family = docElem.getAttribute("font-family") if docElem.hasAttribute("font-size"): size = docElem.getAttribute("font-size") fontsize = int(size) font = QFont(family, fontsize) string = el.toxml() string = re.sub("\<.*?\>", " ", string) string = string.replace(" ", " ") width = min( QFontMetrics(font).width(string.strip()), rect.width()) height = QFontMetrics(font).height() anchor = "start" if docElem.hasAttribute("text-anchor"): anchor = docElem.getAttribute("text-anchor") top = rect.top() if len(listOfRects) > 0: top = listOfRects[-1].bottom() if anchor == "start": spanRect = QRectF(rect.left(), top, width, height) listOfRects.append(spanRect) elif anchor == "end": spanRect = QRectF(rect.right() - width, top, width, height) listOfRects.append(spanRect) else: # Middle spanRect = QRectF(rect.center().x() - (width * 0.5), top, width, height) listOfRects.append(spanRect) # Now we have all the rects, we can check each and draw a # polygon around them. heightAdjust = ( rect.height() - (listOfRects[-1].bottom() - rect.top())) / len(listOfRects) for i in range(len(listOfRects)): span = listOfRects[i] addtionalHeight = i * heightAdjust if i == 0: listOfPoints.append(span.topLeft()) listOfPoints.append(span.topRight()) else: if listOfRects[i - 1].width() < span.width(): listOfPoints.append( QPointF(span.right(), span.top() + addtionalHeight)) listOfPoints.insert( 0, QPointF(span.left(), span.top() + addtionalHeight)) else: bottom = listOfRects[i - 1].bottom( ) + addtionalHeight - heightAdjust listOfPoints.append( QPointF(listOfRects[i - 1].right(), bottom)) listOfPoints.insert( 0, QPointF(listOfRects[i - 1].left(), bottom)) listOfPoints.append(QPointF(span.right(), rect.bottom())) listOfPoints.insert(0, QPointF(span.left(), rect.bottom())) path = QPainterPath() path.moveTo(listOfPoints[0]) for p in range(1, len(listOfPoints)): path.lineTo(listOfPoints[p]) path.closeSubpath() listOfPoints = [] for point in path.toFillPolygon(adjust): listOfPoints.append(point) shapeDesc["boundingBox"] = listOfPoints if (shape.type() == "KoSvgTextShapeID" and textOnly is True): shapeDesc["text"] = shape.toSvg() list.append(shapeDesc)
def paint(self, painter, option, index): assert isinstance(painter, QPainter) if index.data(Qt.UserRole+1): if app_constants.HIGH_QUALITY_THUMBS: painter.setRenderHint(QPainter.SmoothPixmapTransform) painter.setRenderHint(QPainter.Antialiasing) gallery = index.data(Qt.UserRole+1) title = gallery.title artist = gallery.artist title_color = app_constants.GRID_VIEW_TITLE_COLOR artist_color = app_constants.GRID_VIEW_ARTIST_COLOR label_color = app_constants.GRID_VIEW_LABEL_COLOR # Enable this to see the defining box #painter.drawRect(option.rect) # define font size if 20 > len(title) > 15: title_size = "font-size:{}px;".format(self.font_size) elif 30 > len(title) > 20: title_size = "font-size:{}px;".format(self.font_size-1) elif 40 > len(title) >= 30: title_size = "font-size:{}px;".format(self.font_size-2) elif 50 > len(title) >= 40: title_size = "font-size:{}px;".format(self.font_size-3) elif len(title) >= 50: title_size = "font-size:{}px;".format(self.font_size-4) else: title_size = "font-size:{}px;".format(self.font_size) if 30 > len(artist) > 20: artist_size = "font-size:{}px;".format(self.font_size) elif 40 > len(artist) >= 30: artist_size = "font-size:{}px;".format(self.font_size-1) elif len(artist) >= 40: artist_size = "font-size:{}px;".format(self.font_size-2) else: artist_size = "font-size:{}px;".format(self.font_size) #painter.setPen(QPen(Qt.NoPen)) #option.rect = option.rect.adjusted(11, 10, 0, 0) option.rect.setWidth(self.W) option.rect.setHeight(self.H) rec = option.rect.getRect() x = rec[0] y = rec[1] w = rec[2] h = rec[3] text_area = QTextDocument() text_area.setDefaultFont(option.font) text_area.setHtml(""" <head> <style> #area {{ display:flex; width:{6}px; height:{7}px }} #title {{ position:absolute; color: {4}; font-weight:bold; {0} }} #artist {{ position:absolute; color: {5}; top:20px; right:0; {1} }} </style> </head> <body> <div id="area"> <center> <div id="title">{2} </div> <div id="artist">{3} </div> </div> </center> </body> """.format(title_size, artist_size, title, artist, title_color, artist_color, 130+app_constants.SIZE_FACTOR, 1+app_constants.SIZE_FACTOR)) text_area.setTextWidth(w) #chapter_area = QTextDocument() #chapter_area.setDefaultFont(option.font) #chapter_area.setHtml(""" #<font color="black">{}</font> #""".format("chapter")) #chapter_area.setTextWidth(w) def center_img(width): new_x = x if width < w: diff = w - width offset = diff//2 new_x += offset return new_x def img_too_big(start_x): txt_layout = misc.text_layout("Image is too big!", w, self.title_font, self.title_font_m) clipping = QRectF(x, y+h//4, w, app_constants.GRIDBOX_LBL_H - 10) txt_layout.draw(painter, QPointF(x, y+h//4), clip=clipping) # if we can't find a cached image pix_cache = QPixmapCache.find(self.key(gallery.profile)) if isinstance(pix_cache, QPixmap): self.image = pix_cache img_x = center_img(self.image.width()) if self.image.width() > w or self.image.height() > h: img_too_big(img_x) else: if self.image.height() < self.image.width(): #to keep aspect ratio painter.drawPixmap(QPoint(img_x,y), self.image) else: painter.drawPixmap(QPoint(img_x,y), self.image) else: self.image = QPixmap(gallery.profile) img_x = center_img(self.image.width()) QPixmapCache.insert(self.key(gallery.profile), self.image) if self.image.width() > w or self.image.height() > h: img_too_big(img_x) else: if self.image.height() < self.image.width(): #to keep aspect ratio painter.drawPixmap(QPoint(img_x,y), self.image) else: painter.drawPixmap(QPoint(img_x,y), self.image) # draw ribbon type painter.save() painter.setPen(Qt.NoPen) if app_constants.DISPLAY_GALLERY_RIBBON: type_ribbon_w = type_ribbon_l = w*0.11 rib_top_1 = QPointF(x+w-type_ribbon_l-type_ribbon_w, y) rib_top_2 = QPointF(x+w-type_ribbon_l, y) rib_side_1 = QPointF(x+w, y+type_ribbon_l) rib_side_2 = QPointF(x+w, y+type_ribbon_l+type_ribbon_w) ribbon_polygon = QPolygonF([rib_top_1, rib_top_2, rib_side_1, rib_side_2]) ribbon_path = QPainterPath() ribbon_path.setFillRule(Qt.WindingFill) ribbon_path.addPolygon(ribbon_polygon) ribbon_path.closeSubpath() painter.setBrush(QBrush(QColor(self._ribbon_color(gallery.type)))) painter.drawPath(ribbon_path) # draw if favourited if gallery.fav == 1: star_ribbon_w = star_ribbon_l = w*0.08 rib_top_1 = QPointF(x+star_ribbon_l, y) rib_side_1 = QPointF(x, y+star_ribbon_l) rib_top_2 = QPointF(x+star_ribbon_l+star_ribbon_w, y) rib_side_2 = QPointF(x, y+star_ribbon_l+star_ribbon_w) rib_star_mid_1 = QPointF((rib_top_1.x()+rib_side_1.x())/2, (rib_top_1.y()+rib_side_1.y())/2) rib_star_factor = star_ribbon_l/4 rib_star_p1_1 = rib_star_mid_1 + QPointF(rib_star_factor, -rib_star_factor) rib_star_p1_2 = rib_star_p1_1 + QPointF(-rib_star_factor, -rib_star_factor) rib_star_p1_3 = rib_star_mid_1 + QPointF(-rib_star_factor, rib_star_factor) rib_star_p1_4 = rib_star_p1_3 + QPointF(-rib_star_factor, -rib_star_factor) crown_1 = QPolygonF([rib_star_p1_1, rib_star_p1_2, rib_star_mid_1, rib_star_p1_4, rib_star_p1_3]) painter.setBrush(QBrush(QColor("yellow"))) painter.drawPolygon(crown_1) ribbon_polygon = QPolygonF([rib_top_1, rib_side_1, rib_side_2, rib_top_2]) ribbon_path = QPainterPath() ribbon_path.setFillRule(Qt.WindingFill) ribbon_path.addPolygon(ribbon_polygon) ribbon_path.closeSubpath() painter.drawPath(ribbon_path) #painter.setPen(QColor("#d35400")) #painter.drawPolyline(rib_top_1, rib_star_p1_1, rib_star_p1_2, rib_star_mid_1, rib_star_p1_4, rib_star_p1_3, rib_side_1) #painter.drawLine(rib_top_1, rib_top_2) #painter.drawLine(rib_top_2, rib_side_2) #painter.drawLine(rib_side_1, rib_side_2) painter.restore() if app_constants._REFRESH_EXTERNAL_VIEWER: if app_constants.USE_EXTERNAL_VIEWER: self.external_icon = self.file_icons.get_external_file_icon() else: self.external_icon = self.file_icons.get_default_file_icon() if gallery.state == self.G_DOWNLOAD: painter.save() dl_box = QRect(x, y, w, 20) painter.setBrush(QBrush(QColor(0,0,0,123))) painter.setPen(QColor('white')) painter.drawRect(dl_box) painter.drawText(dl_box, Qt.AlignCenter, 'Downloading...') painter.restore() else: if app_constants.DISPLAY_GALLERY_TYPE: self.type_icon = self.file_icons.get_file_icon(gallery.path) if self.type_icon and not self.type_icon.isNull(): self.type_icon.paint(painter, QRect(x+2, y+app_constants.THUMB_H_SIZE-16, 16, 16)) if app_constants.USE_EXTERNAL_PROG_ICO: if self.external_icon and not self.external_icon.isNull(): self.external_icon.paint(painter, QRect(x+w-30, y+app_constants.THUMB_H_SIZE-28, 28, 28)) def draw_text_label(lbl_h): #draw the label for text painter.save() painter.translate(x, y+app_constants.THUMB_H_SIZE) box_color = QBrush(QColor(label_color))#QColor(0,0,0,123)) painter.setBrush(box_color) rect = QRect(0, 0, w, lbl_h) #x, y, width, height painter.fillRect(rect, box_color) painter.restore() return rect if option.state & QStyle.State_MouseOver or\ option.state & QStyle.State_Selected: title_layout = misc.text_layout(title, w, self.title_font, self.title_font_m) artist_layout = misc.text_layout(artist, w, self.artist_font, self.artist_font_m) t_h = title_layout.boundingRect().height() a_h = artist_layout.boundingRect().height() if app_constants.GALLERY_FONT_ELIDE: lbl_rect = draw_text_label(min(t_h+a_h+3, app_constants.GRIDBOX_LBL_H)) else: lbl_rect = draw_text_label(app_constants.GRIDBOX_LBL_H) clipping = QRectF(x, y+app_constants.THUMB_H_SIZE, w, app_constants.GRIDBOX_LBL_H - 10) painter.setPen(QColor(title_color)) title_layout.draw(painter, QPointF(x, y+app_constants.THUMB_H_SIZE), clip=clipping) painter.setPen(QColor(artist_color)) artist_layout.draw(painter, QPointF(x, y+app_constants.THUMB_H_SIZE+t_h), clip=clipping) #painter.fillRect(option.rect, QColor) else: if app_constants.GALLERY_FONT_ELIDE: lbl_rect = draw_text_label(self.text_label_h) else: lbl_rect = draw_text_label(app_constants.GRIDBOX_LBL_H) # draw text painter.save() alignment = QTextOption(Qt.AlignCenter) alignment.setUseDesignMetrics(True) title_rect = QRectF(0,0,w, self.title_font_m.height()) artist_rect = QRectF(0,self.artist_font_m.height(),w, self.artist_font_m.height()) painter.translate(x, y+app_constants.THUMB_H_SIZE) if app_constants.GALLERY_FONT_ELIDE: painter.setFont(self.title_font) painter.setPen(QColor(title_color)) painter.drawText(title_rect, self.title_font_m.elidedText(title, Qt.ElideRight, w-10), alignment) painter.setPen(QColor(artist_color)) painter.setFont(self.artist_font) alignment.setWrapMode(QTextOption.NoWrap) painter.drawText(artist_rect, self.title_font_m.elidedText(artist, Qt.ElideRight, w-10), alignment) else: text_area.setDefaultFont(QFont(self.font_name)) text_area.drawContents(painter) ##painter.resetTransform() painter.restore() if option.state & QStyle.State_Selected: painter.save() selected_rect = QRectF(x, y, w, lbl_rect.height()+app_constants.THUMB_H_SIZE) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(QColor(164,164,164,120))) painter.drawRoundedRect(selected_rect, 5, 5) #painter.fillRect(selected_rect, QColor(164,164,164,120)) painter.restore() if gallery.dead_link: painter.save() selected_rect = QRectF(x, y, w, lbl_rect.height()+app_constants.THUMB_H_SIZE) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(QColor(255,0,0,120))) p_path = QPainterPath() p_path.setFillRule(Qt.WindingFill) p_path.addRoundedRect(selected_rect, 5,5) p_path.addRect(x,y, 20, 20) p_path.addRect(x+w-20,y, 20, 20) painter.drawPath(p_path.simplified()) painter.setPen(QColor("white")) txt_layout = misc.text_layout("Cannot find gallery source!", w, self.title_font, self.title_font_m) txt_layout.draw(painter, QPointF(x, y+h*0.3)) painter.restore() if app_constants.DEBUG: painter.save() painter.setBrush(QBrush(QColor("red"))) painter.setPen(QColor("white")) txt_l = self.title_font_m.width(str(gallery.id)) painter.drawRect(x, y+40, txt_l*2, self.title_font_m.height()) painter.drawText(x+1, y+51, str(gallery.id)) painter.restore() if option.state & QStyle.State_Selected: painter.setPen(QPen(option.palette.highlightedText().color())) else: super().paint(painter, option, index)
def paint(self, painter, QStyleOptionGraphicsItem, widget=None): """Overloaded QWidget paint method. Draws the Node starting with the title, then content, background, corner fixes, and outline. Args: painter: QWidget painter class. QStyleOptionGraphicsItem: ??? (not used). widget: The parent widget if there is one(not used). Todo: * Find out what QStyleOptionGraphicsItem is for. """ # title path_title = QPainterPath() path_title.setFillRule(Qt.WindingFill) path_title.addRoundedRect(0, 0, self.width, self.title_height, self.edge_roundness, self.edge_roundness) path_title.addRect(0, self.title_height - self.edge_roundness, self.edge_roundness, self.edge_roundness) path_title.addRect(self.width - self.edge_roundness, self.title_height - self.edge_roundness, self.edge_roundness, self.edge_roundness) painter.setPen(Qt.NoPen) painter.setBrush(self._brush_title_grad) painter.drawPath(path_title.simplified()) # content path_content = QPainterPath() path_content.setFillRule(Qt.WindingFill) # add the rectange for the content area background with rounded edges path_content.addRoundedRect(0, self.title_height, self.width, self.height - self.title_height, self.edge_roundness, self.edge_roundness) # next 2 addRects are to create 2 small squares below the title # since the are rounded(which we do not want) path_content.addRect(0, self.title_height, self.edge_roundness, self.edge_roundness) path_content.addRect(self.width - self.edge_roundness, self.title_height, self.edge_roundness, self.edge_roundness) painter.setPen(Qt.NoPen) painter.setBrush(self._brush_background) painter.drawPath(path_content.simplified()) # selected outline if self.isSelected(): path_outline = QPainterPath() path_outline.addRoundedRect(-2, -2, self.width + 4, self.height + 4, self.edge_roundness, self.edge_roundness) painter.setBrush(Qt.NoBrush) painter.setPen(self._pen_selected) painter.drawPath(path_outline.simplified()) # outline path_outline = QPainterPath() path_outline.addRoundedRect(0, 0, self.width, self.height, self.edge_roundness, self.edge_roundness) painter.setBrush(Qt.NoBrush) if self.hovered: #painter.setPen(self._pen_hovered) #painter.drawPath(path_outline.simplified()) painter.setPen(self._pen_default) painter.drawPath(path_outline.simplified()) else: painter.setPen(self._pen_default) painter.drawPath(path_outline.simplified())
def paint(self, painter, option, index): assert isinstance(painter, QPainter) if index.data(Qt.UserRole+1): if app_constants.HIGH_QUALITY_THUMBS: painter.setRenderHint(QPainter.SmoothPixmapTransform) painter.setRenderHint(QPainter.Antialiasing) gallery = index.data(Qt.UserRole+1) title = gallery.title artist = gallery.artist title_color = app_constants.GRID_VIEW_TITLE_COLOR artist_color = app_constants.GRID_VIEW_ARTIST_COLOR label_color = app_constants.GRID_VIEW_LABEL_COLOR # Enable this to see the defining box #painter.drawRect(option.rect) # define font size if 20 > len(title) > 15: title_size = "font-size:{}px;".format(self.font_size) elif 30 > len(title) > 20: title_size = "font-size:{}px;".format(self.font_size-1) elif 40 > len(title) >= 30: title_size = "font-size:{}px;".format(self.font_size-2) elif 50 > len(title) >= 40: title_size = "font-size:{}px;".format(self.font_size-3) elif len(title) >= 50: title_size = "font-size:{}px;".format(self.font_size-4) else: title_size = "font-size:{}px;".format(self.font_size) if 30 > len(artist) > 20: artist_size = "font-size:{}px;".format(self.font_size) elif 40 > len(artist) >= 30: artist_size = "font-size:{}px;".format(self.font_size-1) elif len(artist) >= 40: artist_size = "font-size:{}px;".format(self.font_size-2) else: artist_size = "font-size:{}px;".format(self.font_size) #painter.setPen(QPen(Qt.NoPen)) #option.rect = option.rect.adjusted(11, 10, 0, 0) option.rect.setWidth(self.W) option.rect.setHeight(self.H) rec = option.rect.getRect() x = rec[0] y = rec[1] w = rec[2] h = rec[3] text_area = QTextDocument() text_area.setDefaultFont(option.font) text_area.setHtml(""" <head> <style> #area {{ display:flex; width:140px; height:10px }} #title {{ position:absolute; color: {4}; font-weight:bold; {0} }} #artist {{ position:absolute; color: {5}; top:20px; right:0; {1} }} </style> </head> <body> <div id="area"> <center> <div id="title">{2} </div> <div id="artist">{3} </div> </div> </center> </body> """.format(title_size, artist_size, title, artist, title_color, artist_color)) text_area.setTextWidth(w) #chapter_area = QTextDocument() #chapter_area.setDefaultFont(option.font) #chapter_area.setHtml(""" #<font color="black">{}</font> #""".format("chapter")) #chapter_area.setTextWidth(w) def center_img(width): new_x = x if width < w: diff = w - width offset = diff//2 new_x += offset return new_x # if we can't find a cached image pix_cache = QPixmapCache.find(self.key(gallery.profile)) if isinstance(pix_cache, QPixmap): self.image = pix_cache img_x = center_img(self.image.width()) if self.image.height() < self.image.width(): #to keep aspect ratio painter.drawPixmap(QPoint(img_x,y), self.image) else: painter.drawPixmap(QPoint(img_x,y), self.image) else: self.image = QPixmap(gallery.profile) img_x = center_img(self.image.width()) QPixmapCache.insert(self.key(gallery.profile), self.image) if self.image.height() < self.image.width(): #to keep aspect ratio painter.drawPixmap(QPoint(img_x,y), self.image) else: painter.drawPixmap(QPoint(img_x,y), self.image) # draw star if it's favorited if gallery.fav == 1: painter.drawPixmap(QPointF(x,y), QPixmap(app_constants.STAR_PATH)) if app_constants._REFRESH_EXTERNAL_VIEWER: if app_constants.USE_EXTERNAL_VIEWER: self.external_icon = self.file_icons.get_external_file_icon() else: self.external_icon = self.file_icons.get_default_file_icon() if gallery.state == self.G_DOWNLOAD: painter.save() dl_box = QRect(x, y, w, 20) painter.setBrush(QBrush(QColor(0,0,0,123))) painter.setPen(QColor('white')) painter.drawRect(dl_box) painter.drawText(dl_box, Qt.AlignCenter, 'Downloading...') painter.restore() else: if app_constants.DISPLAY_GALLERY_TYPE: self.type_icon = self.file_icons.get_file_icon(gallery.path) if self.type_icon and not self.type_icon.isNull(): self.type_icon.paint(painter, QRect(x+2, y+app_constants.THUMB_H_SIZE-16, 16, 16)) if app_constants.USE_EXTERNAL_PROG_ICO: if self.external_icon and not self.external_icon.isNull(): self.external_icon.paint(painter, QRect(x+w-30, y+app_constants.THUMB_H_SIZE-28, 28, 28)) def draw_text_label(lbl_h): #draw the label for text painter.save() painter.translate(x, y+app_constants.THUMB_H_SIZE) box_color = QBrush(QColor(label_color))#QColor(0,0,0,123)) painter.setBrush(box_color) rect = QRect(0, 0, w, lbl_h) #x, y, width, height painter.fillRect(rect, box_color) painter.restore() return rect if option.state & QStyle.State_MouseOver or\ option.state & QStyle.State_Selected: title_layout = self.text_layout(title, w, self.title_font, self.title_font_m) artist_layout = self.text_layout(artist, w, self.artist_font, self.artist_font_m) t_h = title_layout.boundingRect().height() a_h = artist_layout.boundingRect().height() if app_constants.GALLERY_FONT_ELIDE: lbl_rect = draw_text_label(min(t_h+a_h+3, app_constants.GRIDBOX_LBL_H)) else: lbl_rect = draw_text_label(app_constants.GRIDBOX_LBL_H) clipping = QRectF(x, y+app_constants.THUMB_H_SIZE, w, app_constants.GRIDBOX_LBL_H - 10) title_layout.draw(painter, QPointF(x, y+app_constants.THUMB_H_SIZE), clip=clipping) artist_layout.draw(painter, QPointF(x, y+app_constants.THUMB_H_SIZE+t_h), clip=clipping) #painter.fillRect(option.rect, QColor) else: if app_constants.GALLERY_FONT_ELIDE: lbl_rect = draw_text_label(self.text_label_h) else: lbl_rect = draw_text_label(app_constants.GRIDBOX_LBL_H) # draw text painter.save() alignment = QTextOption(Qt.AlignCenter) alignment.setUseDesignMetrics(True) title_rect = QRectF(0,0,w, self.title_font_m.height()) artist_rect = QRectF(0,self.artist_font_m.height(),w, self.artist_font_m.height()) painter.translate(x, y+app_constants.THUMB_H_SIZE) if app_constants.GALLERY_FONT_ELIDE: painter.setFont(self.title_font) painter.setPen(QColor(title_color)) painter.drawText(title_rect, self.title_font_m.elidedText(title, Qt.ElideRight, w-10), alignment) painter.setPen(QColor(artist_color)) painter.setFont(self.artist_font) alignment.setWrapMode(QTextOption.NoWrap) painter.drawText(artist_rect, self.title_font_m.elidedText(artist, Qt.ElideRight, w-10), alignment) else: text_area.setDefaultFont(QFont(self.font_name)) text_area.drawContents(painter) ##painter.resetTransform() painter.restore() if option.state & QStyle.State_Selected: painter.save() selected_rect = QRectF(x, y, w, lbl_rect.height()+app_constants.THUMB_H_SIZE) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(QColor(164,164,164,120))) p_path = QPainterPath() p_path.setFillRule(Qt.WindingFill) p_path.addRoundedRect(selected_rect, 5,5) p_path.addRect(x,y, 20, 20) p_path.addRect(x+w-20,y, 20, 20) painter.drawPath(p_path.simplified()) #painter.fillRect(selected_rect, QColor(164,164,164,120)) painter.restore() #if option.state & QStyle.State_Selected: # painter.setPen(QPen(option.palette.highlightedText().color())) else: super().paint(painter, option, index)