QTfrontend/model/hats.cpp
changeset 6061 15b4b485a1c5
parent 6060 fdfc01419815
child 6160 863d3edf5690
equal deleted inserted replaced
6060:fdfc01419815 6061:15b4b485a1c5
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2008-2011 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 HatsModel::HatsModel(QObject* parent) :
       
    27   QAbstractListModel(parent)
       
    28 {
       
    29     QFile hhfile;
       
    30     hhfile.setFileName(cfgdir->absolutePath() + "/Data/Graphics/Hedgehog/Idle.png");
       
    31     if (!hhfile.exists()) hhfile.setFileName(datadir->absolutePath() + "/Graphics/Hedgehog/Idle.png");
       
    32     QPixmap hhpix = QPixmap(QFileInfo(hhfile).absoluteFilePath()).copy(0, 0, 32, 32);
       
    33 
       
    34     QDir tmpdir;
       
    35     tmpdir.cd(cfgdir->absolutePath());
       
    36     tmpdir.cd("Data");
       
    37     tmpdir.cd("Graphics");
       
    38     tmpdir.cd("Hats");
       
    39 
       
    40     tmpdir.setFilter(QDir::Files);
       
    41 
       
    42     QStringList userhatsList = tmpdir.entryList(QStringList("*.png"));
       
    43     for (QStringList::Iterator it = userhatsList.begin(); it != userhatsList.end(); ++it )
       
    44     {
       
    45         QString str = QString(*it).replace(QRegExp("^(.*)\\.png"), "\\1");
       
    46         QPixmap pix(cfgdir->absolutePath() + "/Data/Graphics/Hats/" + str + ".png");
       
    47 
       
    48         QPixmap tmppix(32, 37);
       
    49         tmppix.fill(QColor(Qt::transparent));
       
    50 
       
    51         QPainter painter(&tmppix);
       
    52         painter.drawPixmap(QPoint(0, 5), hhpix);
       
    53         painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
       
    54         if(pix.width() > 32)
       
    55             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
       
    56         painter.end();
       
    57 
       
    58         hats.append(qMakePair(str, QIcon(tmppix)));
       
    59     }
       
    60 
       
    61     tmpdir.cd(datadir->absolutePath());
       
    62     tmpdir.cd("Graphics");
       
    63     tmpdir.cd("Hats");
       
    64 
       
    65     QStringList hatsList = tmpdir.entryList(QStringList("*.png"));
       
    66     for (QStringList::Iterator it = hatsList.begin(); it != hatsList.end(); ++it )
       
    67     {
       
    68         if (userhatsList.contains(*it,Qt::CaseInsensitive)) continue;
       
    69         QString str = (*it).replace(QRegExp("^(.*)\\.png"), "\\1");
       
    70         QPixmap pix(datadir->absolutePath() + "/Graphics/Hats/" + str + ".png");
       
    71 
       
    72         QPixmap tmppix(32, 37);
       
    73         tmppix.fill(QColor(Qt::transparent));
       
    74 
       
    75         QPainter painter(&tmppix);
       
    76         painter.drawPixmap(QPoint(0, 5), hhpix);
       
    77         painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
       
    78         if(pix.width() > 32)
       
    79             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
       
    80         painter.end();
       
    81 
       
    82         hats.append(qMakePair(str, QIcon(tmppix)));
       
    83     }
       
    84     // Reserved hats
       
    85     tmpdir.cd("Reserved");
       
    86     hatsList = tmpdir.entryList(QStringList(playerHash+"*.png"));
       
    87     for (QStringList::Iterator it = hatsList.begin(); it != hatsList.end(); ++it )
       
    88     {
       
    89         QString str = (*it).replace(QRegExp("^(.*)\\.png"), "\\1");
       
    90         QPixmap pix(datadir->absolutePath() + "/Graphics/Hats/Reserved/" + str + ".png");
       
    91 
       
    92         QPixmap tmppix(32, 37);
       
    93         tmppix.fill(QColor(Qt::transparent));
       
    94 
       
    95         QPainter painter(&tmppix);
       
    96         painter.drawPixmap(QPoint(0, 5), hhpix);
       
    97         painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
       
    98         painter.end();
       
    99 
       
   100         hats.append(qMakePair("Reserved "+str.remove(0,32), QIcon(tmppix)));
       
   101     }
       
   102 }
       
   103 
       
   104 QVariant HatsModel::headerData(int section,
       
   105             Qt::Orientation orientation, int role) const
       
   106 {
       
   107     Q_UNUSED(section);
       
   108     Q_UNUSED(orientation);
       
   109     Q_UNUSED(role);
       
   110 
       
   111     return QVariant();
       
   112 }
       
   113 
       
   114 int HatsModel::rowCount(const QModelIndex &parent) const
       
   115 {
       
   116     if (parent.isValid())
       
   117         return 0;
       
   118     else
       
   119         return hats.size();
       
   120 }
       
   121 
       
   122 /*int HatsModel::columnCount(const QModelIndex & parent) const
       
   123 {
       
   124     if (parent.isValid())
       
   125         return 0;
       
   126     else
       
   127         return 2;
       
   128 }
       
   129 */
       
   130 QVariant HatsModel::data(const QModelIndex &index,
       
   131                          int role) const
       
   132 {
       
   133     if (!index.isValid() || index.row() < 0
       
   134         || index.row() >= hats.size()
       
   135         || (role != Qt::DisplayRole && role != Qt::DecorationRole))
       
   136         return QVariant();
       
   137 
       
   138     if (role == Qt::DisplayRole)
       
   139         return hats.at(index.row()).first;
       
   140     else // role == Qt::DecorationRole
       
   141         return hats.at(index.row()).second;
       
   142 }