2017-09-07 19:03:18 +02:00
|
|
|
#include "TextModel.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <QBrush>
|
|
|
|
|
|
|
|
TextModel::TextModel(QObject *parent)
|
2018-08-06 00:06:33 +02:00
|
|
|
: QAbstractTableModel(parent) {
|
2017-09-07 19:03:18 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextModel::setData(const TextMapType &textMap) {
|
|
|
|
beginResetModel();
|
|
|
|
m_textMap = textMap;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TextModel::rowCount(const QModelIndex &) const {
|
|
|
|
return m_textMap.keys.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TextModel::columnCount(const QModelIndex &) const {
|
|
|
|
return m_textMap.languages.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|