1
0
Fork 0

AircraftModel hacking for package support.

This commit is contained in:
James Turner 2015-03-13 17:29:20 +01:00
parent 217db33b99
commit 9ebdce3995
2 changed files with 106 additions and 37 deletions

View file

@ -2,7 +2,7 @@
//
// Written by James Turner, started March 2015.
//
// Copyright (C) 2014 James Turner <zakalawe@mac.com>
// Copyright (C) 2015 James Turner <zakalawe@mac.com>
//
// 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 <simgear/props/props_io.hxx>
#include <simgear/structure/exception.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/package/Package.hxx>
#include <simgear/package/Catalog.hxx>
#include <simgear/package/Install.hxx>
// FlightGear
#include <Main/globals.hxx>
using namespace simgear::pkg;
AircraftItem::AircraftItem()
{
// oh for C++11 initialisers
@ -344,17 +349,26 @@ QVariant AircraftItemModel::data(const QModelIndex& index, int role) const
}
const AircraftItem* item(m_items.at(index.row()));
quint32 variantIndex = m_activeVariant.at(index.row());
return dataFromItem(item, variantIndex, role);
}
QVariant AircraftItemModel::dataFromItem(const AircraftItem* item, quint32 variantIndex, int role) const
{
if (role == AircraftVariantCountRole) {
return item->variants.count();
}
if (role >= AircraftVariantDescriptionRole) {
if (role == AircraftThumbnailCountRole) {
QPixmap p = item->thumbnail();
return p.isNull() ? 0 : 1;
}
if ((role >= AircraftVariantDescriptionRole) && (role < AircraftThumbnailRole)) {
int variantIndex = role - AircraftVariantDescriptionRole;
return item->variants.at(variantIndex)->description;
}
quint32 variantIndex = m_activeVariant.at(index.row());
if (variantIndex) {
if (variantIndex <= item->variants.count()) {
// show the selected variant
@ -372,6 +386,12 @@ QVariant AircraftItemModel::data(const QModelIndex& index, int role) const
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;
}
@ -379,6 +399,34 @@ QVariant AircraftItemModel::data(const QModelIndex& index, int role) const
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)
{
if (role == AircraftVariantRole) {

View file

@ -2,7 +2,7 @@
//
// Written by James Turner, started March 2015.
//
// Copyright (C) 2014 James Turner <zakalawe@mac.com>
// Copyright (C) 2015 James Turner <zakalawe@mac.com>
//
// 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);
/**
* 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;