QTfrontend/model/HatModel.cpp
author unc0rr
Fri, 27 Sep 2013 00:54:18 +0400
changeset 9466 8b48c27201af
parent 9080 9b42757d7e71
child 9738 18bb6d1fc9d9
permissions -rw-r--r--
- Load default binds from settings.ini - Frontend doesn't send dbind commands - Refactor '/dbind' command implementation copypasta
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1236
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
     1
/*
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
9080
9b42757d7e71 bump copyright year for Andrey entries
unc0rr
parents: 8434
diff changeset
     3
 * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
1236
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
     4
 *
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
     8
 *
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    13
 *
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    17
 */
f9110fd03754 Add stubs for hats implementation
unc0rr
parents:
diff changeset
    18
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    19
/**
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    20
 * @file
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    21
 * @brief HatModel class implementation
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    22
 */
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    23
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    24
#include "HatModel.h"
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    25
1239
4901abe4c3b0 - Finish hat selection widget
unc0rr
parents: 1238
diff changeset
    26
#include <QDir>
4901abe4c3b0 - Finish hat selection widget
unc0rr
parents: 1238
diff changeset
    27
#include <QPixmap>
1281
1f8456577a39 Draw a hedgehog under the hat in hats combobox
unc0rr
parents: 1239
diff changeset
    28
#include <QPainter>
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    29
#include <QList>
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    30
#include "hwform.h" // player hash
1237
7f3105a15d5d Fix build
unc0rr
parents: 1236
diff changeset
    31
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    32
#include "DataManager.h"
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    33
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    34
HatModel::HatModel(QObject* parent) :
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    35
    QStandardItemModel(parent)
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    36
{}
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    37
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    38
void HatModel::loadHats()
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    39
{
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    40
    // this method resets the contents of this model (important to know for views).
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    41
    QStandardItemModel::beginResetModel();
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    42
    QStandardItemModel::clear();
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    43
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    44
    // New hats to add to model
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    45
    QList<QStandardItem *> hats;
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    46
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    47
    // we'll need the DataManager a few times, so let's get a reference to it
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    48
    DataManager & dataMgr = DataManager::instance();
6196
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6167
diff changeset
    49
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    50
    // Default hat icon
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 6953
diff changeset
    51
    QPixmap hhpix = QPixmap("physfs://Graphics/Hedgehog/Idle.png").copy(0, 0, 32, 32);
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    52
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    53
    // my reserved hats
6196
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6167
diff changeset
    54
    QStringList hatsList = dataMgr.entryList(
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6196
diff changeset
    55
                               "Graphics/Hats/Reserved",
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6196
diff changeset
    56
                               QDir::Files,
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6196
diff changeset
    57
                               QStringList(playerHash+"*.png")
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6196
diff changeset
    58
                           );
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    59
    int nReserved = hatsList.size();
1281
1f8456577a39 Draw a hedgehog under the hat in hats combobox
unc0rr
parents: 1239
diff changeset
    60
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    61
    // regular hats
6196
c16e84558f71 minor cleanups, mostly in own code
sheepluva
parents: 6167
diff changeset
    62
    hatsList.append(dataMgr.entryList(
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6196
diff changeset
    63
                        "Graphics/Hats",
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6196
diff changeset
    64
                        QDir::Files,
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6196
diff changeset
    65
                        QStringList("*.png")
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6196
diff changeset
    66
                    )
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    67
                   );
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    68
    int nHats = hatsList.size();
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    69
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    70
    // Add each hat
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    71
    for (int i = 0; i < nHats; i++)
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    72
    {
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    73
        bool isReserved = (i < nReserved);
1239
4901abe4c3b0 - Finish hat selection widget
unc0rr
parents: 1238
diff changeset
    74
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    75
        if (isReserved) continue; // For some reason, reserved hats were added in 9.19-dev, so this will hide them. Uncomment to show them.
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    76
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    77
        QString str = hatsList.at(i);
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    78
        str = str.remove(QRegExp("\\.png$"));
8434
4821897a0f10 big indentation/whitespaces cleanup
sheepluva
parents: 8385
diff changeset
    79
        QPixmap pix(
8049
133e22b5c410 Get rid of DataManager::findFileForRead
unc0rr
parents: 6953
diff changeset
    80
                "physfs://Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str +
8434
4821897a0f10 big indentation/whitespaces cleanup
sheepluva
parents: 8385
diff changeset
    81
                ".png"
6167
728cabee2c9f simplified some stuff I wrote
sheepluva
parents: 6160
diff changeset
    82
        );
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    83
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    84
        // rename properly
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    85
        if (isReserved)
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    86
            str = "Reserved "+str.remove(0,32);
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    87
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    88
        QPixmap tmppix(32, 37);
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    89
        tmppix.fill(QColor(Qt::transparent));
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    90
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    91
        QPainter painter(&tmppix);
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    92
        painter.drawPixmap(QPoint(0, 5), hhpix);
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    93
        painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    94
        if(pix.width() > 32)
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    95
            painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    96
        painter.end();
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 4976
diff changeset
    97
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
    98
        if (str == "NoHat")
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
    99
            hats.prepend(new QStandardItem(QIcon(tmppix), str));
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6061
diff changeset
   100
        else
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
   101
            hats.append(new QStandardItem(QIcon(tmppix), str));
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2874
diff changeset
   102
    }
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
   103
8374
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
   104
    QStandardItemModel::appendColumn(hats);
3a1708759c4f Hat selection reworked
dag10
parents: 8049
diff changeset
   105
    QStandardItemModel::endResetModel();
8385
9e8924ff9813 Convert feedback page to dialog (+some fixes from unC0Rr)
dag10
parents: 8374
diff changeset
   106
}