1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Quick Extras module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qquickpicture_p.h" |
41 | |
42 | #if QT_CONFIG(picture) |
43 | |
44 | #include <QQmlFile> |
45 | #include <QDebug> |
46 | |
47 | /*! |
48 | \qmltype Picture |
49 | \inherits QQuickPaintedItem |
50 | \inqmlmodule QtQuick.Extras |
51 | \since QtQuick.Extras 1.4 |
52 | \ingroup extras |
53 | \ingroup extras-non-interactive |
54 | \brief An indicator that displays a colorized QPicture icon. |
55 | |
56 | Picture displays icons in a scalable vector format. It can also colorize |
57 | the icons via the \l color property. |
58 | |
59 | The icon to display is set with the \l source property. |
60 | |
61 | For example, if you have access to the ISO 7000 icons that come with Qt |
62 | Enterprise, you can specify the following URL: |
63 | |
64 | \code |
65 | "qrc:/iso-icons/iso_grs_7000_4_0001.dat" |
66 | \endcode |
67 | |
68 | Due to the |
69 | \l {http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=65977} |
70 | {large selection of icons} available in this package, it is advisable to |
71 | use Qt Creator's Qt Quick Designer tool to browse and select icons, as this |
72 | property will then be set automatically. |
73 | */ |
74 | |
75 | QQuickPicture::QQuickPicture(QQuickItem *parent) |
76 | : QQuickPaintedItem(parent) |
77 | { |
78 | const qreal defaultFontHeight = QFontMetricsF(QFont()).height(); |
79 | setImplicitWidth(defaultFontHeight * 4); |
80 | setImplicitHeight(defaultFontHeight * 4); |
81 | } |
82 | |
83 | QQuickPicture::~QQuickPicture() |
84 | { |
85 | } |
86 | |
87 | void QQuickPicture::paint(QPainter *painter) |
88 | { |
89 | const QSize size = boundingRect().size().toSize(); |
90 | // Don't want the scale to apply to the fill. |
91 | painter->save(); |
92 | painter->scale(sx: qreal(size.width()) / mPicture.boundingRect().width(), |
93 | sy: qreal(size.height()) / mPicture.boundingRect().height()); |
94 | painter->drawPicture(x: 0, y: 0, p: mPicture); |
95 | painter->restore(); |
96 | |
97 | if (mColor.isValid()) { |
98 | painter->setCompositionMode(QPainter::CompositionMode_SourceIn); |
99 | painter->fillRect(x: 0, y: 0, w: size.width(), h: size.height(), b: mColor); |
100 | } |
101 | } |
102 | |
103 | /*! |
104 | \qmlproperty url Picture::source |
105 | |
106 | This property specifies the URL of the icon to use. The URL must point to a |
107 | local file that contains \l QPicture data. For example: |
108 | |
109 | \code |
110 | "mypicture.dat" |
111 | \endcode |
112 | */ |
113 | QUrl QQuickPicture::source() const |
114 | { |
115 | return mSource; |
116 | } |
117 | |
118 | void QQuickPicture::setSource(const QUrl &source) |
119 | { |
120 | if (mSource != source) { |
121 | mSource = source; |
122 | const QString fileName = QQmlFile::urlToLocalFileOrQrc(source); |
123 | if (!mPicture.load(fileName)) { |
124 | qWarning().nospace() << "Failed to load " << fileName << "; does it exist?" ; |
125 | mPicture = QPicture(); |
126 | } |
127 | |
128 | setImplicitWidth(mPicture.boundingRect().width()); |
129 | setImplicitHeight(mPicture.boundingRect().height()); |
130 | |
131 | update(); |
132 | emit sourceChanged(); |
133 | } |
134 | } |
135 | |
136 | /*! |
137 | \qmlproperty color Picture::color |
138 | |
139 | This property specifies the color of the indicator. |
140 | |
141 | The default value is \c "black". |
142 | */ |
143 | QColor QQuickPicture::color() const |
144 | { |
145 | return mColor; |
146 | } |
147 | |
148 | void QQuickPicture::setColor(const QColor &color) |
149 | { |
150 | if (mColor != color) { |
151 | mColor = color; |
152 | update(); |
153 | emit colorChanged(); |
154 | } |
155 | } |
156 | |
157 | void QQuickPicture::resetColor() |
158 | { |
159 | setColor(QColor()); |
160 | } |
161 | |
162 | #endif // QT_CONFIG(picture) |
163 | |