1 | // Copyright (C) 2017 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickcolorimage_p.h" |
5 | |
6 | #include <QtQuick/private/qquickimagebase_p_p.h> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | QQuickColorImage::QQuickColorImage(QQuickItem *parent) |
11 | : QQuickImage(parent) |
12 | { |
13 | } |
14 | |
15 | QColor QQuickColorImage::color() const |
16 | { |
17 | return m_color; |
18 | } |
19 | |
20 | void QQuickColorImage::setColor(const QColor &color) |
21 | { |
22 | if (m_color == color) |
23 | return; |
24 | |
25 | m_color = color; |
26 | if (isComponentComplete()) |
27 | load(); |
28 | emit colorChanged(); |
29 | } |
30 | |
31 | void QQuickColorImage::resetColor() |
32 | { |
33 | setColor(Qt::transparent); |
34 | } |
35 | |
36 | QColor QQuickColorImage::defaultColor() const |
37 | { |
38 | return m_defaultColor; |
39 | } |
40 | |
41 | void QQuickColorImage::setDefaultColor(const QColor &color) |
42 | { |
43 | if (m_defaultColor == color) |
44 | return; |
45 | |
46 | m_defaultColor = color; |
47 | emit defaultColorChanged(); |
48 | } |
49 | |
50 | void QQuickColorImage::resetDefaultColor() |
51 | { |
52 | setDefaultColor(Qt::transparent); |
53 | } |
54 | |
55 | void QQuickColorImage::pixmapChange() |
56 | { |
57 | QQuickImage::pixmapChange(); |
58 | if (m_color.alpha() > 0 && m_color != m_defaultColor) { |
59 | QQuickImageBasePrivate *d = static_cast<QQuickImageBasePrivate *>(QQuickItemPrivate::get(item: this)); |
60 | QImage image = d->pix.image(); |
61 | if (!image.isNull()) { |
62 | QPainter painter(&image); |
63 | painter.setCompositionMode(QPainter::CompositionMode_SourceIn); |
64 | painter.fillRect(image.rect(), color: m_color); |
65 | d->pix.setImage(image); |
66 | } |
67 | } |
68 | } |
69 | |
70 | QT_END_NAMESPACE |
71 | |
72 | #include "moc_qquickcolorimage_p.cpp" |
73 |