QTfrontend/model/GameStyleModel.cpp
author koda
Sun, 27 Jan 2013 02:09:02 +0100
changeset 8449 2816230a107e
parent 8419 d99f46b676b5
child 8466 29b891dbf2a0
permissions -rw-r--r--
shut a few frontend warnings out
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     1
/*
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     4
 *
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     8
 *
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    12
 * GNU General Public License for more details.
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    13
 *
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    17
 */
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    18
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    19
/**
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    20
 * @file
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    21
 * @brief GameStyleModel class implementation
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    22
 */
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    23
7258
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6958
diff changeset
    24
#include <QTextStream>
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6958
diff changeset
    25
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    26
#include "physfs.h"
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    27
#include "GameStyleModel.h"
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    28
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    29
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    30
void GameStyleModel::loadGameStyles()
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    31
{
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    32
    const QString appDir = QString(PHYSFS_getBaseDir());
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    33
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    34
    beginResetModel();
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    35
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    36
    // empty list, so that we can (re)fill it
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    37
    QStandardItemModel::clear();
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    38
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    39
    QList<QStandardItem * > items;
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    40
    items.append(new QStandardItem("Normal"));
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    41
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    42
    // define a separator item
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    43
    QStandardItem * separator = new QStandardItem("---");
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    44
    separator->setData(QLatin1String("separator"), Qt::AccessibleDescriptionRole);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    45
    separator->setFlags(separator->flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    46
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    47
    items.append(separator);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    48
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    49
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    50
    QStringList scripts = DataManager::instance().entryList(
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    51
                             QString("Scripts/Multiplayer"),
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    52
                             QDir::Files,
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    53
                             QStringList("*.lua")
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    54
                         );
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    55
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    56
    foreach(QString script, scripts)
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    57
    {
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    58
        script = script.remove(".lua", Qt::CaseInsensitive);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    59
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 7258
diff changeset
    60
        QFile scriptCfgFile(QString("physfs://Scripts/Multiplayer/%2.cfg").arg(script));
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    61
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    62
        QString name = script;
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    63
        name = name.replace("_", " ");
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    64
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    65
        QString scheme = "locked";
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    66
        QString weapons = "locked";
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    67
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    68
        if (scriptCfgFile.exists() && scriptCfgFile.open(QFile::ReadOnly))
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    69
        {
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    70
            QTextStream input(&scriptCfgFile);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    71
            input >> scheme;
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    72
            input >> weapons;
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    73
            scriptCfgFile.close();
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    74
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    75
            if (!scheme.isEmpty())
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    76
                scheme.replace("_", " ");
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    77
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    78
            if (!weapons.isEmpty())
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    79
                weapons.replace("_", " ");
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    80
        }
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    81
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    82
        // detect if script is dlc
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    83
        QString scriptPath = PHYSFS_getRealDir(QString("Scripts/Multiplayer/%1.lua").arg(script).toLocal8Bit().data());
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    84
        bool isDLC = !scriptPath.startsWith(appDir);
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    85
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    86
        QStandardItem * item = new QStandardItem((isDLC ? "*" : "") + name);
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    87
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    88
        item->setData(script, ScriptRole);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    89
        item->setData(scheme, SchemeRole);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    90
        item->setData(weapons, WeaponsRole);
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    91
        item->setData(isDLC, IsDlcRole);
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    92
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    93
        items.append(item);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    94
    }
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    95
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    96
    QStandardItemModel::appendColumn(items);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    97
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    98
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    99
    endResetModel();
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   100
}
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   101
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   102
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   103
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   104