| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "ddciiconpalette.h" |
| 6 |
|
| 7 |
#include <QDebug> |
| 8 |
#include <QUrl> |
| 9 |
#include <QUrlQuery> |
| 10 |
#include <QPalette> |
| 11 |
|
| 12 |
DGUI_BEGIN_NAMESPACE |
| 13 |
|
| 14 |
DDciIconPalette::DDciIconPalette(QColor foreground, QColor background, QColor highlight, QColor highlightForeground) |
| 15 |
{ |
| 16 |
colors.reserve(PaletteCount); |
| 17 |
colors.insert(Foreground, foreground); |
| 18 |
colors.insert(Background, background); |
| 19 |
colors.insert(HighlightForeground, highlightForeground); |
| 20 |
colors.insert(Highlight, highlight); |
| 21 |
} |
| 22 |
|
| 23 |
bool DDciIconPalette::operator==(const DDciIconPalette &other) const |
| 24 |
{ |
| 25 |
for (int i = Foreground; i < PaletteCount; ++i) { |
| 26 |
if (colors.at(i) != other.colors.at(i)) |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
bool DDciIconPalette::operator!=(const DDciIconPalette &other) const |
| 34 |
{ |
| 35 |
return !(*this == other); |
| 36 |
} |
| 37 |
|
| 38 |
QColor DDciIconPalette::foreground() const |
| 39 |
{ |
| 40 |
return colors.at(Foreground); |
| 41 |
} |
| 42 |
|
| 43 |
void DDciIconPalette::setForeground(const QColor &foreground) |
| 44 |
{ |
| 45 |
colors[Foreground] = foreground; |
| 46 |
} |
| 47 |
|
| 48 |
QColor DDciIconPalette::background() const |
| 49 |
{ |
| 50 |
return colors[Background]; |
| 51 |
} |
| 52 |
|
| 53 |
void DDciIconPalette::setBackground(const QColor &background) |
| 54 |
{ |
| 55 |
colors[Background] = background; |
| 56 |
} |
| 57 |
|
| 58 |
QColor DDciIconPalette::highlightForeground() const |
| 59 |
{ |
| 60 |
return colors[HighlightForeground]; |
| 61 |
} |
| 62 |
|
| 63 |
void DDciIconPalette::setHighlightForeground(const QColor &highlightForeground) |
| 64 |
{ |
| 65 |
colors[HighlightForeground] = highlightForeground; |
| 66 |
} |
| 67 |
|
| 68 |
QColor DDciIconPalette::highlight() const |
| 69 |
{ |
| 70 |
return colors[Highlight]; |
| 71 |
} |
| 72 |
|
| 73 |
void DDciIconPalette::setHighlight(const QColor &highlight) |
| 74 |
{ |
| 75 |
colors[Highlight] = highlight; |
| 76 |
} |
| 77 |
|
| 78 |
static QString _d_dciIconPaletteHost() |
| 79 |
{ |
| 80 |
return QLatin1String("dtk.dci.palette"); |
| 81 |
} |
| 82 |
|
| 83 |
QString DDciIconPalette::convertToString(const DDciIconPalette &palette) |
| 84 |
{ |
| 85 |
QUrl url; |
| 86 |
url.setHost(_d_dciIconPaletteHost()); |
| 87 |
QUrlQuery query; |
| 88 |
if (palette.foreground().isValid()) |
| 89 |
query.addQueryItem(QLatin1String("foreground"), palette.foreground().name(QColor::HexArgb)); |
| 90 |
if (palette.background().isValid()) |
| 91 |
query.addQueryItem(QLatin1String("background"), palette.background().name(QColor::HexArgb)); |
| 92 |
if (palette.highlight().isValid()) |
| 93 |
query.addQueryItem(QLatin1String("highlight"), palette.highlight().name(QColor::HexArgb)); |
| 94 |
if (palette.highlightForeground().isValid()) |
| 95 |
query.addQueryItem(QLatin1String("highlightForeground"), palette.highlightForeground().name(QColor::HexArgb)); |
| 96 |
|
| 97 |
url.setQuery(query); |
| 98 |
return url.toString(); |
| 99 |
} |
| 100 |
|
| 101 |
DDciIconPalette DDciIconPalette::convertFromString(const QString &data) |
| 102 |
{ |
| 103 |
QUrl url(data); |
| 104 |
if (url.host() != _d_dciIconPaletteHost()) |
| 105 |
return DDciIconPalette(); |
| 106 |
|
| 107 |
QUrlQuery query(url.toString(QUrl::RemoveScheme | QUrl::RemoveAuthority).mid(1)); |
| 108 |
QColor foreground; |
| 109 |
if (query.hasQueryItem(QLatin1String("foreground"))) |
| 110 |
foreground = query.queryItemValue(QLatin1String("foreground")); |
| 111 |
QColor background; |
| 112 |
if (query.hasQueryItem(QLatin1String("background"))) |
| 113 |
background = query.queryItemValue(QLatin1String("background")); |
| 114 |
QColor highlight; |
| 115 |
if (query.hasQueryItem(QLatin1String("highlight"))) |
| 116 |
highlight = query.queryItemValue(QLatin1String("highlight")); |
| 117 |
QColor highlightForeground; |
| 118 |
if (query.hasQueryItem(QLatin1String("highlightForeground"))) |
| 119 |
highlightForeground = query.queryItemValue(QLatin1String("highlightForeground")); |
| 120 |
return DDciIconPalette(foreground, background, highlight, highlightForeground); |
| 121 |
} |
| 122 |
|
| 123 |
DDciIconPalette DDciIconPalette::fromQPalette(const QPalette &pa) |
| 124 |
{ |
| 125 |
return DDciIconPalette(pa.windowText().color(), pa.window().color(), |
| 126 |
pa.highlight().color(), pa.highlightedText().color()); |
| 127 |
} |
| 128 |
|
| 129 |
DGUI_END_NAMESPACE |
| 130 |
|
| 131 |
QT_BEGIN_NAMESPACE |
| 132 |
DGUI_USE_NAMESPACE |
| 133 |
#ifndef QT_NO_DEBUG_STREAM |
| 134 |
|
| 135 |
static inline QString getColorName(QColor color) { |
| 136 |
if (color.isValid()) |
| 137 |
return color.name(QColor::HexArgb); |
| 138 |
return QLatin1String("InValid"); |
| 139 |
}; |
| 140 |
|
| 141 |
QDebug operator<<(QDebug dbg, const DDciIconPalette &pal) |
| 142 |
{ |
| 143 |
QDebugStateSaver saver(dbg); |
| 144 |
QDebug nospace = dbg.nospace(); |
| 145 |
nospace << "DDciIconPalette(foreground: " << getColorName(pal.foreground()) |
| 146 |
<< ",background: " << getColorName(pal.background()) |
| 147 |
<< ",highlight: " << getColorName(pal.highlight()) |
| 148 |
<< ",highlightForeground: " << getColorName(pal.highlightForeground()) |
| 149 |
<< ")"; |
| 150 |
return dbg; |
| 151 |
} |
| 152 |
#endif |
| 153 |
QT_END_NAMESPACE |