def drag_icon(self, cover, multiple): cover = cover.scaledToHeight(120, Qt.SmoothTransformation) if multiple: base_width = cover.width() base_height = cover.height() base = QImage(base_width+21, base_height+21, QImage.Format_ARGB32_Premultiplied) base.fill(QColor(255, 255, 255, 0).rgba()) p = QPainter(base) rect = QRect(20, 0, base_width, base_height) p.fillRect(rect, QColor('white')) p.drawRect(rect) rect.moveLeft(10) rect.moveTop(10) p.fillRect(rect, QColor('white')) p.drawRect(rect) rect.moveLeft(0) rect.moveTop(20) p.fillRect(rect, QColor('white')) p.save() p.setCompositionMode(p.CompositionMode_SourceAtop) p.drawImage(rect.topLeft(), cover) p.restore() p.drawRect(rect) p.end() cover = base return QPixmap.fromImage(cover)
def paintEvent(self, event): ''' @param: event QPaintEvent ''' if not self._use_background: return painter = QPainter(self) rect = self._side_widget.rect().adjusted(0, 0, 1, 0) rect = self.style().visualRect(self.layoutDirection(), self.geometry(), rect) styleHelper.verticalGradient(painter, rect, rect) if not self._background_pixmap.isNull(): pixmap_rect = QRect(self._background_pixmap.rect()) pixmap_rect.moveTo(rect.topLeft()) while pixmap_rect.top() < rect.bottom(): source_rect = QRect(pixmap_rect.intersected(rect)) source_rect.moveTo(0, 0) painter.drawPixmap(pixmap_rect.topLeft(), self._background_pixmap, source_rect) pixmap_rect.moveTop(pixmap_rect.bottom() - 10) painter.setPen(styleHelper.borderColor()) painter.drawLine(rect.topRight(), rect.bottomRight()) # QColor light = styleHelper.sidebarHighlight() painter.setPen(light) painter.drawLine(rect.bottomLeft(), rect.bottomRight())
def drawControl(self, element, option, painter, widget): ''' @param: element ControlElement @param: option QStyleOption @param: painter QPainter @param: widget QWidget ''' v_opt = option if element != self.CE_TabBarTab or not isinstance( v_opt, QStyleOptionTab): QProxyStyle.drawControl(element, option, painter, widget) return rect = v_opt.rect selected = v_opt.state & self.State_Selected vertical_tabs = v_opt.shape == QTabBar.RoundedWest text = v_opt.text if selected: # background painter.save() grad = QLinearGradient(rect.topLeft(), rect.topRight()) grad.setColorAt(0, QColor(255, 255, 255, 140)) grad.setColorAt(0, QColor(255, 255, 255, 210)) painter.fillRect(rect.adjusted(0, 0, 0, -1), grad) painter.restore() # shadows painter.setPen(QColor(0, 0, 0, 110)) painter.drawLine(rect.topLeft() + QPoint(1, -1), rect.topRight() - QPoint(0, 1)) painter.drawLine(rect.bottomLeft(), rect.bottomRight()) painter.setPen(QColor(0, 0, 0, 40)) painter.drawLine(rect.topLeft(), rect.bottomLeft()) # highlights painter.setPen(QColor(255, 255, 255, 50)) painter.drawLine(rect.topLeft() + QPoint(0, -2), rect.topRight() - QPoint(0, 2)) painter.drawLine(rect.bottomLeft() + QPoint(0, 1), rect.bottomRight() + QPoint(0, 1)) painter.setPen(QColor(255, 255, 255, 40)) painter.drawLine(rect.topLeft() + QPoint(0, 0), rect.topRight()) painter.drawLine(rect.topRight() + QPoint(0, 1), rect.bottomRight() - QPoint(0, 1)) painter.drawLine(rect.bottomLeft() + QPoint(0, -1), rect.bottomRight() - QPoint(0, 1)) m = QTransform() if vertical_tabs: m = QTransform.fromTranslate(rect.left(), rect.bottom()) m.rotate(-90) else: m = QTransform.fromTranslate(rect.left(), rect.top()) draw_rect = QRect(QPoint(0, 0), m.mapRect(rect).size()) painter.save() painter.setTransform(m) icon_rect = QRect(QPoint(8, 0), v_opt.iconSize) text_rect = QRect(icon_rect.topRight() + QPoint(4, 0), draw_rect.size()) text_rect.setRight(draw_rect.width()) icon_rect.translate(0, (draw_rect.height() - icon_rect.height()) / 2) boldFont = QFont(painter.font()) boldFont.setPointSizeF(styleHelper.sidebarFontSize()) boldFont.setBold(True) painter.setFont(boldFont) painter.setPen(selected and QColor(255, 255, 255, 160) or QColor(0, 0, 0, 110)) textFlags = Qt.AlignHCenter | Qt.AlignVCenter painter.drawText(text_rect, textFlags, text) painter.setPen(selected and QColor(60, 60, 60) or styleHelper.panelTextColor()) if widget: fader_key = 'tab_' + text + '_fader' animation_key = 'tab_' + text + '_animation' tab_hover = widget.property('tab_hover') # int fader = widget.property(fader_key) # QPropertyAnimation animation = widget.property(animation_key) if not animation: mut_widget = widget fader = 0 mut_widget.setProperty(fader_key, fader) animation = QPropertyAnimation(mut_widget, fader_key, mut_widget) animation.valueChanged.connect(mut_widget.update) mut_widget.setProperty(animation_key, animation) if text == tab_hover: if animation.state( ) != QAbstractAnimation.Running and fader != 40: animation.stop() animation.setDuration(80) animation.setEndValue(40) animation.start() else: if animation.state( ) != QAbstractAnimation.Running and fader != 0: animation.stop() animation.setDuration(160) animation.setEndValue(0) animation.start() if not selected: painter.save() painter.fillRect(draw_rect, QColor(255, 255, 255, fader)) painter.setPen(QPen(QColor(255, 255, 255, fader), 1.0)) painter.drawLine( draw_rect.topLeft(), vertical_tabs and draw_rect.bottomLeft() or draw_rect.topRight()) painter.drawLine( draw_rect.bottomRight(), vertical_tabs and draw_rect.topRight() or draw_rect.bottomLeft()) painter.restore() if selected: iconMode = QIcon.Selected else: iconMode = QIcon.Normal styleHelper.drawIconWithShadow(v_opt.icon, icon_rect, painter, iconMode) painter.drawText(text_rect.translated(0, -1), textFlags, text) painter.restore()
def drawIconWithShadow(self, icon, rect, painter, iconMode, radius=3, color=QColor(0, 0, 0, 130), offset=QPoint(1, -2)): ''' @brief: Draw a cached pixmap with shadow @param: icon QIcon @param: rect QRect @param: painter QPainter @param: iconMode QIcon.Mode @param: radius int @param: color QColor @param: offset QPoint ''' cache = QPixmap() pixmapName = 'icon %s %s %s' % (icon.cacheKey(), iconMode, rect.height()) cache = QPixmapCache.find(pixmapName) if not cache: px = icon.pixmap(rect.size(), iconMode) px.setDevicePixelRatio(gVar.app.devicePixelRatio()) cache = QPixmap(px.size() + QSize(radius * 2, radius * 2)) cache.setDevicePixelRatio(px.devicePixelRatioF()) cache.fill(Qt.transparent) cachePainter = QPainter(cache) # Draw shadow tmp = QImage(px.size() + QSize(radius * 2, radius * 2 + 1), QImage.Format_ARGB32_Premultiplied) tmp.setDevicePixelRatio(px.devicePixelRatioF()) tmp.fill(Qt.transparent) tmpPainter = QPainter(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_Source) tmpPainter.drawPixmap(QPoint(radius, radius), px) tmpPainter.end() # blur the alpha channel blurred = QImage(tmp.size(), QImage.Format_ARGB32_Premultiplied) blurred.fill(Qt.transparent) blurPainter = QPainter(blurred) # TODO: #qt_blurImage(blurPainter, tmp, radius, False, True) blurPainter.end() tmp = blurred # blacken the image... tmpPainter.begin(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_SourceIn) tmpPainter.fillRect(tmp.rect(), color) tmpPainter.end() tmpPainter.begin(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_SourceIn) tmpPainter.fillRect(tmp.rect(), color) tmpPainter.end() # draw the blurred drop shadow... cachePainter.drawImage( QRect(0, 0, cache.rect().width() / cache.devicePixelRatioF(), cache.rect().height() / cache.devicePixelRatioF()), tmp) # Draw the actual pixmap... cachePainter.drawPixmap(QPoint(radius, radius) + offset, px) if self.usePixmapCache(): QPixmapCache.insert(pixmapName, cache) sip.delete(cachePainter) sip.delete(tmpPainter) sip.delete(blurPainter) targetRect = QRect(cache.rect()) targetRect.setWidth(cache.rect().width() / cache.devicePixelRatioF()) targetRect.setHeight(cache.rect().height() / cache.devicePixelRatioF()) targetRect.moveCenter(rect.center()) painter.drawPixmap(targetRect.topLeft() - offset, cache)