VariableModel.cpp
383 lines
| 11.6 KiB
| text/x-c
|
CppLexer
|
r163 | #include <Variable/Variable.h> | ||
r849 | #include <Variable/VariableController.h> | |||
|
r112 | #include <Variable/VariableModel.h> | ||
|
r488 | #include <Common/DateUtils.h> | ||
r849 | #include <Common/MimeTypesDef.h> | |||
|
r709 | #include <Common/StringUtils.h> | ||
|
r488 | |||
|
r165 | #include <Data/IDataSeries.h> | ||
|
r112 | |||
r878 | #include <DataSource/DataSourceController.h> | |||
#include <Time/TimeController.h> | ||||
r849 | #include <QMimeData> | |||
|
r277 | #include <QSize> | ||
r401 | #include <unordered_map> | |||
|
r274 | |||
|
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; | ||||
|
r719 | const auto NBPOINTS_COLUMN = 3; | ||
const auto UNIT_COLUMN = 4; | ||||
const auto MISSION_COLUMN = 5; | ||||
const auto PLUGIN_COLUMN = 6; | ||||
const auto NB_COLUMNS = 7; | ||||
|
r149 | |||
|
r275 | // Column properties | ||
|
r276 | const auto DEFAULT_HEIGHT = 25; | ||
const auto DEFAULT_WIDTH = 100; | ||||
|
r275 | |||
struct ColumnProperties { | ||||
|
r276 | ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, | ||
int height = DEFAULT_HEIGHT) | ||||
: m_Name{name}, m_Width{width}, m_Height{height} | ||||
{ | ||||
} | ||||
|
r275 | |||
QString m_Name; | ||||
|
r276 | int m_Width; | ||
int m_Height; | ||||
|
r275 | }; | ||
|
r550 | const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{ | ||
|
r719 | {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, | ||
{TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {NBPOINTS_COLUMN, {QObject::tr("Nb points")}}, | ||||
{UNIT_COLUMN, {QObject::tr("Unit")}}, {MISSION_COLUMN, {QObject::tr("Mission")}}, | ||||
{PLUGIN_COLUMN, {QObject::tr("Plugin")}}}; | ||||
|
r275 | |||
|
r274 | /// Format for datetimes | ||
const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); | ||||
|
r709 | QString uniqueName(const QString &defaultName, | ||
const std::vector<std::shared_ptr<Variable> > &variables) | ||||
{ | ||||
auto forbiddenNames = std::vector<QString>(variables.size()); | ||||
std::transform(variables.cbegin(), variables.cend(), forbiddenNames.begin(), | ||||
[](const auto &variable) { return variable->name(); }); | ||||
auto uniqueName = StringUtils::uniqueName(defaultName, forbiddenNames); | ||||
Q_ASSERT(!uniqueName.isEmpty()); | ||||
return uniqueName; | ||||
} | ||||
r401 | ||||
|
r149 | } // namespace | ||
|
r112 | struct VariableModel::VariableModelPrivate { | ||
/// Variables created in SciQlop | ||||
|
r165 | std::vector<std::shared_ptr<Variable> > m_Variables; | ||
|
r421 | std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress; | ||
r849 | VariableController *m_VariableController; | |||
r401 | ||||
r402 | /// Return the row index of the variable. -1 if it's not found | |||
r401 | int indexOfVariable(Variable *variable) const noexcept; | |||
|
r112 | }; | ||
r849 | VariableModel::VariableModel(VariableController *parent) | |||
|
r149 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | ||
|
r112 | { | ||
r849 | impl->m_VariableController = parent; | |||
|
r112 | } | ||
|
r708 | void VariableModel::addVariable(std::shared_ptr<Variable> variable) noexcept | ||
|
r112 | { | ||
|
r153 | auto insertIndex = rowCount(); | ||
beginInsertRows({}, insertIndex, insertIndex); | ||||
|
r709 | // Generates unique name for the variable | ||
variable->setName(uniqueName(variable->name(), impl->m_Variables)); | ||||
|
r165 | impl->m_Variables.push_back(variable); | ||
|
r368 | connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); | ||
|
r112 | |||
|
r153 | endInsertRows(); | ||
|
r708 | } | ||
|
r710 | bool VariableModel::containsVariable(std::shared_ptr<Variable> variable) const noexcept | ||
{ | ||||
auto end = impl->m_Variables.cend(); | ||||
return std::find(impl->m_Variables.cbegin(), end, variable) != end; | ||||
} | ||||
|
r708 | std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, | ||
const QVariantHash &metadata) noexcept | ||||
{ | ||||
r756 | auto variable = std::make_shared<Variable>(name, metadata); | |||
|
r708 | addVariable(variable); | ||
|
r153 | |||
|
r165 | return variable; | ||
|
r112 | } | ||
|
r149 | |||
|
r332 | 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()); | ||||
} | ||||
|
r421 | |||
// Removes variable from progress map | ||||
impl->m_VariableToProgress.erase(variable); | ||||
|
r332 | } | ||
r401 | ||||
|
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 | } | ||
|
r684 | std::vector<std::shared_ptr<Variable> > VariableModel::variables() const | ||
{ | ||||
return impl->m_Variables; | ||||
} | ||||
r401 | void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress) | |||
{ | ||||
r432 | if (progress > 0.0) { | |||
impl->m_VariableToProgress[variable] = progress; | ||||
} | ||||
else { | ||||
impl->m_VariableToProgress.erase(variable); | ||||
} | ||||
r401 | auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN); | |||
emit dataChanged(modelIndex, modelIndex); | ||||
} | ||||
|
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()) { | ||
|
r151 | switch (index.column()) { | ||
case NAME_COLUMN: | ||||
|
r163 | return variable->name(); | ||
|
r654 | case TSTART_COLUMN: { | ||
auto range = variable->realRange(); | ||||
return range != INVALID_RANGE | ||||
? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT) | ||||
: QVariant{}; | ||||
} | ||||
case TEND_COLUMN: { | ||||
auto range = variable->realRange(); | ||||
return range != INVALID_RANGE | ||||
? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT) | ||||
: QVariant{}; | ||||
} | ||||
|
r719 | case NBPOINTS_COLUMN: | ||
return variable->nbPoints(); | ||||
|
r550 | case UNIT_COLUMN: | ||
return variable->metadata().value(QStringLiteral("units")); | ||||
case MISSION_COLUMN: | ||||
return variable->metadata().value(QStringLiteral("mission")); | ||||
|
r551 | case PLUGIN_COLUMN: | ||
return variable->metadata().value(QStringLiteral("plugin")); | ||||
|
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)"); | ||||
} | ||||
} | ||||
r402 | else if (role == VariableRoles::ProgressRole) { | |||
r401 | if (auto variable = impl->m_Variables.at(index.row())) { | |||
|
r421 | auto it = impl->m_VariableToProgress.find(variable); | ||
r401 | if (it != impl->m_VariableToProgress.cend()) { | |||
return it->second; | ||||
} | ||||
} | ||||
} | ||||
|
r151 | |||
|
r149 | return QVariant{}; | ||
} | ||||
QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | ||||
{ | ||||
|
r277 | if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { | ||
|
r150 | return QVariant{}; | ||
} | ||||
if (orientation == Qt::Horizontal) { | ||||
|
r275 | auto propertiesIt = COLUMN_PROPERTIES.find(section); | ||
if (propertiesIt != COLUMN_PROPERTIES.cend()) { | ||||
// Role is either DisplayRole or SizeHintRole | ||||
|
r277 | return (role == Qt::DisplayRole) | ||
? QVariant{propertiesIt->m_Name} | ||||
: QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; | ||||
|
r275 | } | ||
else { | ||||
qWarning(LOG_VariableModel()) | ||||
<< tr("Can't get header data (unknown column %1)").arg(section); | ||||
|
r150 | } | ||
} | ||||
|
r149 | |||
return QVariant{}; | ||||
} | ||||
|
r368 | |||
r849 | Qt::ItemFlags VariableModel::flags(const QModelIndex &index) const | |||
{ | ||||
return QAbstractTableModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; | ||||
} | ||||
Qt::DropActions VariableModel::supportedDropActions() const | ||||
{ | ||||
r870 | return Qt::CopyAction | Qt::MoveAction; | |||
r849 | } | |||
Qt::DropActions VariableModel::supportedDragActions() const | ||||
{ | ||||
return Qt::CopyAction | Qt::MoveAction; | ||||
} | ||||
QStringList VariableModel::mimeTypes() const | ||||
{ | ||||
r878 | return {MIME_TYPE_VARIABLE_LIST, MIME_TYPE_TIME_RANGE}; | |||
r849 | } | |||
QMimeData *VariableModel::mimeData(const QModelIndexList &indexes) const | ||||
{ | ||||
auto mimeData = new QMimeData; | ||||
QList<std::shared_ptr<Variable> > variableList; | ||||
r878 | ||||
SqpRange firstTimeRange; | ||||
r849 | for (const auto &index : indexes) { | |||
r850 | if (index.column() == 0) { // only the first column | |||
auto variable = impl->m_Variables.at(index.row()); | ||||
if (variable.get() && index.isValid()) { | ||||
r878 | ||||
if (variableList.isEmpty()) { | ||||
// Gets the range of the first variable | ||||
firstTimeRange = std::move(variable->range()); | ||||
} | ||||
r850 | variableList << variable; | |||
} | ||||
r849 | } | |||
} | ||||
r878 | auto variablesEncodedData = impl->m_VariableController->mimeDataForVariables(variableList); | |||
mimeData->setData(MIME_TYPE_VARIABLE_LIST, variablesEncodedData); | ||||
if (variableList.count() == 1) { | ||||
// No time range MIME data if multiple variables are dragged | ||||
auto timeEncodedData = TimeController::mimeDataForTimeRange(firstTimeRange); | ||||
mimeData->setData(MIME_TYPE_TIME_RANGE, timeEncodedData); | ||||
} | ||||
r849 | ||||
return mimeData; | ||||
} | ||||
bool VariableModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, | ||||
int column, const QModelIndex &parent) const | ||||
{ | ||||
r870 | // drop of a product | |||
return data->hasFormat(MIME_TYPE_PRODUCT_LIST); | ||||
r849 | } | |||
bool VariableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, | ||||
const QModelIndex &parent) | ||||
{ | ||||
r874 | auto dropDone = false; | |||
r870 | ||||
if (data->hasFormat(MIME_TYPE_PRODUCT_LIST)) { | ||||
r878 | auto productList | |||
= DataSourceController::productsDataForMimeData(data->data(MIME_TYPE_PRODUCT_LIST)); | ||||
r870 | ||||
for (auto metaData : productList) { | ||||
emit requestVariable(metaData.toHash()); | ||||
} | ||||
dropDone = true; | ||||
} | ||||
return dropDone; | ||||
r849 | } | |||
r422 | void VariableModel::abortProgress(const QModelIndex &index) | |||
{ | ||||
if (auto variable = impl->m_Variables.at(index.row())) { | ||||
emit abortProgessRequested(variable); | ||||
} | ||||
} | ||||
|
r368 | void VariableModel::onVariableUpdated() noexcept | ||
{ | ||||
// Finds variable that has been updated in the model | ||||
if (auto updatedVariable = dynamic_cast<Variable *>(sender())) { | ||||
r401 | auto updatedVariableIndex = impl->indexOfVariable(updatedVariable); | |||
if (updatedVariableIndex > -1) { | ||||
emit dataChanged(createIndex(updatedVariableIndex, 0), | ||||
createIndex(updatedVariableIndex, columnCount() - 1)); | ||||
|
r368 | } | ||
} | ||||
} | ||||
r401 | ||||
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; | ||||
} | ||||
} | ||||