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 QPoint
44from PyQt5.QtGui import QColor, QImage, QLinearGradient, QPainter
45
46from colors import Colors
47from demoitem import DemoItem
48
49
50class ImageItem(DemoItem):
51    def __init__(self, image, maxWidth, maxHeight, parent=None, adjustSize=False, scale=1.0):
52        super(ImageItem, self).__init__(parent)
53
54        self.image = image
55        self.maxWidth = maxWidth
56        self.maxHeight = maxHeight
57        self.adjustSize = adjustSize
58        self.scale = scale
59
60    def createImage(self, transform):
61        original = QImage(self.image)
62        if original.isNull():
63            return original
64
65        size = transform.map(QPoint(self.maxWidth, self.maxHeight))
66        w = size.x()
67        h = size.y()
68
69        # Optimization: if image is smaller than maximum allowed size, just
70        # return the loaded image.
71        if original.size().height() <= h and original.size().width() <= w and not self.adjustSize and self.scale == 1:
72            return original
73
74        # Calculate what the size of the final image will be.
75        w = min(w, float(original.size().width()) * self.scale)
76        h = min(h, float(original.size().height()) * self.scale)
77
78        adjustx = 1.0
79        adjusty = 1.0
80        if self.adjustSize:
81            adjustx = min(transform.m11(), transform.m22())
82            adjusty = max(transform.m22(), adjustx)
83            w *= adjustx
84            h *= adjusty
85
86        # Create a new image with correct size, and draw original on it.
87        image = QImage(int(w + 2), int(h + 2),
88                QImage.Format_ARGB32_Premultiplied)
89        image.fill(QColor(0, 0, 0, 0).rgba())
90        painter = QPainter(image)
91        painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
92        if self.adjustSize:
93            painter.scale(adjustx, adjusty)
94        if self.scale != 1:
95            painter.scale(self.scale, self.scale)
96        painter.drawImage(0, 0, original)
97
98        if not self.adjustSize:
99            # Blur out edges.
100            blur = 30
101
102            if h < original.height():
103                brush1 = QLinearGradient(0, h - blur, 0, h)
104                brush1.setSpread(QLinearGradient.PadSpread)
105                brush1.setColorAt(0.0, QColor(0, 0, 0, 0))
106                brush1.setColorAt(1.0, Colors.sceneBg1)
107                painter.fillRect(0, int(h) - blur, original.width(), int(h),
108                        brush1)
109
110            if w < original.width():
111                brush2 = QLinearGradient(w - blur, 0, w, 0)
112                brush2.setSpread(QLinearGradient.PadSpread)
113                brush2.setColorAt(0.0, QColor(0, 0, 0, 0))
114                brush2.setColorAt(1.0, Colors.sceneBg1)
115                painter.fillRect(int(w) - blur, 0, int(w), original.height(),
116                        brush2)
117
118        return image
119