// SPDX-License-Identifier: LGPL-2.1-or-later /**************************************************************************** * * * Copyright (c) 2024 The FreeCAD Project Association AISBL * * * * This file is part of FreeCAD. * * * * FreeCAD is free software: you can redistribute it and/or modify it * * under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 2.1 of the * * License, or (at your option) any later version. * * * * FreeCAD is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with FreeCAD. If not, see * * . * * * ***************************************************************************/ #include "PreCompiled.h" #ifndef _PreComp_ #include #include #include #include #include #endif #include "ThemeSelectorWidget.h" #include <3rdParty/GSL/include/gsl/pointers> #include #include #include using namespace StartGui; ThemeSelectorWidget::ThemeSelectorWidget(QWidget* parent) : QWidget(parent) , _titleLabel {nullptr} , _descriptionLabel {nullptr} , _buttons {nullptr, nullptr, nullptr} { setObjectName(QLatin1String("ThemeSelectorWidget")); setupUi(); qApp->installEventFilter(this); } void ThemeSelectorWidget::setupButtons(QBoxLayout* layout) { if (!layout) { return; } std::map themeMap {{Theme::Classic, tr("Classic")}, {Theme::Light, tr("Light")}, {Theme::Dark, tr("Dark")}}; std::map iconMap { {Theme::Classic, QIcon(QLatin1String(":/thumbnails/Classic512.png"))}, {Theme::Light, QIcon(QLatin1String(":/thumbnails/OpenLight512.png"))}, {Theme::Dark, QIcon(QLatin1String(":/thumbnails/OpenDark512.png"))}}; auto hGrp = App::GetApplication().GetParameterGroupByPath( "User parameter:BaseApp/Preferences/MainWindow"); auto styleSheetName = QString::fromStdString(hGrp->GetASCII("StyleSheet")); for (const auto& theme : themeMap) { auto button = gsl::owner(new QToolButton()); button->setCheckable(true); button->setAutoExclusive(true); button->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextUnderIcon); button->setText(theme.second); button->setIcon(iconMap[theme.first]); button->setIconSize(iconMap[theme.first].actualSize(QSize(256, 256))); if (theme.first == Theme::Classic && styleSheetName.isEmpty()) { button->setChecked(true); } else if (theme.first == Theme::Light && styleSheetName.contains(QLatin1String("light"), Qt::CaseSensitivity::CaseInsensitive)) { button->setChecked(true); } else if (theme.first == Theme::Dark && styleSheetName.contains(QLatin1String("dark"), Qt::CaseSensitivity::CaseInsensitive)) { button->setChecked(true); } connect(button, &QToolButton::clicked, this, [this, theme] { themeChanged(theme.first); }); layout->addWidget(button); _buttons[static_cast(theme.first)] = button; } } void ThemeSelectorWidget::setupUi() { auto* outerLayout = gsl::owner(new QVBoxLayout(this)); auto* buttonLayout = gsl::owner(new QHBoxLayout); _titleLabel = gsl::owner(new QLabel); _descriptionLabel = gsl::owner(new QLabel); outerLayout->addWidget(_titleLabel); outerLayout->addWidget(_descriptionLabel); outerLayout->addLayout(buttonLayout); setupButtons(buttonLayout); retranslateUi(); } void ThemeSelectorWidget::themeChanged(Theme newTheme) { // Run the appropriate preference pack: auto prefPackManager = Gui::Application::Instance->prefPackManager(); switch (newTheme) { case Theme::Classic: prefPackManager->apply("Classic"); break; case Theme::Dark: prefPackManager->apply("Dark"); break; case Theme::Light: prefPackManager->apply("Light"); break; } } bool ThemeSelectorWidget::eventFilter(QObject* object, QEvent* event) { if (object == this && event->type() == QEvent::LanguageChange) { this->retranslateUi(); } return QWidget::eventFilter(object, event); } void ThemeSelectorWidget::retranslateUi() { _titleLabel->setText(QLatin1String("

") + tr("Theme") + QLatin1String("

")); _descriptionLabel->setText(tr("More themes are available online using the Addon Manager")); _buttons[static_cast(Theme::Classic)]->setText(tr("Classic", "Visual theme name")); _buttons[static_cast(Theme::Light)]->setText(tr("Light", "Visual theme name")); _buttons[static_cast(Theme::Dark)]->setText(tr("Dark", "Visual theme name")); }