def paintEvent(self, ev): if self._ui_model: logo_painter = QPainter(self) icon_bank = self._ui_model.get_icon_bank() logo_path = icon_bank.get_kunquat_logo_path() logo_renderer = QSvgRenderer(logo_path) logo_renderer.render(logo_painter)
def __init__(self, svgImage, width, height, parent=None): super(SvgButton, self).__init__(parent) svg_renderer = QSvgRenderer(svgImage) image = QImage(width, height, QImage.Format_ARGB32) # Set the ARGB to 0 to prevent rendering artifacts image.fill(0x00000000) svg_renderer.render(QPainter(image)) pixmap = QPixmap.fromImage(image) icon = QIcon(pixmap) self.setIcon(icon) self.setIconSize(QSize(width, height))
class Hamburger(QPushButton): def __init__(self, parent=None): self.hamburger = QImage(128, 128, QImage.Format_ARGB32) QPushButton.__init__(self, parent) self.svgrenderer = QSvgRenderer("icons\\hamburger.svg") paint = QPainter(self.hamburger) paint.setRenderHint(QPainter.HighQualityAntialiasing) paint.setRenderHint(QPainter.SmoothPixmapTransform) self.svgrenderer.render(paint) paint.end() pixmap = QPixmap() self.setIcon(QIcon(pixmap.fromImage(self.hamburger))) self.setStyleSheet("QPushButton, QPushButton:pressed{background-color: rgba(0,0,0,0)}")
class CheckBox(QCheckBox): def __init__(self, *args): super(CheckBox, self).__init__(*args) self.setMouseTracking(True) if tuple() in [type(i) for i in args]: for arg in args: if isinstance(arg) == tuple(): color = arg else: color = (QColor(0, 150, 136), QColor(255, 255, 255)) self.color1 = color[0] self.color2 = color[1] self.svgrenderer = QSvgRenderer("icons\\check.svg") self.check = QImage(500, 200, QImage.Format_ARGB32) self.resize(30, 30) paint = QPainter(self.check) self.svgrenderer.render(paint) paint.end() def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.SmoothPixmapTransform) self.img = QPixmap() if self.isChecked(): painter.setBrush(self.color1) painter.drawRoundedRect(QRect(-1, -1, 31, 31), 7, 7) painter.drawPixmap(QRect(-2, -5, 35, 40), self.img.fromImage(self.check)) else: pen = QPen() pen.setWidth(2) painter.setPen(pen) # hand draw rect painter.drawLine(QPoint(0, 0), QPoint(30, 0)) painter.drawLine(QPoint(30, 0), QPoint(30, 30)) painter.drawLine(QPoint(0, 0), QPoint(0, 30)) painter.drawLine(QPoint(0, 30), QPoint(30, 30)) painter.end() def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: if self.isChecked(): self.setChecked(False) else: self.setChecked(True) self.repaint()
class CheckBox(QCheckBox): def __init__(self, *args): super(CheckBox, self).__init__(*args) self.setMouseTracking(True) if tuple() in [type(i) for i in args]: for arg in args: if type(arg) == tuple(): color = arg else: color = (QColor(0, 150, 136), QColor(255, 255, 255)) self.color1 = color[0] self.color2 = color[1] self.svgrenderer = QSvgRenderer("icons\\check.svg") self.check = QImage(500, 200, QImage.Format_ARGB32) self.resize(30, 30) paint = QPainter(self.check) self.svgrenderer.render(paint) paint.end() def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.SmoothPixmapTransform) self.img = QPixmap() if self.isChecked(): painter.setBrush(self.color1) painter.drawRoundedRect(QRect(-1, -1, 31, 31), 7, 7) painter.drawPixmap(QRect(-2, -5, 35, 40), self.img.fromImage(self.check)) else: pen = QPen() pen.setWidth(2) painter.setPen(pen) # hand draw rect painter.drawLine(QPoint(0, 0), QPoint(30, 0)) painter.drawLine(QPoint(30, 0), QPoint(30, 30)) painter.drawLine(QPoint(0, 0), QPoint(0, 30)) painter.drawLine(QPoint(0, 30), QPoint(30, 30)) painter.end() def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: if self.isChecked(): self.setChecked(False) else: self.setChecked(True) self.repaint()
def __init__(self, parent=None): self.hamburger = QImage(128, 128, QImage.Format_ARGB32) QPushButton.__init__(self, parent) self.svgrenderer = QSvgRenderer("icons\\hamburger.svg") paint = QPainter(self.hamburger) paint.setRenderHint(QPainter.HighQualityAntialiasing) paint.setRenderHint(QPainter.SmoothPixmapTransform) self.svgrenderer.render(paint) paint.end() pixmap = QPixmap() self.setIcon(QIcon(pixmap.fromImage(self.hamburger))) self.setStyleSheet( "QPushButton, QPushButton:pressed{background-color: rgba(0,0,0,0)} QPushButton:hover {background-color: rgba(255,255,255,100); border-radius: 32px} " )
def svg2icon(path, img_type=QImage): img = QImage(64, 64, QImage.Format_ARGB32) svgrenderer = QSvgRenderer(path) paint = QPainter(img) paint.setRenderHint(QPainter.HighQualityAntialiasing) paint.setRenderHint(QPainter.SmoothPixmapTransform) paint.setBrush(QColor(255, 255, 255, 255)) paint.drawRect(QRect(-1, -1, 65, 65)) svgrenderer.render(paint) paint.end() if img_type == QImage: return img elif img_type == QPixmap: pix = QPixmap pix = pix.fromImage(img) return pix
def __init__(self, *args): super(CheckBox, self).__init__(*args) self.setMouseTracking(True) if tuple() in [type(i) for i in args]: for arg in args: if isinstance(arg) == tuple(): color = arg else: color = (QColor(0, 150, 136), QColor(255, 255, 255)) self.color1 = color[0] self.color2 = color[1] self.svgrenderer = QSvgRenderer("icons\\check.svg") self.check = QImage(500, 200, QImage.Format_ARGB32) self.resize(30, 30) paint = QPainter(self.check) self.svgrenderer.render(paint) paint.end()
def freeze(self): if self.isFrozen: return self.renderer = QSvgRenderer(QByteArray(str(self.queryObject))) if self.root != None: b = self.getBoundaries() self.renderer.setViewBox(b) self.root.setAttribute('width', b.width()) self.root.setAttribute('height', b.height()) self.isFrozen = True
def __init__(self, *args): super(CheckBox, self).__init__(*args) self.setMouseTracking(True) if tuple() in [type(i) for i in args]: for arg in args: if type(arg) == tuple(): color = arg else: color = (QColor(0, 150, 136), QColor(255, 255, 255)) self.color1 = color[0] self.color2 = color[1] self.svgrenderer = QSvgRenderer("icons\\check.svg") self.check = QImage(500, 200, QImage.Format_ARGB32) self.resize(30, 30) paint = QPainter(self.check) self.svgrenderer.render(paint) paint.end()
def __init__(self, name, signPath='', scale=_defaultScale): self.__scale = scale self.__name = name if signPath: substrings = signPath.split('.') last = len(substrings) - 1 if substrings[last] == 'pp' or substrings[last] == 'PP': self.__sign = PPRenderer(signPath, scale) else: self.__sign = QSvgRenderer(signPath) else: self.__sign = PPRenderer('', scale) path = QPainterPath() path.moveTo(-16.0 * scale, -16.0 * scale) path.lineTo(16.0 * scale, -16.0 * scale) path.lineTo(16.0 * scale, 16.0 * scale) path.lineTo(-16.0 * scale, 16.0 * scale) path.lineTo(-16.0 * scale, -16.0 * scale) self.__sign.setPainterPath(path) self.verticalBoundPoints = [] self.horizontalBoundPoints = [] self.__textPoint = QPointF()
class mutableSvgRenderer: def __init__(self, path, controller): self.controller = controller self.nodeLookup = {} self.queryObject = None self.active = set() self.reset = set() self.locks = set() self.childProperites = {} self.globalProperties = {} self.numClones = 0 self.queryObject = pq(filename=path) #cleanedXml = scourString(str(self.queryObject),).encode("UTF-8") #self.queryObject = pq(cleanedXml) self.isFrozen = False self.root = self.buildTree(self.queryObject.root.getroot(),parent=None) self.root.verify() self.freeze() self.lastTarget = None def buildTree(self, xmlObject,parent=None,buildClones=False): newNode = mutableSvgNode(self,self.controller,xmlObject,parent,isClone=buildClones) for child in xmlObject.getchildren(): newChild = self.buildTree(child, newNode) newNode.addChild(newChild) return newNode def getElement(self, key): return self.globalProperties[key] def freeze(self): if self.isFrozen: return self.renderer = QSvgRenderer(QByteArray(str(self.queryObject))) if self.root != None: b = self.getBoundaries() self.renderer.setViewBox(b) self.root.setAttribute('width', b.width()) self.root.setAttribute('height', b.height()) self.isFrozen = True def forceFreeze(self): self.isFrozen = False self.freeze() def thaw(self): self.isFrozen = False def handleFrame(self, userState, node=None, results={'__EVENT__ABSORBED__':False,'__SVG__DIRTY__':False}, runLocks=False): if node == None: self.reset = set(self.active) self.active = set() # empty out the active set... afterward, anything that's in reset that isn't in active will need to have reset called # First run on all locked nodes for n in set(self.locks): # make a copy - this will be edited as we iterate, but we really only care about what was in it in the first place temp = results.get('__SVG__DIRTY__',True) if n.eventProgram != None: results['__SVG__DIRTY__'] = True results = self.handleFrame(userState, n, results, runLocks=True) results['__SVG__DIRTY__'] = results.get('__SVG__DIRTY__',True) or temp if not results.get('__EVENT__ABSORBED__',True): results = self.handleFrame(userState, self.root, results) # Run any resets... needReset = self.reset.difference(self.active) needReset = needReset.difference(self.locks) for n in needReset: temp = results.get('__SVG__DIRTY__',True) if n.resetProgram != None: results['__SVG__DIRTY__'] = True n.runReset(userState,results) self.reset.discard(n) results['__SVG__DIRTY__'] = results.get('__SVG__DIRTY__',True) or temp # again, we want to let it be clean only if every call explicitly marks it clean # Okay, we're finally done - we need to check if something dirtified the SVG: if results.get('__SVG__DIRTY__',True): self.thaw() # Clean up our little internal flags if results.has_key('__SVG__DIRTY__'): del results['__SVG__DIRTY__'] return results else: if results.get('__EVENT__ABSORBED__',True): return results id = node.attributes.get('id',None) if id != None: inBounds = self.eventInElement(userState,id) else: inBounds = True # If there's no id, we'll have to check the children... # If there's no id there, we'll have to assume the frontmost, deepest child is in bounds. # This isn't ideal, but the SVG is screwed up so this is the best we can do if inBounds or runLocks: for child in reversed(node.children): # DFS, using the frontmost elements first if not runLocks and child in self.locks: continue results = self.handleFrame(userState, child, results) if results.get('__EVENT__ABSORBED__',True) == True: break # Okay, no child has absorbed the event yet... so I actually get to call my event if I'm visible and I have one if node.eventProgram != None and node.getAttribute('visibility') != 'hidden' and results.get('__EVENT__ABSORBED__',True) == False: results = node.runCustomEvent(userState,signals=results) # we'll start by assuming it's clean; provided no custom code touches the appearance, this will come back intact self.active.add(node) if results.get('__LOCK__',False): self.locks.add(node) else: self.locks.discard(node) if results.has_key('__LOCK__'): del results['__LOCK__'] return results def getBoundaries(self, id=None): if id == None: id = self.root.attributes['id'] b = self.renderer.boundsOnElement(id) if self.renderer.elementExists(id): m = self.renderer.matrixForElement(id) #print "id: %s b: %s m: %s" % (id,b,m) b = m.mapRect(b) return b def getVisibleBoundaries(self, id=None): objBounds = self.getBoundaries(id) viewBounds = self.renderer.viewBoxF() objBounds.setLeft(max(objBounds.left(),viewBounds.left())) objBounds.setTop(max(objBounds.top(),viewBounds.top())) objBounds.setRight(min(objBounds.right(),viewBounds.right())) objBounds.setBottom(min(objBounds.bottom(),viewBounds.bottom())) return objBounds def eventInElement(self, event, id): self.freeze() if self.renderer.elementExists(id): return self.getBoundaries(id).contains(event.x,event.y) else: return True # If we have an element with no id or that's un-renderable, we have to assume that we're in bounds def render(self, painter, queryString=None): self.freeze() if queryString == None: self.renderer.render(painter,self.renderer.viewBox()) else: id = self.queryObject(queryString).attr('id') if id == None: self.renderer.render(painter,self.renderer.viewBox()) else: self.renderer.render(painter,id,self.getVisibleBoundaries(id)) def defaultSize(self): self.forceFreeze() return self.renderer.defaultSize() def generateCursor(self, element): cursorPixmap = QPixmap(32,32) hotX = element.getAttribute('__hotX') if hotX == None: hotX = element.width()/2 hotY = element.getAttribute('__hotY') if hotY == None: hotY = element.height()/2 cursorPixmap.fill(Qt.transparent) element.moveTo(0,0) painter = QPainter() painter.begin(cursorPixmap) id = element.getAttribute('id') assert id != None and isinstance(id,str) self.render(painter,"#" + id) painter.end() return QCursor(cursorPixmap,hotX,hotY)