1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-26 04:12:41 +01:00
openrw/rwviewer/models/TextModel.cpp

77 lines
2.0 KiB
C++
Raw Normal View History

2017-09-07 19:03:18 +02:00
#include "TextModel.hpp"
#include <iostream>
#include <QBrush>
TextModel::TextModel(QObject *parent)
: QAbstractTableModel(parent) {
2017-09-07 19:03:18 +02:00
}
2018-08-30 02:19:38 +02:00
void TextModel::setMapData(const TextMapType &textMap) {
2017-09-07 19:03:18 +02:00
beginResetModel();
m_textMap = textMap;
endResetModel();
}
int TextModel::rowCount(const QModelIndex &) const {
2018-08-30 02:19:38 +02:00
return static_cast<int>(m_textMap.keys.size());
2017-09-07 19:03:18 +02:00
}
int TextModel::columnCount(const QModelIndex &) const {
2018-08-30 02:19:38 +02:00
return static_cast<int>(m_textMap.languages.size());
2017-09-07 19:03:18 +02:00
}
const GameString &TextModel::lookupIndex(const QModelIndex &index) const {
const auto &language = m_textMap.languages.at(index.column());
const auto &key = m_textMap.keys.at(index.row());
return m_textMap.map_lang_key_tran.at(language).at(key);
}
QVariant TextModel::data(const QModelIndex &index, int role) const {
switch (role) {
case Qt::DisplayRole:
try {
const auto &gameText = this->lookupIndex(index);
auto gameString = GameStringUtil::toString(gameText, m_font);
return QString::fromStdString(gameString);
} catch (const std::out_of_range &) {
return QVariant::Invalid;
} catch (...) {
throw;
}
case Qt::BackgroundRole:
try {
this->lookupIndex(index);
return QVariant::Invalid;
} catch (const std::out_of_range &) {
return QBrush(Qt::red);
}
break;
default:
return QVariant::Invalid;
}
}
QVariant TextModel::headerData(int section, Qt::Orientation orientation, int role) const {
switch (role) {
case Qt::DisplayRole:
switch (orientation) {
case Qt::Horizontal:
return QString::fromStdString(m_textMap.languages[section]);
case Qt::Vertical:
return QString::fromStdString(m_textMap.keys[section]);
default:
return QVariant::Invalid;
}
default:
return QVariant::Invalid;
}
}
void TextModel::fontChanged(font_t font) {
beginResetModel();
m_font = font;
endResetModel();
}