VariableModel.cpp
255 lines
| 7.6 KiB
| text/x-c
|
CppLexer
|
r151 | #include <Variable/Variable.h> | ||
|
r107 | #include <Variable/VariableModel.h> | ||
|
r450 | #include <Common/DateUtils.h> | ||
|
r153 | #include <Data/IDataSeries.h> | ||
|
r107 | |||
|
r257 | #include <QSize> | ||
r369 | #include <unordered_map> | |||
|
r254 | |||
|
r107 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | ||
|
r140 | namespace { | ||
|
r141 | // Column indexes | ||
const auto NAME_COLUMN = 0; | ||||
|
r254 | const auto TSTART_COLUMN = 1; | ||
const auto TEND_COLUMN = 2; | ||||
|
r519 | const auto UNIT_COLUMN = 3; | ||
const auto MISSION_COLUMN = 4; | ||||
const auto NB_COLUMNS = 5; | ||||
|
r140 | |||
|
r255 | // Column properties | ||
|
r256 | const auto DEFAULT_HEIGHT = 25; | ||
const auto DEFAULT_WIDTH = 100; | ||||
|
r255 | |||
struct ColumnProperties { | ||||
|
r256 | ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, | ||
int height = DEFAULT_HEIGHT) | ||||
: m_Name{name}, m_Width{width}, m_Height{height} | ||||
{ | ||||
} | ||||
|
r255 | |||
QString m_Name; | ||||
|
r256 | int m_Width; | ||
int m_Height; | ||||
|
r255 | }; | ||
|
r519 | const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{ | ||
{NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, | ||||
{TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {UNIT_COLUMN, {QObject::tr("Unit")}}, | ||||
{MISSION_COLUMN, {QObject::tr("Mission")}}}; | ||||
|
r255 | |||
|
r254 | /// Format for datetimes | ||
const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); | ||||
r369 | ||||
|
r140 | } // namespace | ||
|
r107 | struct VariableModel::VariableModelPrivate { | ||
/// Variables created in SciQlop | ||||
|
r153 | std::vector<std::shared_ptr<Variable> > m_Variables; | ||
|
r387 | std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress; | ||
r369 | ||||
r370 | /// Return the row index of the variable. -1 if it's not found | |||
r369 | int indexOfVariable(Variable *variable) const noexcept; | |||
|
r107 | }; | ||
|
r140 | VariableModel::VariableModel(QObject *parent) | ||
: QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | ||||
|
r107 | { | ||
} | ||||
r294 | std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, | |||
r471 | const SqpRange &dateTime, | |||
|
r377 | const QVariantHash &metadata) noexcept | ||
|
r107 | { | ||
|
r144 | auto insertIndex = rowCount(); | ||
beginInsertRows({}, insertIndex, insertIndex); | ||||
|
r377 | auto variable = std::make_shared<Variable>(name, dateTime, metadata); | ||
|
r153 | |||
impl->m_Variables.push_back(variable); | ||||
|
r340 | connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); | ||
|
r107 | |||
|
r144 | endInsertRows(); | ||
|
r153 | return variable; | ||
|
r107 | } | ||
|
r140 | |||
|
r306 | void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
if (!variable) { | ||||
qCCritical(LOG_Variable()) << "Can't delete a null variable from the model"; | ||||
return; | ||||
} | ||||
// Finds variable in the model | ||||
auto begin = impl->m_Variables.cbegin(); | ||||
auto end = impl->m_Variables.cend(); | ||||
auto it = std::find(begin, end, variable); | ||||
if (it != end) { | ||||
auto removeIndex = std::distance(begin, it); | ||||
// Deletes variable | ||||
beginRemoveRows({}, removeIndex, removeIndex); | ||||
impl->m_Variables.erase(it); | ||||
endRemoveRows(); | ||||
} | ||||
else { | ||||
qCritical(LOG_VariableModel()) | ||||
<< tr("Can't delete variable %1 from the model: the variable is not in the model") | ||||
.arg(variable->name()); | ||||
} | ||||
|
r387 | |||
// Removes variable from progress map | ||||
impl->m_VariableToProgress.erase(variable); | ||||
|
r306 | } | ||
r369 | ||||
|
r229 | std::shared_ptr<Variable> VariableModel::variable(int index) const | ||
{ | ||||
|
r234 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; | ||
|
r229 | } | ||
r369 | void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress) | |||
{ | ||||
r398 | if (progress > 0.0) { | |||
impl->m_VariableToProgress[variable] = progress; | ||||
} | ||||
else { | ||||
impl->m_VariableToProgress.erase(variable); | ||||
} | ||||
r369 | auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN); | |||
emit dataChanged(modelIndex, modelIndex); | ||||
} | ||||
|
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 | ||||
{ | ||||
|
r142 | if (!index.isValid()) { | ||
return QVariant{}; | ||||
} | ||||
if (index.row() < 0 || index.row() >= rowCount()) { | ||||
return QVariant{}; | ||||
} | ||||
if (role == Qt::DisplayRole) { | ||||
|
r147 | if (auto variable = impl->m_Variables.at(index.row()).get()) { | ||
|
r254 | /// Lambda function that builds the variant to return for a time value | ||
|
r450 | auto dateTimeVariant = [](double secs) { | ||
auto dateTime = DateUtils::dateTime(secs); | ||||
|
r254 | return dateTime.toString(DATETIME_FORMAT); | ||
}; | ||||
|
r142 | switch (index.column()) { | ||
case NAME_COLUMN: | ||||
|
r151 | return variable->name(); | ||
|
r254 | case TSTART_COLUMN: | ||
r505 | return dateTimeVariant(variable->range().m_TStart); | |||
|
r254 | case TEND_COLUMN: | ||
r505 | return dateTimeVariant(variable->range().m_TEnd); | |||
|
r519 | case UNIT_COLUMN: | ||
return variable->metadata().value(QStringLiteral("units")); | ||||
case MISSION_COLUMN: | ||||
return variable->metadata().value(QStringLiteral("mission")); | ||||
|
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)"); | ||||
} | ||||
} | ||||
r370 | else if (role == VariableRoles::ProgressRole) { | |||
r369 | if (auto variable = impl->m_Variables.at(index.row())) { | |||
|
r387 | auto it = impl->m_VariableToProgress.find(variable); | ||
r369 | if (it != impl->m_VariableToProgress.cend()) { | |||
return it->second; | ||||
} | ||||
} | ||||
} | ||||
|
r142 | |||
|
r140 | return QVariant{}; | ||
} | ||||
QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | ||||
{ | ||||
|
r257 | if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { | ||
|
r141 | return QVariant{}; | ||
} | ||||
if (orientation == Qt::Horizontal) { | ||||
|
r255 | auto propertiesIt = COLUMN_PROPERTIES.find(section); | ||
if (propertiesIt != COLUMN_PROPERTIES.cend()) { | ||||
// Role is either DisplayRole or SizeHintRole | ||||
|
r257 | return (role == Qt::DisplayRole) | ||
? QVariant{propertiesIt->m_Name} | ||||
: QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; | ||||
|
r255 | } | ||
else { | ||||
qWarning(LOG_VariableModel()) | ||||
<< tr("Can't get header data (unknown column %1)").arg(section); | ||||
|
r141 | } | ||
} | ||||
|
r140 | |||
return QVariant{}; | ||||
} | ||||
|
r340 | |||
r388 | void VariableModel::abortProgress(const QModelIndex &index) | |||
{ | ||||
if (auto variable = impl->m_Variables.at(index.row())) { | ||||
emit abortProgessRequested(variable); | ||||
} | ||||
} | ||||
|
r340 | void VariableModel::onVariableUpdated() noexcept | ||
{ | ||||
// Finds variable that has been updated in the model | ||||
if (auto updatedVariable = dynamic_cast<Variable *>(sender())) { | ||||
r369 | auto updatedVariableIndex = impl->indexOfVariable(updatedVariable); | |||
if (updatedVariableIndex > -1) { | ||||
emit dataChanged(createIndex(updatedVariableIndex, 0), | ||||
createIndex(updatedVariableIndex, columnCount() - 1)); | ||||
|
r340 | } | ||
} | ||||
} | ||||
r369 | ||||
int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept | ||||
{ | ||||
auto begin = std::cbegin(m_Variables); | ||||
auto end = std::cend(m_Variables); | ||||
auto it | ||||
= std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; }); | ||||
if (it != end) { | ||||
// Gets the index of the variable in the model: we assume here that views have the same | ||||
// order as the model | ||||
return std::distance(begin, it); | ||||
} | ||||
else { | ||||
return -1; | ||||
} | ||||
} | ||||