File indexing completed on 2024-11-10 04:56:48

0001 /*
0002     SPDX-FileCopyrightText: 2019 Valerio Pilo <vpilo@coldshock.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "utils.h"
0008 
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 
0012 namespace
0013 {
0014 const QMap<QString, KDecoration2::BorderSize> s_borderSizes{
0015     {QStringLiteral("None"), KDecoration2::BorderSize::None},
0016     {QStringLiteral("NoSides"), KDecoration2::BorderSize::NoSides},
0017     {QStringLiteral("Tiny"), KDecoration2::BorderSize::Tiny},
0018     {QStringLiteral("Normal"), KDecoration2::BorderSize::Normal},
0019     {QStringLiteral("Large"), KDecoration2::BorderSize::Large},
0020     {QStringLiteral("VeryLarge"), KDecoration2::BorderSize::VeryLarge},
0021     {QStringLiteral("Huge"), KDecoration2::BorderSize::Huge},
0022     {QStringLiteral("VeryHuge"), KDecoration2::BorderSize::VeryHuge},
0023     {QStringLiteral("Oversized"), KDecoration2::BorderSize::Oversized}};
0024 const QMap<KDecoration2::BorderSize, QString> s_borderSizeNames{
0025     {KDecoration2::BorderSize::None, i18n("No Window Borders")},
0026     {KDecoration2::BorderSize::NoSides, i18n("No Side Window Borders")},
0027     {KDecoration2::BorderSize::Tiny, i18n("Tiny Window Borders")},
0028     {KDecoration2::BorderSize::Normal, i18n("Normal Window Borders")},
0029     {KDecoration2::BorderSize::Large, i18n("Large Window Borders")},
0030     {KDecoration2::BorderSize::VeryLarge, i18n("Very Large Window Borders")},
0031     {KDecoration2::BorderSize::Huge, i18n("Huge Window Borders")},
0032     {KDecoration2::BorderSize::VeryHuge, i18n("Very Huge Window Borders")},
0033     {KDecoration2::BorderSize::Oversized, i18n("Oversized Window Borders")}};
0034 
0035 const QHash<KDecoration2::DecorationButtonType, QChar> s_buttonNames{
0036     {KDecoration2::DecorationButtonType::Menu, QChar('M')},
0037     {KDecoration2::DecorationButtonType::ApplicationMenu, QChar('N')},
0038     {KDecoration2::DecorationButtonType::OnAllDesktops, QChar('S')},
0039     {KDecoration2::DecorationButtonType::ContextHelp, QChar('H')},
0040     {KDecoration2::DecorationButtonType::Minimize, QChar('I')},
0041     {KDecoration2::DecorationButtonType::Maximize, QChar('A')},
0042     {KDecoration2::DecorationButtonType::Close, QChar('X')},
0043     {KDecoration2::DecorationButtonType::KeepAbove, QChar('F')},
0044     {KDecoration2::DecorationButtonType::KeepBelow, QChar('B')},
0045     {KDecoration2::DecorationButtonType::Shade, QChar('L')},
0046     {KDecoration2::DecorationButtonType::Spacer, QChar('_')},
0047 };
0048 }
0049 
0050 namespace Utils
0051 {
0052 
0053 QString buttonsToString(const DecorationButtonsList &buttons)
0054 {
0055     auto buttonToString = [](KDecoration2::DecorationButtonType button) -> QChar {
0056         const auto it = s_buttonNames.constFind(button);
0057         if (it != s_buttonNames.constEnd()) {
0058             return it.value();
0059         }
0060         return QChar();
0061     };
0062     QString ret;
0063     for (auto button : buttons) {
0064         ret.append(buttonToString(button));
0065     }
0066     return ret;
0067 }
0068 
0069 DecorationButtonsList buttonsFromString(const QString &buttons)
0070 {
0071     DecorationButtonsList ret;
0072     for (auto it = buttons.begin(); it != buttons.end(); ++it) {
0073         for (auto it2 = s_buttonNames.constBegin(); it2 != s_buttonNames.constEnd(); ++it2) {
0074             if (it2.value() == (*it)) {
0075                 ret << it2.key();
0076             }
0077         }
0078     }
0079     return ret;
0080 }
0081 
0082 DecorationButtonsList readDecorationButtons(const KConfigGroup &config, const QString &key, const DecorationButtonsList &defaultValue)
0083 {
0084     return buttonsFromString(config.readEntry(key, buttonsToString(defaultValue)));
0085 }
0086 
0087 KDecoration2::BorderSize stringToBorderSize(const QString &name)
0088 {
0089     auto it = s_borderSizes.constFind(name);
0090     if (it == s_borderSizes.constEnd()) {
0091         // non sense values are interpreted just like normal
0092         return KDecoration2::BorderSize::Normal;
0093     }
0094     return it.value();
0095 }
0096 
0097 QString borderSizeToString(KDecoration2::BorderSize size)
0098 {
0099     return s_borderSizes.key(size);
0100 }
0101 
0102 const QMap<KDecoration2::BorderSize, QString> &getBorderSizeNames()
0103 {
0104     return s_borderSizeNames;
0105 }
0106 
0107 } // namespace Utils