QTfrontend/model/ThemeModel.cpp
author unc0rr
Fri, 25 Jan 2013 22:33:26 +0400
changeset 8436 b89aacebb9db
parent 8419 d99f46b676b5
child 8464 c708b4f5cffc
permissions -rw-r--r--
- Also pass unknown cmds to the server when in room - Fix some warnings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6948
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
     1
/*
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
     2
 * Hedgewars, a free turn based strategy game
6952
7f70f37bbf08 license header year range adjustments
sheepluva
parents: 6948
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
6948
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
     4
 *
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
     8
 *
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    12
 * GNU General Public License for more details.
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    13
 *
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    15
 * along with this program; if not, write to the Free Software
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    17
 */
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    18
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    19
/**
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    20
 * @file
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    21
 * @brief ThemeModel class implementation
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6937
diff changeset
    22
 */
5289
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    23
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8377
diff changeset
    24
#include "physfs.h"
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    25
#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
    26
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    27
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
    28
    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
    29
{
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    30
    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
    31
}
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    32
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    33
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
    34
{
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    35
    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
    36
        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
    37
    else
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    38
        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
    39
}
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    40
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    41
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    42
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
    43
{
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    44
    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
    45
        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
    46
    else
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    47
        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
    48
}
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
    49
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    50
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    51
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
    52
{
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8377
diff changeset
    53
    const QString appDir = QString(PHYSFS_getBaseDir());
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8377
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
    beginResetModel();
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    56
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    57
    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
    58
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    59
    QStringList themes =
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    60
        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
    61
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    62
    m_data.clear();
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    63
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    64
#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
    65
    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
    66
#endif
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    67
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    68
    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
    69
    {
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    70
        // themes without icon are supposed to be hidden
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 7955
diff changeset
    71
        QString iconpath = QString("physfs://Themes/%1/icon.png").arg(theme);
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    72
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    73
        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
    74
            continue;
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    75
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    76
        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
    77
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8377
diff changeset
    78
        // detect if theme is dlc
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8377
diff changeset
    79
        QString themeDir = PHYSFS_getRealDir(QString("Themes/%1/icon.png").arg(theme).toLocal8Bit().data());
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8377
diff changeset
    80
        dataset.insert(Qt::UserRole + 2, !themeDir.startsWith(appDir));
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8377
diff changeset
    81
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8049
diff changeset
    82
        // set icon path
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8049
diff changeset
    83
        dataset.insert(Qt::UserRole + 1, iconpath);
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8049
diff changeset
    84
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    85
        // set name
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    86
        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
    87
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    88
        // load and set icon
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8049
diff changeset
    89
        QIcon icon;
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8049
diff changeset
    90
        icon.addPixmap(QPixmap(iconpath), QIcon::Normal);
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8049
diff changeset
    91
        icon.addPixmap(QPixmap(iconpath), QIcon::Disabled);
7776
e78eff243e25 Workaround over QIcon failing to load from physfs
unc0rr
parents: 6952
diff changeset
    92
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    93
        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
    94
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    95
        // load and set preview icon
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 7955
diff changeset
    96
        QIcon preview(QString("physfs://Themes/%1/icon@2x.png").arg(theme));
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    97
        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
    98
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
    99
        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
   100
    }
9d18b61bd3eb - Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff changeset
   101
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
   102
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6061
diff changeset
   103
    endResetModel();
8436
b89aacebb9db - Also pass unknown cmds to the server when in room
unc0rr
parents: 8419
diff changeset
   104
}