VariableModel.cpp
120 lines
| 3.0 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r151 | #include <Variable/Variable.h> | ||
Alexandre Leroux
|
r107 | #include <Variable/VariableModel.h> | ||
Alexandre Leroux
|
r153 | #include <Data/IDataSeries.h> | ||
Alexandre Leroux
|
r107 | |||
Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | ||||
Alexandre Leroux
|
r140 | namespace { | ||
Alexandre Leroux
|
r141 | // Column indexes | ||
const auto NAME_COLUMN = 0; | ||||
const auto UNIT_COLUMN = 1; | ||||
const auto MISSION_COLUMN = 2; | ||||
Alexandre Leroux
|
r140 | const auto NB_COLUMNS = 3; | ||
} // namespace | ||||
Alexandre Leroux
|
r107 | struct VariableModel::VariableModelPrivate { | ||
/// Variables created in SciQlop | ||||
Alexandre Leroux
|
r153 | std::vector<std::shared_ptr<Variable> > m_Variables; | ||
Alexandre Leroux
|
r107 | }; | ||
Alexandre Leroux
|
r140 | VariableModel::VariableModel(QObject *parent) | ||
: QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | ||||
Alexandre Leroux
|
r107 | { | ||
} | ||||
Alexandre Leroux
|
r153 | std::shared_ptr<Variable> | ||
VariableModel::createVariable(const QString &name, | ||||
std::unique_ptr<IDataSeries> defaultDataSeries) noexcept | ||||
Alexandre Leroux
|
r107 | { | ||
Alexandre Leroux
|
r144 | auto insertIndex = rowCount(); | ||
beginInsertRows({}, insertIndex, insertIndex); | ||||
Alexandre Leroux
|
r107 | /// @todo For the moment, the other data of the variable is initialized with default values | ||
auto variable | ||||
Alexandre Leroux
|
r153 | = std::make_shared<Variable>(name, QStringLiteral("unit"), QStringLiteral("mission")); | ||
variable->addDataSeries(std::move(defaultDataSeries)); | ||||
impl->m_Variables.push_back(variable); | ||||
Alexandre Leroux
|
r107 | |||
Alexandre Leroux
|
r144 | endInsertRows(); | ||
Alexandre Leroux
|
r153 | return variable; | ||
Alexandre Leroux
|
r107 | } | ||
Alexandre Leroux
|
r140 | |||
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 | ||||
{ | ||||
Alexandre Leroux
|
r142 | if (!index.isValid()) { | ||
return QVariant{}; | ||||
} | ||||
if (index.row() < 0 || index.row() >= rowCount()) { | ||||
return QVariant{}; | ||||
} | ||||
if (role == Qt::DisplayRole) { | ||||
Alexandre Leroux
|
r147 | if (auto variable = impl->m_Variables.at(index.row()).get()) { | ||
Alexandre Leroux
|
r142 | switch (index.column()) { | ||
case NAME_COLUMN: | ||||
Alexandre Leroux
|
r151 | return variable->name(); | ||
Alexandre Leroux
|
r142 | case UNIT_COLUMN: | ||
Alexandre Leroux
|
r151 | return variable->unit(); | ||
Alexandre Leroux
|
r142 | case MISSION_COLUMN: | ||
Alexandre Leroux
|
r151 | return variable->mission(); | ||
Alexandre Leroux
|
r142 | 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)"); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r140 | return QVariant{}; | ||
} | ||||
QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | ||||
{ | ||||
Alexandre Leroux
|
r141 | 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); | ||||
} | ||||
Alexandre Leroux
|
r140 | |||
return QVariant{}; | ||||
} | ||||