/**********************************************************************************
* Copyright 2020-2022 Michael McBride
*
* This file is part of PF2 Character Manager
*
* PF2 Character Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**********************************************************************************/
#include "string"
#include "armormodel.h"
#include "armor.h"
/*
* Armor Model for Armor.qml file
*
*/
ArmorModel::ArmorModel(QObject *parent)
: QAbstractListModel(parent),
mList(nullptr)
{
selected = 0;
}
int ArmorModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() || !mList)
return 0;
return mList->items().size();
}
QVariant ArmorModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !mList)
return QVariant();
const ArmorItem item = mList->items().at(index.row());
switch (role) {
case DescRole:
return QVariant(item.Description);
case IDRole:
return QVariant(item.id);
case NameRole:
if (item.Level == 0)
return QVariant(item.Name);
else
{
QVariant txt = item.Name;
txt = txt.toString() + " (level " + item.Level.toString() +")";
return txt;
}
case LevelRole:
return QVariant(item.Level);
case CategoryRole:
return QVariant(item.Category);
case PriceRole:
{
QVariant txt = "";
if (item.Price.toInt() < 100)
{
txt = (item.Price.toInt() /10);
txt = txt.toString() + " sp";
}
else
{
txt = (item.Price.toInt() / 100);
txt = txt.toString() + " gp";
}
return QVariant(txt);
}
case ACBonusRole:
return QVariant("+" + item.ACBonus.toString());
case DexCapRole:
if(item.DEXCap.toInt()<0) return "";
return QVariant("+" + item.DEXCap.toString());
case CheckPenaltyRole:
if(item.CheckPenality.toInt()>=0) return "";
return QVariant(item.CheckPenality);
case SpeedPenaltyRole:
return QVariant(item.SpeedPenality);
case StrengthRole:
return QVariant(item.Strength);
case BulkRole:
{
QVariant txt="";
if(item.Bulk.toInt()>0) {
if(item.Bulk.toInt()!=1) {
txt=int(item.Bulk.toInt()/10);
} else {
txt="Light";
}
} else txt="-";
return QVariant(txt);
}
case GroupRole:
return Armor::groupToTxt(item.Group);
case TraitsRole:
return Armor::traitToTxt(item.Traits);
case HardnessRole:
return QVariant(item.Hardness);
case HPRole:
return QVariant(item.HP);
case BTRole:
return QVariant(item.BT);
case SourceRole:
return QVariant(item.Source);
case PageRole:
return QVariant(item.Page);
case AONRole:
return QVariant(item.AON);
case CatTypeRole:
return QVariant(item.CatType);
}
return QVariant();
}
void ArmorModel::setList(Armor *list)
{
beginResetModel();
if (mList)
mList->disconnect(this);
mList = list;
if (mList) {
connect(mList, &Armor::preArmorAppended, this, [=]() {
const int index = mList->items().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(mList, &Armor::postArmorAppended, this, [=]() {
endInsertRows();
});
connect(mList, &Armor::listChanged, this, [=]() {
beginResetModel();
this->dataChanged(index(0), index(rowCount()));
endResetModel();
});
}
endResetModel();
}
QHash<int, QByteArray> ArmorModel::roleNames() const
{
QHash<int, QByteArray> names;
names[DescRole] = "Description";
names[IDRole] = "id";
names[NameRole] = "Name";
names[LevelRole] = "Level";
names[CategoryRole] = "Category";
names[PriceRole] = "Price";
names[ACBonusRole] = "ACBonus";
names[DexCapRole] = "DexCap";
names[CheckPenaltyRole] = "CheckPenalty";
names[SpeedPenaltyRole] = "SpeedPenalty";
names[StrengthRole] = "Strength";
names[BulkRole] = "Bulk";
names[GroupRole] = "Group";
names[TraitsRole] = "Traits";
names[HardnessRole] = "Hardness";
names[HPRole] = "HP";
names[BTRole] = "BT";
names[SourceRole] = "Source";
names[PageRole] = "Page";
names[AONRole] = "AON";
names[CatTypeRole] = "CatType";
return names;
}
Armor *ArmorModel::list() const
{
return mList;
}