mirror of https://github.com/cutefishos/calamares
Merge branch 'fix-slideshow'
commit
c3c4b79147
@ -0,0 +1,258 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
||||
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Slideshow.h"
|
||||
|
||||
#include "Branding.h"
|
||||
#include "utils/Dirs.h"
|
||||
#include "utils/Logger.h"
|
||||
#include "utils/Qml.h"
|
||||
#include "utils/Retranslator.h"
|
||||
|
||||
#include <QLabel>
|
||||
#include <QMutexLocker>
|
||||
#include <QQmlComponent>
|
||||
#include <QQmlEngine>
|
||||
#include <QQuickItem>
|
||||
#include <QQuickWidget>
|
||||
#include <QTimer>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace Calamares
|
||||
{
|
||||
|
||||
Slideshow::~Slideshow() {}
|
||||
|
||||
SlideshowQML::SlideshowQML( QWidget* parent )
|
||||
: Slideshow( parent )
|
||||
, m_qmlShow( new QQuickWidget )
|
||||
, m_qmlComponent( nullptr )
|
||||
, m_qmlObject( nullptr )
|
||||
{
|
||||
m_qmlShow->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
|
||||
m_qmlShow->setResizeMode( QQuickWidget::SizeRootObjectToView );
|
||||
m_qmlShow->engine()->addImportPath( CalamaresUtils::qmlModulesDir().absolutePath() );
|
||||
|
||||
cDebug() << "QML import paths:" << Logger::DebugList( m_qmlShow->engine()->importPathList() );
|
||||
#if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 )
|
||||
CALAMARES_RETRANSLATE( if ( m_qmlShow ) { m_qmlShow->engine()->retranslate(); } )
|
||||
#endif
|
||||
|
||||
if ( Branding::instance()->slideshowAPI() == 2 )
|
||||
{
|
||||
cDebug() << "QML load on startup, API 2.";
|
||||
loadQmlV2();
|
||||
}
|
||||
}
|
||||
|
||||
SlideshowQML::~SlideshowQML()
|
||||
{
|
||||
delete m_qmlObject;
|
||||
delete m_qmlComponent;
|
||||
delete m_qmlShow;
|
||||
}
|
||||
|
||||
QWidget*
|
||||
SlideshowQML::widget()
|
||||
{
|
||||
return m_qmlShow;
|
||||
}
|
||||
|
||||
void
|
||||
SlideshowQML::loadQmlV2()
|
||||
{
|
||||
QMutexLocker l( &m_mutex );
|
||||
if ( !m_qmlComponent && !Calamares::Branding::instance()->slideshowPath().isEmpty() )
|
||||
{
|
||||
m_qmlComponent = new QQmlComponent( m_qmlShow->engine(),
|
||||
QUrl::fromLocalFile( Calamares::Branding::instance()->slideshowPath() ),
|
||||
QQmlComponent::CompilationMode::Asynchronous );
|
||||
connect( m_qmlComponent, &QQmlComponent::statusChanged, this, &SlideshowQML::loadQmlV2Complete );
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SlideshowQML::loadQmlV2Complete()
|
||||
{
|
||||
QMutexLocker l( &m_mutex );
|
||||
if ( m_qmlComponent && m_qmlComponent->isReady() && !m_qmlObject )
|
||||
{
|
||||
cDebug() << "QML component complete, API 2";
|
||||
// Don't do this again
|
||||
disconnect( m_qmlComponent, &QQmlComponent::statusChanged, this, &SlideshowQML::loadQmlV2Complete );
|
||||
|
||||
QObject* o = m_qmlComponent->create();
|
||||
m_qmlObject = qobject_cast< QQuickItem* >( o );
|
||||
if ( !m_qmlObject )
|
||||
{
|
||||
delete o;
|
||||
}
|
||||
else
|
||||
{
|
||||
cDebug() << Logger::SubEntry << "Loading" << Calamares::Branding::instance()->slideshowPath();
|
||||
|
||||
// setContent() is public API, but not documented publicly.
|
||||
// It is marked \internal in the Qt sources, but does exactly
|
||||
// what is needed: sets up visual parent by replacing the root
|
||||
// item, and handling resizes.
|
||||
m_qmlShow->setContent(
|
||||
QUrl::fromLocalFile( Calamares::Branding::instance()->slideshowPath() ), m_qmlComponent, m_qmlObject );
|
||||
if ( isActive() )
|
||||
{
|
||||
// We're alreay visible! Must have been slow QML loading, and we
|
||||
// passed onActivate already.
|
||||
changeSlideShowState( Slideshow::Start );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Applies V1 and V2 QML activation / deactivation:
|
||||
* - V1 loads the QML in @p widget on activation. Sets root object property
|
||||
* *activatedInCalamares* as appropriate.
|
||||
* - V2 calls onActivate() or onLeave() in the QML as appropriate. Also
|
||||
* sets the *activatedInCalamares* property.
|
||||
*/
|
||||
void
|
||||
SlideshowQML::changeSlideShowState( Action state )
|
||||
{
|
||||
QMutexLocker l( &m_mutex );
|
||||
bool activate = state == Slideshow::Start;
|
||||
|
||||
if ( Branding::instance()->slideshowAPI() == 2 )
|
||||
{
|
||||
// The QML was already loaded in the constructor, need to start it
|
||||
CalamaresUtils::callQMLFunction( m_qmlObject, activate ? "onActivate" : "onLeave" );
|
||||
}
|
||||
else if ( !Calamares::Branding::instance()->slideshowPath().isEmpty() )
|
||||
{
|
||||
// API version 1 assumes onCompleted is the trigger
|
||||
if ( activate )
|
||||
{
|
||||
m_qmlShow->setSource( QUrl::fromLocalFile( Calamares::Branding::instance()->slideshowPath() ) );
|
||||
}
|
||||
// needs the root object for property setting, below
|
||||
m_qmlObject = m_qmlShow->rootObject();
|
||||
}
|
||||
|
||||
// V1 API has picked up the root object for use, V2 passed it in.
|
||||
if ( m_qmlObject )
|
||||
{
|
||||
static const char propertyName[] = "activatedInCalamares";
|
||||
auto property = m_qmlObject->property( propertyName );
|
||||
if ( property.isValid() && ( property.type() == QVariant::Bool ) && ( property.toBool() != activate ) )
|
||||
{
|
||||
m_qmlObject->setProperty( propertyName, activate );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ( Branding::instance()->slideshowAPI() == 2 ) && ( state == Slideshow::Stop ) )
|
||||
{
|
||||
delete m_qmlObject;
|
||||
m_qmlObject = nullptr;
|
||||
}
|
||||
|
||||
m_state = state;
|
||||
}
|
||||
|
||||
SlideshowPictures::SlideshowPictures( QWidget* parent )
|
||||
: Slideshow( parent )
|
||||
, m_label( new QLabel( parent ) )
|
||||
, m_timer( new QTimer( this ) )
|
||||
, m_imageIndex( 0 )
|
||||
, m_images( Branding::instance()->slideshowImages() )
|
||||
{
|
||||
m_label->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
|
||||
m_label->setAlignment( Qt::AlignCenter );
|
||||
m_timer->setInterval( std::chrono::milliseconds( 2000 ) );
|
||||
connect( m_timer, &QTimer::timeout, this, &SlideshowPictures::next );
|
||||
}
|
||||
|
||||
SlideshowPictures::~SlideshowPictures()
|
||||
{
|
||||
delete m_timer;
|
||||
delete m_label;
|
||||
}
|
||||
|
||||
QWidget*
|
||||
SlideshowPictures::widget()
|
||||
{
|
||||
return m_label;
|
||||
}
|
||||
|
||||
void
|
||||
SlideshowPictures::changeSlideShowState( Calamares::Slideshow::Action a )
|
||||
{
|
||||
QMutexLocker l( &m_mutex );
|
||||
m_state = a;
|
||||
if ( a == Slideshow::Start )
|
||||
{
|
||||
m_imageIndex = -1;
|
||||
if ( m_images.count() < 1 )
|
||||
{
|
||||
m_label->setPixmap( QPixmap( ":/data/images/squid.svg" ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
m_timer->start();
|
||||
QTimer::singleShot( 0, this, &SlideshowPictures::next );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_timer->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SlideshowPictures::next()
|
||||
{
|
||||
QMutexLocker l( &m_mutex );
|
||||
|
||||
if ( m_imageIndex < 0 )
|
||||
{
|
||||
// Initialization, don't do the advance-by-one
|
||||
m_imageIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_imageIndex++;
|
||||
if ( m_imageIndex >= m_images.count() )
|
||||
{
|
||||
m_imageIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ( m_imageIndex >= m_images.count() )
|
||||
{
|
||||
// Unusual case: timer is running, but we have 0 images to display.
|
||||
// .. this would have been caught in changeSlideShowState(), which
|
||||
// .. special-cases 0 images.
|
||||
return;
|
||||
}
|
||||
|
||||
m_label->setPixmap( QPixmap( m_images.at( m_imageIndex ) ) );
|
||||
}
|
||||
|
||||
|
||||
} // namespace Calamares
|
@ -0,0 +1,142 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
||||
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* Calamares is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Calamares 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBCALAMARESUI_SLIDESHOW_H
|
||||
#define LIBCALAMARESUI_SLIDESHOW_H
|
||||
|
||||
#include <QMutex>
|
||||
#include <QStringList>
|
||||
#include <QWidget>
|
||||
|
||||
class QLabel;
|
||||
class QTimer;
|
||||
class QQmlComponent;
|
||||
class QQuickItem;
|
||||
class QQuickWidget;
|
||||
|
||||
namespace Calamares
|
||||
{
|
||||
|
||||
/** @brief API for Slideshow objects
|
||||
*
|
||||
* A Slideshow (subclass) object is created by the ExecutionViewStep
|
||||
* and needs to manage its own configuration (e.g. from Branding).
|
||||
* The slideshow is started and stopped when it becomes visible
|
||||
* and when installation is over, by calling changeSlideShowState()
|
||||
* as appropriate.
|
||||
*/
|
||||
class Slideshow : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/// @brief State-change of the slideshow, for changeSlideShowState()
|
||||
enum Action
|
||||
{
|
||||
Start,
|
||||
Stop
|
||||
};
|
||||
|
||||
Slideshow( QWidget* parent = nullptr )
|
||||
: QObject( parent )
|
||||
{
|
||||
}
|
||||
virtual ~Slideshow();
|
||||
|
||||
/// @brief Is the slideshow being shown **right now**?
|
||||
bool isActive() const { return m_state == Start; }
|
||||
|
||||
/** @brief The actual widget to show the user.
|
||||
*
|
||||
* Depending on the style of slideshow, this might be a QQuickWidget,
|
||||
* or a QLabel, or something else entirely.
|
||||
*/
|
||||
virtual QWidget* widget() = 0;
|
||||
|
||||
/** @brief Tells the slideshow we activated or left the show.
|
||||
*
|
||||
* If @p state is @c Slideshow::Start, calls suitable activation procedures.
|
||||
* If @p state is @c Slideshow::Stop, calls deactivation procedures.
|
||||
*/
|
||||
virtual void changeSlideShowState( Action a ) = 0;
|
||||
|
||||
protected:
|
||||
QMutex m_mutex;
|
||||
Action m_state = Stop;
|
||||
};
|
||||
|
||||
/** @brief Slideshow using a QML file
|
||||
*
|
||||
* This is the "classic" slideshow in Calamares, which runs some QML
|
||||
* while the installation is in progress. It is configured through
|
||||
* Branding settings *slideshow* and *slideshowAPI*, showing the QML
|
||||
* file from *slideshow*. The API version influences when and how the
|
||||
* QML is loaded; version 1 does so only when the slideshow is activated,
|
||||
* while version 2 does so asynchronously.
|
||||
*/
|
||||
class SlideshowQML : public Slideshow
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SlideshowQML( QWidget* parent );
|
||||
virtual ~SlideshowQML() override;
|
||||
|
||||
QWidget* widget() override;
|
||||
void changeSlideShowState( Action a ) override;
|
||||
|
||||
public slots:
|
||||
void loadQmlV2Complete();
|
||||
void loadQmlV2(); ///< Loads the slideshow QML (from branding) for API version 2
|
||||
|
||||
private:
|
||||
QQuickWidget* m_qmlShow;
|
||||
QQmlComponent* m_qmlComponent;
|
||||
QQuickItem* m_qmlObject; ///< The actual show
|
||||
};
|
||||
|
||||
/** @brief Slideshow using images
|
||||
*
|
||||
* This is an "oldschool" slideshow, but new in Calamares, which
|
||||
* displays static image files one-by-one. It is for systems that
|
||||
* do not use QML at all. It is configured through the Branding
|
||||
* setting *slideshow*. When using this widget, the setting must
|
||||
* be a list of filenames; the API is set to -1.
|
||||
*/
|
||||
class SlideshowPictures : public Slideshow
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SlideshowPictures( QWidget* parent );
|
||||
virtual ~SlideshowPictures() override;
|
||||
|
||||
QWidget* widget() override;
|
||||
virtual void changeSlideShowState( Action a ) override;
|
||||
|
||||
public slots:
|
||||
void next();
|
||||
|
||||
private:
|
||||
QLabel* m_label;
|
||||
QTimer* m_timer;
|
||||
int m_imageIndex;
|
||||
QStringList m_images;
|
||||
};
|
||||
|
||||
} // namespace Calamares
|
||||
#endif
|
Loading…
Reference in New Issue