1#############################################################################
2##
3## Copyright (C) 2013 Riverbank Computing Limited.
4## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5## All rights reserved.
6##
7## This file is part of the examples of PyQt.
8##
9## $QT_BEGIN_LICENSE:LGPL$
10## Commercial Usage
11## Licensees holding valid Qt Commercial licenses may use this file in
12## accordance with the Qt Commercial License Agreement provided with the
13## Software or, alternatively, in accordance with the terms contained in
14## a written agreement between you and Nokia.
15##
16## GNU Lesser General Public License Usage
17## Alternatively, this file may be used under the terms of the GNU Lesser
18## General Public License version 2.1 as published by the Free Software
19## Foundation and appearing in the file LICENSE.LGPL included in the
20## packaging of this file.  Please review the following information to
21## ensure the GNU Lesser General Public License version 2.1 requirements
22## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23##
24## In addition, as a special exception, Nokia gives you certain additional
25## rights.  These rights are described in the Nokia Qt LGPL Exception
26## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27##
28## GNU General Public License Usage
29## Alternatively, this file may be used under the terms of the GNU
30## General Public License version 3.0 as published by the Free Software
31## Foundation and appearing in the file LICENSE.GPL included in the
32## packaging of this file.  Please review the following information to
33## ensure the GNU General Public License version 3.0 requirements will be
34## met: http://www.gnu.org/copyleft/gpl.html.
35##
36## If you have questions regarding the use of this file, please contact
37## Nokia at qt-info@nokia.com.
38## $QT_END_LICENSE$
39##
40#############################################################################
41
42
43from PyQt5.QtCore import Qt
44from PyQt5.QtGui import (QColor, QFontMetrics, QImage, QLinearGradient,
45        QPainter, QPen)
46
47from colors import Colors
48from demoitem import DemoItem
49
50
51class HeadingItem(DemoItem):
52    def __init__(self, text, parent=None):
53        super(HeadingItem, self).__init__(parent)
54
55        self.text = text
56        self.noSubPixeling = True
57
58    def createImage(self, transform):
59        sx = min(transform.m11(), transform.m22())
60        sy = max(transform.m22(), sx)
61        fm = QFontMetrics(Colors.headingFont())
62
63        w = fm.width(self.text) + 1
64        h = fm.height()
65        xShadow = 3.0
66        yShadow = 3.0
67
68        image = QImage(int((w + xShadow) * sx), int((h + yShadow) * sy),
69                QImage.Format_ARGB32_Premultiplied)
70        image.fill(QColor(0, 0, 0, 0).rgba())
71        painter = QPainter(image)
72        painter.setFont(Colors.headingFont())
73        painter.scale(sx, sy)
74
75        # Draw shadow.
76        brush_shadow = QLinearGradient(xShadow, yShadow, w, yShadow)
77        brush_shadow.setSpread(QLinearGradient.PadSpread)
78        if Colors.useEightBitPalette:
79            brush_shadow.setColorAt(0.0, QColor(0, 0, 0))
80        else:
81            brush_shadow.setColorAt(0.0, QColor(0, 0, 0, 100))
82        pen_shadow = QPen()
83        pen_shadow.setBrush(brush_shadow)
84        painter.setPen(pen_shadow)
85        painter.drawText(int(xShadow), int(yShadow), int(w), int(h),
86                Qt.AlignLeft, self.text)
87
88        # Draw text.
89        brush_text = QLinearGradient(0, 0, w, w)
90        brush_text.setSpread(QLinearGradient.PadSpread)
91        brush_text.setColorAt(0.0, QColor(255, 255, 255))
92        brush_text.setColorAt(0.2, QColor(255, 255, 255))
93        brush_text.setColorAt(0.5, QColor(190, 190, 190))
94        pen_text = QPen()
95        pen_text.setBrush(brush_text)
96        painter.setPen(pen_text)
97        painter.drawText(0, 0, int(w), int(h), Qt.AlignLeft, self.text)
98
99        return image
100
101    def animationStarted(self, id=0):
102        self.noSubPixeling = False
103
104    def animationStopped(self, id=0):
105        self.noSubPixeling = True
106