QTfrontend/model/GameStyleModel.cpp
author Wuzzy <Wuzzy2@mail.ru>
Sat, 04 May 2019 00:56:27 +0200
changeset 14896 12ffbbf81ee1
parent 12686 d5e09024d722
permissions -rw-r--r--
Frontend controls: Add menu key
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
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10108
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
6958
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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 9998
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
6958
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"
8466
29b891dbf2a0 forgot this one
nemo
parents: 8419
diff changeset
    28
#include "hwconsts.h"
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    29
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    30
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    31
void GameStyleModel::loadGameStyles()
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    32
{
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    33
    beginResetModel();
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    34
12685
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    35
    QIcon dlcIcon;
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    36
    dlcIcon.addFile(":/res/dlcMarker.png", QSize(), QIcon::Normal, QIcon::On);
12686
d5e09024d722 Change DLC icon (thanks, nemo!)
Wuzzy <almikes@aol.com>
parents: 12685
diff changeset
    37
    dlcIcon.addFile(":/res/dlcMarkerSelected.png", QSize(), QIcon::Selected, QIcon::On);
12685
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    38
    QPixmap emptySpace = QPixmap(7, 15);
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    39
    emptySpace.fill(QColor(0, 0, 0, 0));
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    40
    QIcon notDlcIcon = QIcon(emptySpace);
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    41
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    42
    // empty list, so that we can (re)fill it
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    43
    QStandardItemModel::clear();
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    44
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    45
    QList<QStandardItem * > items;
12685
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    46
    items.append(new QStandardItem(notDlcIcon, "Normal"));
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    47
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    48
    // define a separator item
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    49
    QStandardItem * separator = new QStandardItem("---");
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    50
    separator->setData(QLatin1String("separator"), Qt::AccessibleDescriptionRole);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    51
    separator->setFlags(separator->flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    52
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    53
    items.append(separator);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    54
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    55
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    56
    QStringList scripts = DataManager::instance().entryList(
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    57
                             QString("Scripts/Multiplayer"),
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    58
                             QDir::Files,
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    59
                             QStringList("*.lua")
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    60
                         );
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    61
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    62
    foreach(QString script, scripts)
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    63
    {
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    64
        script = script.remove(".lua", Qt::CaseInsensitive);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    65
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 7258
diff changeset
    66
        QFile scriptCfgFile(QString("physfs://Scripts/Multiplayer/%2.cfg").arg(script));
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    67
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    68
        QString name = script;
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    69
        name = name.replace("_", " ");
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    70
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    71
        QString scheme = "locked";
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    72
        QString weapons = "locked";
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    73
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    74
        if (scriptCfgFile.exists() && scriptCfgFile.open(QFile::ReadOnly))
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    75
        {
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    76
            QTextStream input(&scriptCfgFile);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    77
            input >> scheme;
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    78
            input >> weapons;
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    79
            scriptCfgFile.close();
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    80
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    81
            if (!scheme.isEmpty())
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    82
                scheme.replace("_", " ");
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    83
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    84
            if (!weapons.isEmpty())
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    85
                weapons.replace("_", " ");
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    86
        }
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    87
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    88
        // 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
    89
        QString scriptPath = PHYSFS_getRealDir(QString("Scripts/Multiplayer/%1.lua").arg(script).toLocal8Bit().data());
8466
29b891dbf2a0 forgot this one
nemo
parents: 8419
diff changeset
    90
        bool isDLC = !scriptPath.startsWith(datadir->absolutePath());
8419
d99f46b676b5 Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents: 8049
diff changeset
    91
12685
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    92
        QStandardItem * item;
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    93
        if (isDLC)
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    94
            item = new QStandardItem(dlcIcon, name);
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    95
        else
ad1fd7ae479d Mark custom maps/forts/scripts with circle icon instead of icon
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    96
            item = new QStandardItem(notDlcIcon, name);
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    97
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    98
        item->setData(script, ScriptRole);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
    99
        item->setData(scheme, SchemeRole);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   100
        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
   101
        item->setData(isDLC, IsDlcRole);
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   102
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   103
        items.append(item);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   104
    }
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   105
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   106
    QStandardItemModel::appendColumn(items);
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   107
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   108
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   109
    endResetModel();
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   110
}
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   111
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   112
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   113
8230a516ba93 hello GameStyleModel
sheepluva
parents:
diff changeset
   114