1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#include "reportmodel.hpp"
#include <algorithm>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <apps/opencs/model/doc/messages.hpp>
#include <apps/opencs/model/world/universalid.hpp>
#include "../world/columns.hpp"
CSMTools::ReportModel::ReportModel(bool fieldColumn, bool severityColumn)
: mColumnField(-1)
, mColumnSeverity(-1)
{
int index = 3;
if (severityColumn)
mColumnSeverity = index++;
if (fieldColumn)
mColumnField = index++;
mColumnDescription = index;
}
int CSMTools::ReportModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return static_cast<int>(mRows.size());
}
int CSMTools::ReportModel::columnCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return mColumnDescription + 1;
}
QVariant CSMTools::ReportModel::data(const QModelIndex& index, int role) const
{
if (role != Qt::DisplayRole && role != Qt::UserRole)
return QVariant();
switch (index.column())
{
case Column_Type:
if (role == Qt::UserRole)
return QString::fromUtf8(mRows.at(index.row()).mId.getTypeName().c_str());
else
return static_cast<int>(mRows.at(index.row()).mId.getType());
case Column_Id:
{
CSMWorld::UniversalId id = mRows.at(index.row()).mId;
switch (id.getArgumentType())
{
case CSMWorld::UniversalId::ArgumentType_None:
return QString("-");
case CSMWorld::UniversalId::ArgumentType_Index:
return QString::number(id.getIndex());
case CSMWorld::UniversalId::ArgumentType_Id:
return QString::fromStdString(id.getId());
case CSMWorld::UniversalId::ArgumentType_RefId:
return QString::fromStdString(id.getRefId().toString());
}
return QString("unsupported");
}
case Column_Hint:
return QString::fromUtf8(mRows.at(index.row()).mHint.c_str());
}
if (index.column() == mColumnDescription)
return QString::fromUtf8(mRows.at(index.row()).mMessage.c_str());
if (index.column() == mColumnField)
{
std::string field;
std::istringstream stream(mRows.at(index.row()).mHint);
char type, ignore;
int fieldIndex;
if ((stream >> type >> ignore >> fieldIndex) && (type == 'r' || type == 'R'))
{
field = CSMWorld::Columns::getName(static_cast<CSMWorld::Columns::ColumnId>(fieldIndex));
}
return QString::fromUtf8(field.c_str());
}
if (index.column() == mColumnSeverity)
{
return QString::fromUtf8(CSMDoc::Message::toString(mRows.at(index.row()).mSeverity).c_str());
}
return QVariant();
}
QVariant CSMTools::ReportModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Vertical)
return QVariant();
switch (section)
{
case Column_Type:
return "Type";
case Column_Id:
return "ID";
}
if (section == mColumnDescription)
return "Description";
if (section == mColumnField)
return "Field";
if (section == mColumnSeverity)
return "Severity";
return "-";
}
bool CSMTools::ReportModel::removeRows(int row, int count, const QModelIndex& parent)
{
if (parent.isValid())
return false;
if (count > 0)
{
beginRemoveRows(parent, row, row + count - 1);
mRows.erase(mRows.begin() + row, mRows.begin() + row + count);
endRemoveRows();
}
return true;
}
void CSMTools::ReportModel::add(const CSMDoc::Message& message)
{
beginInsertRows(QModelIndex(), static_cast<int>(mRows.size()), static_cast<int>(mRows.size()));
mRows.push_back(message);
endInsertRows();
}
void CSMTools::ReportModel::flagAsReplaced(int index)
{
CSMDoc::Message& line = mRows.at(index);
std::string hint = line.mHint;
if (hint.empty() || hint[0] != 'R')
throw std::logic_error("trying to flag message as replaced that is not replaceable");
hint[0] = 'r';
line.mHint = std::move(hint);
emit dataChanged(this->index(index, 0), this->index(index, columnCount()));
}
const CSMWorld::UniversalId& CSMTools::ReportModel::getUniversalId(int row) const
{
return mRows.at(row).mId;
}
std::string CSMTools::ReportModel::getHint(int row) const
{
return mRows.at(row).mHint;
}
void CSMTools::ReportModel::clear()
{
if (!mRows.empty())
{
beginRemoveRows(QModelIndex(), 0, static_cast<int>(mRows.size()) - 1);
mRows.clear();
endRemoveRows();
}
}
int CSMTools::ReportModel::countErrors() const
{
int count = 0;
for (std::vector<CSMDoc::Message>::const_iterator iter(mRows.begin()); iter != mRows.end(); ++iter)
if (iter->mSeverity == CSMDoc::Message::Severity_Error
|| iter->mSeverity == CSMDoc::Message::Severity_SeriousError)
++count;
return count;
}
|