File indexing completed on 2024-04-28 05:27:05

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Mikhail Zolotukhin <zomial@protonmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include <QFile>
0008 #include <QStandardPaths>
0009 #include <QSvgRenderer>
0010 
0011 #include "auroraedecorationpainter.h"
0012 
0013 const QString AuroraeDecorationPainter::s_auroraeThemesPath =
0014     QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/aurorae/themes/");
0015 
0016 AuroraeDecorationPainter::AuroraeDecorationPainter(const QString &themeName)
0017     : DecorationPainter()
0018     , m_themeName(themeName)
0019     , m_themePath(s_auroraeThemesPath + themeName + '/')
0020 {
0021 }
0022 
0023 void AuroraeDecorationPainter::paintButton(QPainter &painter, const QString &buttonType, const QString &buttonState) const
0024 {
0025     const QString buttonFileName = buttonTypeToFileName(buttonType);
0026     const QString elementIdName = buttonStateToElementId(buttonState);
0027 
0028     const QString buttonFilePath = m_themePath + buttonFileName;
0029 
0030     QSvgRenderer buttonRenderer;
0031     buttonRenderer.load(buttonFilePath) || buttonRenderer.load(buttonFilePath + "z");
0032     buttonRenderer.render(&painter, elementIdName, DecorationPainter::ButtonGeometry);
0033 }
0034 
0035 QString AuroraeDecorationPainter::buttonTypeToFileName(const QString &buttonType) const
0036 {
0037     if (buttonType == QStringLiteral("maximized")) {
0038         static const QString restoreButtonFileName = QStringLiteral("restore.svg");
0039         if (QFile(m_themePath + restoreButtonFileName).exists()) {
0040             return restoreButtonFileName;
0041         } else {
0042             return QStringLiteral("maximize.svg");
0043         }
0044     } else {
0045         return buttonType + QStringLiteral(".svg");
0046     }
0047 }
0048 
0049 QString AuroraeDecorationPainter::buttonStateToElementId(const QString &buttonState) const
0050 {
0051     // Aurorae themes do not contain pressed and hovered inactive states,
0052     // so we put backdrop option to the end, so that first we use pressed and hover ones
0053     if (buttonState.contains(QStringLiteral("active"))) {
0054         return QStringLiteral("pressed-center");
0055     } else if (buttonState.contains(QStringLiteral("hover"))) {
0056         return QStringLiteral("hover-center");
0057     } else if (buttonState.contains(QStringLiteral("backdrop"))) {
0058         return QStringLiteral("inactive-center");
0059     } else {
0060         return QStringLiteral("active-center");
0061     }
0062 }