def paintEvent(self, event): canvas_size = self.rect() width = self.current_pixmap_size.width() extrax = canvas_size.width() - width if extrax < 0: extrax = 0 x = int(extrax/2.) height = self.current_pixmap_size.height() extray = canvas_size.height() - height if extray < 0: extray = 0 y = int(extray/2.) target = QRect(x, y, width, height) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, self.pixmap.scaled(target.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) if gprefs['bd_overlay_cover_size']: sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u'\u00a0%d x %d\u00a0'%(self.pixmap.width(), self.pixmap.height()) flags = Qt.AlignBottom|Qt.AlignRight|Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255,255,255))) p.drawText(sztgt, flags, sz) p.end()
def render_emblems(item, emblems): emblems = tuple(emblems) if not emblems: return icon = self.rendered_emblem_cache.get(emblems, None) if icon is None: pixmaps = [] for emblem in emblems: pm = self.emblem_cache.get(emblem, None) if pm is None: pm = self.emblem_cache[emblem] = QPixmap( I(emblem)).scaled(self.iconSize(), transformMode=Qt.SmoothTransformation) pixmaps.append(pm) num = len(pixmaps) w, h = pixmaps[0].width(), pixmaps[0].height() if num == 1: icon = self.rendered_emblem_cache[emblems] = QIcon(pixmaps[0]) else: canvas = QPixmap((num * w) + ((num-1)*2), h) canvas.fill(Qt.transparent) painter = QPainter(canvas) for i, pm in enumerate(pixmaps): painter.drawPixmap(i * (w + 2), 0, pm) painter.end() icon = self.rendered_emblem_cache[emblems] = canvas item.setData(0, Qt.DecorationRole, icon)
def paintEvent(self, event): canvas_size = self.rect() width = self.current_pixmap_size.width() extrax = canvas_size.width() - width if extrax < 0: extrax = 0 x = int(extrax / 2.) height = self.current_pixmap_size.height() extray = canvas_size.height() - height if extray < 0: extray = 0 y = int(extray / 2.) target = QRect(x, y, width, height) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap( target, self.pixmap.scaled(target.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) if gprefs['bd_overlay_cover_size']: sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u'\u00a0%d x %d\u00a0' % (self.pixmap.width(), self.pixmap.height()) flags = Qt.AlignBottom | Qt.AlignRight | Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255, 255, 255))) p.drawText(sztgt, flags, sz) p.end()
def paintEvent(self, event): QWidget.paintEvent(self, event) pmap = self._pixmap if pmap.isNull(): return w, h = pmap.width(), pmap.height() ow, oh = w, h cw, ch = self.rect().width(), self.rect().height() scaled, nw, nh = fit_image(w, h, cw, ch) if scaled: pmap = pmap.scaled(nw, nh, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) w, h = pmap.width(), pmap.height() x = int(abs(cw - w) / 2.0) y = int(abs(ch - h) / 2.0) target = QRect(x, y, w, h) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) if self.draw_border: pen = QPen() pen.setWidth(self.BORDER_WIDTH) p.setPen(pen) p.drawRect(target) if self.show_size: sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u"\u00a0%d x %d\u00a0" % (ow, oh) flags = Qt.AlignBottom | Qt.AlignRight | Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255, 255, 255))) p.drawText(sztgt, flags, sz) p.end()
def paintEvent(self, event): QWidget.paintEvent(self, event) pmap = self._pixmap if pmap.isNull(): return w, h = pmap.width(), pmap.height() cw, ch = self.rect().width(), self.rect().height() scaled, nw, nh = fit_image(w, h, cw, ch) if scaled: pmap = pmap.scaled(nw, nh, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) w, h = pmap.width(), pmap.height() x = int(abs(cw - w) / 2.) y = int(abs(ch - h) / 2.) target = QRect(x, y, w, h) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) pen = QPen() pen.setWidth(self.BORDER_WIDTH) p.setPen(pen) if self.draw_border: p.drawRect(target) #p.drawRect(self.rect()) p.end()
def paintEvent(self, event): pmap = self.blank if self.pixmap is None or self.pixmap.isNull( ) else self.pixmap target = self.rect() scaled, width, height = fit_image(pmap.width(), pmap.height(), target.width(), target.height()) target.setRect(target.x(), target.y(), width, height) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) if self.pixmap is not None and not self.pixmap.isNull(): sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u'\u00a0%d x %d\u00a0' % (self.pixmap.width(), self.pixmap.height()) flags = Qt.AlignBottom | Qt.AlignRight | Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255, 255, 255))) p.drawText(sztgt, flags, sz) p.end()
def paintEvent(self, event): QWidget.paintEvent(self, event) pmap = self._pixmap if pmap.isNull(): return w, h = pmap.width(), pmap.height() cw, ch = self.rect().width(), self.rect().height() scaled, nw, nh = fit_image(w, h, cw, ch) if scaled: pmap = pmap.scaled(nw, nh, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) w, h = pmap.width(), pmap.height() x = int(abs(cw - w)/2.) y = int(abs(ch - h)/2.) target = QRect(x, y, w, h) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) pen = QPen() pen.setWidth(self.BORDER_WIDTH) p.setPen(pen) if self.draw_border: p.drawRect(target) #p.drawRect(self.rect()) p.end()
def render_emblems(item, emblems): emblems = tuple(emblems) if not emblems: return icon = self.rendered_emblem_cache.get(emblems, None) if icon is None: pixmaps = [] for emblem in emblems: pm = self.emblem_cache.get(emblem, None) if pm is None: pm = self.emblem_cache[emblem] = QPixmap( I(emblem)).scaled( self.iconSize(), transformMode=Qt.SmoothTransformation) pixmaps.append(pm) num = len(pixmaps) w, h = pixmaps[0].width(), pixmaps[0].height() if num == 1: icon = self.rendered_emblem_cache[emblems] = QIcon( pixmaps[0]) else: canvas = QPixmap((num * w) + ((num - 1) * 2), h) canvas.fill(Qt.transparent) painter = QPainter(canvas) for i, pm in enumerate(pixmaps): painter.drawPixmap(i * (w + 2), 0, pm) painter.end() icon = self.rendered_emblem_cache[emblems] = canvas item.setData(0, Qt.DecorationRole, icon)
def paintEvent(self, event): w = self.viewport().rect().width() painter = QPainter(self.viewport()) painter.setClipRect(event.rect()) floor = event.rect().bottom() ceiling = event.rect().top() fv = self.firstVisibleBlock().blockNumber() origin = self.contentOffset() doc = self.document() lines = [] for num, text in self.headers: top, bot = num, num + 3 if bot < fv: continue y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top)).translated(origin).y() y_bot = self.blockBoundingGeometry(doc.findBlockByNumber(bot)).translated(origin).y() if max(y_top, y_bot) < ceiling: continue if min(y_top, y_bot) > floor: break painter.setFont(self.heading_font) br = painter.drawText(3, y_top, w, y_bot - y_top - 5, Qt.TextSingleLine, text) painter.setPen(QPen(self.palette().text(), 2)) painter.drawLine(0, br.bottom()+3, w, br.bottom()+3) for top, bot, kind in self.changes: if bot < fv: continue y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top)).translated(origin).y() y_bot = self.blockBoundingGeometry(doc.findBlockByNumber(bot)).translated(origin).y() if max(y_top, y_bot) < ceiling: continue if min(y_top, y_bot) > floor: break if y_top != y_bot: painter.fillRect(0, y_top, w, y_bot - y_top, self.diff_backgrounds[kind]) lines.append((y_top, y_bot, kind)) if top in self.images: img, maxw = self.images[top][:2] if bot > top + 1 and not img.isNull(): y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top+1)).translated(origin).y() + 3 y_bot -= 3 scaled, imgw, imgh = fit_image(img.width(), img.height(), w - 3, y_bot - y_top) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.drawPixmap(QRect(3, y_top, imgw, imgh), img) painter.end() PlainTextEdit.paintEvent(self, event) painter = QPainter(self.viewport()) painter.setClipRect(event.rect()) for top, bottom, kind in sorted(lines, key=lambda (t, b, k):{'replace':0}.get(k, 1)): painter.setPen(QPen(self.diff_foregrounds[kind], 1)) painter.drawLine(0, top, w, top) painter.drawLine(0, bottom - 1, w, bottom - 1)
def paintEvent(self, event): canvas_size = self.rect() width = self.current_pixmap_size.width() extrax = canvas_size.width() - width if extrax < 0: extrax = 0 x = int(extrax/2.) height = self.current_pixmap_size.height() extray = canvas_size.height() - height if extray < 0: extray = 0 y = int(extray/2.) target = QRect(x, y, width, height) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, self.pixmap.scaled(target.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) p.end()
def paintEvent(self, event): canvas_size = self.rect() width = self.current_pixmap_size.width() extrax = canvas_size.width() - width if extrax < 0: extrax = 0 x = int(extrax / 2.) height = self.current_pixmap_size.height() extray = canvas_size.height() - height if extray < 0: extray = 0 y = int(extray / 2.) target = QRect(x, y, width, height) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap( target, self.pixmap.scaled(target.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) p.end()
def paintEvent(self, event): pmap = self.blank if self.pixmap is None or self.pixmap.isNull() else self.pixmap target = self.rect() scaled, width, height = fit_image(pmap.width(), pmap.height(), target.width(), target.height()) target.setRect(target.x(), target.y(), width, height) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) if self.pixmap is not None and not self.pixmap.isNull(): sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u'\u00a0%d x %d\u00a0'%(self.pixmap.width(), self.pixmap.height()) flags = Qt.AlignBottom|Qt.AlignRight|Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255,255,255))) p.drawText(sztgt, flags, sz) p.end()
def magnify_icons(a, b): a = a.pixmap(16,16) b = b.pixmap(16,16) img = [QPixmap(16,16), QPixmap(16,16)] for i in img: i.fill(QColor(0, 0, 0, 0)) g = a.toImage() for n in range(256): x, y = n%16, n//16 c = QColor(g.pixel(x, y)) s = (c.redF() + c.greenF() + c.blueF()) / 3.0 l = s * 4.2 if l > 1.0: l = 1.0 c.setRgbF(s, s, s, l) g.setPixel(x, y, c.rgba()) p = QPainter() p.begin(i) p.drawImage( QRectF(6, 0, 8, 16), g, QRectF(0, 0, 10, 16)) p.drawPixmap(QRectF(0, 0, 10, 16), b, QRectF(6, 1, 10, 15)) p.end() a, b = b, a return tuple(map(QIcon, img))
def paintEvent(self, event): QWidget.paintEvent(self, event) pmap = self._pixmap if pmap.isNull(): return w, h = pmap.width(), pmap.height() ow, oh = w, h cw, ch = self.rect().width(), self.rect().height() scaled, nw, nh = fit_image(w, h, cw, ch) if scaled: pmap = pmap.scaled(nw, nh, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) w, h = pmap.width(), pmap.height() x = int(abs(cw - w) / 2.) y = int(abs(ch - h) / 2.) target = QRect(x, y, w, h) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) if self.draw_border: pen = QPen() pen.setWidth(self.BORDER_WIDTH) p.setPen(pen) p.drawRect(target) if self.show_size: sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u'\u00a0%d x %d\u00a0' % (ow, oh) flags = Qt.AlignBottom | Qt.AlignRight | Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255, 255, 255))) p.drawText(sztgt, flags, sz) p.end()
def paintEvent(self, ev): if self.before_image is None: return canvas_size = self.rect() p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(canvas_size, self.before_image, self.before_image.rect()) if self.after_image is not None: width = self._current_width iw = self.after_image.width() sh = min(self.after_image.height(), canvas_size.height()) if self.flip_forwards: source = QRect(max(0, iw - width), 0, width, sh) else: source = QRect(0, 0, width, sh) target = QRect(source) target.moveLeft(0) p.drawPixmap(target, self.after_image, source) p.end()
def paintEvent(self, event): w = self.viewport().rect().width() painter = QPainter(self.viewport()) painter.setClipRect(event.rect()) floor = event.rect().bottom() ceiling = event.rect().top() fv = self.firstVisibleBlock().blockNumber() origin = self.contentOffset() doc = self.document() lines = [] for num, text in self.headers: top, bot = num, num + 3 if bot < fv: continue y_top = self.blockBoundingGeometry( doc.findBlockByNumber(top)).translated(origin).y() y_bot = self.blockBoundingGeometry( doc.findBlockByNumber(bot)).translated(origin).y() if max(y_top, y_bot) < ceiling: continue if min(y_top, y_bot) > floor: break painter.setFont(self.heading_font) br = painter.drawText(3, y_top, w, y_bot - y_top - 5, Qt.TextSingleLine, text) painter.setPen(QPen(self.palette().text(), 2)) painter.drawLine(0, br.bottom() + 3, w, br.bottom() + 3) for top, bot, kind in self.changes: if bot < fv: continue y_top = self.blockBoundingGeometry( doc.findBlockByNumber(top)).translated(origin).y() y_bot = self.blockBoundingGeometry( doc.findBlockByNumber(bot)).translated(origin).y() if max(y_top, y_bot) < ceiling: continue if min(y_top, y_bot) > floor: break if y_top != y_bot: painter.fillRect(0, y_top, w, y_bot - y_top, self.diff_backgrounds[kind]) lines.append((y_top, y_bot, kind)) if top in self.images: img, maxw = self.images[top][:2] if bot > top + 1 and not img.isNull(): y_top = self.blockBoundingGeometry( doc.findBlockByNumber(top + 1)).translated(origin).y() + 3 y_bot -= 3 scaled, imgw, imgh = fit_image(img.width(), img.height(), w - 3, y_bot - y_top) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.drawPixmap(QRect(3, y_top, imgw, imgh), img) painter.end() PlainTextEdit.paintEvent(self, event) painter = QPainter(self.viewport()) painter.setClipRect(event.rect()) for top, bottom, kind in sorted(lines, key=lambda (t, b, k): {'replace': 0}.get(k, 1)): painter.setPen(QPen(self.diff_foregrounds[kind], 1)) painter.drawLine(0, top, w, top) painter.drawLine(0, bottom - 1, w, bottom - 1)
def createCompositeCursor(cursor, overlayCursor, hotX = None, hotY = None, offsetX = 0, offsetY = 0): """ Returns a composite 32x32 cursor using two other cursors. This is useful for creating composite cursor images from two (or more) cursors. For example, the pencil cursor includes a horizontal and vertical symbol when drawing a horizontal or vertical line. This function can be used to create these cursors without having to create each one by hand. The payoff is when the developer/artist wants to change the base cursor image (i.e. the pencil cursor) without having to redraw and save all the other versions of the cursor in the set. @param cursor: The main cursor. @type cursor: QCursor @param overlayCursor: The cursor to overlay on top of I{cursor}. @type overlayCursor: QCursor @param hotX: The X coordinate of the hotspot. If none is given, the hotspot of I{cursor} is used. @type hotX: int @param hotY: The Y coordinate of the hotspot. If none is given, the hotspot of I{cursor} is used. @type hotY: int @param offsetX: The X offset in which to draw the overlay cursor onto I{cursor}. The default is 0. @type offsetX: int @param offsetY: The Y offset in which to draw the overlay cursor onto I{cursor}. The default is 0. @type offsetY: int @return: The composite cursor. @rtype: QCursor @note: It would be easy and useful to allow overlayCursor to be a QPixmap. I'll add this when it becomes helpful. --Mark 2008-03-06. """ # Method: # 1. Get cursor's pixmap and create a painter from it. # 2. Get the pixmap from the overlay cursor and draw it onto the # cursor pixmap to create the composite pixmap. # 3. Create and return a new QCursor from the composite pixmap. # Mark 2008-03-05 assert isinstance(cursor, QCursor) assert isinstance(overlayCursor, QCursor) if hotX is None: hotX = cursor.hotSpot().x() if hotY is None: hotY = cursor.hotSpot().y() pixmap = cursor.pixmap() overlayPixmap = overlayCursor.pixmap() painter = QPainter(pixmap) painter.drawPixmap(offsetX, offsetY, overlayPixmap) painter.end() return QCursor(pixmap, hotX, hotY)
class PaintBoard(QWidget): # Define virtual panel coordinates for different shapes/regions VPCoord_Start = [316, 332] #LeftTopX, Y VPCoord_Circle = [316, 332, 336, 363] #LeftTopX, Y, RightBotX, Y VPCoord_Rect = [336, 332, 356, 363] #LeftTopX, Y, RightBotX, Y VPCoord_Tri = [316, 363, 336, 395] #LeftTopX, Y, RightBotX, Y VPCoord_Line = [336, 363, 356, 395] #LeftTopX, Y, RightBotX, Y # A flag to check if the user is currently using the virtual panel usingVP = False def __init__(self, sizeX, sizeY, Parent=None): ''' Constructor ''' super(PaintBoard, self).__init__(Parent) self.__InitData(sizeX, sizeY) #Initialize Data first, then interface/view self.__InitView() print("Init PaintBoard") def __InitView(self): self.setFixedSize(self.__size) def __InitData(self, sizeX, sizeY): self.__size = QSize(sizeX, sizeY) self.__board = QPixmap( self.__size) # Make a new QPixmap as paint board,350px * 250px self.__board.fill(Qt.white) #Fill the paint board with white self.__IsEmpty = True #board is empty by default self.EraserMode = False #eraser mode is disabled by default self.__lastPos = None self.__currentPos = QPoint(0, 0) self.__painter = QPainter() self.__thickness = 1 #default pen thickness is 1 self.__penColor = QColor("black") #default color is black self.__colorList = QColor.colorNames() #get the list of colors def Clear(self): #Clear the board self.__board.fill(Qt.white) self.update() self.__IsEmpty = True def ChangePenColor(self, color="black"): self.__penColor = QColor(color) def ChangePenThickness(self, thickness=1): self.__thickness = thickness def IsEmpty(self): #Is the board empty return self.__IsEmpty def GetContentAsQImage(self): #return the content of the board (return QImage) image = self.__board.toImage() return image def paintEvent(self, paintEvent): self.__painter.begin(self) self.__painter.drawPixmap(0, 0, self.__board) self.__painter.end() # print("inside paintEvent") def penPressEvent(self, pos): self.__currentPos = QPoint(pos[0], pos[1]) self.__lastPos = self.__currentPos def penMoveEvent(self, pos, pressure, liftedDeque): pen_x = pos[0] pen_y = pos[1] pen_pressure = pressure # print(liftedDeque) # print(self.__lastPos) if self.__lastPos is None: self.__lastPos = QPoint(pen_x, pen_y) self.__currentPos = QPoint(pen_x, pen_y) self.__painter.begin(self.__board) if self.EraserMode == False: #Non-Eraser mode self.__penColor = QColor("blue") self.__painter.setPen(QPen( self.__penColor, self.__thickness)) #Set pen color, thickness else: #Eraser mode: pen color is white, thickness is 6 self.__painter.setPen(QPen(Qt.white, 6)) self.__painter.drawLine(self.__lastPos, self.__currentPos) self.__painter.end() self.__lastPos = self.__currentPos self.update() #Show updates # If ever detected the pen is lifted, reset the __lastPos variable in order to reposition the pen if (True in liftedDeque): self.__lastPos = None # Virtual Panel event def penVPEvent(self, pos, pressure): pass ''' # Check if the pressure is over 500 if(pen_pressure > 400): # Check which region the pen is in and prepare to draw shape accordingly if(pen_x < self.VPCoord_Circle[2] and pen_y < self.VPCoord_Circle[3]): print("A") elif(pen_x < self.VPCoord_Rect[2] and pen_y < self.VPCoord_Rect[3]): print("B") elif(pen_x < self.VPCoord_Tri[2] and pen_y < self.VPCoord_Tri[3]): print("C") elif(pen_x < self.VPCoord_Line[2] and pen_y < self.VPCoord_Line[3]): print("D") ''' def penReleaseEvent(self, pos): self.__IsEmpty = False #board is not empty def paintEllipse(self, center_x, center_y, radias1, radias2): self.__painter.begin(self.__board) self.__penColor = QColor("black") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawEllipse(QPoint(center_x, center_y), radias1, radias2) self.__painter.end() self.update() #Show updates def paintRect(self, center_x, center_y, upper_left_x, upper_left_y): width = abs(2 * (center_x - upper_left_x)) height = abs(2 * (center_y - upper_left_y)) self.__painter.begin(self.__board) self.__penColor = QColor("black") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawRect(upper_left_x, upper_left_y, width, height) self.__painter.end() self.update() #Show updates def paintTriangle(self, points): self.__painter.begin(self.__board) self.__penColor = QColor("black") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawPolygon(points) self.__painter.end() self.update() #Show updates def paintPolyg(self, x1, y1, x2, y2, x3, y3, x4, y4): self.__painter.begin(self.__board) self.__penColor = QColor("black") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawPolygon(QPoint(x1, y1), QPoint(x2, y2), QPoint(x3, y3), QPoint(x4, y4)) self.__painter.end() self.update() #Show updates def paintLine(self, P1_x, P1_y, P2_x, P2_y): P1 = QPoint(P1_x, P1_y) P2 = QPoint(P2_x, P2_y) self.__painter.begin(self.__board) self.__penColor = QColor("black") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawLine(P1, P2) self.__painter.end() self.update() #Show updates def paintAuxLine(self, P1_x, P1_y, P2_x, P2_y): P1 = QPoint(P1_x, P1_y) P2 = QPoint(P2_x, P2_y) self.__painter.begin(self.__board) self.__penColor = QColor("red") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawLine(P1, P2) self.__painter.end() self.update() #Show updates def PointsInCircum(self, r, n, phi): return [(math.cos(2 * pi / n * x + phi) * r, math.sin(2 * pi / n * x + phi) * r) for x in range(0, n + 1) ] # defining the points on the Circum of a circle def paintPCircle(self, center_x, center_y, start_x, start_y, pers_x, pers_y): point = [start_x, start_y] center = [center_x, center_y] distance = [(point[0] - center[0]), (point[1] - center[1])] phi = math.atan2(distance[1], distance[0]) r = math.sqrt(distance[0] * distance[0] + distance[1] * distance[1]) points = self.PointsInCircum(r, 300, phi) #Drawing the circle in perspective dist_x = center_x - pers_x print("dist_x", dist_x, pers_x, pers_y, center_x, center_y) points = [(j[0], (j[1] + center_x - pers_y) * ((j[0] + center_x) / dist_x) + pers_y - center_y) for j in points] self.__painter.begin(self.__board) self.__penColor = QColor("blue") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) for x in points: self.__painter.drawPoint(round(x[0] + center[0], 3), round(x[1] / 1.00 + center[1], 3)) self.__painter.end() # returns true if given angle is in the second quadrant; # requires angle in degree and in range -180 to 180 def _isAngleInSecondQuadrant(self, angle): if angle > 90 and angle < 180: return True else: return False # returns true if given angle is in the third quadrant; # requires angle in degree and in range -180 to 180 def _isAngleInThirdQuadrant(self, angle): if angle < -90 and angle > -180: return True else: return False def paintArc(self, center_x, center_y, start_x, start_y, end_x, end_y): radius = math.sqrt( math.pow(center_x - start_x, 2) + math.pow(center_y - start_y, 2)) rect = QRectF(center_x - radius, center_y - radius, radius * 2, radius * 2) # start angle calculation startAngle = math.degrees( math.atan2(center_y - start_y, start_x - center_x)) # end angle calculation endAngle = math.degrees(math.atan2(center_y - end_y, end_x - center_x)) # span angle calculation spanAngle = endAngle - startAngle # assume user always wants to draw clock-wise if spanAngle > 0: spanAngle = -1 * (360 - spanAngle) #print("start angle is " + str(startAngle)) #print("span angle is " + str(spanAngle)) self.__painter.begin(self.__board) self.__penColor = QColor("black") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawArc(rect, 16 * startAngle, 16 * spanAngle) self.__painter.end() self.update() #Show updates def paintAuxArc(self, center_x, center_y, start_x, start_y, end_x, end_y): radius = math.sqrt( math.pow(center_x - start_x, 2) + math.pow(center_y - start_y, 2)) rect = QRectF(center_x - radius, center_y - radius, radius * 2, radius * 2) # start angle calculation startAngle = math.degrees( math.atan2(center_y - start_y, start_x - center_x)) # end angle calculation endAngle = math.degrees(math.atan2(center_y - end_y, end_x - center_x)) # span angle calculation spanAngle = 0 if (self._isAngleInThirdQuadrant(startAngle) and self._isAngleInSecondQuadrant(endAngle) ) or (self._isAngleInThirdQuadrant(endAngle) and self._isAngleInSecondQuadrant(startAngle)) or ( startAngle == 180 and self._isAngleInThirdQuadrant(endAngle) ) or (startAngle == -180 and self._isAngleInSecondQuadrant(endAngle)) or ( endAngle == 180 and self._isAngleInThirdQuadrant(startAngle)) or ( endAngle == -180 and self._isAngleInSecondQuadrant(startAngle)): if startAngle == 180 or self._isAngleInThirdQuadrant(startAngle): spanAngle = endAngle - (startAngle + 360) else: spanAngle = (endAngle + 360) - startAngle else: spanAngle = endAngle - startAngle #print("start angle is " + str(startAngle)) #print("span angle is " + str(spanAngle)) self.__painter.begin(self.__board) self.__penColor = QColor("red") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawArc(rect, 16 * startAngle, 16 * spanAngle) self.__painter.end() self.update() #Show updates def paintBezierSpline(self, pointListX, pointListY): P1 = QPoint(int(pointListX[0]), int(pointListY[0])) path = QtGui.QPainterPath() path.moveTo(P1) self.__painter.begin(self.__board) self.__penColor = QColor("black") self.__painter.setPen(QPen(self.__penColor, self.__thickness)) i = 0 while i < len(pointListX) - 3: P2 = QPoint(int(pointListX[i + 1]), int(pointListY[i + 1])) P3 = QPoint(int(pointListX[i + 2]), int(pointListY[i + 2])) P4 = QPoint(int(pointListX[i + 3]), int(pointListY[i + 3])) path.cubicTo(P2, P3, P4) self.__painter.drawPath(path) i += 3 self.__painter.end() self.update() #Show updates
def createCompositeCursor(cursor, overlayCursor, hotX=None, hotY=None, offsetX=0, offsetY=0): """ Returns a composite 32x32 cursor using two other cursors. This is useful for creating composite cursor images from two (or more) cursors. For example, the pencil cursor includes a horizontal and vertical symbol when drawing a horizontal or vertical line. This function can be used to create these cursors without having to create each one by hand. The payoff is when the developer/artist wants to change the base cursor image (i.e. the pencil cursor) without having to redraw and save all the other versions of the cursor in the set. @param cursor: The main cursor. @type cursor: QCursor @param overlayCursor: The cursor to overlay on top of I{cursor}. @type overlayCursor: QCursor @param hotX: The X coordinate of the hotspot. If none is given, the hotspot of I{cursor} is used. @type hotX: int @param hotY: The Y coordinate of the hotspot. If none is given, the hotspot of I{cursor} is used. @type hotY: int @param offsetX: The X offset in which to draw the overlay cursor onto I{cursor}. The default is 0. @type offsetX: int @param offsetY: The Y offset in which to draw the overlay cursor onto I{cursor}. The default is 0. @type offsetY: int @return: The composite cursor. @rtype: QCursor @note: It would be easy and useful to allow overlayCursor to be a QPixmap. I'll add this when it becomes helpful. --Mark 2008-03-06. """ # Method: # 1. Get cursor's pixmap and create a painter from it. # 2. Get the pixmap from the overlay cursor and draw it onto the # cursor pixmap to create the composite pixmap. # 3. Create and return a new QCursor from the composite pixmap. # Mark 2008-03-05 assert isinstance(cursor, QCursor) assert isinstance(overlayCursor, QCursor) if hotX is None: hotX = cursor.hotSpot().x() if hotY is None: hotY = cursor.hotSpot().y() pixmap = cursor.pixmap() overlayPixmap = overlayCursor.pixmap() painter = QPainter(pixmap) painter.drawPixmap(offsetX, offsetY, overlayPixmap) painter.end() return QCursor(pixmap, hotX, hotY)
class PaintBoard(QWidget): # Define virtual panel coordinates for different shapes/regions VPCoord_Start = [316, 332] #LeftTopX, Y VPCoord_Circle = [316, 332, 336, 363] #LeftTopX, Y, RightBotX, Y VPCoord_Rect = [336, 332, 356, 363] #LeftTopX, Y, RightBotX, Y VPCoord_Tri = [316, 363, 336, 395] #LeftTopX, Y, RightBotX, Y VPCoord_Line = [336, 363, 356, 395] #LeftTopX, Y, RightBotX, Y # A flag to check if the user is currently using the virtual panel usingVP = False def __init__(self, sizeX, sizeY, Parent=None): ''' Constructor ''' super(PaintBoard, self).__init__(Parent) self.__InitData(sizeX, sizeY) #Initialize Data first, then interface/view self.__InitView() print("Init PaintBoard") def __InitView(self): self.setFixedSize(self.__size) def __InitData(self, sizeX, sizeY): self.__size = QSize(sizeX, sizeY) self.__board = QPixmap( self.__size) # Make a new QPixmap as paint board,350px * 250px self.__board.fill(Qt.white) #Fill the paint board with white self.__IsEmpty = True #board is empty by default self.EraserMode = False #eraser mode is disabled by default self.__lastPos = None self.__currentPos = QPoint(0, 0) self.__painter = QPainter() self.__thickness = 1 #default pen thickness is 1 self.__penColor = QColor("black") #default color is black self.__colorList = QColor.colorNames() #get the list of colors def Clear(self): #Clear the board self.__board.fill(Qt.white) self.update() self.__IsEmpty = True def ChangePenColor(self, color="black"): self.__penColor = QColor(color) def ChangePenThickness(self, thickness=1): self.__thickness = thickness def IsEmpty(self): #Is the board empty return self.__IsEmpty def GetContentAsQImage(self): #return the content of the board (return QImage) image = self.__board.toImage() return image def paintEvent(self, paintEvent): self.__painter.begin(self) self.__painter.drawPixmap(0, 0, self.__board) self.__painter.end() # print("inside paintEvent") def penPressEvent(self, pos): self.__currentPos = QPoint(pos[0], pos[1]) self.__lastPos = self.__currentPos def penMoveEvent(self, pos, pressure): pen_x = pos[0] pen_y = pos[1] pen_pressure = pressure if self.__lastPos is None: self.__lastPos = QPoint(pen_x, pen_y) elif (abs(pen_x - self.__lastPos.x()) > 21 or abs(pen_y - self.__lastPos.y()) > 21): self.__lastPos = QPoint(pen_x, pen_y) self.__currentPos = QPoint(pen_x, pen_y) self.__painter.begin(self.__board) if self.EraserMode == False: #Non-Eraser mode self.__painter.setPen(QPen( self.__penColor, self.__thickness)) #Set pen color, thickness else: #Eraser mode: pen color is white, thickness is 6 self.__painter.setPen(QPen(Qt.white, 6)) self.__painter.drawLine(self.__lastPos, self.__currentPos) self.__painter.end() self.__lastPos = self.__currentPos self.update() #Show updates # Virtual Panel event def penVPEvent(self, pos, pressure): pass ''' # Check if the pressure is over 500 if(pen_pressure > 400): # Check which region the pen is in and prepare to draw shape accordingly if(pen_x < self.VPCoord_Circle[2] and pen_y < self.VPCoord_Circle[3]): print("A") elif(pen_x < self.VPCoord_Rect[2] and pen_y < self.VPCoord_Rect[3]): print("B") elif(pen_x < self.VPCoord_Tri[2] and pen_y < self.VPCoord_Tri[3]): print("C") elif(pen_x < self.VPCoord_Line[2] and pen_y < self.VPCoord_Line[3]): print("D") ''' def penReleaseEvent(self, pos): self.__IsEmpty = False #board is not empty def paintEllipse(self, center_x, center_y, radias1, radias2): self.__painter.begin(self.__board) self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawEllipse(QPoint(center_x, center_y), radias1, radias2) self.__painter.end() self.update() #Show updates def paintRect(self, center_x, center_y, upper_left_x, upper_left_y): width = abs(2 * (center_x - upper_left_x)) height = abs(2 * (center_y - upper_left_y)) self.__painter.begin(self.__board) self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawRect(upper_left_x, upper_left_y, width, height) self.__painter.end() self.update() #Show updates def paintTriangle(self, points): self.__painter.begin(self.__board) self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawPolygon(points) self.__painter.end() self.update() #Show updates def paintLine(self, P1_x, P1_y, P2_x, P2_y): P1 = QPoint(P1_x, P1_y) P2 = QPoint(P2_x, P2_y) self.__painter.begin(self.__board) self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawLine(P1, P2) self.__painter.end() self.update() #Show updates def paintArc(self, center_x, center_y, start_x, start_y, end_x, end_y): radius = math.sqrt( math.pow(center_x - start_x, 2) + math.pow(center_y - start_y, 2)) rect = QRectF(center_x - radius, center_y - radius, radius * 2, radius * 2) startAngle = 16 * math.atan2(start_x - center_y, start_x - center_x) * 180.0 / math.pi endAngle = 16 * math.atan2(end_y - center_y, end_x - center_x) * 180.0 / math.pi spanAngle = endAngle - startAngle self.__painter.begin(self.__board) self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawArc(rect, startAngle, spanAngle) self.__painter.end() self.update() #Show updates def paintBezierSpline(self, pointListX, pointListY): P1 = QPoint(int(pointListX[0]), int(pointListY[0])) path = QtGui.QPainterPath() path.moveTo(P1) self.__painter.begin(self.__board) self.__painter.setPen(QPen(self.__penColor, self.__thickness)) i = 0 while i < len(pointListX) - 3: P2 = QPoint(int(pointListX[i + 1]), int(pointListY[i + 1])) P3 = QPoint(int(pointListX[i + 2]), int(pointListY[i + 2])) P4 = QPoint(int(pointListX[i + 3]), int(pointListY[i + 3])) path.cubicTo(P2, P3, P4) self.__painter.drawPath(path) i += 3 self.__painter.end() self.update() #Show updates