QTfrontend/model/ThemeModel.cpp
author sheepluva
Fri, 27 Apr 2012 11:47:37 +0200
changeset 6938 217ed62e872c
parent 6937 7f77fa908a4e
child 6948 7271ce89950f
permissions -rw-r--r--
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
     1
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
     2
#include "ThemeModel.h"
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
     3
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
     4
ThemeModel::ThemeModel(QObject *parent) :
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
     5
    QAbstractListModel(parent)
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
     6
{
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
     7
    m_data = QList<QMap<int, QVariant> >();
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
     8
}
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
     9
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    10
int ThemeModel::rowCount(const QModelIndex &parent) const
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    11
{
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    12
    if(parent.isValid())
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    13
        return 0;
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    14
    else
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    15
        return m_data.size();
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    16
}
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    17
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    18
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    19
QVariant ThemeModel::data(const QModelIndex &index, int role) const
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    20
{
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    21
    if(index.column() > 0 || index.row() >= m_data.size())
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    22
        return QVariant();
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    23
    else
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    24
        return m_data.at(index.row()).value(role);
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    25
}
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    26
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    27
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    28
void ThemeModel::loadThemes()
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    29
{
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    30
    beginResetModel();
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    31
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    32
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    33
    DataManager & datamgr = DataManager::instance();
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    34
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    35
    QStringList themes =
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    36
        datamgr.entryList("Themes", QDir::AllDirs | QDir::NoDotAndDotDot);
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    37
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    38
    m_data.clear();
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    39
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    40
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    41
    m_data.reserve(themes.size());
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    42
#endif
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    43
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    44
    foreach (QString theme, themes)
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    45
    {
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    46
        // themes without icon are supposed to be hidden
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    47
        QString iconpath =
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    48
            datamgr.findFileForRead(QString("Themes/%1/icon.png").arg(theme));
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    49
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    50
        if (!QFile::exists(iconpath))
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    51
            continue;
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    52
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    53
        QMap<int, QVariant> dataset;
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    54
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    55
        // set name
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    56
        dataset.insert(Qt::DisplayRole, theme);
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    57
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    58
        // load and set icon
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    59
        QIcon icon(iconpath);
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    60
        dataset.insert(Qt::DecorationRole, icon);
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    61
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    62
        // load and set preview icon
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    63
        QIcon preview(datamgr.findFileForRead(QString("Themes/%1/icon@2x.png").arg(theme)));
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    64
        dataset.insert(Qt::UserRole, preview);
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    65
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    66
        m_data.append(dataset);
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    67
    }
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    68
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    69
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    70
    endResetModel();
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    71
}
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    72
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    73
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    74
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    75