File indexing completed on 2024-11-24 03:56:28

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "settings_dialog.hpp"
0008 #include "ui_settings_dialog.h"
0009 
0010 #include <QFormLayout>
0011 
0012 #include "app/application.hpp"
0013 #include "app/settings/settings.hpp"
0014 #include "app/widgets/no_close_on_enter.hpp"
0015 
0016 
0017 class app::SettingsDialog::Private : public app::Ui::SettingsDialog
0018 {
0019 public:
0020     app::widgets::NoCloseOnEnter ncoe;
0021 };
0022 
0023 
0024 static QIcon best_icon(const QIcon& icon, const QSize& target_size)
0025 {
0026     for ( const auto& size : icon.availableSizes() )
0027         if ( size.width() >= target_size.width() )
0028             return icon;
0029     return icon.pixmap(target_size);;
0030 }
0031 
0032 
0033 app::SettingsDialog::SettingsDialog ( QWidget* parent ) :
0034     QDialog(parent), d(std::make_unique<Private>())
0035 {
0036     d->setupUi(this);
0037     installEventFilter(&d->ncoe);
0038 
0039     for ( const auto& group : app::settings::Settings::instance().groups() )
0040     {
0041         if ( group->has_visible_settings() )
0042         {
0043             new QListWidgetItem(best_icon(group->icon(), d->list_widget->iconSize()), group->label(), d->list_widget);
0044             d->stacked_widget->addWidget(group->make_widget(d->stacked_widget));
0045         }
0046     }
0047 
0048     d->list_widget->setCurrentRow(0);
0049 }
0050 
0051 app::SettingsDialog::~SettingsDialog() = default;
0052 
0053 
0054 void app::SettingsDialog::changeEvent(QEvent *e)
0055 {
0056     QDialog::changeEvent(e);
0057 
0058     if ( e->type() == QEvent::LanguageChange)
0059     {
0060         d->retranslateUi(this);
0061         int i = 0;
0062         for ( const auto& group : app::settings::Settings::instance() )
0063         {
0064             if ( group->has_visible_settings() )
0065             {
0066                 d->list_widget->item(i)->setText(group->label());
0067                 i++;
0068             }
0069         }
0070     }
0071 }