source: calamares/trunk/fuentes/src/modules/plasmalnf/PlasmaLnfPage.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 5.0 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "PlasmaLnfPage.h"
20
21#include "ui_page_plasmalnf.h"
22
23#include "utils/Logger.h"
24#include "utils/Retranslator.h"
25
26#include <QAbstractButton>
27
28#include <KPackage/Package>
29#include <KPackage/PackageLoader>
30
31ThemeInfo::ThemeInfo( const KPluginMetaData& data )
32    : id( data.pluginId() )
33    , name( data.name() )
34    , description( data.description() )
35    , widget( nullptr )
36{
37}
38
39static ThemeInfoList plasma_themes()
40{
41    ThemeInfoList packages;
42
43    QList<KPluginMetaData> pkgs = KPackage::PackageLoader::self()->listPackages( "Plasma/LookAndFeel" );
44
45    for ( const KPluginMetaData& data : pkgs )
46    {
47        if ( data.isValid() && !data.isHidden() && !data.name().isEmpty() )
48        {
49            packages << ThemeInfo{ data };
50        }
51    }
52
53    return packages;
54}
55
56
57PlasmaLnfPage::PlasmaLnfPage( QWidget* parent )
58    : QWidget( parent )
59    , ui( new Ui::PlasmaLnfPage )
60    , m_showAll( false )
61    , m_buttonGroup( nullptr )
62{
63    ui->setupUi( this );
64    CALAMARES_RETRANSLATE(
65    {
66        ui->retranslateUi( this );
67        ui->generalExplanation->setText( tr(
68            "Please choose a look-and-feel for the KDE Plasma Desktop. "
69            "You can also skip this step and configure the look-and-feel "
70            "once the system is installed. Clicking on a look-and-feel "
71            "selection will give you a live preview of that look-and-feel.") );
72        updateThemeNames();
73        fillUi();
74    }
75    )
76}
77
78void
79PlasmaLnfPage::setLnfPath( const QString& path )
80{
81    m_lnfPath = path;
82}
83
84void
85PlasmaLnfPage::setEnabledThemes(const ThemeInfoList& themes, bool showAll )
86{
87    m_enabledThemes = themes;
88
89    if ( showAll )
90    {
91        auto plasmaThemes = plasma_themes();
92        for ( auto& installed_theme : plasmaThemes )
93            if ( !m_enabledThemes.findById( installed_theme.id ) )
94                m_enabledThemes.append( installed_theme );
95    }
96
97    updateThemeNames();
98    winnowThemes();
99    fillUi();
100}
101
102void
103PlasmaLnfPage::setEnabledThemesAll()
104{
105    // Don't need to set showAll=true, because we're already passing in
106    // the complete list of installed themes.
107    setEnabledThemes( plasma_themes(), false );
108}
109
110void
111PlasmaLnfPage::setPreselect( const QString& id )
112{
113    m_preselect = id;
114    if ( !m_enabledThemes.isEmpty() )
115        fillUi();
116}
117
118void PlasmaLnfPage::updateThemeNames()
119{
120    auto plasmaThemes = plasma_themes();
121    for ( auto& enabled_theme : m_enabledThemes )
122    {
123        ThemeInfo* t = plasmaThemes.findById( enabled_theme.id );
124        if ( t != nullptr )
125        {
126            enabled_theme.name = t->name;
127            enabled_theme.description = t->description;
128        }
129    }
130}
131
132void PlasmaLnfPage::winnowThemes()
133{
134    auto plasmaThemes = plasma_themes();
135    bool winnowed = true;
136    int winnow_index = 0;
137    while ( winnowed )
138    {
139        winnowed = false;
140        winnow_index = 0;
141
142        for ( auto& enabled_theme : m_enabledThemes )
143        {
144            ThemeInfo* t = plasmaThemes.findById( enabled_theme.id );
145            if ( t == nullptr )
146            {
147                cDebug() << "Removing" << enabled_theme.id;
148                winnowed = true;
149                break;
150            }
151            ++winnow_index;
152        }
153
154        if ( winnowed )
155        {
156            m_enabledThemes.removeAt( winnow_index );
157        }
158    }
159}
160
161void PlasmaLnfPage::fillUi()
162{
163    if ( m_enabledThemes.isEmpty() )
164    {
165        return;
166    }
167
168    if ( !m_buttonGroup )
169    {
170        m_buttonGroup = new QButtonGroup( this );
171        m_buttonGroup->setExclusive( true );
172    }
173
174    int c = 1; // After the general explanation
175    for ( auto& theme : m_enabledThemes )
176    {
177        if ( !theme.widget )
178        {
179            ThemeWidget* w = new ThemeWidget( theme );
180            m_buttonGroup->addButton( w->button() );
181            ui->verticalLayout->insertWidget( c, w );
182            connect( w, &ThemeWidget::themeSelected, this, &PlasmaLnfPage::plasmaThemeSelected);
183            theme.widget = w;
184        }
185        else
186        {
187            theme.widget->updateThemeName( theme );
188        }
189        if ( theme.id == m_preselect )
190        {
191            const QSignalBlocker b( theme.widget->button() );
192            theme.widget->button()->setChecked( true );
193        }
194        ++c;
195    }
196}
Note: See TracBrowser for help on using the repository browser.