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

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

sync with github

File size: 3.5 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2017-2018, 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 "ThemeWidget.h"
20
21#include "ThemeInfo.h"
22
23#include "utils/CalamaresUtilsGui.h"
24#include "utils/Logger.h"
25#include "Branding.h"
26
27#include <QDir>
28#include <QFileInfo>
29#include <QHBoxLayout>
30#include <QLabel>
31#include <QRadioButton>
32#include <QString>
33
34/**
35 * Massage the given @p path to the most-likely
36 * path that actually contains a screenshot. For
37 * empty image paths, returns the QRC path for an
38 * empty screenshot. Returns blank if the path
39 * doesn't exist anywhere in the search paths.
40 */
41static QString _munge_imagepath( const QString& path )
42{
43    if ( path.isEmpty() )
44        return ":/view-preview.png";
45
46    if ( path.startsWith( '/' ) )
47        return path;
48
49    if ( QFileInfo::exists( path ) )
50        return path;
51
52    QFileInfo fi( QDir( Calamares::Branding::instance()->componentDirectory() ), path );
53    if ( fi.exists() )
54        return fi.absoluteFilePath();
55
56    return QString();
57}
58
59ThemeWidget::ThemeWidget(const ThemeInfo& info, QWidget* parent)
60    : QWidget( parent )
61    , m_id( info.id )
62    , m_check( new QRadioButton( info.name.isEmpty() ? info.id : info.name, parent ) )
63    , m_description( new QLabel( info.description, parent ) )
64{
65    const QSize image_size{
66        qMax(12 * CalamaresUtils::defaultFontHeight(), 120),
67        qMax(8 * CalamaresUtils::defaultFontHeight(), 80) };
68
69    QHBoxLayout* layout = new QHBoxLayout( this );
70    this->setLayout( layout );
71
72    layout->addWidget( m_check, 1 );
73
74    QPixmap image( _munge_imagepath( info.imagePath ) );
75    if ( image.isNull() )
76    {
77        // Not found or not specified, so convert the name into some (horrible, likely)
78        // color instead.
79        image = QPixmap( image_size );
80        auto hash_color = qHash( info.imagePath.isEmpty() ? info.id : info.imagePath );
81        cDebug() << "Theme image" << info.imagePath << "not found, hash" << hash_color;
82        image.fill( QColor( QRgb( hash_color ) ) );
83    }
84
85    image = image.scaled( image_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
86
87    QLabel* image_label = new QLabel( this );
88    image_label->setPixmap( image );
89    image_label->setMinimumSize( image_size );
90    image_label->setMaximumSize( image_size );
91    image_label->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
92    layout->addWidget( image_label, 1 );
93    layout->addWidget( m_description, 3 );
94
95    connect( m_check, &QRadioButton::toggled, this, &ThemeWidget::clicked );
96}
97
98void
99ThemeWidget::clicked( bool checked )
100{
101    if ( checked )
102        emit themeSelected( m_id );
103}
104
105QAbstractButton*
106ThemeWidget::button() const
107{
108    return m_check;
109}
110
111void ThemeWidget::updateThemeName(const ThemeInfo& info)
112{
113    m_check->setText( info.name );
114    m_description->setText( info.description );
115}
Note: See TracBrowser for help on using the repository browser.