VariableModel.cpp
117 lines
| 2.9 KiB
| text/x-c
|
CppLexer
|
r112 | #include <Variable/VariableModel.h> | ||
#include <Variable/Variable.h> | ||||
Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | ||||
|
r149 | namespace { | ||
|
r150 | // Column indexes | ||
const auto NAME_COLUMN = 0; | ||||
const auto UNIT_COLUMN = 1; | ||||
const auto MISSION_COLUMN = 2; | ||||
|
r149 | const auto NB_COLUMNS = 3; | ||
} // namespace | ||||
|
r112 | struct VariableModel::VariableModelPrivate { | ||
/// Variables created in SciQlop | ||||
std::vector<std::unique_ptr<Variable> > m_Variables; | ||||
}; | ||||
|
r149 | VariableModel::VariableModel(QObject *parent) | ||
: QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | ||||
|
r112 | { | ||
} | ||||
Variable *VariableModel::createVariable(const QString &name) noexcept | ||||
{ | ||||
|
r153 | auto insertIndex = rowCount(); | ||
beginInsertRows({}, insertIndex, insertIndex); | ||||
|
r112 | /// @todo For the moment, the other data of the variable is initialized with default values | ||
auto variable | ||||
= std::make_unique<Variable>(name, QStringLiteral("unit"), QStringLiteral("mission")); | ||||
impl->m_Variables.push_back(std::move(variable)); | ||||
|
r153 | endInsertRows(); | ||
return impl->m_Variables.at(insertIndex).get(); | ||||
|
r112 | } | ||
|
r149 | |||
int VariableModel::columnCount(const QModelIndex &parent) const | ||||
{ | ||||
Q_UNUSED(parent); | ||||
return NB_COLUMNS; | ||||
} | ||||
int VariableModel::rowCount(const QModelIndex &parent) const | ||||
{ | ||||
Q_UNUSED(parent); | ||||
return impl->m_Variables.size(); | ||||
} | ||||
QVariant VariableModel::data(const QModelIndex &index, int role) const | ||||
{ | ||||
|
r151 | if (!index.isValid()) { | ||
return QVariant{}; | ||||
} | ||||
if (index.row() < 0 || index.row() >= rowCount()) { | ||||
return QVariant{}; | ||||
} | ||||
if (role == Qt::DisplayRole) { | ||||
const auto &variable = impl->m_Variables.at(index.row()); | ||||
if (variable) { | ||||
switch (index.column()) { | ||||
case NAME_COLUMN: | ||||
return variable->m_Name; | ||||
case UNIT_COLUMN: | ||||
return variable->m_Unit; | ||||
case MISSION_COLUMN: | ||||
return variable->m_Mission; | ||||
default: | ||||
// No action | ||||
break; | ||||
} | ||||
qWarning(LOG_VariableModel()) | ||||
<< tr("Can't get data (unknown column %1)").arg(index.column()); | ||||
} | ||||
else { | ||||
qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); | ||||
} | ||||
} | ||||
|
r149 | return QVariant{}; | ||
} | ||||
QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | ||||
{ | ||||
|
r150 | if (role != Qt::DisplayRole) { | ||
return QVariant{}; | ||||
} | ||||
if (orientation == Qt::Horizontal) { | ||||
switch (section) { | ||||
case NAME_COLUMN: | ||||
return tr("Name"); | ||||
case UNIT_COLUMN: | ||||
return tr("Unit"); | ||||
case MISSION_COLUMN: | ||||
return tr("Mission"); | ||||
default: | ||||
// No action | ||||
break; | ||||
} | ||||
qWarning(LOG_VariableModel()) | ||||
<< tr("Can't get header data (unknown column %1)").arg(section); | ||||
} | ||||
|
r149 | |||
return QVariant{}; | ||||
} | ||||