QTfrontend/model/hats.cpp
changeset 7006 6af78154dc62
parent 6852 9e724f4863a3
parent 7003 e118ee168577
child 7009 23131a8b4c3a
equal deleted inserted replaced
6852:9e724f4863a3 7006:6af78154dc62
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2008-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 #include <QDir>
       
    20 #include <QPixmap>
       
    21 #include <QPainter>
       
    22 #include "hwconsts.h"
       
    23 #include "hwform.h"
       
    24 #include "hats.h"
       
    25 
       
    26 #include "HWDataManager.h"
       
    27 
       
    28 HatsModel::HatsModel(QObject* parent) :
       
    29     QAbstractListModel(parent)
       
    30 {
       
    31     HWDataManager & dataMgr = HWDataManager::instance();
       
    32 
       
    33     QPixmap hhpix = QPixmap(
       
    34                         dataMgr.findFileForRead("Graphics/Hedgehog/Idle.png")
       
    35                     ).copy(0, 0, 32, 32);
       
    36 
       
    37     // my reserved hats
       
    38     QStringList hatsList = dataMgr.entryList(
       
    39                                "Graphics/Hats/Reserved",
       
    40                                QDir::Files,
       
    41                                QStringList(playerHash+"*.png")
       
    42                            );
       
    43 
       
    44     int nReserved = hatsList.size();
       
    45 
       
    46     // regular hats
       
    47     hatsList.append(dataMgr.entryList(
       
    48                         "Graphics/Hats",
       
    49                         QDir::Files,
       
    50                         QStringList("*.png")
       
    51                     )
       
    52                    );
       
    53 
       
    54 
       
    55     int nHats = hatsList.size();
       
    56 
       
    57     for (int i = 0; i < nHats; i++)
       
    58     {
       
    59         bool isReserved = (i < nReserved);
       
    60 
       
    61         QString str = hatsList.at(i);
       
    62         str = str.remove(QRegExp("\\.png$"));
       
    63         QPixmap pix(
       
    64             dataMgr.findFileForRead(
       
    65                 "Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str +
       
    66                 ".png"
       
    67             )
       
    68         );
       
    69 
       
    70         // rename properly
       
    71         if (isReserved)
       
    72             str = "Reserved "+str.remove(0,32);
       
    73 
       
    74         QPixmap tmppix(32, 37);
       
    75         tmppix.fill(QColor(Qt::transparent));
       
    76 
       
    77         QPainter painter(&tmppix);
       
    78         painter.drawPixmap(QPoint(0, 5), hhpix);
       
    79         painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
       
    80         if(pix.width() > 32)
       
    81             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
       
    82         painter.end();
       
    83 
       
    84         if (str == "NoHat")
       
    85             hats.prepend(qMakePair(str, QIcon(tmppix)));
       
    86         else
       
    87             hats.append(qMakePair(str, QIcon(tmppix)));
       
    88     }
       
    89 }
       
    90 
       
    91 QVariant HatsModel::headerData(int section,
       
    92                                Qt::Orientation orientation, int role) const
       
    93 {
       
    94     Q_UNUSED(section);
       
    95     Q_UNUSED(orientation);
       
    96     Q_UNUSED(role);
       
    97 
       
    98     return QVariant();
       
    99 }
       
   100 
       
   101 int HatsModel::rowCount(const QModelIndex &parent) const
       
   102 {
       
   103     if (parent.isValid())
       
   104         return 0;
       
   105     else
       
   106         return hats.size();
       
   107 }
       
   108 
       
   109 /*int HatsModel::columnCount(const QModelIndex & parent) const
       
   110 {
       
   111     if (parent.isValid())
       
   112         return 0;
       
   113     else
       
   114         return 2;
       
   115 }
       
   116 */
       
   117 QVariant HatsModel::data(const QModelIndex &index,
       
   118                          int role) const
       
   119 {
       
   120     if (!index.isValid() || index.row() < 0
       
   121             || index.row() >= hats.size()
       
   122             || (role != Qt::DisplayRole && role != Qt::DecorationRole))
       
   123         return QVariant();
       
   124 
       
   125     if (role == Qt::DisplayRole)
       
   126         return hats.at(index.row()).first;
       
   127     else // role == Qt::DecorationRole
       
   128         return hats.at(index.row()).second;
       
   129 }