QTfrontend/model/playerslistmodel.cpp
changeset 7725 4ad05a478c6c
parent 7723 ca05b576ec74
child 7727 bd252cacabe8
equal deleted inserted replaced
7724:36c539c9cfb1 7725:4ad05a478c6c
       
     1 #include <QModelIndexList>
       
     2 #include <QModelIndex>
       
     3 #include <QPainter>
       
     4 
     1 #include "playerslistmodel.h"
     5 #include "playerslistmodel.h"
     2 
     6 
     3 PlayersListModel::PlayersListModel(QObject *parent) :
     7 PlayersListModel::PlayersListModel(QObject *parent) :
     4     QStringListModel(parent)
     8     QAbstractListModel(parent)
     5 {
     9 {
     6 
    10 
     7 }
    11 }
     8 
    12 
       
    13 
       
    14 int PlayersListModel::rowCount(const QModelIndex &parent) const
       
    15 {
       
    16     if(parent.isValid())
       
    17         return 0;
       
    18     else
       
    19         return m_data.size();
       
    20 }
       
    21 
       
    22 
       
    23 QVariant PlayersListModel::data(const QModelIndex &index, int role) const
       
    24 {
       
    25     if(!index.isValid())
       
    26         return QVariant(QVariant::Invalid);
       
    27 
       
    28     return m_data.at(index.row()).value(role);
       
    29 }
       
    30 
       
    31 
       
    32 bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value, int role)
       
    33 {
       
    34     if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
       
    35         return false;
       
    36 
       
    37     m_data[index.row()].insert(role, value);
       
    38 
       
    39     emit dataChanged(index, index);
       
    40 
       
    41     return true;
       
    42 }
       
    43 
       
    44 
       
    45 bool PlayersListModel::insertRow(int row, const QModelIndex &parent)
       
    46 {
       
    47     return insertRows(row, 1, parent);
       
    48 }
       
    49 
       
    50 
       
    51 bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent)
       
    52 {
       
    53     if(parent.isValid() || row > rowCount() || row < 0 || count < 1)
       
    54         return false;
       
    55 
       
    56     beginInsertRows(parent, row, row + count - 1);
       
    57 
       
    58     for(int i = 0; i < count; ++i)
       
    59         m_data.insert(row, DataEntry());
       
    60 
       
    61     endInsertRows();
       
    62 
       
    63     return true;
       
    64 }
       
    65 
       
    66 
       
    67 bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent)
       
    68 {
       
    69     if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1)
       
    70         return false;
       
    71 
       
    72     beginRemoveRows(parent, row, row + count - 1);
       
    73 
       
    74     for(int i = 0; i < count; ++i)
       
    75         m_data.removeAt(row);
       
    76 
       
    77     endRemoveRows();
       
    78 
       
    79     return true;
       
    80 }
       
    81 
       
    82 
     9 void PlayersListModel::addPlayer(const QString & nickname)
    83 void PlayersListModel::addPlayer(const QString & nickname)
    10 {
    84 {
    11     insertRows(rowCount(), 1);
    85     insertRow(rowCount());
    12 
    86 
    13     setData(index(rowCount() - 1), nickname);
    87     setData(index(rowCount() - 1), nickname);
       
    88 
       
    89     updateIcon(index(rowCount() - 1));
    14 }
    90 }
       
    91 
       
    92 
       
    93 void PlayersListModel::removePlayer(const QString & nickname)
       
    94 {
       
    95     QModelIndexList mil = match(index(0, 0), Qt::DisplayRole, nickname);
       
    96 
       
    97     if(mil.size())
       
    98         removeRow(mil[0].row());
       
    99 }
       
   100 
       
   101 
       
   102 void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet)
       
   103 {
       
   104     QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
       
   105 
       
   106     if(mil.size())
       
   107     {
       
   108         setData(mil[0], flagType, isSet);
       
   109         updateIcon(mil[0]);
       
   110     }
       
   111 }
       
   112 
       
   113 
       
   114 void PlayersListModel::updateIcon(const QModelIndex & index)
       
   115 {
       
   116     quint32 iconNum = 0;
       
   117 
       
   118     QList<bool> flags;
       
   119     flags
       
   120         << index.data(Ready).toBool()
       
   121         << index.data(ServerAdmin).toBool()
       
   122         << index.data(RoomAdmin).toBool()
       
   123         << index.data(Registered).toBool()
       
   124         << index.data(Friend).toBool()
       
   125         << index.data(Ignore).toBool()
       
   126         ;
       
   127 
       
   128     for(int i = flags.size() - 1; i >= 0; --i)
       
   129         if(flags[i])
       
   130             iconNum |= 1 << i;
       
   131 
       
   132     if(m_icons().contains(iconNum))
       
   133     {
       
   134         setData(index, m_icons().value(iconNum), Qt::DecorationRole);
       
   135         qDebug("cached");
       
   136     }
       
   137     else
       
   138     {
       
   139         QPixmap result(24, 16);
       
   140         result.fill(Qt::transparent);
       
   141 
       
   142         QPainter painter(&result);
       
   143 
       
   144         if(index.data(Ready).toBool())
       
   145             painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png"));
       
   146 
       
   147         QString mainIconName(":/res/chat/");
       
   148 
       
   149         if(index.data(RoomAdmin).toBool())
       
   150             mainIconName += "roomadmin";
       
   151         else if(index.data(ServerAdmin).toBool())
       
   152             mainIconName += "serveradmin";
       
   153         else
       
   154             mainIconName += "hedgehog";
       
   155 
       
   156         if(!index.data(Registered).toBool())
       
   157             mainIconName += "_gray";
       
   158 
       
   159         painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png"));
       
   160 
       
   161         if(index.data(Ignore).toBool())
       
   162             painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png"));
       
   163         else
       
   164         if(index.data(Friend).toBool())
       
   165             painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png"));
       
   166 
       
   167         painter.end();
       
   168 
       
   169         QIcon icon(result);
       
   170 
       
   171         setData(index, icon, Qt::DecorationRole);
       
   172         m_icons().insert(iconNum, icon);
       
   173     }
       
   174 
       
   175     if(index.data(Ignore).toBool())
       
   176         setData(index, Qt::gray, Qt::ForegroundRole);
       
   177     else
       
   178     if(index.data(Friend).toBool())
       
   179         setData(index, Qt::green, Qt::ForegroundRole);
       
   180     else
       
   181         setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole);
       
   182 }
       
   183 
       
   184 
       
   185 QHash<quint32, QIcon> & PlayersListModel::m_icons()
       
   186 {
       
   187     static QHash<quint32, QIcon> iconsCache;
       
   188 
       
   189     return iconsCache;
       
   190 }