QTfrontend/model/HatModel.cpp
changeset 8374 3a1708759c4f
parent 8049 133e22b5c410
child 8385 9e8924ff9813
equal deleted inserted replaced
8372:3c193ec03e09 8374:3a1708759c4f
    24 #include "HatModel.h"
    24 #include "HatModel.h"
    25 
    25 
    26 #include <QDir>
    26 #include <QDir>
    27 #include <QPixmap>
    27 #include <QPixmap>
    28 #include <QPainter>
    28 #include <QPainter>
       
    29 #include <QList>
    29 #include "hwform.h" // player hash
    30 #include "hwform.h" // player hash
    30 
    31 
    31 #include "DataManager.h"
    32 #include "DataManager.h"
    32 
    33 
    33 HatModel::HatModel(QObject* parent) :
    34 HatModel::HatModel(QObject* parent) :
    34     QAbstractListModel(parent)
    35     QStandardItemModel(parent)
    35 {
    36 {}
    36     hats = QVector<QPair<QString, QIcon> >();
       
    37 }
       
    38 
    37 
    39 void HatModel::loadHats()
    38 void HatModel::loadHats()
    40 {
    39 {
    41     // this method resets the contents of this model (important to know for views).
    40     // this method resets the contents of this model (important to know for views).
    42     beginResetModel();
    41     QStandardItemModel::beginResetModel();
       
    42     QStandardItemModel::clear();
    43 
    43 
    44     // prepare hats Vector
    44     // New hats to add to model
    45     hats.clear();
    45     QList<QStandardItem *> hats;
    46 
    46 
       
    47     // we'll need the DataManager a few times, so let's get a reference to it
    47     DataManager & dataMgr = DataManager::instance();
    48     DataManager & dataMgr = DataManager::instance();
    48 
    49 
       
    50     // Default hat icon
    49     QPixmap hhpix = QPixmap("physfs://Graphics/Hedgehog/Idle.png").copy(0, 0, 32, 32);
    51     QPixmap hhpix = QPixmap("physfs://Graphics/Hedgehog/Idle.png").copy(0, 0, 32, 32);
    50 
    52 
    51     // my reserved hats
    53     // my reserved hats
    52     QStringList hatsList = dataMgr.entryList(
    54     QStringList hatsList = dataMgr.entryList(
    53                                "Graphics/Hats/Reserved",
    55                                "Graphics/Hats/Reserved",
    54                                QDir::Files,
    56                                QDir::Files,
    55                                QStringList(playerHash+"*.png")
    57                                QStringList(playerHash+"*.png")
    56                            );
    58                            );
    57 
       
    58     int nReserved = hatsList.size();
    59     int nReserved = hatsList.size();
    59 
    60 
    60     // regular hats
    61     // regular hats
    61     hatsList.append(dataMgr.entryList(
    62     hatsList.append(dataMgr.entryList(
    62                         "Graphics/Hats",
    63                         "Graphics/Hats",
    63                         QDir::Files,
    64                         QDir::Files,
    64                         QStringList("*.png")
    65                         QStringList("*.png")
    65                     )
    66                     )
    66                    );
    67                    );
    67 
       
    68 
       
    69     int nHats = hatsList.size();
    68     int nHats = hatsList.size();
    70 
    69 
       
    70     // Add each hat
    71     for (int i = 0; i < nHats; i++)
    71     for (int i = 0; i < nHats; i++)
    72     {
    72     {
    73         bool isReserved = (i < nReserved);
    73         bool isReserved = (i < nReserved);
       
    74 
       
    75         if (isReserved) continue; // For some reason, reserved hats were added in 9.19-dev, so this will hide them. Uncomment to show them.
    74 
    76 
    75         QString str = hatsList.at(i);
    77         QString str = hatsList.at(i);
    76         str = str.remove(QRegExp("\\.png$"));
    78         str = str.remove(QRegExp("\\.png$"));
    77         QPixmap pix(            
    79         QPixmap pix(            
    78                 "physfs://Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str +
    80                 "physfs://Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str +
    92         if(pix.width() > 32)
    94         if(pix.width() > 32)
    93             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
    95             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
    94         painter.end();
    96         painter.end();
    95 
    97 
    96         if (str == "NoHat")
    98         if (str == "NoHat")
    97             hats.prepend(qMakePair(str, QIcon(tmppix)));
    99             hats.prepend(new QStandardItem(QIcon(tmppix), str));
    98         else
   100         else
    99             hats.append(qMakePair(str, QIcon(tmppix)));
   101             hats.append(new QStandardItem(QIcon(tmppix), str));
   100     }
   102     }
   101 
   103 
   102 
   104     QStandardItemModel::appendColumn(hats);
   103     endResetModel();
   105     QStandardItemModel::endResetModel();
   104 }
   106 }
   105 
       
   106 QVariant HatModel::headerData(int section,
       
   107                                Qt::Orientation orientation, int role) const
       
   108 {
       
   109     Q_UNUSED(section);
       
   110     Q_UNUSED(orientation);
       
   111     Q_UNUSED(role);
       
   112 
       
   113     return QVariant();
       
   114 }
       
   115 
       
   116 int HatModel::rowCount(const QModelIndex &parent) const
       
   117 {
       
   118     if (parent.isValid())
       
   119         return 0;
       
   120     else
       
   121         return hats.size();
       
   122 }
       
   123 
       
   124 /*int HatModel::columnCount(const QModelIndex & parent) const
       
   125 {
       
   126     if (parent.isValid())
       
   127         return 0;
       
   128     else
       
   129         return 2;
       
   130 }
       
   131 */
       
   132 QVariant HatModel::data(const QModelIndex &index,
       
   133                          int role) const
       
   134 {
       
   135     if (!index.isValid() || index.row() < 0
       
   136             || index.row() >= hats.size()
       
   137             || (role != Qt::DisplayRole && role != Qt::DecorationRole))
       
   138         return QVariant();
       
   139 
       
   140     if (role == Qt::DisplayRole)
       
   141         return hats.at(index.row()).first;
       
   142     else // role == Qt::DecorationRole
       
   143         return hats.at(index.row()).second;
       
   144 }