File indexing completed on 2025-02-09 06:01:30

0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include <KColorSchemeManager>
0005 #include <KSharedConfig>
0006 #include <QAbstractItemModel>
0007 #include <KColorScheme>
0008 
0009 #include "colorschemer.h"
0010 // #include <QDebug>
0011 
0012 ColorSchemer::ColorSchemer(QObject *parent)
0013     : QObject(parent)
0014     , c(new KColorSchemeManager(this))
0015 {
0016 }
0017 
0018 QAbstractItemModel *ColorSchemer::model() const
0019 {
0020     return c->model();
0021 }
0022 
0023 void ColorSchemer::apply(int idx)
0024 {
0025     c->activateScheme(c->model()->index(idx, 0));
0026 }
0027 
0028 void ColorSchemer::apply(const QString &name)
0029 {
0030     c->activateScheme(c->indexForScheme(name));
0031 }
0032 
0033 int ColorSchemer::indexForScheme(const QString &name) const
0034 {
0035     auto index = c->indexForScheme(name).row();
0036     if (index == -1) {
0037         index = 0;
0038     }
0039     return index;
0040 }
0041 
0042 QString ColorSchemer::nameForIndex(int index) const
0043 {
0044     return c->model()->data(c->model()->index(index, 0), Qt::DisplayRole).toString();
0045 }
0046 
0047 QVariantMap ColorSchemer::getUsefullColors(int index) const
0048 {
0049     QString schemePath = c->model()->data(c->model()->index(index, 0), Qt::UserRole).toString();
0050 
0051     KSharedConfigPtr schemeConfig = KSharedConfig::openConfig(schemePath, KConfig::SimpleConfig);
0052 
0053     KColorScheme activeView(QPalette::Active, KColorScheme::View, schemeConfig);
0054 
0055     QString bodyColor = activeView.background(activeView.NormalBackground).color().name(QColor::HexRgb);
0056     QString textColor = activeView.foreground(activeView.NormalText).color().name(QColor::HexRgb);
0057     QString titleColor = activeView.foreground(activeView.InactiveText).color().name(QColor::HexRgb);
0058     QString linkColor = activeView.foreground(activeView.LinkText).color().name(QColor::HexRgb);
0059     QString visitedLinkColor = activeView.foreground(activeView.VisitedText).color().name(QColor::HexRgb);
0060     QString codeColor = activeView.background(activeView.AlternateBackground).color().name(QColor::HexRgb);
0061 
0062     QVariantMap res;
0063     res.insert(QStringLiteral("--bodyColor"), bodyColor);
0064     res.insert(QStringLiteral("--textColor"), textColor);
0065     res.insert(QStringLiteral("--titleColor"), titleColor);
0066     res.insert(QStringLiteral("--linkColor"), linkColor);
0067     res.insert(QStringLiteral("--visitedLinkColor"), visitedLinkColor);
0068     res.insert(QStringLiteral("--codeColor"), codeColor);
0069 
0070     return res;
0071 }
0072 
0073 #include "moc_colorschemer.cpp"