##// END OF EJS Templates
Removed forgotten files form previous impl of VC, fixed wrong submodules...
Removed forgotten files form previous impl of VC, fixed wrong submodules init (was always erasing changes :( ) Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>

File last commit:

r27:c08d1b8ad297
r31:dec007be0b03
Show More
VariableModel2.cpp
270 lines | 8.1 KiB | text/x-c | CppLexer
/ src / Variable / VariableModel2.cpp
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 #include <QMimeData>
#include <QSize>
#include <QTimer>
#include <unordered_map>
Quick variable model copied from existing one...
r21 #include <Variable/Variable.h>
#include <Variable/VariableController2.h>
#include <Variable/VariableModel2.h>
#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 <Data/IDataSeries.h>
#include <DataSource/DataSourceController.h>
#include <Time/TimeController.h>
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,
int height = DEFAULT_HEIGHT)
: m_Name{name}, m_Width{width}, m_Height{height}
{
}
QString m_Name;
int m_Width;
int m_Height;
};
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<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;
}
} // namespace
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 VariableModel2::VariableModel2(QObject *parent)
: QAbstractTableModel{parent}
Quick variable model copied from existing one...
r21 {
}
int VariableModel2::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return NB_COLUMNS;
}
int VariableModel2::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 return _variables.size();
Quick variable model copied from existing one...
r21 }
QVariant VariableModel2::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant{};
}
if (index.row() < 0 || index.row() >= rowCount()) {
return QVariant{};
}
if (role == Qt::DisplayRole) {
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 if (auto variable = _variables[index.row()]) {
Quick variable model copied from existing one...
r21 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{};
}
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 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;
}
}
}
else if (role == VariableRoles::ProgressRole) {
return QVariant{};
}
return QVariant{};
}
QVariant VariableModel2::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
return QVariant{};
}
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}};
}
}
return QVariant{};
}
Qt::ItemFlags VariableModel2::flags(const QModelIndex &index) const
{
return QAbstractTableModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
Qt::DropActions VariableModel2::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
Qt::DropActions VariableModel2::supportedDragActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
QStringList VariableModel2::mimeTypes() const
{
return {MIME_TYPE_VARIABLE_LIST, MIME_TYPE_TIME_RANGE};
}
QMimeData *VariableModel2::mimeData(const QModelIndexList &indexes) const
{
auto mimeData = new QMimeData;
std::vector<std::shared_ptr<Variable> > variables;
DateTimeRange firstTimeRange;
for (const auto &index : indexes) {
if (index.column() == 0) { // only the first column
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 auto variable = _variables[index.row()];
Quick variable model copied from existing one...
r21 if (variable.get() && index.isValid()) {
if (variables.size()==0) {
// Gets the range of the first variable
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 firstTimeRange = variable->range();
Quick variable model copied from existing one...
r21 }
variables.push_back(variable);
}
}
}
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 auto variablesEncodedData = Variable::mimeData(variables);
Quick variable model copied from existing one...
r21 mimeData->setData(MIME_TYPE_VARIABLE_LIST, variablesEncodedData);
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);
}
return mimeData;
}
bool VariableModel2::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
int column, const QModelIndex &parent) const
{
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 Q_UNUSED(column);
Quick variable model copied from existing one...
r21 // 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));
}
bool VariableModel2::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
const QModelIndex &parent)
{
auto dropDone = false;
if (data->hasFormat(MIME_TYPE_PRODUCT_LIST)) {
auto productList
= DataSourceController::productsDataForMimeData(data->data(MIME_TYPE_PRODUCT_LIST));
for (auto metaData : productList) {
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 emit createVariable(metaData.toHash());
Quick variable model copied from existing one...
r21 }
dropDone = true;
}
else if (data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid()) {
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 auto variable = _variables[parent.row()];
Quick variable model copied from existing one...
r21 auto range = TimeController::timeRangeForMimeData(data->data(MIME_TYPE_TIME_RANGE));
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 emit asyncChangeRange(variable, range);
Quick variable model copied from existing one...
r21
dropDone = true;
}
return dropDone;
}
void VariableModel2::variableUpdated() noexcept
{
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 emit dataChanged(QModelIndex(),QModelIndex());
}
Quick variable model copied from existing one...
r21
Removed bad dependency between VC and VariableModel, moved mime stuff...
r27 void VariableModel2::variableAdded(const std::shared_ptr<Variable> & variable)
{
if(!SciQLop::containers::contains(_variables,variable))
{
beginInsertRows(QModelIndex(), this->_variables.size(), this->_variables.size());
this->_variables.push_back(variable);
endInsertRows();
connect(variable.get(),&Variable::updated,this,&VariableModel2::variableUpdated);
}
}
void VariableModel2::variableDeleted(const std::shared_ptr<Variable> &variable)
{
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 }