QTfrontend/model/hats.cpp
changeset 6160 863d3edf5690
parent 6061 15b4b485a1c5
child 6167 728cabee2c9f
equal deleted inserted replaced
6159:c780b8cf4d75 6160:863d3edf5690
    21 #include <QPainter>
    21 #include <QPainter>
    22 #include "hwconsts.h"
    22 #include "hwconsts.h"
    23 #include "hwform.h"
    23 #include "hwform.h"
    24 #include "hats.h"
    24 #include "hats.h"
    25 
    25 
       
    26 #include "HWDataManager.h"
       
    27 
    26 HatsModel::HatsModel(QObject* parent) :
    28 HatsModel::HatsModel(QObject* parent) :
    27   QAbstractListModel(parent)
    29   QAbstractListModel(parent)
    28 {
    30 {
    29     QFile hhfile;
    31     QFile * hhfile =
    30     hhfile.setFileName(cfgdir->absolutePath() + "/Data/Graphics/Hedgehog/Idle.png");
    32         HWDataManager::instance().findFileForRead("Graphics/Hedgehog/Idle.png");
    31     if (!hhfile.exists()) hhfile.setFileName(datadir->absolutePath() + "/Graphics/Hedgehog/Idle.png");
    33     QPixmap hhpix = QPixmap(hhfile->fileName()).copy(0, 0, 32, 32);
    32     QPixmap hhpix = QPixmap(QFileInfo(hhfile).absoluteFilePath()).copy(0, 0, 32, 32);
       
    33 
    34 
    34     QDir tmpdir;
    35     // this QFile is not needed any further
    35     tmpdir.cd(cfgdir->absolutePath());
    36     delete hhfile;
    36     tmpdir.cd("Data");
       
    37     tmpdir.cd("Graphics");
       
    38     tmpdir.cd("Hats");
       
    39 
    37 
    40     tmpdir.setFilter(QDir::Files);
    38     // my reserved hats
       
    39     QStringList hatsList = HWDataManager::instance().entryList(
       
    40                                    "Graphics/Hats/Reserved",
       
    41                                    QDir::Files,
       
    42                                    QStringList(playerHash+"*.png")
       
    43                                );
    41 
    44 
    42     QStringList userhatsList = tmpdir.entryList(QStringList("*.png"));
    45     int nReserved = hatsList.size();
    43     for (QStringList::Iterator it = userhatsList.begin(); it != userhatsList.end(); ++it )
    46 
       
    47     // regular hats
       
    48     hatsList.append(HWDataManager::instance().entryList(
       
    49                                    "Graphics/Hats",
       
    50                                    QDir::Files,
       
    51                                    QStringList("*.png")
       
    52                                )
       
    53                    );
       
    54 
       
    55 
       
    56     int nHats = hatsList.size();
       
    57 
       
    58     for (int i = 0; i < nHats; i++)
    44     {
    59     {
    45         QString str = QString(*it).replace(QRegExp("^(.*)\\.png"), "\\1");
    60         bool isReserved = (i < nReserved);
    46         QPixmap pix(cfgdir->absolutePath() + "/Data/Graphics/Hats/" + str + ".png");
    61 
       
    62         QString str = hatsList.at(i);
       
    63         str = str.remove(QRegExp("\\.png$"));
       
    64         QFile * hatFile = HWDataManager::instance().findFileForRead(
       
    65                                             "Graphics/Hats/" + 
       
    66                                             QString(isReserved?"Reserved/":"") +
       
    67                                             str + ".png"
       
    68                                         );
       
    69         QPixmap pix(hatFile->fileName());
       
    70 
       
    71         // this QFile isn't needed any further
       
    72         delete hatFile;
       
    73 
       
    74         // rename properly
       
    75         if (isReserved)
       
    76             str = "Reserved "+str.remove(0,32);
    47 
    77 
    48         QPixmap tmppix(32, 37);
    78         QPixmap tmppix(32, 37);
    49         tmppix.fill(QColor(Qt::transparent));
    79         tmppix.fill(QColor(Qt::transparent));
    50 
    80 
    51         QPainter painter(&tmppix);
    81         QPainter painter(&tmppix);
    53         painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
    83         painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
    54         if(pix.width() > 32)
    84         if(pix.width() > 32)
    55             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
    85             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
    56         painter.end();
    86         painter.end();
    57 
    87 
    58         hats.append(qMakePair(str, QIcon(tmppix)));
    88         if (str == "NoHat")
    59     }
    89             hats.prepend(qMakePair(str, QIcon(tmppix)));
    60 
    90         else
    61     tmpdir.cd(datadir->absolutePath());
    91             hats.append(qMakePair(str, QIcon(tmppix)));
    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     }
    92     }
   102 }
    93 }
   103 
    94 
   104 QVariant HatsModel::headerData(int section,
    95 QVariant HatsModel::headerData(int section,
   105             Qt::Orientation orientation, int role) const
    96             Qt::Orientation orientation, int role) const