[libcalamares] Move RequirementsModel to libcalamares (1/2)

- Add the model and support code to libcalamares. The model still
  has some cruft that should be in the Welcome config.
This commit is contained in:
Adriaan de Groot 2020-04-02 21:33:07 +02:00
parent d8ecd302e1
commit e5562a5069
3 changed files with 167 additions and 0 deletions

View File

@ -42,6 +42,7 @@ set( libSources
modulesystem/Module.cpp modulesystem/Module.cpp
modulesystem/Requirement.cpp modulesystem/Requirement.cpp
modulesystem/RequirementsChecker.cpp modulesystem/RequirementsChecker.cpp
modulesystem/RequirementsModel.cpp
# Network service # Network service
network/Manager.cpp network/Manager.cpp

View File

@ -0,0 +1,81 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2019-2020, 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 "RequirementsModel.h"
namespace Calamares
{
void
RequirementsModel::setRequirementsList( const Calamares::RequirementsList& requirements )
{
emit beginResetModel();
m_requirements = requirements;
auto isUnSatisfied = []( const Calamares::RequirementEntry& e ) { return !e.satisfied; };
auto isMandatoryAndUnSatisfied = []( const Calamares::RequirementEntry& e ) { return e.mandatory && !e.satisfied; };
m_satisfiedRequirements = std::none_of( m_requirements.begin(), m_requirements.end(), isUnSatisfied );
m_satisfiedMandatory = std::none_of( m_requirements.begin(), m_requirements.end(), isMandatoryAndUnSatisfied );
emit satisfiedRequirementsChanged( m_satisfiedRequirements );
emit satisfiedMandatoryChanged();
emit endResetModel();
}
int
RequirementsModel::rowCount( const QModelIndex& ) const
{
return m_requirements.count();
}
QVariant
RequirementsModel::data( const QModelIndex& index, int role ) const
{
const auto requirement = m_requirements.at( index.row() );
switch ( role )
{
case Roles::Name:
return requirement.name;
case Roles::Details:
return requirement.enumerationText();
case Roles::NegatedText:
return requirement.negatedText();
case Roles::Satisfied:
return requirement.satisfied;
case Roles::Mandatory:
return requirement.mandatory;
default:
return QVariant();
}
}
QHash< int, QByteArray >
RequirementsModel::roleNames() const
{
static QHash< int, QByteArray > roles;
roles[ Roles::Name ] = "name";
roles[ Roles::Details ] = "details";
roles[ Roles::NegatedText ] = "negatedText";
roles[ Roles::Satisfied ] = "satisfied";
roles[ Roles::Mandatory ] = "mandatory";
return roles;
}
} // namespace Calamares

View File

@ -0,0 +1,85 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2019-2020, 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 CALAMARES_REQUIREMENTSMODEL_H
#define CALAMARES_REQUIREMENTSMODEL_H
#include "Requirement.h"
#include "DllMacro.h"
#include <QAbstractListModel>
namespace Calamares
{
class DLLEXPORT RequirementsModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY( bool satisfiedRequirements READ satisfiedRequirements NOTIFY satisfiedRequirementsChanged FINAL )
Q_PROPERTY( bool satisfiedMandatory READ satisfiedMandatory NOTIFY satisfiedMandatoryChanged FINAL )
public:
using QAbstractListModel::QAbstractListModel;
enum Roles : short
{
Name,
Satisfied,
Mandatory,
Details,
NegatedText,
HasDetails
};
bool satisfiedRequirements() const { return m_satisfiedRequirements; }
bool satisfiedMandatory() const { return m_satisfiedMandatory; }
const Calamares::RequirementEntry& getEntry( const int& index ) const
{
if ( index > count() || index < 0 )
{
return *( new Calamares::RequirementEntry() );
}
return m_requirements.at( index );
}
void setRequirementsList( const Calamares::RequirementsList& requirements );
QVariant data( const QModelIndex& index, int role ) const override;
int rowCount( const QModelIndex& ) const override;
int count() const { return m_requirements.count(); }
protected:
QHash< int, QByteArray > roleNames() const override;
private:
Calamares::RequirementsList m_requirements;
bool m_satisfiedRequirements = false;
bool m_satisfiedMandatory = false;
signals:
void satisfiedRequirementsChanged( bool value );
void satisfiedMandatoryChanged();
void warningMessageChanged();
};
} // namespace Calamares
#endif