diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index c7bb96266..91dce96cd 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -42,6 +42,7 @@ set( libSources modulesystem/Module.cpp modulesystem/Requirement.cpp modulesystem/RequirementsChecker.cpp + modulesystem/RequirementsModel.cpp # Network service network/Manager.cpp diff --git a/src/libcalamares/modulesystem/RequirementsModel.cpp b/src/libcalamares/modulesystem/RequirementsModel.cpp new file mode 100644 index 000000000..4cd065dd7 --- /dev/null +++ b/src/libcalamares/modulesystem/RequirementsModel.cpp @@ -0,0 +1,81 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019-2020, Adriaan de Groot + * + * 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 . + */ + +#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 diff --git a/src/libcalamares/modulesystem/RequirementsModel.h b/src/libcalamares/modulesystem/RequirementsModel.h new file mode 100644 index 000000000..47f80e3ae --- /dev/null +++ b/src/libcalamares/modulesystem/RequirementsModel.h @@ -0,0 +1,85 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019-2020, Adriaan de Groot + * + * 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 . + */ + +#ifndef CALAMARES_REQUIREMENTSMODEL_H +#define CALAMARES_REQUIREMENTSMODEL_H + +#include "Requirement.h" + +#include "DllMacro.h" + +#include + +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