File indexing completed on 2024-06-16 05:12:00

0001 #include "characterlist.h"
0002 
0003 #include "charactersheet/charactersheetmodel.h"
0004 
0005 #include <QDebug>
0006 
0007 CharacterList::CharacterList(CharacterSheetModel* sourceModel, QObject* parent)
0008     : QAbstractListModel(parent), m_source(sourceModel)
0009 {
0010 }
0011 
0012 int CharacterList::rowCount(const QModelIndex& parent) const
0013 {
0014     if(parent.isValid() || !m_source)
0015         return 0;
0016     return m_source->columnCount(QModelIndex());
0017 }
0018 
0019 QHash<int, QByteArray> CharacterList::roleNames() const
0020 {
0021     return QHash<int, QByteArray>({{NameRole, "name"}, {UuidRole, "uuid"}});
0022 }
0023 
0024 Qt::ItemFlags CharacterList::flags(const QModelIndex& index) const
0025 {
0026     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
0027 }
0028 
0029 QVariant CharacterList::data(const QModelIndex& index, int role) const
0030 {
0031     QVector<int> roles({NameRole, UuidRole, Qt::DisplayRole, Qt::EditRole});
0032     if(!index.isValid() || !roles.contains(role))
0033         return QVariant();
0034 
0035     auto r= index.row();
0036 
0037     if(r == 0)
0038     {
0039         return (role == Qt::DisplayRole) ? tr("Mock Data") : "";
0040     }
0041 
0042     auto idx= m_source->index(0, r);
0043 
0044     if(roles.contains(role))
0045     {
0046         int realRole= NameRole;
0047 
0048         if(role == UuidRole)
0049             realRole= role;
0050         auto var
0051             = idx.data(realRole == NameRole ? CharacterSheetModel::NameRole : CharacterSheetModel::UuidRole).toString();
0052 
0053         if(var.isEmpty())
0054             var= tr("Character %1").arg(r);
0055         return var;
0056     }
0057     else
0058         return QVariant();
0059 }