From 6e05a1ef0550e6f8026334383fb2b46754581920 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 6 Aug 2019 12:02:29 +0200 Subject: [PATCH] [packagechooser] Load translated strings as well - This makes it possible to put the translations into the config file, and have them displayed when the Calamares language changes. --- .../packagechooser/PackageChooserViewStep.cpp | 26 +------------ src/modules/packagechooser/PackageModel.cpp | 38 +++++++++++++++++-- src/modules/packagechooser/PackageModel.h | 18 +++++++++ 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/modules/packagechooser/PackageChooserViewStep.cpp b/src/modules/packagechooser/PackageChooserViewStep.cpp index 4476eb9e6..a3b853b39 100644 --- a/src/modules/packagechooser/PackageChooserViewStep.cpp +++ b/src/modules/packagechooser/PackageChooserViewStep.cpp @@ -232,31 +232,7 @@ PackageChooserViewStep::fillModel( const QVariantList& items ) continue; } - QString id = CalamaresUtils::getString( item_map, "id" ); - QString package = CalamaresUtils::getString( item_map, "package" ); - QString name = CalamaresUtils::getString( item_map, "name" ); - QString description = CalamaresUtils::getString( item_map, "description" ); - QString screenshot = CalamaresUtils::getString( item_map, "screenshot" ); - - if ( name.isEmpty() && id.isEmpty() ) - { - name = tr( "No product" ); - } - else if ( name.isEmpty() ) - { - cWarning() << "PackageChooser item" << id << "has an empty name."; - continue; - } - if ( description.isEmpty() ) - { - description = tr( "No description provided." ); - } - if ( screenshot.isEmpty() ) - { - screenshot = QStringLiteral( ":/images/no-selection.png" ); - } - - m_model->addPackage( PackageItem { id, package, name, description, screenshot } ); + m_model->addPackage( PackageItem( item_map ) ); } } diff --git a/src/modules/packagechooser/PackageModel.cpp b/src/modules/packagechooser/PackageModel.cpp index f133f4fbd..3283cbe6f 100644 --- a/src/modules/packagechooser/PackageModel.cpp +++ b/src/modules/packagechooser/PackageModel.cpp @@ -19,6 +19,7 @@ #include "PackageModel.h" #include "utils/Logger.h" +#include "utils/Variant.h" const NamedEnumTable< PackageChooserMode >& roleNames() @@ -73,6 +74,31 @@ PackageItem::PackageItem( const QString& a_id, { } +PackageItem::PackageItem::PackageItem( const QVariantMap& item_map ) + : id( CalamaresUtils::getString( item_map, "id" ) ) + , package( CalamaresUtils::getString( item_map, "package" ) ) + , name( CalamaresUtils::Locale::TranslatedString( item_map, "name" ) ) + , description( CalamaresUtils::Locale::TranslatedString( item_map, "description" ) ) + , screenshot( CalamaresUtils::getString( item_map, "screenshot" ) ) +{ + if ( name.isEmpty() && id.isEmpty() ) + { + name = QObject::tr( "No product" ); + } + else if ( name.isEmpty() ) + { + cWarning() << "PackageChooser item" << id << "has an empty name."; + } + if ( description.isEmpty() ) + { + description = QObject::tr( "No description provided." ); + } + if ( screenshot.isNull() ) + { + screenshot = QPixmap( QStringLiteral( ":/images/no-selection.png" ) ); + } +} + PackageListModel::PackageListModel( QObject* parent ) : QAbstractListModel( parent ) @@ -90,10 +116,14 @@ PackageListModel::~PackageListModel() {} void PackageListModel::addPackage( PackageItem&& p ) { - int c = m_packages.count(); - beginInsertRows( QModelIndex(), c, c ); - m_packages.append( p ); - endInsertRows(); + // Only add valid packages + if ( !p.name.isEmpty() ) + { + int c = m_packages.count(); + beginInsertRows( QModelIndex(), c, c ); + m_packages.append( p ); + endInsertRows(); + } } int diff --git a/src/modules/packagechooser/PackageModel.h b/src/modules/packagechooser/PackageModel.h index 68e19a25d..f42ff3123 100644 --- a/src/modules/packagechooser/PackageModel.h +++ b/src/modules/packagechooser/PackageModel.h @@ -56,12 +56,26 @@ struct PackageItem */ PackageItem( const QString& id, const QString& package, const QString& name, const QString& description ); + /** @brief Creates a PackageItem from given strings. + * + * Set all the text members and load the screenshot from the given + * @p screenshotPath, which may be a QRC path (:/path/in/qrc) or + * a filesystem path, whatever QPixmap understands. + */ PackageItem( const QString& id, const QString& package, const QString& name, const QString& description, const QString& screenshotPath ); + /** @brief Creates a PackageItem from a QVariantMap + * + * This is intended for use when loading PackageItems from a + * configuration map. It will look up the various keys in the map + * and handle translation strings as well. + */ + PackageItem( const QVariantMap& map ); + // TODO: implement this PackageItem fromAppStream( const QString& filename ); }; @@ -75,6 +89,10 @@ public: PackageListModel( QObject* parent ); virtual ~PackageListModel() override; + /** @brief Add a package @p to the model + * + * Only valid packages are added -- that is, they must have a name. + */ void addPackage( PackageItem&& p ); int rowCount( const QModelIndex& index ) const override;