Get rid of other uses of QLabels to draw icons

This commit is contained in:
Kovid Goyal 2016-11-02 18:06:00 +05:30
parent d3599a92fe
commit c89a61234e
3 changed files with 19 additions and 20 deletions

View File

@ -4,11 +4,12 @@ __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'
from PyQt5.Qt import (
QDialog, Qt, QPixmap, QIcon, QSize, QVBoxLayout, QHBoxLayout, QLabel,
QCheckBox, QDialogButtonBox)
QDialog, Qt, QIcon, QVBoxLayout, QHBoxLayout, QLabel, QCheckBox, QDialogButtonBox
)
from calibre import confirm_config_name
from calibre.gui2 import dynamic
from calibre.gui2.dialogs.message_box import Icon
class Dialog(QDialog):
@ -22,16 +23,14 @@ class Dialog(QDialog):
self.h = h = QHBoxLayout()
l.addLayout(h)
self.label = la = QLabel(self)
la.setScaledContents(True), la.setMaximumSize(QSize(96, 96)), la.setMinimumSize(QSize(96, 96))
la.setPixmap(QPixmap(I(icon)))
la.setObjectName("label")
self.icon_widget = Icon(self)
self.icon_widget.set_icon(QIcon(I(icon)))
self.msg = m = QLabel(self)
m.setMinimumWidth(300), m.setWordWrap(True), m.setObjectName("msg")
m.setMinimumWidth(350), m.setWordWrap(True), m.setObjectName("msg")
m.setText(msg)
h.addWidget(la), h.addSpacing(10), h.addWidget(m)
h.addWidget(self.icon_widget), h.addSpacing(10), h.addWidget(m)
self.again = a = QCheckBox((confirm_msg or _("&Show this warning again")), self)
a.setChecked(True), a.setObjectName("again")

View File

@ -19,23 +19,25 @@ from calibre.gui2 import gprefs
class Icon(QWidget):
def __init__(self, parent=None):
def __init__(self, parent=None, size=None):
QWidget.__init__(self, parent)
self.pixmap = None
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
self.size = size or 64
def set_icon(self, qicon):
self.pixmap = qicon.pixmap(64, 64)
self.pixmap = qicon.pixmap(self.size, self.size)
self.update()
def sizeHint(self):
return QSize(64, 64)
return QSize(self.size, self.size)
def paintEvent(self, ev):
if self.pixmap is not None:
x = (self.width() - 64) // 2
y = (self.height() - 64) // 2
x = (self.width() - self.size) // 2
y = (self.height() - self.size) // 2
p = QPainter(self)
p.drawPixmap(x, y, 64, 64, self.pixmap)
p.drawPixmap(x, y, self.size, self.size, self.pixmap)
class MessageBox(QDialog): # {{{

View File

@ -18,6 +18,7 @@ from PyQt5.Qt import (
from calibre.constants import __appname__, __version__, islinux
from calibre.gui2 import (gprefs, min_available_height, available_width,
show_restart_warning)
from calibre.gui2.dialogs.message_box import Icon
from calibre.gui2.preferences import init_gui, AbortCommit, get_plugin
from calibre.customize.ui import preferences_plugins
@ -76,9 +77,8 @@ class TitleBar(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.l = l = QHBoxLayout(self)
self.icon = i = QLabel('')
i.setScaledContents(True)
l.addWidget(i), i.setFixedSize(QSize(ICON_SIZE, ICON_SIZE))
self.icon = Icon(self, size=ICON_SIZE)
l.addWidget(self.icon)
self.title = QLabel('')
self.title.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
l.addWidget(self.title)
@ -91,8 +91,7 @@ class TitleBar(QWidget):
self.show_msg()
def show_plugin(self, plugin=None):
self.pmap = (QIcon(I('lt.png') if plugin is None else plugin.icon)).pixmap(ICON_SIZE, ICON_SIZE)
self.icon.setPixmap(self.pmap)
self.icon.set_icon(QIcon(I('lt.png') if plugin is None else plugin.icon))
self.title.setText('<h1>' + (_('Preferences') if plugin is None else plugin.gui_name))
def show_msg(self, msg=None):
@ -421,4 +420,3 @@ if __name__ == '__main__':
p = Preferences(gui)
p.exec_()
gui.shutdown()