| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "themeController.h" |
| 24 |
#include <QApplication> |
| 25 |
#include <QDBusConnection> |
| 26 |
#include <QDBusMessage> |
| 27 |
#include <QDBusReply> |
| 28 |
#include <QDebug> |
| 29 |
#include <QGuiApplication> |
| 30 |
#include <QPainter> |
| 31 |
#include <QPainterPath> |
| 32 |
#include <QStringList> |
| 33 |
#include <QVariant> |
| 34 |
#include <QtMath> |
| 35 |
|
| 36 |
#define ColorDifference 10 |
| 37 |
|
| 38 |
static WidgetThemeFlag g_widgetThemeFlag = DefaultTheme; |
| 39 |
static ThemeFlag g_themeFlag = LightTheme; |
| 40 |
static IconFlag g_iconFlag = ClassicStyle; |
| 41 |
static int g_fontSize = 11; |
| 42 |
static QGSettings *g_gsetting = nullptr; |
| 43 |
QStringList applist{ |
| 44 |
// "kylin-nm", |
| 45 |
"ukui-menu", |
| 46 |
"ukui-panel", |
| 47 |
"ukui-sidebar", |
| 48 |
"ukui-volume-control-applet-qt", |
| 49 |
"panelukui-panel", |
| 50 |
"ukui-power-manager-tray", |
| 51 |
// "ukui-bluetooth", |
| 52 |
"sogouimebs", |
| 53 |
"kylin-device-daemoon", |
| 54 |
"ukui-notifications", |
| 55 |
"ukui-flash-disk", |
| 56 |
"mktip", |
| 57 |
"kylin-video"}; |
| 58 |
static inline QString getUppercaseLetters(QString str) |
| 59 |
{ |
| 60 |
if (str.isEmpty()) { |
| 61 |
return str; |
| 62 |
} |
| 63 |
|
| 64 |
QString firstStr = str.left(1).toUpper(); |
| 65 |
QString remainingStr = str.mid(1); |
| 66 |
|
| 67 |
return firstStr + remainingStr; |
| 68 |
} |
| 69 |
|
| 70 |
static inline QString getDTColorGroup(QPalette::ColorGroup colorGroup) |
| 71 |
{ |
| 72 |
// QPalette::ColorGroup colorGroup; |
| 73 |
QString str; |
| 74 |
if (colorGroup == QPalette::Active) |
| 75 |
str = "active"; |
| 76 |
else if (colorGroup == QPalette::Inactive) |
| 77 |
str = "inactive"; |
| 78 |
else if (colorGroup == QPalette::Disabled) |
| 79 |
str = "disable"; |
| 80 |
else if (colorGroup == QPalette::NColorGroups) |
| 81 |
str = "ncolorgroups"; |
| 82 |
else if (colorGroup == QPalette::Current) |
| 83 |
str = "current"; |
| 84 |
else if (colorGroup == QPalette::All) |
| 85 |
str = "all"; |
| 86 |
else if (colorGroup == QPalette::Normal) |
| 87 |
str = "normal"; |
| 88 |
return str; |
| 89 |
} |
| 90 |
|
| 91 |
static inline QPalette::ColorRole getColorRole(QString str) |
| 92 |
{ |
| 93 |
QPalette::ColorRole colorRole; |
| 94 |
if (str.contains("windowtext")) |
| 95 |
colorRole = QPalette::WindowText; |
| 96 |
else if (str.contains("button")) |
| 97 |
colorRole = QPalette::Button; |
| 98 |
else if (str.contains("light")) |
| 99 |
colorRole = QPalette::Light; |
| 100 |
else if (str.contains("midlight")) |
| 101 |
colorRole = QPalette::Midlight; |
| 102 |
else if (str.contains("dark")) |
| 103 |
colorRole = QPalette::Dark; |
| 104 |
else if (str.contains("mid")) |
| 105 |
colorRole = QPalette::Mid; |
| 106 |
else if (str.contains("text")) |
| 107 |
colorRole = QPalette::Text; |
| 108 |
else if (str.contains("brighttext")) |
| 109 |
colorRole = QPalette::BrightText; |
| 110 |
else if (str.contains("buttontext")) |
| 111 |
colorRole = QPalette::ButtonText; |
| 112 |
else if (str.contains("base")) |
| 113 |
colorRole = QPalette::Base; |
| 114 |
else if (str.contains("window")) |
| 115 |
colorRole = QPalette::Window; |
| 116 |
else if (str.contains("shadow")) |
| 117 |
colorRole = QPalette::Shadow; |
| 118 |
else if (str.contains("highlight")) |
| 119 |
colorRole = QPalette::Highlight; |
| 120 |
else if (str.contains("highlightedtext")) |
| 121 |
colorRole = QPalette::HighlightedText; |
| 122 |
else if (str.contains("link")) |
| 123 |
colorRole = QPalette::Link; |
| 124 |
else if (str.contains("linkvisited")) |
| 125 |
colorRole = QPalette::LinkVisited; |
| 126 |
else if (str.contains("alternatebase")) |
| 127 |
colorRole = QPalette::AlternateBase; |
| 128 |
else if (str.contains("norole")) |
| 129 |
colorRole = QPalette::NoRole; |
| 130 |
else if (str.contains("tooltipbase")) |
| 131 |
colorRole = QPalette::ToolTipBase; |
| 132 |
else if (str.contains("tooltiptext")) |
| 133 |
colorRole = QPalette::ToolTipText; |
| 134 |
else if (str.contains("placeholdertext")) |
| 135 |
colorRole = QPalette::PlaceholderText; |
| 136 |
else if (str.contains("accent")) |
| 137 |
colorRole = QPalette::Accent; |
| 138 |
else if (str.contains("ncolorroles")) |
| 139 |
colorRole = QPalette::NColorRoles; |
| 140 |
|
| 141 |
return colorRole; |
| 142 |
} |
| 143 |
|
| 144 |
static inline qreal mixQreal(qreal a, qreal b, qreal bias) |
| 145 |
{ |
| 146 |
return a + (b - a) * bias; |
| 147 |
} |
| 148 |
static inline QColor adjustColor(const QColor c, int hRange = 0, int sRange = 0, int lRange = 0, int aRange = 0) |
| 149 |
{ |
| 150 |
QColor cc; |
| 151 |
int s, l, a; |
| 152 |
|
| 153 |
if ((c.hslSaturationF() + sRange * 1.0 / 100.0) > 1) { |
| 154 |
// qWarning() << "adjustColor s out of range1"; |
| 155 |
s = 255; |
| 156 |
} else if ((c.hslSaturationF() + sRange * 1.0 / 100.0) < 0) { |
| 157 |
// qWarning() << "adjustColor s out of range0"; |
| 158 |
s = 0; |
| 159 |
} else |
| 160 |
s = (c.hslSaturationF() + sRange * 1.0 / 100.0) * 255.0; |
| 161 |
|
| 162 |
if ((c.lightnessF() + lRange * 1.0 / 100.0) > 0.97) { // 根据设计稿寻光色彩识别 l应<=0.97 |
| 163 |
qWarning() << "adjustColor l out of range1" << c.lightnessF() << lRange << (c.lightnessF() + lRange * 1.0 / 100.0); |
| 164 |
l = 255.0 * 0.97; |
| 165 |
} else if ((c.lightnessF() + lRange * 1.0 / 100.0) < 0.15) { // 根据设计稿上l最小取的均值 |
| 166 |
qWarning() << "adjustColor l out of range0"; |
| 167 |
l = 0.15 * 255.0; |
| 168 |
} else |
| 169 |
l = (c.lightnessF() + lRange * 1.0 / 100.0) * 255.0; |
| 170 |
|
| 171 |
if ((c.alphaF() + aRange * 1.0 / 100.0) > 1) { |
| 172 |
qWarning() << "adjustColor a out of range1"; |
| 173 |
a = 255; |
| 174 |
} else if ((c.alphaF() + aRange * 1.0 / 100.0) < 0) { |
| 175 |
qWarning() << "adjustColor a out of range0"; |
| 176 |
a = 0; |
| 177 |
} else |
| 178 |
a = (c.alphaF() + aRange * 1.0 / 100.0) * 255.0; |
| 179 |
cc.setHsl(c.hslHue() + hRange, s, l, a); |
| 180 |
return cc; |
| 181 |
} |
| 182 |
|
| 183 |
QPixmap ThemeController::drawSymbolicColoredPixmap(const QPixmap &source) |
| 184 |
{ |
| 185 |
QColor gray(128, 128, 128); |
| 186 |
QColor standard(31, 32, 34); |
| 187 |
QImage img = source.toImage(); |
| 188 |
for (int x = 0; x < img.width(); x++) { |
| 189 |
for (int y = 0; y < img.height(); y++) { |
| 190 |
auto color = img.pixelColor(x, y); |
| 191 |
if (color.alpha() > 0) { |
| 192 |
if (qAbs(color.red() - gray.red()) < 20 && qAbs(color.green() - gray.green()) < 20 && qAbs(color.blue() - gray.blue()) < 20) { |
| 193 |
color.setRed(255); |
| 194 |
color.setGreen(255); |
| 195 |
color.setBlue(255); |
| 196 |
img.setPixelColor(x, y, color); |
| 197 |
} else if (qAbs(color.red() - standard.red()) < 20 && qAbs(color.green() - standard.green()) < 20 && qAbs(color.blue() - standard.blue()) < 20) { |
| 198 |
color.setRed(255); |
| 199 |
color.setGreen(255); |
| 200 |
color.setBlue(255); |
| 201 |
img.setPixelColor(x, y, color); |
| 202 |
} else { |
| 203 |
img.setPixelColor(x, y, color); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
return QPixmap::fromImage(img); |
| 209 |
} |
| 210 |
|
| 211 |
QPixmap ThemeController::drawColoredPixmap(const QPixmap &source, const QColor &sampleColor) |
| 212 |
{ |
| 213 |
QImage img = source.toImage(); |
| 214 |
for(int x = 0 ; x < img.width() ;x++) |
| 215 |
{ |
| 216 |
for( int y = 0; y < img.height();y++) |
| 217 |
{ |
| 218 |
auto color = img.pixelColor(x,y); |
| 219 |
if(color.alpha() > 0) |
| 220 |
{ |
| 221 |
color.setRed(sampleColor.red()); |
| 222 |
color.setGreen(sampleColor.green()); |
| 223 |
color.setBlue(sampleColor.blue()); |
| 224 |
img.setPixelColor(x,y,color); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
return QPixmap::fromImage(img); |
| 229 |
} |
| 230 |
|
| 231 |
QColor ThemeController::getCurrentIconColor() |
| 232 |
{ |
| 233 |
QPixmap pixmap = QIcon::fromTheme("open-menu-symbolic").pixmap(16, 16); |
| 234 |
QImage img = pixmap.toImage(); |
| 235 |
for (int x = 0; x < img.width(); x++) { |
| 236 |
for (int y = 0; y < img.height(); y++) { |
| 237 |
auto color = img.pixelColor(x, y); |
| 238 |
if (color.alpha() > 0) { |
| 239 |
return color; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
QColor ThemeController::mixColor(const QColor &c1, const QColor &c2, qreal bias) |
| 246 |
{ |
| 247 |
if (bias <= 0.0) { |
| 248 |
return c1; |
| 249 |
} |
| 250 |
if (bias >= 1.0) { |
| 251 |
return c2; |
| 252 |
} |
| 253 |
if (qIsNaN(bias)) { |
| 254 |
return c1; |
| 255 |
} |
| 256 |
|
| 257 |
qreal r = mixQreal(c1.redF(), c2.redF(), bias); |
| 258 |
qreal g = mixQreal(c1.greenF(), c2.greenF(), bias); |
| 259 |
qreal b = mixQreal(c1.blueF(), c2.blueF(), bias); |
| 260 |
qreal a = mixQreal(c1.alphaF(), c2.alphaF(), bias); |
| 261 |
|
| 262 |
return QColor::fromRgbF(r, g, b, a); |
| 263 |
} |
| 264 |
|
| 265 |
WidgetThemeFlag ThemeController::widgetTheme() |
| 266 |
{ |
| 267 |
return g_widgetThemeFlag; |
| 268 |
} |
| 269 |
|
| 270 |
ThemeFlag ThemeController::themeMode() |
| 271 |
{ |
| 272 |
return g_themeFlag; |
| 273 |
} |
| 274 |
|
| 275 |
IconFlag ThemeController::iconTheme() |
| 276 |
{ |
| 277 |
return g_iconFlag; |
| 278 |
} |
| 279 |
|
| 280 |
int ThemeController::systemFontSize() |
| 281 |
{ |
| 282 |
if (!g_gsetting) |
| 283 |
return 11; |
| 284 |
if (g_gsetting->keys().contains("systemFontSize")) { |
| 285 |
g_fontSize = (int)g_gsetting->get("systemFontSize").toDouble(); |
| 286 |
} |
| 287 |
return g_fontSize; |
| 288 |
} |
| 289 |
|
| 290 |
QPixmap ThemeController::drawFashionBackground(const QRect &rect, int sub_width, int sub_height, int radius, int flag) |
| 291 |
{ |
| 292 |
|
| 293 |
// int radius = 6; |
| 294 |
// int sub_width = 32; |
| 295 |
// int sub_height = 36; |
| 296 |
QPixmap framePixmap(rect.size()); |
| 297 |
|
| 298 |
framePixmap.fill(Qt::transparent); |
| 299 |
QRect drawRect; |
| 300 |
drawRect = rect.adjusted(0, 0, 1, 1); |
| 301 |
|
| 302 |
auto baseColor = qApp->palette().color(QPalette::Active, QPalette::Button); |
| 303 |
|
| 304 |
QColor startColor = mixColor(baseColor, getCustomColorFromDT("kwhite"), 0.5); |
| 305 |
QColor endColor = mixColor(baseColor, getCustomColorFromDT("kblack"), 0.1); |
| 306 |
|
| 307 |
QLinearGradient linearGradient; |
| 308 |
QPainterPath path; |
| 309 |
if (flag) // right |
| 310 |
{ |
| 311 |
path.moveTo(drawRect.bottomRight() - QPoint(0, radius + sub_height)); |
| 312 |
path.lineTo(drawRect.bottomRight() - QPoint(0, radius)); |
| 313 |
path.quadTo(drawRect.bottomRight(), drawRect.bottomRight() - QPoint(radius, 0)); |
| 314 |
path.lineTo(drawRect.bottomRight() - QPoint(radius + sub_width, 0)); |
| 315 |
path.quadTo(drawRect.bottomRight(), drawRect.bottomRight() - QPoint(0, radius + sub_height)); |
| 316 |
|
| 317 |
linearGradient.setColorAt(0, startColor); |
| 318 |
linearGradient.setColorAt(1, endColor); |
| 319 |
linearGradient.setStart(drawRect.right(), drawRect.bottom() - (radius + sub_height)); |
| 320 |
linearGradient.setFinalStop(drawRect.right(), drawRect.bottom()); |
| 321 |
} else // left |
| 322 |
{ |
| 323 |
path.moveTo(drawRect.bottomLeft() - QPoint(0, radius + sub_height)); |
| 324 |
path.lineTo(drawRect.bottomLeft() - QPoint(0, radius)); |
| 325 |
path.quadTo(drawRect.bottomLeft(), drawRect.bottomLeft() + QPoint(radius, 0)); |
| 326 |
path.lineTo(drawRect.bottomLeft() + QPoint(radius + sub_width, 0)); |
| 327 |
path.quadTo(drawRect.bottomLeft(), drawRect.bottomLeft() - QPoint(0, radius + sub_height)); |
| 328 |
|
| 329 |
linearGradient.setColorAt(0, startColor); |
| 330 |
linearGradient.setColorAt(1, endColor); |
| 331 |
linearGradient.setStart(drawRect.left(), drawRect.bottom() - (radius + sub_height)); |
| 332 |
linearGradient.setFinalStop(drawRect.left(), drawRect.bottom()); |
| 333 |
} |
| 334 |
QPainter painter(&framePixmap); |
| 335 |
painter.setRenderHint(QPainter::Antialiasing); |
| 336 |
painter.setPen(Qt::transparent); |
| 337 |
painter.setBrush(linearGradient); |
| 338 |
painter.drawPath(path); |
| 339 |
return framePixmap; |
| 340 |
} |
| 341 |
|
| 342 |
bool ThemeController::isPixmapPureColor(const QPixmap &pixmap) |
| 343 |
{ |
| 344 |
QColor symbolic_color = getCustomColorFromDT("kgray-17"); |
| 345 |
|
| 346 |
if (pixmap.isNull()) { |
| 347 |
qWarning("pixmap is null!"); |
| 348 |
return false; |
| 349 |
} |
| 350 |
QImage image = pixmap.toImage(); |
| 351 |
|
| 352 |
QVector<QColor> vector; |
| 353 |
int total_red = 0; |
| 354 |
int total_green = 0; |
| 355 |
int total_blue = 0; |
| 356 |
bool pure = true; |
| 357 |
for (int y = 0; y < image.height(); ++y) { |
| 358 |
for (int x = 0; x < image.width(); ++x) { |
| 359 |
if (image.pixelColor(x, y).alphaF() > 0.3) { |
| 360 |
QColor color = image.pixelColor(x, y); |
| 361 |
vector << color; |
| 362 |
total_red += color.red(); |
| 363 |
total_green += color.green(); |
| 364 |
total_blue += color.blue(); |
| 365 |
int dr = qAbs(color.red() - symbolic_color.red()); |
| 366 |
int dg = qAbs(color.green() - symbolic_color.green()); |
| 367 |
int db = qAbs(color.blue() - symbolic_color.blue()); |
| 368 |
if (dr > ColorDifference || dg > ColorDifference || db > ColorDifference) |
| 369 |
pure = false; |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if (pure) |
| 375 |
return true; |
| 376 |
|
| 377 |
qreal squareRoot_red = 0; |
| 378 |
qreal squareRoot_green = 0; |
| 379 |
qreal squareRoot_blue = 0; |
| 380 |
qreal average_red = total_red / vector.count(); |
| 381 |
qreal average_green = total_green / vector.count(); |
| 382 |
qreal average_blue = total_blue / vector.count(); |
| 383 |
for (QColor color : vector) { |
| 384 |
squareRoot_red += (color.red() - average_red) * (color.red() - average_red); |
| 385 |
squareRoot_green += (color.green() - average_green) * (color.green() - average_green); |
| 386 |
squareRoot_blue += (color.blue() - average_blue) * (color.blue() - average_blue); |
| 387 |
} |
| 388 |
|
| 389 |
qreal arithmeticSquareRoot_red = qSqrt(squareRoot_red / vector.count()); |
| 390 |
qreal arithmeticSquareRoot_green = qSqrt(squareRoot_green / vector.count()); |
| 391 |
qreal arithmeticSquareRoot_blue = qSqrt(squareRoot_blue / vector.count()); |
| 392 |
return arithmeticSquareRoot_red < 2.0 && arithmeticSquareRoot_green < 2.0 && arithmeticSquareRoot_blue < 2.0; |
| 393 |
} |
| 394 |
|
| 395 |
QIcon ThemeController::overlayIcons(const QIcon &icon1, const QIcon &icon2, int iconWidth, int iconHeight, int offsetX, int offsetY) |
| 396 |
{ |
| 397 |
if (!icon1.isNull() && !icon2.isNull()) { |
| 398 |
QSize size = icon1.actualSize(QSize(iconWidth, iconHeight)); |
| 399 |
|
| 400 |
QPixmap pixmap(size); |
| 401 |
pixmap.fill(Qt::transparent); |
| 402 |
|
| 403 |
QPainter painter(&pixmap); |
| 404 |
painter.setRenderHint(QPainter::Antialiasing); |
| 405 |
painter.setRenderHint(QPainter::TextAntialiasing); |
| 406 |
painter.setRenderHint(QPainter::SmoothPixmapTransform); |
| 407 |
|
| 408 |
icon1.paint(&painter, QRect(0, 0, size.width(), size.height()), Qt::AlignCenter); |
| 409 |
if (iconWidth > offsetX && iconHeight > offsetY) |
| 410 |
icon2.paint(&painter, QRect(offsetX, offsetY, pixmap.width() - offsetX, size.height() - offsetY), Qt::AlignCenter); |
| 411 |
else |
| 412 |
icon2.paint(&painter, QRect(pixmap.width() - 16, pixmap.height() - 16, 16, 16), Qt::AlignCenter); |
| 413 |
|
| 414 |
QIcon resultIcon(pixmap); |
| 415 |
|
| 416 |
return resultIcon; |
| 417 |
} else { |
| 418 |
return QIcon(); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
QPixmap ThemeController::overlayPixmaps(const QPixmap &pixmap1, const QPixmap &pixmap2, int offsetX, int offsetY) |
| 423 |
{ |
| 424 |
if (!pixmap1.isNull() && !pixmap2.isNull()) { |
| 425 |
QPixmap resultPixmap(pixmap1.size()); |
| 426 |
resultPixmap.fill(Qt::transparent); |
| 427 |
|
| 428 |
QPainter painter(&resultPixmap); |
| 429 |
painter.setRenderHint(QPainter::Antialiasing); |
| 430 |
painter.setRenderHint(QPainter::TextAntialiasing); |
| 431 |
painter.setRenderHint(QPainter::SmoothPixmapTransform); |
| 432 |
|
| 433 |
painter.drawPixmap(QRect(0, 0, pixmap1.width(), pixmap1.height()), pixmap1); |
| 434 |
|
| 435 |
if (pixmap1.width() > offsetX && pixmap1.height() > offsetY) |
| 436 |
painter.drawPixmap(QRect(offsetX, offsetY, pixmap1.width() - offsetX, pixmap1.height() - offsetY), pixmap2); |
| 437 |
else |
| 438 |
painter.drawPixmap(QRect(pixmap1.width() - 16, pixmap1.height() - 16, 16, 16), pixmap2); |
| 439 |
|
| 440 |
painter.end(); |
| 441 |
|
| 442 |
return resultPixmap; |
| 443 |
} else { |
| 444 |
return QPixmap(); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
QColor ThemeController::lanhuHSLToQtHsl(int h, int s, int l, int a) |
| 449 |
{ |
| 450 |
QColor c; |
| 451 |
c.setHsl(h, s * 255.0 / 100.0, l * 255.0 / 100.0, a); |
| 452 |
return c; |
| 453 |
} |
| 454 |
|
| 455 |
QColor ThemeController::adjustH(const QColor c, int range) |
| 456 |
{ |
| 457 |
QColor cc; |
| 458 |
if ((c.hslHue() + range) >= 0) |
| 459 |
cc.setHsl(c.hslHue() + range, c.hslSaturation(), c.lightness(), c.alpha()); |
| 460 |
else |
| 461 |
cc.setHsl(c.hslHue(), c.hslSaturation(), c.lightness(), c.alpha()); |
| 462 |
|
| 463 |
return cc; |
| 464 |
} |
| 465 |
|
| 466 |
QColor ThemeController::adjustS(const QColor c, int range) |
| 467 |
{ |
| 468 |
QColor cc; |
| 469 |
int s; |
| 470 |
|
| 471 |
if ((c.hslSaturationF() + range * 1.0 / 100.0) > 1) { |
| 472 |
// qWarning() << "adjustS s out of range1"; |
| 473 |
s = 255; |
| 474 |
} else if ((c.hslSaturationF() + range * 1.0 / 100.0) < 0) { |
| 475 |
// qWarning() << "adjustS s out of range0"; |
| 476 |
s = 0; |
| 477 |
} else |
| 478 |
s = (c.hslSaturationF() + range * 1.0 / 100.0) * 255.0; |
| 479 |
cc.setHsl(c.hslHue(), s, c.lightness(), c.alpha()); |
| 480 |
return cc; |
| 481 |
} |
| 482 |
|
| 483 |
QColor ThemeController::adjustL(const QColor c, int range) |
| 484 |
{ |
| 485 |
QColor cc; |
| 486 |
int l; |
| 487 |
|
| 488 |
if ((c.lightnessF() + range * 1.0 / 100.0) > 0.97) { |
| 489 |
// qWarning() << "adjustL l out of range1"; |
| 490 |
l = 255.0 * 0.97; |
| 491 |
} else if ((c.lightnessF() + range * 1.0 / 100.0) < 0.15) { |
| 492 |
// qWarning() << "adjustL l out of range0" << c.lightnessF() << range << (c.lightnessF() + range * 1.0 / 100.0); |
| 493 |
l = 255.0 * 0.15; |
| 494 |
} else |
| 495 |
l = (c.lightnessF() + range * 1.0 / 100.0) * 255.0; |
| 496 |
|
| 497 |
cc.setHsl(c.hslHue(), c.hslSaturation(), l, c.alpha()); |
| 498 |
return cc; |
| 499 |
} |
| 500 |
|
| 501 |
QColor ThemeController::adjustA(const QColor c, int range) |
| 502 |
{ |
| 503 |
QColor cc; |
| 504 |
int a; |
| 505 |
if ((c.alphaF() + range * 1.0 / 100.0) > 1) { |
| 506 |
// qWarning() << "adjustA a out of range1"; |
| 507 |
a = 255; |
| 508 |
} else if ((c.alphaF() + range * 1.0 / 100.0) < 0) { |
| 509 |
// qWarning() << "adjustA a out of range0"; |
| 510 |
a = 0; |
| 511 |
} else |
| 512 |
a = (c.alphaF() + range * 1.0 / 100.0) * 255.0; |
| 513 |
|
| 514 |
cc.setHsl(c.hslHue(), c.hslSaturation(), c.lightness(), a); |
| 515 |
return cc; |
| 516 |
} |
| 517 |
|
| 518 |
QColor ThemeController::highlightClick(bool isDark, QPalette p) |
| 519 |
{ |
| 520 |
if (isDark) { |
| 521 |
return adjustColor(p.color(QPalette::Active, QPalette::Highlight), 0, -25, -34); // hightlight->210,72%,71% 210,47%,37% |
| 522 |
} |
| 523 |
return adjustColor(p.color(QPalette::Active, QPalette::Highlight), 3, 10, 31); // hightlight->209,87%,57% 212,97%,88% |
| 524 |
} |
| 525 |
|
| 526 |
QColor ThemeController::highlightHover(bool isDark, QPalette p) |
| 527 |
{ |
| 528 |
if (isDark) { |
| 529 |
return adjustColor(p.color(QPalette::Active, QPalette::Highlight), 0, -26, -40); // 210,46%,31% |
| 530 |
} |
| 531 |
return adjustColor(p.color(QPalette::Active, QPalette::Highlight), 4, 2, 36); // 213,89%,93% |
| 532 |
} |
| 533 |
|
| 534 |
QColor ThemeController::buttonDarkClick(bool isDark, QPalette p) |
| 535 |
{ |
| 536 |
if (isDark) { |
| 537 |
return adjustColor(p.color(QPalette::Disabled, QPalette::ButtonText), 0, 1, 5); |
| 538 |
} |
| 539 |
return QColor(); |
| 540 |
} |
| 541 |
|
| 542 |
bool ThemeController::systemLang() |
| 543 |
{ |
| 544 |
QString systemLang = QLocale::system().name(); |
| 545 |
if (systemLang == "ug_CN" || systemLang == "ky_KG" || systemLang == "kk_KZ") |
| 546 |
return true; |
| 547 |
else |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
QColor ThemeController::getPaletteColorFromDT(QString dtString, QPalette::ColorGroup group) |
| 552 |
{ |
| 553 |
QColor color; |
| 554 |
char *m_char = dtString.toLocal8Bit().data(); |
| 555 |
if (qApp->property(m_char).isValid() && qApp->property(m_char).canConvert<QBrush>()) { |
| 556 |
QBrush brush = qApp->property(m_char).value<QBrush>(); |
| 557 |
color = brush.color(); |
| 558 |
} |
| 559 |
|
| 560 |
if (!color.isValid()) |
| 561 |
{ |
| 562 |
QPalette::ColorGroup colorGroup; |
| 563 |
colorGroup = qApp->palette().currentColorGroup(); |
| 564 |
QPalette::ColorRole colorRole = getColorRole(dtString); |
| 565 |
color = qApp->palette().color(colorGroup, colorRole); |
| 566 |
} |
| 567 |
return color; |
| 568 |
} |
| 569 |
|
| 570 |
QColor ThemeController::getCustomColorFromDT(QString dtString) |
| 571 |
{ |
| 572 |
QColor color; |
| 573 |
char *m_char = dtString.toLocal8Bit().data(); |
| 574 |
if (qApp->property(m_char).isValid() && qApp->property(m_char).canConvert<QBrush>()) { |
| 575 |
QBrush brush = qApp->property(m_char).value<QBrush>(); |
| 576 |
color = brush.color(); |
| 577 |
} |
| 578 |
return color; |
| 579 |
} |
| 580 |
|
| 581 |
QString ThemeController::getFontStylesFromDT(QString dtString) |
| 582 |
{ |
| 583 |
char *m_char = dtString.toLocal8Bit().data(); |
| 584 |
if (qApp->property(m_char).isValid() && qApp->property(m_char).canConvert<QString>()) { |
| 585 |
return qApp->property(m_char).value<QString>(); |
| 586 |
} |
| 587 |
return QString(); |
| 588 |
} |
| 589 |
|
| 590 |
int ThemeController::getRadiusFromDT(QString dtString) |
| 591 |
{ |
| 592 |
char *m_char = dtString.toLocal8Bit().data(); |
| 593 |
if (qApp->property(m_char).isValid() && qApp->property(m_char).canConvert<int>()) { |
| 594 |
return qApp->property(m_char).value<int>(); |
| 595 |
} |
| 596 |
return -1; |
| 597 |
} |
| 598 |
|
| 599 |
int ThemeController::getLineOrMarginFromDT(QString dtString) |
| 600 |
{ |
| 601 |
char *m_char = dtString.toLocal8Bit().data(); |
| 602 |
if (qApp->property(m_char).isValid() && qApp->property(m_char).canConvert<QColor>()) { |
| 603 |
return qApp->property(m_char).value<int>(); |
| 604 |
} |
| 605 |
return -1; |
| 606 |
} |
| 607 |
|
| 608 |
void ThemeController::getGradientFromDT(QString key, QColor &startColor, QColor &endColor) |
| 609 |
{ |
| 610 |
// int type; |
| 611 |
// QVariantMap option; |
| 612 |
|
| 613 |
// QDBusConnection bus = QDBusConnection::sessionBus(); |
| 614 |
// QDBusMessage message = QDBusMessage::createMethodCall( |
| 615 |
// "com.kylin.themeConfig", |
| 616 |
// "/com/kylin/ThemeConfig", |
| 617 |
// "com.kylin.ThemeConfig.DTParameters", |
| 618 |
// "GetDTThemeConfig"); |
| 619 |
|
| 620 |
// QVariantList arguments; |
| 621 |
// arguments.append(key); |
| 622 |
// message.setArguments(arguments); |
| 623 |
|
| 624 |
// QDBusReply<QVariantMap> reply = bus.call(message); |
| 625 |
|
| 626 |
// if (!reply.isValid()) { |
| 627 |
// qDebug() << "complex type failed!"; |
| 628 |
// } else { |
| 629 |
// option = reply.value(); |
| 630 |
|
| 631 |
// if (option.contains("type")) { |
| 632 |
// type = option.value("type").toUInt(); |
| 633 |
// } |
| 634 |
|
| 635 |
// if (type == 3 && option.contains(QStringLiteral("start_color")) && option.contains(QStringLiteral("end_color"))) { |
| 636 |
|
| 637 |
// QByteArray startvalue = option.value(QStringLiteral("start_color")).toByteArray(); |
| 638 |
// uint8_t c[4]; |
| 639 |
// c[0] = startvalue[0]; |
| 640 |
// c[1] = startvalue[1]; |
| 641 |
// c[2] = startvalue[2]; |
| 642 |
// c[3] = startvalue[3]; |
| 643 |
// startColor.setRed(c[0]); |
| 644 |
// startColor.setGreen(c[1]); |
| 645 |
// startColor.setBlue(c[2]); |
| 646 |
// startColor.setAlpha(c[3]); |
| 647 |
|
| 648 |
// QByteArray endvalue = option.value(QStringLiteral("end_color")).toByteArray(); |
| 649 |
// c[0] = endvalue[0]; |
| 650 |
// c[1] = endvalue[1]; |
| 651 |
// c[2] = endvalue[2]; |
| 652 |
// c[3] = endvalue[3]; |
| 653 |
// endColor.setRed(c[0]); |
| 654 |
// endColor.setGreen(c[1]); |
| 655 |
// endColor.setBlue(c[2]); |
| 656 |
// endColor.setAlpha(c[3]); |
| 657 |
// } |
| 658 |
// } |
| 659 |
char *m_char = key.toLocal8Bit().data(); |
| 660 |
|
| 661 |
if (qApp->property(m_char).isValid() && qApp->property(m_char).canConvert<QBrush>()) { |
| 662 |
QBrush brush = qApp->property(m_char).value<QBrush>(); |
| 663 |
if(brush.gradient() && brush.gradient()->stops().length() > 1) |
| 664 |
{ |
| 665 |
startColor = brush.gradient()->stops().at(0).second; |
| 666 |
endColor = brush.gradient()->stops().at(1).second; |
| 667 |
} |
| 668 |
else |
| 669 |
{ |
| 670 |
startColor = brush.color(); |
| 671 |
endColor = brush.color(); |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
ThemeController::ThemeController() |
| 677 |
: m_gsetting(nullptr) |
| 678 |
{ |
| 679 |
if (QGSettings::isSchemaInstalled(FITTHEMEWINDOW)) { |
| 680 |
m_gsetting = new QGSettings(FITTHEMEWINDOW); |
| 681 |
g_gsetting = m_gsetting; |
| 682 |
initThemeStyle(); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
ThemeController::~ThemeController() |
| 687 |
{ |
| 688 |
} |
| 689 |
|
| 690 |
void ThemeController::initThemeStyle() |
| 691 |
{ |
| 692 |
if (!m_gsetting) |
| 693 |
return; |
| 694 |
if (m_gsetting->keys().contains("styleName")) { |
| 695 |
QString styleName = m_gsetting->get("styleName").toString(); |
| 696 |
if (styleName == "ukui-dark" || styleName == "ukui-black" || (styleName == "ukui-default" && applist.contains(QApplication::applicationName()))) |
| 697 |
g_themeFlag = DarkTheme; |
| 698 |
else |
| 699 |
g_themeFlag = LightTheme; |
| 700 |
} |
| 701 |
|
| 702 |
// 初始化图标主题 |
| 703 |
if (m_gsetting->keys().contains("iconThemeName")) { |
| 704 |
QString iconThemeName = m_gsetting->get("iconThemeName").toString(); |
| 705 |
if (iconThemeName == "ukui-icon-theme-default") |
| 706 |
g_iconFlag = DefaultStyle; |
| 707 |
else { |
| 708 |
g_iconFlag = ClassicStyle; |
| 709 |
} |
| 710 |
} |
| 711 |
if (m_gsetting->keys().contains("widgetThemeName")) { |
| 712 |
// 初始化样式主题 |
| 713 |
QString widgetThemeName = m_gsetting->get("widgetThemeName").toString(); |
| 714 |
if (widgetThemeName == "default") |
| 715 |
g_widgetThemeFlag = DefaultTheme; |
| 716 |
else if (widgetThemeName == "classical") |
| 717 |
g_widgetThemeFlag = ClassicTheme; |
| 718 |
else if(widgetThemeName == "fashion") |
| 719 |
g_widgetThemeFlag = FashionTheme; |
| 720 |
else if(widgetThemeName == "dawnlight") |
| 721 |
g_widgetThemeFlag = DawnLightTheme; |
| 722 |
} |
| 723 |
} |