summaryrefslogtreecommitdiffstats
path: root/src/model.cpp
blob: 672f91e8f4c5aab41266b8acd43d2c5b528b10d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "model.h"
#include <QIcon>
#include <QPixmap>

Model::Model(QList<Session*> e, QObject *parent)
    : QAbstractListModel(parent),
      rowCount_(e.size()), entries_(e)
{
    printf("model with %d entries created\n", this->entries_.size());
}

Model::~Model()
{
}

int Model::rowCount(const QModelIndex &parent) const
{
    return (parent.isValid() && parent.column() != 0) ? 0 : rowCount_;
}

QVariant Model::data(const QModelIndex &index, int role) const
{
    printf("request for model row %d role %d\n", index.row(), role);
    if (!index.isValid())
        return QVariant();
    if (role == Qt::DisplayRole)
        return this->entries_.at(index.row())->shortDescription();
    if (role == Qt::ToolTipRole)
        return this->entries_.at(index.row())->description();
    if (role == Qt::DecorationRole) {
        // TODO: use cache for icons
        if (index.column() == 0) {
            const Session* e(this->entries_.at(index.row()));

            QString icon(e->icon());

            if (QFileInfo(icon).isAbsolute()) {
                // try to load icon from file
                return QIcon(icon);
            } else {
                // try to load icon from QResource
                return QIcon(":" + icon.toLower());
            }
        }
    }
    return QVariant();
}