1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 19:23:52 +00:00
openmw/apps/opencs/model/tools/reportmodel.cpp

201 lines
4.7 KiB
C++
Raw Normal View History

2012-12-11 14:35:47 +00:00
#include "reportmodel.hpp"
2022-10-19 17:02:00 +00:00
#include <algorithm>
#include <memory>
2015-03-29 13:28:31 +00:00
#include <sstream>
2022-09-22 18:26:05 +00:00
#include <stdexcept>
2015-03-29 13:28:31 +00:00
2022-10-19 17:02:00 +00:00
#include <apps/opencs/model/doc/messages.hpp>
#include <apps/opencs/model/world/universalid.hpp>
2015-03-29 13:28:31 +00:00
#include "../world/columns.hpp"
2012-12-11 14:35:47 +00:00
2022-09-22 18:26:05 +00:00
CSMTools::ReportModel::ReportModel(bool fieldColumn, bool severityColumn)
: mColumnField(-1)
, mColumnSeverity(-1)
2015-03-29 13:28:31 +00:00
{
int index = 3;
if (severityColumn)
mColumnSeverity = index++;
2015-03-29 13:28:31 +00:00
if (fieldColumn)
mColumnField = index++;
2015-03-29 13:28:31 +00:00
mColumnDescription = index;
2015-03-29 13:28:31 +00:00
}
2022-09-22 18:26:05 +00:00
int CSMTools::ReportModel::rowCount(const QModelIndex& parent) const
2012-12-11 14:35:47 +00:00
{
if (parent.isValid())
return 0;
2021-05-02 06:43:44 +00:00
return static_cast<int>(mRows.size());
2012-12-11 14:35:47 +00:00
}
2022-09-22 18:26:05 +00:00
int CSMTools::ReportModel::columnCount(const QModelIndex& parent) const
2012-12-11 14:35:47 +00:00
{
if (parent.isValid())
return 0;
2022-09-22 18:26:05 +00:00
return mColumnDescription + 1;
2012-12-11 14:35:47 +00:00
}
2022-09-22 18:26:05 +00:00
QVariant CSMTools::ReportModel::data(const QModelIndex& index, int role) const
2012-12-11 14:35:47 +00:00
{
2022-09-22 18:26:05 +00:00
if (role != Qt::DisplayRole && role != Qt::UserRole)
2012-12-11 14:35:47 +00:00
return QVariant();
2015-03-29 11:55:31 +00:00
switch (index.column())
{
case Column_Type:
2022-09-22 18:26:05 +00:00
if (role == Qt::UserRole)
return QString::fromUtf8(mRows.at(index.row()).mId.getTypeName().c_str());
2017-02-21 10:49:04 +00:00
else
2022-09-22 18:26:05 +00:00
return static_cast<int>(mRows.at(index.row()).mId.getType());
2015-03-29 11:55:31 +00:00
case Column_Id:
{
2022-09-22 18:26:05 +00:00
CSMWorld::UniversalId id = mRows.at(index.row()).mId;
2015-03-29 11:55:31 +00:00
2022-09-22 18:26:05 +00:00
if (id.getArgumentType() == CSMWorld::UniversalId::ArgumentType_Id)
return QString::fromUtf8(id.getId().c_str());
2022-09-22 18:26:05 +00:00
return QString("-");
2015-03-29 11:55:31 +00:00
}
2015-03-29 11:55:31 +00:00
case Column_Hint:
2022-09-22 18:26:05 +00:00
return QString::fromUtf8(mRows.at(index.row()).mHint.c_str());
2015-03-29 11:55:31 +00:00
}
2022-09-22 18:26:05 +00:00
if (index.column() == mColumnDescription)
return QString::fromUtf8(mRows.at(index.row()).mMessage.c_str());
2015-03-29 13:28:31 +00:00
2022-09-22 18:26:05 +00:00
if (index.column() == mColumnField)
2015-03-29 13:28:31 +00:00
{
std::string field;
2022-09-22 18:26:05 +00:00
std::istringstream stream(mRows.at(index.row()).mHint);
2015-03-29 13:28:31 +00:00
char type, ignore;
int fieldIndex;
2022-09-22 18:26:05 +00:00
if ((stream >> type >> ignore >> fieldIndex) && (type == 'r' || type == 'R'))
2015-03-29 13:28:31 +00:00
{
2022-09-22 18:26:05 +00:00
field = CSMWorld::Columns::getName(static_cast<CSMWorld::Columns::ColumnId>(fieldIndex));
2015-03-29 13:28:31 +00:00
}
2022-09-22 18:26:05 +00:00
return QString::fromUtf8(field.c_str());
2015-03-29 13:28:31 +00:00
}
2022-09-22 18:26:05 +00:00
if (index.column() == mColumnSeverity)
{
2022-09-22 18:26:05 +00:00
return QString::fromUtf8(CSMDoc::Message::toString(mRows.at(index.row()).mSeverity).c_str());
}
2015-03-29 11:55:31 +00:00
return QVariant();
2012-12-11 14:35:47 +00:00
}
2022-09-22 18:26:05 +00:00
QVariant CSMTools::ReportModel::headerData(int section, Qt::Orientation orientation, int role) const
2012-12-11 14:35:47 +00:00
{
2022-09-22 18:26:05 +00:00
if (role != Qt::DisplayRole)
2012-12-11 14:35:47 +00:00
return QVariant();
2022-09-22 18:26:05 +00:00
if (orientation == Qt::Vertical)
2012-12-11 14:35:47 +00:00
return QVariant();
2015-03-29 11:55:31 +00:00
switch (section)
{
2022-09-22 18:26:05 +00:00
case Column_Type:
return "Type";
case Column_Id:
return "ID";
2015-03-29 11:55:31 +00:00
}
2022-09-22 18:26:05 +00:00
if (section == mColumnDescription)
2015-03-29 13:28:31 +00:00
return "Description";
2022-09-22 18:26:05 +00:00
if (section == mColumnField)
2015-03-29 13:28:31 +00:00
return "Field";
2022-09-22 18:26:05 +00:00
if (section == mColumnSeverity)
return "Severity";
2015-03-29 11:55:31 +00:00
return "-";
2012-12-11 14:35:47 +00:00
}
2022-09-22 18:26:05 +00:00
bool CSMTools::ReportModel::removeRows(int row, int count, const QModelIndex& parent)
2012-12-11 14:35:47 +00:00
{
if (parent.isValid())
return false;
2022-09-22 18:26:05 +00:00
if (count > 0)
{
2022-09-22 18:26:05 +00:00
beginRemoveRows(parent, row, row + count - 1);
2022-09-22 18:26:05 +00:00
mRows.erase(mRows.begin() + row, mRows.begin() + row + count);
endRemoveRows();
}
2012-12-11 14:35:47 +00:00
return true;
}
2022-09-22 18:26:05 +00:00
void CSMTools::ReportModel::add(const CSMDoc::Message& message)
2012-12-11 14:35:47 +00:00
{
2022-09-22 18:26:05 +00:00
beginInsertRows(QModelIndex(), static_cast<int>(mRows.size()), static_cast<int>(mRows.size()));
2022-09-22 18:26:05 +00:00
mRows.push_back(message);
2012-12-11 14:35:47 +00:00
endInsertRows();
}
2022-09-22 18:26:05 +00:00
void CSMTools::ReportModel::flagAsReplaced(int index)
2015-04-16 16:50:22 +00:00
{
2022-09-22 18:26:05 +00:00
CSMDoc::Message& line = mRows.at(index);
2015-04-16 16:50:22 +00:00
std::string hint = line.mHint;
2022-09-22 18:26:05 +00:00
if (hint.empty() || hint[0] != 'R')
throw std::logic_error("trying to flag message as replaced that is not replaceable");
2015-04-16 16:50:22 +00:00
hint[0] = 'r';
line.mHint = hint;
2022-09-22 18:26:05 +00:00
emit dataChanged(this->index(index, 0), this->index(index, columnCount()));
2015-04-16 16:50:22 +00:00
}
2022-09-22 18:26:05 +00:00
const CSMWorld::UniversalId& CSMTools::ReportModel::getUniversalId(int row) const
{
2022-09-22 18:26:05 +00:00
return mRows.at(row).mId;
}
2022-09-22 18:26:05 +00:00
std::string CSMTools::ReportModel::getHint(int row) const
{
2022-09-22 18:26:05 +00:00
return mRows.at(row).mHint;
2015-03-11 14:54:45 +00:00
}
void CSMTools::ReportModel::clear()
{
if (!mRows.empty())
{
2022-09-22 18:26:05 +00:00
beginRemoveRows(QModelIndex(), 0, static_cast<int>(mRows.size()) - 1);
mRows.clear();
endRemoveRows();
}
}
int CSMTools::ReportModel::countErrors() const
{
int count = 0;
2022-09-22 18:26:05 +00:00
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;
}