| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2021~2021 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: BSD-3-Clause |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "fcitxtheme.h" |
| 8 |
#include "font.h" |
| 9 |
#include <QDebug> |
| 10 |
#include <QMargins> |
| 11 |
#include <QPixmap> |
| 12 |
#include <QSettings> |
| 13 |
#include <QStandardPaths> |
| 14 |
|
| 15 |
namespace fcitx { |
| 16 |
|
| 17 |
bool readBool(const QSettings &settings, const QString &name, |
| 18 |
bool defaultValue) { |
| 19 |
return settings.value(name, defaultValue ? "True" : "False").toString() == |
| 20 |
"True"; |
| 21 |
} |
| 22 |
|
| 23 |
QMargins readMargin(const QSettings &settings) { |
| 24 |
settings.allKeys(); |
| 25 |
return QMargins(settings.value("Left", 0).toInt(), |
| 26 |
settings.value("Top", 0).toInt(), |
| 27 |
settings.value("Right", 0).toInt(), |
| 28 |
settings.value("Bottom", 0).toInt()); |
| 29 |
} |
| 30 |
|
| 31 |
QColor readColor(const QSettings &settings, const QString &name, |
| 32 |
const QString &defaultValue) { |
| 33 |
QString colorString = settings.value(name, defaultValue).toString(); |
| 34 |
QColor color(defaultValue); |
| 35 |
if (colorString.startsWith("#")) { |
| 36 |
if (colorString.size() == 7) { |
| 37 |
// Parse #RRGGBB |
| 38 |
color = QColor(colorString.toUpper()); |
| 39 |
} else if (colorString.size() == 9) { |
| 40 |
// Qt accept "#AARRGGBB" |
| 41 |
auto newColorString = |
| 42 |
QStringLiteral("#%1%2") |
| 43 |
.arg(colorString.mid(7, 2), colorString.mid(1, 6)) |
| 44 |
.toUpper(); |
| 45 |
color = QColor(newColorString); |
| 46 |
} |
| 47 |
} |
| 48 |
return color; |
| 49 |
} |
| 50 |
|
| 51 |
void BackgroundImage::load(const QString &name, QSettings &settings) { |
| 52 |
settings.allKeys(); |
| 53 |
image_ = QPixmap(); |
| 54 |
overlay_ = QPixmap(); |
| 55 |
if (auto image = settings.value("Image").toString(); !image.isEmpty()) { |
| 56 |
auto file = QStandardPaths::locate( |
| 57 |
QStandardPaths::GenericDataLocation, |
| 58 |
QStringLiteral("fcitx5/themes/%1/%2").arg(name, image)); |
| 59 |
image_.load(file); |
| 60 |
} |
| 61 |
if (auto image = settings.value("Overlay").toString(); !image.isEmpty()) { |
| 62 |
auto file = QStandardPaths::locate( |
| 63 |
QStandardPaths::GenericDataLocation, |
| 64 |
QStringLiteral("fcitx5/themes/%1/%2").arg(name, image)); |
| 65 |
overlay_.load(file); |
| 66 |
} |
| 67 |
|
| 68 |
settings.beginGroup("Margin"); |
| 69 |
margin_ = readMargin(settings); |
| 70 |
settings.endGroup(); |
| 71 |
|
| 72 |
if (image_.isNull()) { |
| 73 |
QColor color = readColor(settings, "Color", "#ffffff"); |
| 74 |
QColor borderColor = readColor(settings, "BorderColor", "#00ffffff"); |
| 75 |
int borderWidth = settings.value("BorderWidth", 0).toInt(); |
| 76 |
fillBackground(borderColor, color, borderWidth); |
| 77 |
} |
| 78 |
|
| 79 |
settings.beginGroup("OverlayClipMargin"); |
| 80 |
overlayClipMargin_ = readMargin(settings); |
| 81 |
settings.endGroup(); |
| 82 |
|
| 83 |
hideOverlayIfOversize_ = |
| 84 |
settings.value("HideOverlayIfOversize", "False").toString() == "True"; |
| 85 |
overlayOffsetX_ = settings.value("OverlayOffsetX", 0).toInt(); |
| 86 |
overlayOffsetY_ = settings.value("OverlayOffsetY", 0).toInt(); |
| 87 |
gravity_ = settings.value("Gravity", "TopLeft").toString(); |
| 88 |
} |
| 89 |
|
| 90 |
void BackgroundImage::loadFromValue(const QColor &border, |
| 91 |
const QColor &background, QMargins margin, |
| 92 |
int borderWidth) { |
| 93 |
image_ = QPixmap(); |
| 94 |
overlay_ = QPixmap(); |
| 95 |
margin_ = margin; |
| 96 |
fillBackground(border, background, borderWidth); |
| 97 |
overlayClipMargin_ = QMargins(); |
| 98 |
hideOverlayIfOversize_ = false; |
| 99 |
overlayOffsetX_ = 0; |
| 100 |
overlayOffsetY_ = 0; |
| 101 |
gravity_ = QString(); |
| 102 |
} |
| 103 |
|
| 104 |
void BackgroundImage::fillBackground(const QColor &border, |
| 105 |
const QColor &background, |
| 106 |
int borderWidth) { |
| 107 |
image_ = QPixmap(margin_.left() + margin_.right() + 1, |
| 108 |
margin_.top() + margin_.bottom() + 1); |
| 109 |
borderWidth = std::min({borderWidth, margin_.left(), margin_.right(), |
| 110 |
margin_.top(), margin_.bottom()}); |
| 111 |
borderWidth = std::max(0, borderWidth); |
| 112 |
|
| 113 |
QPainter painter; |
| 114 |
painter.begin(&image_); |
| 115 |
painter.setCompositionMode(QPainter::CompositionMode_Source); |
| 116 |
if (borderWidth) { |
| 117 |
painter.fillRect(image_.rect(), border); |
| 118 |
} |
| 119 |
painter.fillRect(QRect(borderWidth, borderWidth, |
| 120 |
image_.width() - borderWidth * 2, |
| 121 |
image_.height() - borderWidth * 2), |
| 122 |
background); |
| 123 |
painter.end(); |
| 124 |
} |
| 125 |
|
| 126 |
void ActionImage::load(const QString &name, QSettings &settings) { |
| 127 |
settings.allKeys(); |
| 128 |
image_ = QPixmap(); |
| 129 |
valid_ = false; |
| 130 |
if (auto image = settings.value("Image").toString(); !image.isEmpty()) { |
| 131 |
auto file = QStandardPaths::locate( |
| 132 |
QStandardPaths::GenericDataLocation, |
| 133 |
QStringLiteral("fcitx5/themes/%1/%2").arg(name, image)); |
| 134 |
image_.load(file); |
| 135 |
valid_ = !image_.isNull(); |
| 136 |
} |
| 137 |
|
| 138 |
settings.beginGroup("ClickMargin"); |
| 139 |
margin_ = readMargin(settings); |
| 140 |
settings.endGroup(); |
| 141 |
} |
| 142 |
|
| 143 |
void ActionImage::reset() { |
| 144 |
image_ = QPixmap(); |
| 145 |
valid_ = false; |
| 146 |
margin_ = QMargins(0, 0, 0, 0); |
| 147 |
} |
| 148 |
|
| 149 |
FcitxTheme::FcitxTheme(QObject *parent) |
| 150 |
: QObject(parent), configPath_(QStandardPaths::writableLocation( |
| 151 |
QStandardPaths::GenericConfigLocation) |
| 152 |
.append("/fcitx5/conf/classicui.conf")), |
| 153 |
watcher_(new QFileSystemWatcher) { |
| 154 |
connect(watcher_, &QFileSystemWatcher::fileChanged, this, |
| 155 |
&FcitxTheme::configChanged); |
| 156 |
watcher_->addPath(configPath_); |
| 157 |
|
| 158 |
configChanged(); |
| 159 |
} |
| 160 |
|
| 161 |
FcitxTheme::~FcitxTheme() {} |
| 162 |
|
| 163 |
void FcitxTheme::configChanged() { |
| 164 |
// Since fcitx is doing things like delete and move, we need to re-add the |
| 165 |
// path. |
| 166 |
watcher_->removePath(configPath_); |
| 167 |
watcher_->addPath(configPath_); |
| 168 |
QSettings settings(configPath_, QSettings::IniFormat); |
| 169 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 170 |
settings.setIniCodec("UTF-8"); |
| 171 |
#endif |
| 172 |
settings.childGroups(); |
| 173 |
font_ = parseFont(settings.value("Font", "Sans Serif 9").toString()); |
| 174 |
fontMetrics_ = QFontMetrics(font_); |
| 175 |
vertical_ = |
| 176 |
settings.value("Vertical Candidate List", "False").toString() == "True"; |
| 177 |
wheelForPaging_ = |
| 178 |
settings.value("WheelForPaging", "True").toString() == "True"; |
| 179 |
theme_ = settings.value("Theme", "default").toString(); |
| 180 |
|
| 181 |
themeChanged(); |
| 182 |
} |
| 183 |
|
| 184 |
void FcitxTheme::themeChanged() { |
| 185 |
if (!themeConfigPath_.isEmpty()) { |
| 186 |
watcher_->removePath(themeConfigPath_); |
| 187 |
} |
| 188 |
auto themeConfig = |
| 189 |
QStringLiteral("/fcitx5/themes/%1/theme.conf").arg(theme_); |
| 190 |
themeConfigPath_ = |
| 191 |
QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) |
| 192 |
.append(themeConfig); |
| 193 |
auto file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, |
| 194 |
themeConfig); |
| 195 |
if (file.isEmpty()) { |
| 196 |
file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, |
| 197 |
"fcitx5/themes/default/theme.conf"); |
| 198 |
themeConfigPath_ = QStandardPaths::writableLocation( |
| 199 |
QStandardPaths::GenericDataLocation) |
| 200 |
.append("fcitx5/themes/default/theme.conf"); |
| 201 |
theme_ = "default"; |
| 202 |
} |
| 203 |
|
| 204 |
watcher_->addPath(themeConfigPath_); |
| 205 |
|
| 206 |
// We can not locate default theme. |
| 207 |
if (file.isEmpty()) { |
| 208 |
normalColor_ = QColor("#000000"); |
| 209 |
highlightCandidateColor_ = QColor("#ffffff"); |
| 210 |
fullWidthHighlight_ = true; |
| 211 |
highlightColor_ = QColor("#ffffff"); |
| 212 |
highlightBackgroundColor_ = QColor("#a5a5a5"); |
| 213 |
contentMargin_ = QMargins{2, 2, 2, 2}; |
| 214 |
textMargin_ = QMargins{5, 5, 5, 5}; |
| 215 |
highlightClickMargin_ = QMargins{0, 0, 0, 0}; |
| 216 |
shadowMargin_ = QMargins{0, 0, 0, 0}; |
| 217 |
background_.loadFromValue(highlightBackgroundColor_, highlightColor_, |
| 218 |
contentMargin_, 2); |
| 219 |
highlight_.loadFromValue(highlightBackgroundColor_, |
| 220 |
highlightBackgroundColor_, textMargin_, 0); |
| 221 |
prev_.reset(); |
| 222 |
next_.reset(); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
QSettings settings(file, QSettings::IniFormat); |
| 227 |
settings.childGroups(); |
| 228 |
settings.beginGroup("InputPanel"); |
| 229 |
normalColor_ = readColor(settings, "NormalColor", "#000000"); |
| 230 |
highlightCandidateColor_ = |
| 231 |
readColor(settings, "HighlightCandidateColor", "#ffffff"); |
| 232 |
fullWidthHighlight_ = readBool(settings, "FullWidthHighlight", true); |
| 233 |
highlightColor_ = readColor(settings, "HighlightColor", "#ffffff"); |
| 234 |
highlightBackgroundColor_ = |
| 235 |
readColor(settings, "HighlightBackgroundColor", "#a5a5a5"); |
| 236 |
buttonAlignment_ = |
| 237 |
settings.value("PageButtonAlignment", "Bottom").toString(); |
| 238 |
|
| 239 |
settings.beginGroup("ContentMargin"); |
| 240 |
contentMargin_ = readMargin(settings); |
| 241 |
settings.endGroup(); |
| 242 |
settings.beginGroup("TextMargin"); |
| 243 |
textMargin_ = readMargin(settings); |
| 244 |
settings.endGroup(); |
| 245 |
settings.beginGroup("ShadowMargin"); |
| 246 |
shadowMargin_ = readMargin(settings); |
| 247 |
settings.endGroup(); |
| 248 |
|
| 249 |
settings.beginGroup("Background"); |
| 250 |
background_.load(theme_, settings); |
| 251 |
settings.endGroup(); |
| 252 |
|
| 253 |
settings.beginGroup("Highlight"); |
| 254 |
highlight_.load(theme_, settings); |
| 255 |
settings.beginGroup("HighlightClickMargin"); |
| 256 |
highlightClickMargin_ = readMargin(settings); |
| 257 |
settings.endGroup(); |
| 258 |
settings.endGroup(); |
| 259 |
|
| 260 |
settings.beginGroup("PrevPage"); |
| 261 |
prev_.load(theme_, settings); |
| 262 |
settings.endGroup(); |
| 263 |
|
| 264 |
settings.beginGroup("NextPage"); |
| 265 |
next_.load(theme_, settings); |
| 266 |
settings.endGroup(); |
| 267 |
} |
| 268 |
|
| 269 |
} // namespace fcitx |
| 270 |
|
| 271 |
void fcitx::FcitxTheme::paint(QPainter *painter, |
| 272 |
const fcitx::BackgroundImage &image, |
| 273 |
QRect region) { |
| 274 |
auto marginTop = image.margin_.top(); |
| 275 |
auto marginBottom = image.margin_.bottom(); |
| 276 |
auto marginLeft = image.margin_.left(); |
| 277 |
auto marginRight = image.margin_.right(); |
| 278 |
int resizeHeight = image.image_.height() - marginTop - marginBottom; |
| 279 |
int resizeWidth = image.image_.width() - marginLeft - marginRight; |
| 280 |
|
| 281 |
if (resizeHeight <= 0) { |
| 282 |
resizeHeight = 1; |
| 283 |
} |
| 284 |
|
| 285 |
if (resizeWidth <= 0) { |
| 286 |
resizeWidth = 1; |
| 287 |
} |
| 288 |
|
| 289 |
if (region.height() < 0) { |
| 290 |
region.setHeight(resizeHeight); |
| 291 |
} |
| 292 |
|
| 293 |
if (region.width() < 0) { |
| 294 |
region.setWidth(resizeWidth); |
| 295 |
} |
| 296 |
|
| 297 |
/* |
| 298 |
* 7 8 9 |
| 299 |
* 4 5 6 |
| 300 |
* 1 2 3 |
| 301 |
*/ |
| 302 |
|
| 303 |
if (marginLeft && marginBottom) { |
| 304 |
/* part 1 */ |
| 305 |
painter->drawPixmap( |
| 306 |
QRect(0, region.height() - marginBottom, marginLeft, marginBottom) |
| 307 |
.translated(region.topLeft()), |
| 308 |
image.image_, |
| 309 |
QRect(0, marginTop + resizeHeight, marginLeft, marginBottom)); |
| 310 |
} |
| 311 |
|
| 312 |
if (marginRight && marginBottom) { |
| 313 |
/* part 3 */ |
| 314 |
painter->drawPixmap( |
| 315 |
QRect(region.width() - marginRight, region.height() - marginBottom, |
| 316 |
marginRight, marginBottom) |
| 317 |
.translated(region.topLeft()), |
| 318 |
image.image_, |
| 319 |
QRect(marginLeft + resizeWidth, marginTop + resizeHeight, |
| 320 |
marginRight, marginBottom)); |
| 321 |
} |
| 322 |
|
| 323 |
if (marginLeft && marginTop) { |
| 324 |
/* part 7 */ |
| 325 |
painter->drawPixmap( |
| 326 |
QRect(0, 0, marginLeft, marginTop).translated(region.topLeft()), |
| 327 |
image.image_, QRect(0, 0, marginLeft, marginTop)); |
| 328 |
} |
| 329 |
|
| 330 |
if (marginRight && marginTop) { |
| 331 |
/* part 9 */ |
| 332 |
painter->drawPixmap( |
| 333 |
QRect(region.width() - marginRight, 0, marginRight, marginTop) |
| 334 |
.translated(region.topLeft()), |
| 335 |
image.image_, |
| 336 |
QRect(marginLeft + resizeWidth, 0, marginRight, marginTop)); |
| 337 |
} |
| 338 |
|
| 339 |
/* part 2 & 8 */ |
| 340 |
if (marginTop) { |
| 341 |
painter->drawPixmap( |
| 342 |
QRect(marginLeft, 0, region.width() - marginLeft - marginRight, |
| 343 |
marginTop) |
| 344 |
.translated(region.topLeft()), |
| 345 |
image.image_, QRect(marginLeft, 0, resizeWidth, marginTop)); |
| 346 |
} |
| 347 |
|
| 348 |
if (marginBottom) { |
| 349 |
painter->drawPixmap(QRect(marginLeft, region.height() - marginBottom, |
| 350 |
region.width() - marginLeft - marginRight, |
| 351 |
marginBottom) |
| 352 |
.translated(region.topLeft()), |
| 353 |
image.image_, |
| 354 |
QRect(marginLeft, marginTop + resizeHeight, |
| 355 |
resizeWidth, marginBottom)); |
| 356 |
} |
| 357 |
|
| 358 |
/* part 4 & 6 */ |
| 359 |
if (marginLeft) { |
| 360 |
painter->drawPixmap(QRect(0, marginTop, marginLeft, |
| 361 |
region.height() - marginTop - marginBottom) |
| 362 |
.translated(region.topLeft()), |
| 363 |
image.image_, |
| 364 |
QRect(0, marginTop, marginLeft, resizeHeight)); |
| 365 |
} |
| 366 |
|
| 367 |
if (marginRight) { |
| 368 |
painter->drawPixmap(QRect(region.width() - marginRight, marginTop, |
| 369 |
marginRight, |
| 370 |
region.height() - marginTop - marginBottom) |
| 371 |
.translated(region.topLeft()), |
| 372 |
image.image_, |
| 373 |
QRect(marginLeft + resizeWidth, marginTop, |
| 374 |
marginRight, resizeHeight)); |
| 375 |
} |
| 376 |
|
| 377 |
/* part 5 */ |
| 378 |
{ |
| 379 |
painter->drawPixmap( |
| 380 |
QRect(marginLeft, marginTop, |
| 381 |
region.width() - marginLeft - marginRight, |
| 382 |
region.height() - marginTop - marginBottom) |
| 383 |
.translated(region.topLeft()), |
| 384 |
image.image_, |
| 385 |
QRect(marginLeft, marginTop, resizeWidth, resizeHeight)); |
| 386 |
} |
| 387 |
|
| 388 |
if (image.overlay_.isNull()) { |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
auto clipWidth = region.width() - image.overlayClipMargin_.left() - |
| 393 |
image.overlayClipMargin_.right(); |
| 394 |
auto clipHeight = region.height() - image.overlayClipMargin_.top() - |
| 395 |
image.overlayClipMargin_.bottom(); |
| 396 |
if (clipWidth <= 0 || clipHeight <= 0) { |
| 397 |
return; |
| 398 |
} |
| 399 |
QRect clipRect(region.topLeft() + QPoint(image.overlayClipMargin_.left(), |
| 400 |
image.overlayClipMargin_.top()), |
| 401 |
QSize(clipWidth, clipHeight)); |
| 402 |
|
| 403 |
int x = 0, y = 0; |
| 404 |
if (image.gravity_ == "Top Left" || image.gravity_ == "Center Left" || |
| 405 |
image.gravity_ == "Bottom Left") { |
| 406 |
x = image.overlayOffsetX_; |
| 407 |
} else if (image.gravity_ == "Top Center" || image.gravity_ == "Center" || |
| 408 |
image.gravity_ == "Bottom Center") { |
| 409 |
x = (region.width() - image.overlay_.width()) / 2 + |
| 410 |
image.overlayOffsetX_; |
| 411 |
} else { |
| 412 |
x = region.width() - image.overlay_.width() - image.overlayOffsetX_; |
| 413 |
} |
| 414 |
|
| 415 |
if (image.gravity_ == "Top Left" || image.gravity_ == "Top Center" || |
| 416 |
image.gravity_ == "Top Right") { |
| 417 |
y = image.overlayOffsetY_; |
| 418 |
} else if (image.gravity_ == "Center Left" || image.gravity_ == "Center" || |
| 419 |
image.gravity_ == "Center Right") { |
| 420 |
y = (region.height() - image.overlay_.height()) / 2 + |
| 421 |
image.overlayOffsetY_; |
| 422 |
} else { |
| 423 |
y = region.height() - image.overlay_.height() - image.overlayOffsetY_; |
| 424 |
} |
| 425 |
QRect rect(QPoint(x, y) + region.topLeft(), image.overlay_.size()); |
| 426 |
QRect finalRect = rect.intersected(clipRect); |
| 427 |
if (finalRect.isEmpty()) { |
| 428 |
return; |
| 429 |
} |
| 430 |
|
| 431 |
if (image.hideOverlayIfOversize_ && !clipRect.contains(rect)) { |
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
painter->save(); |
| 436 |
painter->setClipRect(clipRect); |
| 437 |
painter->drawPixmap(rect, image.overlay_); |
| 438 |
painter->restore(); |
| 439 |
} |
| 440 |
|
| 441 |
void fcitx::FcitxTheme::paint(QPainter *painter, |
| 442 |
const fcitx::ActionImage &image, QPoint position, |
| 443 |
float alpha) { |
| 444 |
painter->save(); |
| 445 |
painter->setOpacity(alpha); |
| 446 |
painter->drawPixmap(position, image.image_); |
| 447 |
painter->restore(); |
| 448 |
} |
| 449 |
|
| 450 |
QMargins fcitx::FcitxTheme::highlightMargin() const { |
| 451 |
return highlight_.margin_; |
| 452 |
} |