1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/*
* This file is part of the KDE project
* SPDX-FileCopyrightText: 2014 Arjen Hiemstra <ahiemstra@heimr.nl>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "Theme.h"
#include <QApplication>
#include <QColor>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFont>
#include <QFontDatabase>
#include <QQmlComponent>
#include <QStandardPaths>
#include <QStringList>
#include <QUrl>
#include <QWidget>
#include <KIconLoader>
#include "QmlGlobalEngine.h"
#ifdef Q_OS_WIN
#include <windows.h>
#endif
class Theme::Private
{
public:
Private()
: inheritedTheme(nullptr)
, iconPath("icons/")
, imagePath("images/")
, fontPath("fonts/")
, fontsAdded(false)
, lineCountLandscape(40)
, lineCountPortrait(70)
{
}
void rebuildFontCache();
QString id;
QString name;
QString inherits;
Theme *inheritedTheme;
QVariantMap colors;
QVariantMap sizes;
QVariantMap fonts;
QString basePath;
QString iconPath;
QString imagePath;
QString fontPath;
QHash<QString, QColor> colorCache;
QHash<QString, QFont> fontMap;
bool fontsAdded;
QList<int> addedFonts;
int lineCountLandscape;
int lineCountPortrait;
};
Theme::Theme(QObject *parent)
: QObject(parent)
, d(new Private)
{
qApp->installEventFilter(this);
}
Theme::~Theme()
{
QFontDatabase db;
Q_FOREACH (int id, d->addedFonts) {
db.removeApplicationFont(id);
}
delete d;
}
QString Theme::id() const
{
return d->id;
}
void Theme::setId(const QString &newValue)
{
if (newValue != d->id) {
d->id = newValue;
const QString qmlFileSubPath = QStringLiteral("calligragemini/themes/") + d->id + QStringLiteral("/theme.qml");
const QString qmlFileFullPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, qmlFileSubPath);
d->basePath = QFileInfo(qmlFileFullPath).dir().absolutePath();
emit idChanged();
}
}
QString Theme::name() const
{
return d->name;
}
void Theme::setName(const QString &newValue)
{
if (newValue != d->name) {
d->name = newValue;
emit nameChanged();
}
}
QString Theme::inherits() const
{
return d->inherits;
}
void Theme::setInherits(const QString &newValue)
{
if (newValue != d->inherits) {
if (d->inheritedTheme) {
delete d->inheritedTheme;
d->inheritedTheme = nullptr;
}
d->inherits = newValue;
if (!d->inherits.isEmpty()) {
d->inheritedTheme = Theme::load(d->inherits, this);
connect(d->inheritedTheme, &Theme::fontCacheRebuilt, this, &Theme::fontCacheRebuilt);
}
emit inheritsChanged();
}
}
QVariantMap Theme::colors() const
{
return d->colors;
}
void Theme::setColors(const QVariantMap &newValue)
{
if (newValue != d->colors) {
d->colors = newValue;
emit colorsChanged();
}
}
QColor Theme::color(const QString &name)
{
if (d->colorCache.contains(name))
return d->colorCache.value(name);
QStringList parts = name.split('/');
QColor result;
if (!parts.isEmpty()) {
QVariantMap map = d->colors;
QString current = parts.takeFirst();
while (map.contains(current)) {
QVariant value = map.value(current);
if (value.type() == QVariant::Map) {
if (parts.isEmpty())
break;
map = value.toMap();
current = parts.takeFirst();
} else {
result = value.value<QColor>();
map = QVariantMap();
}
}
}
if (!result.isValid() && d->inheritedTheme) {
result = d->inheritedTheme->color(name);
}
if (!result.isValid()) {
qWarning() << "Unable to find color" << name;
} else {
d->colorCache.insert(name, result);
}
return result;
}
QVariantMap Theme::sizes() const
{
return d->sizes;
}
void Theme::setSizes(const QVariantMap &newValue)
{
if (newValue != d->sizes) {
d->sizes = newValue;
emit sizesChanged();
}
}
float Theme::size(const QString &name)
{
Q_UNUSED(name);
return 0.f;
}
QVariantMap Theme::fonts() const
{
return d->fonts;
}
void Theme::setFonts(const QVariantMap &newValue)
{
if (newValue != d->fonts) {
d->fonts = newValue;
d->fontMap.clear();
emit fontsChanged();
}
}
QFont Theme::font(const QString &name)
{
if (!d->fontsAdded) {
QDir fontDir(d->basePath + '/' + d->fontPath);
QStringList entries = fontDir.entryList(QDir::Files);
QFontDatabase db;
Q_FOREACH (const QString &entry, entries) {
d->addedFonts.append(db.addApplicationFont(fontDir.absoluteFilePath(entry)));
}
d->fontsAdded = true;
}
if (d->fontMap.isEmpty()) {
d->rebuildFontCache();
}
if (d->fontMap.contains(name))
return d->fontMap.value(name);
if (d->inheritedTheme)
return d->inheritedTheme->font(name);
qWarning() << "Unable to find font" << name;
return QFont();
}
QString Theme::fontPath() const
{
return d->fontPath;
}
void Theme::setFontPath(const QString &newValue)
{
if (newValue != d->fontPath) {
if (!d->addedFonts.isEmpty()) {
QFontDatabase db;
Q_FOREACH (int id, d->addedFonts) {
db.removeApplicationFont(id);
}
d->addedFonts.clear();
}
d->fontPath = newValue;
d->fontsAdded = false;
emit fontPathChanged();
}
}
QString Theme::iconPath() const
{
return d->iconPath;
}
void Theme::setIconPath(const QString &newValue)
{
if (newValue != d->iconPath) {
d->iconPath = newValue;
emit iconPathChanged();
}
}
QUrl Theme::icon(const QString &name, bool useSystemFallback)
{
QString url = QString("%1/%2/%3.svg").arg(d->basePath, d->iconPath, name);
if (!QFile::exists(url)) {
if (d->inheritedTheme) {
return d->inheritedTheme->icon(name);
} else {
if (useSystemFallback) {
url = KIconLoader::global()->iconPath(name, -128);
qWarning() << "Attempting to use a system fallback icon" << url;
} else {
qWarning() << "Unable to find icon" << url;
}
}
}
return QUrl::fromLocalFile(url);
}
QIcon Theme::iconActual(const QString &name)
{
return QIcon(icon(name).toLocalFile());
}
QString Theme::imagePath() const
{
return d->imagePath;
}
void Theme::setImagePath(const QString &newValue)
{
if (newValue != d->imagePath) {
d->imagePath = newValue;
emit imagePathChanged();
}
}
QUrl Theme::image(const QString &name)
{
QString url = QString("%1/%2/%3").arg(d->basePath, d->imagePath, name);
if (!QFile::exists(url)) {
if (d->inheritedTheme) {
return d->inheritedTheme->image(name);
} else {
qWarning() << "Unable to find image" << url;
}
}
return QUrl::fromLocalFile(url);
}
Theme *Theme::load(const QString &id, QObject *parent)
{
QString qml;
// Ugly hacky stuff for making things work on Windows
#ifdef Q_OS_WIN
QDir appdir(qApp->applicationDirPath());
// Corrects for mismatched case errors in path (qtdeclarative fails to load)
wchar_t buffer[1024];
QString absolute = appdir.absolutePath();
DWORD rv = ::GetShortPathName((wchar_t *)absolute.utf16(), buffer, 1024);
rv = ::GetLongPathName(buffer, buffer, 1024);
QString correctedPath((QChar *)buffer);
appdir.setPath(correctedPath);
// for now, the app in bin/ and we still use the env.bat script
appdir.cdUp();
qml = QString("%1/bin/data/calligragemini/themes/%2/theme.qml").arg(appdir.canonicalPath(), id);
#else
const QString qmlFileSubPath = QStringLiteral("calligragemini/themes/") + id + QStringLiteral("/theme.qml");
qml = QStandardPaths::locate(QStandardPaths::GenericDataLocation, qmlFileSubPath);
#endif
QQmlComponent themeComponent(QmlGlobalEngine::instance()->engine(), parent);
themeComponent.loadUrl(QUrl::fromLocalFile(qml));
if (themeComponent.isError()) {
qWarning() << themeComponent.errorString();
return nullptr;
}
Theme *theme = qobject_cast<Theme *>(themeComponent.create());
if (!theme) {
qWarning() << "Failed to create theme instance!";
return nullptr;
}
return theme;
}
bool Theme::eventFilter(QObject *target, QEvent *event)
{
if (target == qApp->activeWindow() && target->inherits("QMainWindow") && event->type() == QEvent::Resize) {
d->rebuildFontCache();
emit fontCacheRebuilt();
}
return QObject::eventFilter(target, event);
}
int Theme::adjustedPixel(const int &pixel) const
{
if (!qApp->activeWindow())
return 0;
// If we are in portrait mode, we still assume 1080p for font size purposes
int width = qApp->activeWindow()->height() > qApp->activeWindow()->width() ? qApp->activeWindow()->height() : qApp->activeWindow()->width();
// The pixel size is based on a 1080p screen, and it is accepted that the window size
// will vary slightly on there, depending on whether or not we are full screened (so
// we accept up to 10 pixels less width)
float sizeAdjustment = 1;
if (width > 1920 || width < 1910)
sizeAdjustment = width / 1920.f;
return pixel * sizeAdjustment;
}
void Theme::Private::rebuildFontCache()
{
fontMap.clear();
QFontDatabase db;
for (QVariantMap::ConstIterator itr = fonts.constBegin(); itr != fonts.constEnd(); ++itr) {
QVariantMap map = itr->toMap();
if (map.isEmpty())
continue;
QFont font = db.font(map.value("family").toString(), map.value("style", "Regular").toString(), 10);
if (font.isCopyOf(qApp->font()))
qWarning() << "Could not find font" << map.value("family") << "with style" << map.value("style", "Regular");
if (map.contains("pixelSize")) {
// If we are in portrait mode, we still assume 1080p for font size purposes
int width = qApp->activeWindow()->height() > qApp->activeWindow()->width() ? qApp->activeWindow()->height() : qApp->activeWindow()->width();
// The pixel size is based on a 1080p screen, and it is accepted that the window size
// will vary slightly on there, depending on whether or not we are full screened (so
// we accept up to 10 pixels less width)
float sizeAdjustment = 1;
if (width > 1920 || width < 1910)
sizeAdjustment = width / 1920.f;
font.setPixelSize(map.value("pixelSize").toInt() * sizeAdjustment);
} else {
float lineCount = qApp->activeWindow()->height() > qApp->activeWindow()->width() ? lineCountPortrait : lineCountLandscape;
float lineHeight = qApp->activeWindow()->height() / lineCount;
font.setPixelSize(lineHeight * map.value("size", 1).toFloat());
}
fontMap.insert(itr.key(), font);
}
}
|