VariableModel.cpp
136 lines
| 3.7 KiB
| text/x-c
|
CppLexer
|
r163 | #include <Variable/Variable.h> | ||
|
r112 | #include <Variable/VariableModel.h> | ||
|
r165 | #include <Data/IDataSeries.h> | ||
|
r112 | |||
|
r274 | #include <QDateTime> | ||
|
r112 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | ||
|
r149 | namespace { | ||
|
r150 | // Column indexes | ||
const auto NAME_COLUMN = 0; | ||||
|
r274 | const auto TSTART_COLUMN = 1; | ||
const auto TEND_COLUMN = 2; | ||||
|
r149 | const auto NB_COLUMNS = 3; | ||
|
r274 | /// Format for datetimes | ||
const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); | ||||
|
r149 | } // namespace | ||
|
r112 | struct VariableModel::VariableModelPrivate { | ||
/// Variables created in SciQlop | ||||
|
r165 | std::vector<std::shared_ptr<Variable> > m_Variables; | ||
|
r112 | }; | ||
|
r149 | VariableModel::VariableModel(QObject *parent) | ||
: QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | ||||
|
r112 | { | ||
} | ||||
|
r165 | std::shared_ptr<Variable> | ||
r228 | VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime, | |||
|
r165 | std::unique_ptr<IDataSeries> defaultDataSeries) noexcept | ||
|
r112 | { | ||
|
r153 | auto insertIndex = rowCount(); | ||
beginInsertRows({}, insertIndex, insertIndex); | ||||
|
r112 | /// @todo For the moment, the other data of the variable is initialized with default values | ||
r228 | auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"), | |||
QStringLiteral("mission"), dateTime); | ||||
r235 | variable->setDataSeries(std::move(defaultDataSeries)); | |||
|
r165 | |||
impl->m_Variables.push_back(variable); | ||||
|
r112 | |||
|
r153 | endInsertRows(); | ||
|
r165 | return variable; | ||
|
r112 | } | ||
|
r149 | |||
|
r246 | std::shared_ptr<Variable> VariableModel::variable(int index) const | ||
{ | ||||
|
r251 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; | ||
|
r246 | } | ||
|
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) { | ||||
|
r157 | if (auto variable = impl->m_Variables.at(index.row()).get()) { | ||
|
r274 | /// Lambda function that builds the variant to return for a time value | ||
auto dateTimeVariant = [](double time) { | ||||
auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.); | ||||
return dateTime.toString(DATETIME_FORMAT); | ||||
}; | ||||
|
r151 | switch (index.column()) { | ||
case NAME_COLUMN: | ||||
|
r163 | return variable->name(); | ||
|
r274 | case TSTART_COLUMN: | ||
return dateTimeVariant(variable->dateTime().m_TStart); | ||||
case TEND_COLUMN: | ||||
return dateTimeVariant(variable->dateTime().m_TEnd); | ||||
|
r151 | 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: | ||||
|
r274 | return tr("tStart"); | ||
|
r150 | case MISSION_COLUMN: | ||
|
r274 | return tr("tEnd"); | ||
|
r150 | default: | ||
// No action | ||||
break; | ||||
} | ||||
qWarning(LOG_VariableModel()) | ||||
<< tr("Can't get header data (unknown column %1)").arg(section); | ||||
} | ||||
|
r149 | |||
return QVariant{}; | ||||
} | ||||