| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from qt.core import QBrush, QColor, QFont, QFontMetrics, QPainter, QPen, QPixmap, QSplashScreen, Qt |
| 5 |
|
| 6 |
from calibre.constants import __appname__, numeric_version |
| 7 |
from calibre.gui2 import qapplication_or_fail |
| 8 |
from calibre.utils.localization import _ |
| 9 |
from calibre.utils.monotonic import monotonic |
| 10 |
from calibre.utils.resources import get_image_path as I |
| 11 |
|
| 12 |
|
| 13 |
class SplashScreen(QSplashScreen): |
| 14 |
TITLE_SIZE = 20 |
| 15 |
BODY_SIZE = 12 |
| 16 |
FOOTER_SIZE = 9 |
| 17 |
LOGO_SIZE = 96 |
| 18 |
WIDTH = 550 |
| 19 |
|
| 20 |
def __init__(self, develop=False): |
| 21 |
self.drawn_once = False |
| 22 |
self.develop = develop |
| 23 |
self.title_font = f = QFont() |
| 24 |
f.setPointSize(self.TITLE_SIZE) |
| 25 |
f.setBold(True) |
| 26 |
self.title_height = QFontMetrics(f).lineSpacing() + 2 |
| 27 |
self.body_font = f = QFont() |
| 28 |
f.setPointSize(self.BODY_SIZE) |
| 29 |
self.line_height = QFontMetrics(f).lineSpacing() |
| 30 |
self.total_height = max(self.LOGO_SIZE, self.title_height + 3 * self.line_height) |
| 31 |
self.num_font = f = QFont() |
| 32 |
f.setPixelSize(self.total_height) |
| 33 |
f.setItalic(True), f.setBold(True) |
| 34 |
f = QFontMetrics(f) |
| 35 |
self.num_ch = str(max(3, numeric_version[0])) |
| 36 |
self.footer_font = f = QFont() |
| 37 |
f.setPointSize(self.FOOTER_SIZE) |
| 38 |
f.setItalic(True) |
| 39 |
self.dpr = qapplication_or_fail().devicePixelRatio() |
| 40 |
self.pmap = QPixmap(I('library.png', allow_user_override=False)) |
| 41 |
self.pmap.setDevicePixelRatio(self.dpr) |
| 42 |
self.pmap = self.pmap.scaled( |
| 43 |
int(self.dpr * self.LOGO_SIZE), |
| 44 |
int(self.dpr * self.LOGO_SIZE), |
| 45 |
transformMode=Qt.TransformationMode.SmoothTransformation, |
| 46 |
) |
| 47 |
self.light_brush = QBrush(QColor('#F6F3E9')) |
| 48 |
self.dark_brush = QBrush(QColor('#39322B')) |
| 49 |
pmap = QPixmap(int(self.WIDTH * self.dpr), int(self.total_height * self.dpr)) |
| 50 |
pmap.setDevicePixelRatio(self.dpr) |
| 51 |
pmap.fill(Qt.GlobalColor.transparent) |
| 52 |
QSplashScreen.__init__(self, pmap) |
| 53 |
self.setWindowTitle(__appname__) |
| 54 |
|
| 55 |
def drawContents(self, painter): |
| 56 |
self.drawn_once = True |
| 57 |
painter.save() |
| 58 |
painter.setRenderHint(QPainter.RenderHint.TextAntialiasing, True) |
| 59 |
painter.setRenderHint(QPainter.RenderHint.Antialiasing, True) |
| 60 |
pw = self.LOGO_SIZE |
| 61 |
height = max(pw, self.total_height) |
| 62 |
width = self.width() |
| 63 |
|
| 64 |
|
| 65 |
y = (self.height() - height) // 2 |
| 66 |
bottom = y + height |
| 67 |
painter.fillRect(0, y, width, height, self.light_brush) |
| 68 |
painter.fillRect(0, y, width, self.title_height, self.dark_brush) |
| 69 |
painter.fillRect(0, y, pw, height, self.dark_brush) |
| 70 |
dy = (height - self.LOGO_SIZE) // 2 |
| 71 |
painter.drawPixmap(0, y + dy, self.pmap) |
| 72 |
|
| 73 |
|
| 74 |
painter.setFont(self.num_font) |
| 75 |
num_width = painter.boundingRect(0, 0, 0, 0, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, self.num_ch).width() + 12 |
| 76 |
num_x = width - num_width |
| 77 |
painter.setPen(QPen(QColor('#d6b865'))) |
| 78 |
painter.drawText(num_x, y, num_width, height, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, self.num_ch) |
| 79 |
|
| 80 |
|
| 81 |
x = pw + 10 |
| 82 |
width -= num_width + 5 + x |
| 83 |
painter.setFont(self.title_font) |
| 84 |
painter.drawText( |
| 85 |
x, |
| 86 |
y, |
| 87 |
width, |
| 88 |
self.title_height, |
| 89 |
Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter | Qt.TextFlag.TextSingleLine, |
| 90 |
'CALIBRE', |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
y += self.title_height + 5 |
| 95 |
painter.setPen(QPen(self.dark_brush.color())) |
| 96 |
painter.setFont(self.body_font) |
| 97 |
br = painter.drawText( |
| 98 |
x, |
| 99 |
y, |
| 100 |
width, |
| 101 |
self.line_height, |
| 102 |
Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter | Qt.TextFlag.TextSingleLine, |
| 103 |
_('Starting up, please wait...'), |
| 104 |
) |
| 105 |
starting_up_bottom = br.bottom() |
| 106 |
|
| 107 |
|
| 108 |
m = self.message() |
| 109 |
if m and m.strip(): |
| 110 |
painter.setFont(self.footer_font) |
| 111 |
b = max(starting_up_bottom + 5, bottom - self.line_height) |
| 112 |
painter.drawText( |
| 113 |
x, |
| 114 |
b, |
| 115 |
width, |
| 116 |
self.line_height, |
| 117 |
Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop | Qt.TextFlag.TextSingleLine, |
| 118 |
m, |
| 119 |
) |
| 120 |
|
| 121 |
painter.restore() |
| 122 |
|
| 123 |
def show_message(self, msg): |
| 124 |
self.showMessage(msg) |
| 125 |
self.wait_for_draw() |
| 126 |
|
| 127 |
def wait_for_draw(self): |
| 128 |
|
| 129 |
self.drawn_once = False |
| 130 |
st = monotonic() |
| 131 |
while not self.drawn_once and (monotonic() - st < 0.1): |
| 132 |
qapplication_or_fail().processEvents() |
| 133 |
|
| 134 |
def keyPressEvent(self, a0): |
| 135 |
if not self.develop: |
| 136 |
return QSplashScreen.keyPressEvent(self, a0) |
| 137 |
a0.accept() |
| 138 |
qapplication_or_fail().exit() |
| 139 |
|
| 140 |
|
| 141 |
def main(): |
| 142 |
from calibre.gui2 import Application |
| 143 |
|
| 144 |
app = Application([]) |
| 145 |
spl = SplashScreen(develop=True) |
| 146 |
spl.show() |
| 147 |
spl.show_message('Testing the splash screen message...') |
| 148 |
app.exec() |
| 149 |
|
| 150 |
|
| 151 |
if __name__ == '__main__': |
| 152 |
main() |