Commits

David Mugnai committed ae6e1d1

Models to expose the index data to a view

  • Participants
  • Parent commits a63d760

Comments (0)

Files changed (2)

+#include <QtDebug>
+#include "models.h"
+
+using namespace APOD;
+
+QHash<int, QByteArray> ImagesModel::_initRolesNames() {
+    QHash<int, QByteArray> roles;
+    roles[Qt::DisplayRole] = "display";
+    roles[Qt::UserRole + 0] = "date";
+    roles[Qt::UserRole + 1] = "explanation";
+    roles[Qt::UserRole + 2] = "preview";
+    return roles;
+}
+
+const QHash<int, QByteArray> ImagesModel::roles = ImagesModel::_initRolesNames();
+
+ImagesModel::ImagesModel(QObject *parent) : QAbstractListModel(parent) {}
+
+QHash<int, QByteArray> ImagesModel::roleNames() const {
+    return roles;
+}
+
+int ImagesModel::rowCount(const QModelIndex &parent) const {
+    return !parent.isValid() ? index.size() : 0;
+}
+
+QVariant ImagesModel::data(const QModelIndex &index, int role) const {
+    if(role == Qt::DisplayRole) {
+        auto key = this->index.keys()[index.row()];
+        return this->index[key]->title;
+    }
+    else if(role == Qt::UserRole + 2) {
+        auto key = this->index.keys()[index.row()];
+        auto metadata = this->index[key];
+        return "../cache/" + metadata->date.toString("ddMMyy") + ".jpg";
+    }
+    else
+        return QVariant();
+}
+
+void ImagesModel::addImage(const ImageMetaData *metadata) {
+    auto key = metadata->date.toString("yyyyMMdd").toInt();
+    int ix = 0;
+    for(auto it : index.keys()) {
+        if(it >= key)
+            break;
+        ix++;
+    }
+    emit beginInsertRows(QModelIndex(), ix, ix);
+    index[key] = metadata;
+    emit endInsertRows();
+}
+
+QHash<int, QByteArray> IndexerModel::_initRolesNames() {
+    QHash<int, QByteArray> roles;
+    roles[Qt::DisplayRole] = "display";
+    roles[Qt::UserRole + 0] = "submodel";
+    return roles;
+}
+
+const QHash<int, QByteArray> IndexerModel::roles = IndexerModel::_initRolesNames();
+
+IndexerModel::IndexerModel(const APODIndexer& indexer, QObject *parent) : QAbstractListModel(parent) {
+    QObject::connect(
+        &indexer, SIGNAL(imageReady(const ImageMetaData*)),
+        this, SLOT(onImageReady(const ImageMetaData*)));
+}
+
+QHash<int, QByteArray> IndexerModel::roleNames() const {
+    return roles;
+}
+
+int IndexerModel::rowCount(const QModelIndex &parent) const {
+    return !parent.isValid() ? index.size() : 0;
+}
+
+QVariant IndexerModel::data(const QModelIndex &index, int role) const {
+    if(role == Qt::DisplayRole) {
+        auto entry = this->index.keys()[index.row()];
+        return entry ? QString::number(entry) : "today";
+    }
+    else
+        return QVariant();
+}
+
+void IndexerModel::onImageReady(const ImageMetaData *metadata) {
+    QDate today = QDate::currentDate();
+    auto year = metadata->date == today ? 0 : metadata->date.year();
+    if(index.find(year) == index.end()) {
+        qDebug() << "INSERT";
+        int ix = 0;
+        for(auto it : index.keys()) {
+            if(it >= year)
+                break;
+            ix++;
+        }
+        emit beginInsertRows(QModelIndex(), ix, ix);
+        index[year] = new ImagesModel(this);
+        emit endInsertRows();
+    }
+    index[year]->addImage(metadata);
+}
+
+QObject* IndexerModel::images(const QString& year) {
+    int key = year == "today" ? 0 : year.toInt();
+    return index[key];
+}
+#ifndef MODELS_H
+#define MODELS_H
+
+#include <QObject>
+#include <QAbstractListModel>
+#include "index.h"
+#include "metadata.h"
+
+namespace APOD {
+
+class ImagesModel : public QAbstractListModel {
+    Q_OBJECT
+public:
+    const static QHash<int, QByteArray> roles;
+
+    ImagesModel(QObject *parent=0);
+    virtual int rowCount(const QModelIndex &parent=QModelIndex()) const;
+    virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
+    virtual QHash<int, QByteArray> roleNames() const;
+
+    void addImage(const ImageMetaData*);
+
+private:
+    static QHash<int, QByteArray> _initRolesNames();
+    QMap<int, const ImageMetaData*> index;
+};
+
+class IndexerModel : public QAbstractListModel {
+    Q_OBJECT
+public:
+    const static QHash<int, QByteArray> roles;
+
+    IndexerModel(const APODIndexer& indexer, QObject *parent=0);
+    virtual int rowCount(const QModelIndex &parent=QModelIndex()) const;
+    virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
+    virtual QHash<int, QByteArray> roleNames() const;
+
+    Q_INVOKABLE QObject* images(const QString& year);
+private slots:
+    void onImageReady(const ImageMetaData*);
+
+private:
+    static QHash<int, QByteArray> _initRolesNames();
+    QMap<int, ImagesModel*> index;
+};
+
+}
+#endif // MODELS_H