QTfrontend/model/HatModel.cpp
changeset 6953 4c2dd25630a7
parent 6952 7f70f37bbf08
child 8049 133e22b5c410
equal deleted inserted replaced
6952:7f70f37bbf08 6953:4c2dd25630a7
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 /**
       
    20  * @file
       
    21  * @brief HatModel class implementation
       
    22  */
       
    23 
       
    24 #include "HatModel.h"
       
    25 
       
    26 #include <QDir>
       
    27 #include <QPixmap>
       
    28 #include <QPainter>
       
    29 #include "hwform.h" // player hash
       
    30 
       
    31 #include "DataManager.h"
       
    32 
       
    33 HatModel::HatModel(QObject* parent) :
       
    34     QAbstractListModel(parent)
       
    35 {
       
    36     hats = QVector<QPair<QString, QIcon> >();
       
    37 }
       
    38 
       
    39 void HatModel::loadHats()
       
    40 {
       
    41     // this method resets the contents of this model (important to know for views).
       
    42     beginResetModel();
       
    43 
       
    44     // prepare hats Vector
       
    45     hats.clear();
       
    46 
       
    47     DataManager & dataMgr = DataManager::instance();
       
    48 
       
    49     QPixmap hhpix = QPixmap(
       
    50                         dataMgr.findFileForRead("Graphics/Hedgehog/Idle.png")
       
    51                     ).copy(0, 0, 32, 32);
       
    52 
       
    53     // my reserved hats
       
    54     QStringList hatsList = dataMgr.entryList(
       
    55                                "Graphics/Hats/Reserved",
       
    56                                QDir::Files,
       
    57                                QStringList(playerHash+"*.png")
       
    58                            );
       
    59 
       
    60     int nReserved = hatsList.size();
       
    61 
       
    62     // regular hats
       
    63     hatsList.append(dataMgr.entryList(
       
    64                         "Graphics/Hats",
       
    65                         QDir::Files,
       
    66                         QStringList("*.png")
       
    67                     )
       
    68                    );
       
    69 
       
    70 
       
    71     int nHats = hatsList.size();
       
    72 
       
    73     for (int i = 0; i < nHats; i++)
       
    74     {
       
    75         bool isReserved = (i < nReserved);
       
    76 
       
    77         QString str = hatsList.at(i);
       
    78         str = str.remove(QRegExp("\\.png$"));
       
    79         QPixmap pix(
       
    80             dataMgr.findFileForRead(
       
    81                 "Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str +
       
    82                 ".png"
       
    83             )
       
    84         );
       
    85 
       
    86         // rename properly
       
    87         if (isReserved)
       
    88             str = "Reserved "+str.remove(0,32);
       
    89 
       
    90         QPixmap tmppix(32, 37);
       
    91         tmppix.fill(QColor(Qt::transparent));
       
    92 
       
    93         QPainter painter(&tmppix);
       
    94         painter.drawPixmap(QPoint(0, 5), hhpix);
       
    95         painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
       
    96         if(pix.width() > 32)
       
    97             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
       
    98         painter.end();
       
    99 
       
   100         if (str == "NoHat")
       
   101             hats.prepend(qMakePair(str, QIcon(tmppix)));
       
   102         else
       
   103             hats.append(qMakePair(str, QIcon(tmppix)));
       
   104     }
       
   105 
       
   106 
       
   107     endResetModel();
       
   108 }
       
   109 
       
   110 QVariant HatModel::headerData(int section,
       
   111                                Qt::Orientation orientation, int role) const
       
   112 {
       
   113     Q_UNUSED(section);
       
   114     Q_UNUSED(orientation);
       
   115     Q_UNUSED(role);
       
   116 
       
   117     return QVariant();
       
   118 }
       
   119 
       
   120 int HatModel::rowCount(const QModelIndex &parent) const
       
   121 {
       
   122     if (parent.isValid())
       
   123         return 0;
       
   124     else
       
   125         return hats.size();
       
   126 }
       
   127 
       
   128 /*int HatModel::columnCount(const QModelIndex & parent) const
       
   129 {
       
   130     if (parent.isValid())
       
   131         return 0;
       
   132     else
       
   133         return 2;
       
   134 }
       
   135 */
       
   136 QVariant HatModel::data(const QModelIndex &index,
       
   137                          int role) const
       
   138 {
       
   139     if (!index.isValid() || index.row() < 0
       
   140             || index.row() >= hats.size()
       
   141             || (role != Qt::DisplayRole && role != Qt::DecorationRole))
       
   142         return QVariant();
       
   143 
       
   144     if (role == Qt::DisplayRole)
       
   145         return hats.at(index.row()).first;
       
   146     else // role == Qt::DecorationRole
       
   147         return hats.at(index.row()).second;
       
   148 }