From 9ebdce3995205c3150498f38e9b2e35ee1f0c457 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 13 Mar 2015 17:29:20 +0100 Subject: [PATCH] AircraftModel hacking for package support. --- src/GUI/AircraftModel.cxx | 118 +++++++++++++++++++++++++++----------- src/GUI/AircraftModel.hxx | 25 +++++++- 2 files changed, 106 insertions(+), 37 deletions(-) diff --git a/src/GUI/AircraftModel.cxx b/src/GUI/AircraftModel.cxx index 0802767eb..4fd0a08df 100644 --- a/src/GUI/AircraftModel.cxx +++ b/src/GUI/AircraftModel.cxx @@ -2,7 +2,7 @@ // // Written by James Turner, started March 2015. // -// Copyright (C) 2014 James Turner +// Copyright (C) 2015 James Turner // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -32,10 +32,15 @@ #include #include #include +#include +#include +#include // FlightGear #include
+using namespace simgear::pkg; + AircraftItem::AircraftItem() { // oh for C++11 initialisers @@ -338,46 +343,89 @@ void AircraftItemModel::abandonCurrentScan() } QVariant AircraftItemModel::data(const QModelIndex& index, int role) const - { - if (role == AircraftVariantRole) { - return m_activeVariant.at(index.row()); - } +{ + if (role == AircraftVariantRole) { + return m_activeVariant.at(index.row()); + } - const AircraftItem* item(m_items.at(index.row())); + const AircraftItem* item(m_items.at(index.row())); + quint32 variantIndex = m_activeVariant.at(index.row()); + return dataFromItem(item, variantIndex, role); +} - if (role == AircraftVariantCountRole) { - return item->variants.count(); - } +QVariant AircraftItemModel::dataFromItem(const AircraftItem* item, quint32 variantIndex, int role) const +{ + if (role == AircraftVariantCountRole) { + return item->variants.count(); + } - if (role >= AircraftVariantDescriptionRole) { - int variantIndex = role - AircraftVariantDescriptionRole; - return item->variants.at(variantIndex)->description; - } + if (role == AircraftThumbnailCountRole) { + QPixmap p = item->thumbnail(); + return p.isNull() ? 0 : 1; + } - quint32 variantIndex = m_activeVariant.at(index.row()); - if (variantIndex) { - if (variantIndex <= item->variants.count()) { - // show the selected variant - item = item->variants.at(variantIndex - 1); - } - } + if ((role >= AircraftVariantDescriptionRole) && (role < AircraftThumbnailRole)) { + int variantIndex = role - AircraftVariantDescriptionRole; + return item->variants.at(variantIndex)->description; + } - if (role == Qt::DisplayRole) { - return item->description; - } else if (role == Qt::DecorationRole) { - return item->thumbnail(); - } else if (role == AircraftPathRole) { - return item->path; - } else if (role == AircraftAuthorsRole) { - return item->authors; - } else if ((role >= AircraftRatingRole) && (role < AircraftVariantDescriptionRole)) { - return item->ratings[role - AircraftRatingRole]; - } else if (role == Qt::ToolTipRole) { - return item->path; - } + if (variantIndex) { + if (variantIndex <= item->variants.count()) { + // show the selected variant + item = item->variants.at(variantIndex - 1); + } + } - return QVariant(); - } + if (role == Qt::DisplayRole) { + return item->description; + } else if (role == Qt::DecorationRole) { + return item->thumbnail(); + } else if (role == AircraftPathRole) { + return item->path; + } else if (role == AircraftAuthorsRole) { + return item->authors; + } else if ((role >= AircraftRatingRole) && (role < AircraftVariantDescriptionRole)) { + return item->ratings[role - AircraftRatingRole]; + } else if (role >= AircraftThumbnailRole) { + return item->thumbnail(); + } else if (role == AircraftPackageIdRole) { + // can we fake an ID? otherwise fall through to a null variant + } else if (role == AircraftPackageStatusRole) { + return PackageInstalled; // always the case + } else if (role == Qt::ToolTipRole) { + return item->path; + } + + return QVariant(); +} + +QVariant AircraftItemModel::dataFromPackage(const PackageRef& item, quint32 variantIndex, int role) const +{ + if (role == Qt::DisplayRole) { + return QString::fromStdString(item->description()); + } else if (role == AircraftPathRole) { + // can we return the theoretical path? + } else if (role == AircraftPackageIdRole) { + return QString::fromStdString(item->id()); + } else if (role == AircraftPackageStatusRole) { + bool installed = item->isInstalled(); + if (installed) { + InstallRef i = item->existingInstall(); + if (i->isDownloading()) { + return PackageDownloading; + } + if (i->hasUpdate()) { + return PackageUpdateAvailable; + } + + return PackageInstalled; + } else { + return PackageNotInstalled; + } + } + + return QVariant(); +} bool AircraftItemModel::setData(const QModelIndex &index, const QVariant &value, int role) { diff --git a/src/GUI/AircraftModel.hxx b/src/GUI/AircraftModel.hxx index 24236a281..6771cf665 100644 --- a/src/GUI/AircraftModel.hxx +++ b/src/GUI/AircraftModel.hxx @@ -2,7 +2,7 @@ // // Written by James Turner, started March 2015. // -// Copyright (C) 2014 James Turner +// Copyright (C) 2015 James Turner // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -33,8 +33,14 @@ const int AircraftPathRole = Qt::UserRole + 1; const int AircraftAuthorsRole = Qt::UserRole + 2; const int AircraftVariantRole = Qt::UserRole + 3; const int AircraftVariantCountRole = Qt::UserRole + 4; +const int AircraftThumbnailCountRole = Qt::UserRole + 5; +const int AircraftPackageIdRole = Qt::UserRole + 6; +const int AircraftPackageStatusRole = Qt::UserRole + 7; +const int AircraftPackageProgressRole = Qt::UserRole + 8; + const int AircraftRatingRole = Qt::UserRole + 100; const int AircraftVariantDescriptionRole = Qt::UserRole + 200; +const int AircraftThumbnailRole = Qt::UserRole + 300; class AircraftScanThread; class QDataStream; @@ -87,14 +93,29 @@ public: virtual bool setData(const QModelIndex &index, const QVariant &value, int role); - QModelIndex indexOfAircraftPath(QString path) const; + /** + * given a -set.xml path, return the corresponding model index, if one + * exists. + */ + QModelIndex indexOfAircraftPath(QString path) const; + enum { + PackageNotInstalled, + PackageInstalled, + PackageUpdateAvailable, + PackageDownloading + }; private slots: void onScanResults(); void onScanFinished(); private: + QVariant dataFromItem(const AircraftItem* item, quint32 variantIndex, int role) const; + + QVariant dataFromPackage(const simgear::pkg::PackageRef& item, + quint32 variantIndex, int role) const; + void abandonCurrentScan(); QStringList m_paths;