VariableModel.cpp
136 lines
| 3.7 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r163 | #include <Variable/Variable.h> | ||
Alexandre Leroux
|
r112 | #include <Variable/VariableModel.h> | ||
Alexandre Leroux
|
r165 | #include <Data/IDataSeries.h> | ||
Alexandre Leroux
|
r112 | |||
Alexandre Leroux
|
r274 | #include <QDateTime> | ||
Alexandre Leroux
|
r112 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | ||
Alexandre Leroux
|
r149 | namespace { | ||
Alexandre Leroux
|
r150 | // Column indexes | ||
const auto NAME_COLUMN = 0; | ||||
Alexandre Leroux
|
r274 | const auto TSTART_COLUMN = 1; | ||
const auto TEND_COLUMN = 2; | ||||
Alexandre Leroux
|
r149 | const auto NB_COLUMNS = 3; | ||
Alexandre Leroux
|
r274 | /// Format for datetimes | ||
const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); | ||||
Alexandre Leroux
|
r149 | } // namespace | ||
Alexandre Leroux
|
r112 | struct VariableModel::VariableModelPrivate { | ||
/// Variables created in SciQlop | ||||
Alexandre Leroux
|
r165 | std::vector<std::shared_ptr<Variable> > m_Variables; | ||
Alexandre Leroux
|
r112 | }; | ||
Alexandre Leroux
|
r149 | VariableModel::VariableModel(QObject *parent) | ||
: QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | ||||
Alexandre Leroux
|
r112 | { | ||
} | ||||
Alexandre Leroux
|
r165 | std::shared_ptr<Variable> | ||
r228 | VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime, | |||
Alexandre Leroux
|
r165 | std::unique_ptr<IDataSeries> defaultDataSeries) noexcept | ||
Alexandre Leroux
|
r112 | { | ||
Alexandre Leroux
|
r153 | auto insertIndex = rowCount(); | ||
beginInsertRows({}, insertIndex, insertIndex); | ||||
Alexandre Leroux
|
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)); | |||
Alexandre Leroux
|
r165 | |||
impl->m_Variables.push_back(variable); | ||||
Alexandre Leroux
|
r112 | |||
Alexandre Leroux
|
r153 | endInsertRows(); | ||
Alexandre Leroux
|
r165 | return variable; | ||
Alexandre Leroux
|
r112 | } | ||
Alexandre Leroux
|
r149 | |||
Alexandre Leroux
|
r246 | std::shared_ptr<Variable> VariableModel::variable(int index) const | ||
{ | ||||
Alexandre Leroux
|
r251 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; | ||
Alexandre Leroux
|
r246 | } | ||
Alexandre Leroux
|
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 | ||||
{ | ||||
Alexandre Leroux
|
r151 | if (!index.isValid()) { | ||
return QVariant{}; | ||||
} | ||||
if (index.row() < 0 || index.row() >= rowCount()) { | ||||
return QVariant{}; | ||||
} | ||||
if (role == Qt::DisplayRole) { | ||||
Alexandre Leroux
|
r157 | if (auto variable = impl->m_Variables.at(index.row()).get()) { | ||
Alexandre Leroux
|
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); | ||||
}; | ||||
Alexandre Leroux
|
r151 | switch (index.column()) { | ||
case NAME_COLUMN: | ||||
Alexandre Leroux
|
r163 | return variable->name(); | ||
Alexandre Leroux
|
r274 | case TSTART_COLUMN: | ||
return dateTimeVariant(variable->dateTime().m_TStart); | ||||
case TEND_COLUMN: | ||||
return dateTimeVariant(variable->dateTime().m_TEnd); | ||||
Alexandre Leroux
|
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)"); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r149 | return QVariant{}; | ||
} | ||||
QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | ||||
{ | ||||
Alexandre Leroux
|
r150 | if (role != Qt::DisplayRole) { | ||
return QVariant{}; | ||||
} | ||||
if (orientation == Qt::Horizontal) { | ||||
switch (section) { | ||||
case NAME_COLUMN: | ||||
return tr("Name"); | ||||
case UNIT_COLUMN: | ||||
Alexandre Leroux
|
r274 | return tr("tStart"); | ||
Alexandre Leroux
|
r150 | case MISSION_COLUMN: | ||
Alexandre Leroux
|
r274 | return tr("tEnd"); | ||
Alexandre Leroux
|
r150 | default: | ||
// No action | ||||
break; | ||||
} | ||||
qWarning(LOG_VariableModel()) | ||||
<< tr("Can't get header data (unknown column %1)").arg(section); | ||||
} | ||||
Alexandre Leroux
|
r149 | |||
return QVariant{}; | ||||
} | ||||