1 | // Copyright (C) 2016 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 "qtgradientmanager.h" |
5 | #include <QtGui/QPixmap> |
6 | #include <QtCore/QMetaEnum> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | QtGradientManager::QtGradientManager(QObject *parent) |
11 | : QObject(parent) |
12 | { |
13 | } |
14 | |
15 | QMap<QString, QGradient> QtGradientManager::gradients() const |
16 | { |
17 | return m_idToGradient; |
18 | } |
19 | |
20 | QString QtGradientManager::uniqueId(const QString &id) const |
21 | { |
22 | if (!m_idToGradient.contains(key: id)) |
23 | return id; |
24 | |
25 | QString base = id; |
26 | while (base.size() > 0 && base.at(i: base.size() - 1).isDigit()) |
27 | base = base.left(n: base.size() - 1); |
28 | QString newId = base; |
29 | int counter = 0; |
30 | while (m_idToGradient.contains(key: newId)) { |
31 | ++counter; |
32 | newId = base + QString::number(counter); |
33 | } |
34 | return newId; |
35 | } |
36 | |
37 | QString QtGradientManager::addGradient(const QString &id, const QGradient &gradient) |
38 | { |
39 | QString newId = uniqueId(id); |
40 | |
41 | m_idToGradient[newId] = gradient; |
42 | |
43 | emit gradientAdded(id: newId, gradient); |
44 | |
45 | return newId; |
46 | } |
47 | |
48 | void QtGradientManager::removeGradient(const QString &id) |
49 | { |
50 | if (!m_idToGradient.contains(key: id)) |
51 | return; |
52 | |
53 | emit gradientRemoved(id); |
54 | |
55 | m_idToGradient.remove(key: id); |
56 | } |
57 | |
58 | void QtGradientManager::renameGradient(const QString &id, const QString &newId) |
59 | { |
60 | if (!m_idToGradient.contains(key: id)) |
61 | return; |
62 | |
63 | if (newId == id) |
64 | return; |
65 | |
66 | QString changedId = uniqueId(id: newId); |
67 | QGradient gradient = m_idToGradient.value(key: id); |
68 | |
69 | emit gradientRenamed(id, newId: changedId); |
70 | |
71 | m_idToGradient.remove(key: id); |
72 | m_idToGradient[changedId] = gradient; |
73 | } |
74 | |
75 | void QtGradientManager::changeGradient(const QString &id, const QGradient &newGradient) |
76 | { |
77 | if (!m_idToGradient.contains(key: id)) |
78 | return; |
79 | |
80 | if (m_idToGradient.value(key: id) == newGradient) |
81 | return; |
82 | |
83 | emit gradientChanged(id, newGradient); |
84 | |
85 | m_idToGradient[id] = newGradient; |
86 | } |
87 | |
88 | void QtGradientManager::clear() |
89 | { |
90 | const QMap<QString, QGradient> grads = gradients(); |
91 | for (auto it = grads.cbegin(), end = grads.cend(); it != end; ++it) |
92 | removeGradient(id: it.key()); |
93 | } |
94 | |
95 | QT_END_NAMESPACE |
96 |