##// END OF EJS Templates
Bump TimeSeries and few minor modifications...
Bump TimeSeries and few minor modifications Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>

File last commit:

r69:cc26524fb5d1
r91:c4e273750e74
Show More
VariableModel2.cpp
278 lines | 7.8 KiB | text/x-c | CppLexer
/ src / Variable / VariableModel2.cpp
Quick variable model copied from existing one...
r21 #include <Common/DateUtils.h>
#include <Common/MimeTypesDef.h>
#include <Common/StringUtils.h>
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 #include <Common/containers.h>
Quick variable model copied from existing one...
r21 #include <DataSource/DataSourceController.h>
Switched to new TS impl, but quite broken!...
r67 #include <QMimeData>
#include <QSize>
#include <QTimer>
Quick variable model copied from existing one...
r21 #include <Time/TimeController.h>
Switched to new TS impl, but quite broken!...
r67 #include <Variable/Variable2.h>
#include <Variable/VariableController2.h>
#include <Variable/VariableModel2.h>
#include <unordered_map>
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 namespace
{
// Column indexes
const auto NAME_COLUMN = 0;
const auto TSTART_COLUMN = 1;
const auto TEND_COLUMN = 2;
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;
// Column properties
const auto DEFAULT_HEIGHT = 25;
const auto DEFAULT_WIDTH = 100;
struct ColumnProperties
{
ColumnProperties(const QString& name = {}, int width = DEFAULT_WIDTH,
Quick variable model copied from existing one...
r21 int height = DEFAULT_HEIGHT)
Switched to new TS impl, but quite broken!...
r67 : m_Name{name}, m_Width{width}, m_Height{height}
{}
Quick variable model copied from existing one...
r21
QString m_Name;
int m_Width;
int m_Height;
Switched to new TS impl, but quite broken!...
r67 };
const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
{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")}}};
QString uniqueName(const QString& defaultName,
const std::vector<std::shared_ptr<Variable2>>& variables)
{
Quick variable model copied from existing one...
r21 auto forbiddenNames = std::vector<QString>(variables.size());
std::transform(variables.cbegin(), variables.cend(), forbiddenNames.begin(),
Switched to new TS impl, but quite broken!...
r67 [](const auto& variable) { return variable->name(); });
Quick variable model copied from existing one...
r21 auto uniqueName = StringUtils::uniqueName(defaultName, forbiddenNames);
Q_ASSERT(!uniqueName.isEmpty());
return uniqueName;
Switched to new TS impl, but quite broken!...
r67 }
Quick variable model copied from existing one...
r21
} // namespace
Switched to new TS impl, but quite broken!...
r67 VariableModel2::VariableModel2(QObject* parent) : QAbstractTableModel{parent} {}
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 int VariableModel2::columnCount(const QModelIndex& parent) const
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 Q_UNUSED(parent);
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 return NB_COLUMNS;
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 int VariableModel2::rowCount(const QModelIndex& parent) const
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 Q_UNUSED(parent);
return _variables.size();
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 QVariant VariableModel2::data(const QModelIndex& index, int role) const
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 if(!index.isValid()) { return QVariant{}; }
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 if(index.row() < 0 || index.row() >= rowCount()) { return QVariant{}; }
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 if(role == Qt::DisplayRole)
{
if(auto variable = _variables[index.row()])
{
switch(index.column())
{
case NAME_COLUMN: return variable->name();
case TSTART_COLUMN:
{
if(auto range = variable->realRange(); range.has_value())
return DateUtils::dateTime(range.value().m_TStart)
.toString(DATETIME_FORMAT);
return QVariant{};
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 case TEND_COLUMN:
{
if(auto range = variable->realRange(); range.has_value())
return DateUtils::dateTime(range.value().m_TEnd)
.toString(DATETIME_FORMAT);
return QVariant{};
}
case NBPOINTS_COLUMN: return int(variable->nbPoints());
case UNIT_COLUMN:
return variable->metadata().value(QStringLiteral("units"));
case MISSION_COLUMN:
return variable->metadata().value(QStringLiteral("mission"));
case PLUGIN_COLUMN:
return variable->metadata().value(QStringLiteral("plugin"));
default:
// No action
break;
}
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 }
else if(role == VariableRoles::ProgressRole)
{
Quick variable model copied from existing one...
r21 return QVariant{};
Switched to new TS impl, but quite broken!...
r67 }
return QVariant{};
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 QVariant VariableModel2::headerData(int section, Qt::Orientation orientation,
int role) const
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 if(role != Qt::DisplayRole && role != Qt::SizeHintRole) { return QVariant{}; }
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 if(orientation == Qt::Horizontal)
{
auto propertiesIt = COLUMN_PROPERTIES.find(section);
if(propertiesIt != COLUMN_PROPERTIES.cend())
{
// Role is either DisplayRole or SizeHintRole
return (role == Qt::DisplayRole)
? QVariant{propertiesIt->m_Name}
: QVariant{
QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 }
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 return QVariant{};
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 Qt::ItemFlags VariableModel2::flags(const QModelIndex& index) const
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 return QAbstractTableModel::flags(index) | Qt::ItemIsDragEnabled |
Qt::ItemIsDropEnabled;
Quick variable model copied from existing one...
r21 }
Qt::DropActions VariableModel2::supportedDropActions() const
{
Switched to new TS impl, but quite broken!...
r67 return Qt::CopyAction | Qt::MoveAction;
Quick variable model copied from existing one...
r21 }
Qt::DropActions VariableModel2::supportedDragActions() const
{
Switched to new TS impl, but quite broken!...
r67 return Qt::CopyAction | Qt::MoveAction;
Quick variable model copied from existing one...
r21 }
QStringList VariableModel2::mimeTypes() const
{
Switched to new TS impl, but quite broken!...
r67 return {MIME_TYPE_VARIABLE_LIST, MIME_TYPE_TIME_RANGE};
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 QMimeData* VariableModel2::mimeData(const QModelIndexList& indexes) const
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 auto mimeData = new QMimeData;
std::vector<std::shared_ptr<Variable2>> variables;
DateTimeRange firstTimeRange;
for(const auto& index : indexes)
{
if(index.column() == 0)
{ // only the first column
auto variable = _variables[index.row()];
if(variable.get() && index.isValid())
{
if(variables.size() == 0)
{
// Gets the range of the first variable
firstTimeRange = variable->range();
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 variables.push_back(variable);
}
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 }
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 auto variablesEncodedData = Variable2::mimeData(variables);
mimeData->setData(MIME_TYPE_VARIABLE_LIST, variablesEncodedData);
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 if(variables.size() == 1)
{
// No time range MIME data if multiple variables are dragged
auto timeEncodedData = TimeController::mimeDataForTimeRange(firstTimeRange);
mimeData->setData(MIME_TYPE_TIME_RANGE, timeEncodedData);
}
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 return mimeData;
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 bool VariableModel2::canDropMimeData(const QMimeData* data,
Qt::DropAction action, int row, int column,
const QModelIndex& parent) const
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 Q_UNUSED(column);
// drop of a product
return data->hasFormat(MIME_TYPE_PRODUCT_LIST) ||
(data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid() &&
!data->hasFormat(MIME_TYPE_VARIABLE_LIST));
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 bool VariableModel2::dropMimeData(const QMimeData* data, Qt::DropAction action,
int row, int column,
const QModelIndex& parent)
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 auto dropDone = false;
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 if(data->hasFormat(MIME_TYPE_PRODUCT_LIST))
{
auto productList = DataSourceController::productsDataForMimeData(
data->data(MIME_TYPE_PRODUCT_LIST));
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 for(auto metaData : productList)
{
emit createVariable(metaData.toHash());
Quick variable model copied from existing one...
r21 }
Switched to new TS impl, but quite broken!...
r67 dropDone = true;
}
else if(data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid())
{
auto variable = _variables[parent.row()];
auto range =
TimeController::timeRangeForMimeData(data->data(MIME_TYPE_TIME_RANGE));
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 emit asyncChangeRange(variable, range);
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 dropDone = true;
}
return dropDone;
Quick variable model copied from existing one...
r21 }
Variable update signal forwards variable ID...
r32 void VariableModel2::variableUpdated(QUuid id) noexcept
Quick variable model copied from existing one...
r21 {
Switched to new TS impl, but quite broken!...
r67 emit dataChanged(QModelIndex(), QModelIndex());
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 }
Quick variable model copied from existing one...
r21
Switched to new TS impl, but quite broken!...
r67 void VariableModel2::variableAdded(const std::shared_ptr<Variable2>& variable)
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 {
Switched to new TS impl, but quite broken!...
r67 if(!SciQLop::containers::contains(_variables, variable))
{
beginInsertRows(QModelIndex(), this->_variables.size(),
this->_variables.size());
this->_variables.push_back(variable);
endInsertRows();
connect(variable.get(), &Variable2::updated, this,
&VariableModel2::variableUpdated);
}
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 }
Switched to new TS impl, but quite broken!...
r67 void VariableModel2::variableDeleted(const std::shared_ptr<Variable2>& variable)
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 {
Switched to new TS impl, but quite broken!...
r67 auto it = std::find(_variables.begin(), _variables.end(), variable);
if(it != _variables.end())
{
auto index = std::distance(_variables.begin(), it);
beginRemoveRows(QModelIndex(), index, index);
_variables.erase(it);
endRemoveRows();
}
Quick variable model copied from existing one...
r21 }