#include "animationmodel.h"

AnimationModel::AnimationModel(QHash <int,tbAnimation> &manims, QObject *parent) :
    QAbstractListModel(parent), anims(manims)
{
//    anims = manims;
}


//void AnimationModel::refresh(QList<tbAnimation>manims)
//{
//    anims = manims;
//}


int AnimationModel::rowCount(const QModelIndex &) const
{
    return anims.count();
}


QVariant AnimationModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) return QVariant();
    if (index.row() >= anims.size()) return QVariant();
    if (role == Qt::DisplayRole)
        return tr("(%1) ").arg(anims.values().at(index.row()).id) + anims[anims.values().at(index.row()).id].name;
    if (role == Qt::EditRole)
        return anims[anims.values().at(index.row()).id].name;
    return QVariant();
}


bool AnimationModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (role != Qt::EditRole) return false;
    if (!index.isValid()) return false;
    if (index.row() >= anims.size()) return false;
    anims[anims.values().at(index.row()).id].name = value.toString();
    emit dataChanged(index, index);
    return true;
}
