1#!/usr/local/bin/python3.8 2# -*- coding: utf-8 -*- 3 4# Pixmap Button, a custom Qt widget 5# Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com> 6# 7# This program is free software; you can redistribute it and/or 8# modify it under the terms of the GNU General Public License as 9# published by the Free Software Foundation; either version 2 of 10# the License, or any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# For a full copy of the GNU General Public License see the doc/GPL.txt file. 18 19# ------------------------------------------------------------------------------------------------------------ 20# Imports (Global) 21 22from PyQt5.QtCore import QPointF, QRectF 23from PyQt5.QtGui import QColor, QFont, QPainter, QPixmap 24from PyQt5.QtWidgets import QPushButton 25 26# ------------------------------------------------------------------------------------------------------------ 27# Widget Class 28 29class PixmapButton(QPushButton): 30 def __init__(self, parent): 31 QPushButton.__init__(self, parent) 32 33 self.fPixmapNormal = QPixmap() 34 self.fPixmapDown = QPixmap() 35 self.fPixmapHover = QPixmap() 36 self.fPixmapRect = QRectF(0, 0, 0, 0) 37 38 self.fIsHovered = False 39 40 self.fTopText = "" 41 self.fTopTextColor = QColor() 42 self.fTopTextFont = QFont() 43 44 self.setText("") 45 46 def setPixmaps(self, normal, down, hover): 47 self.fPixmapNormal.load(normal) 48 self.fPixmapDown.load(down) 49 self.fPixmapHover.load(hover) 50 51 width = self.fPixmapNormal.width() 52 height = self.fPixmapNormal.height() 53 54 self.fPixmapRect = QRectF(0, 0, width, height) 55 56 self.setMinimumSize(width, height) 57 self.setMaximumSize(width, height) 58 59 def setTopText(self, text, color, font): 60 self.fTopText = text 61 self.fTopTextColor = color 62 self.fTopTextFont = font 63 64 def enterEvent(self, event): 65 self.fIsHovered = True 66 QPushButton.enterEvent(self, event) 67 68 def leaveEvent(self, event): 69 self.fIsHovered = False 70 QPushButton.leaveEvent(self, event) 71 72 def paintEvent(self, event): 73 painter = QPainter(self) 74 event.accept() 75 76 if not self.isEnabled(): 77 painter.save() 78 painter.setOpacity(0.2) 79 painter.drawPixmap(self.fPixmapRect, self.fPixmapNormal, self.fPixmapRect) 80 painter.restore() 81 82 elif self.isChecked() or self.isDown(): 83 painter.drawPixmap(self.fPixmapRect, self.fPixmapDown, self.fPixmapRect) 84 85 elif self.fIsHovered: 86 painter.drawPixmap(self.fPixmapRect, self.fPixmapHover, self.fPixmapRect) 87 88 else: 89 painter.drawPixmap(self.fPixmapRect, self.fPixmapNormal, self.fPixmapRect) 90 91 if not self.fTopText: 92 return 93 94 painter.save() 95 painter.setPen(self.fTopTextColor) 96 painter.setBrush(self.fTopTextColor) 97 painter.setFont(self.fTopTextFont) 98 painter.drawText(QPointF(10, 16), self.fTopText) 99 painter.restore() 100