@@ -1,136 +1,144 | |||||
1 | #include <Variable/Variable.h> |
|
1 | #include <Variable/Variable.h> | |
2 | #include <Variable/VariableModel.h> |
|
2 | #include <Variable/VariableModel.h> | |
3 |
|
3 | |||
4 | #include <Data/IDataSeries.h> |
|
4 | #include <Data/IDataSeries.h> | |
5 |
|
5 | |||
6 | #include <QDateTime> |
|
6 | #include <QDateTime> | |
7 |
|
7 | |||
8 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") |
|
8 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | |
9 |
|
9 | |||
10 | namespace { |
|
10 | namespace { | |
11 |
|
11 | |||
12 | // Column indexes |
|
12 | // Column indexes | |
13 | const auto NAME_COLUMN = 0; |
|
13 | const auto NAME_COLUMN = 0; | |
14 | const auto TSTART_COLUMN = 1; |
|
14 | const auto TSTART_COLUMN = 1; | |
15 | const auto TEND_COLUMN = 2; |
|
15 | const auto TEND_COLUMN = 2; | |
16 | const auto NB_COLUMNS = 3; |
|
16 | const auto NB_COLUMNS = 3; | |
17 |
|
17 | |||
|
18 | // Column properties | |||
|
19 | ||||
|
20 | struct ColumnProperties { | |||
|
21 | ColumnProperties(const QString &name = {}) : m_Name{name} {} | |||
|
22 | ||||
|
23 | QString m_Name; | |||
|
24 | }; | |||
|
25 | ||||
|
26 | const auto COLUMN_PROPERTIES | |||
|
27 | = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}}, | |||
|
28 | {TSTART_COLUMN, {QObject::tr("tStart")}}, | |||
|
29 | {TEND_COLUMN, {QObject::tr("tEnd")}}}; | |||
|
30 | ||||
18 | /// Format for datetimes |
|
31 | /// Format for datetimes | |
19 | const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); |
|
32 | const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); | |
20 |
|
33 | |||
21 | } // namespace |
|
34 | } // namespace | |
22 |
|
35 | |||
23 | struct VariableModel::VariableModelPrivate { |
|
36 | struct VariableModel::VariableModelPrivate { | |
24 | /// Variables created in SciQlop |
|
37 | /// Variables created in SciQlop | |
25 | std::vector<std::shared_ptr<Variable> > m_Variables; |
|
38 | std::vector<std::shared_ptr<Variable> > m_Variables; | |
26 | }; |
|
39 | }; | |
27 |
|
40 | |||
28 | VariableModel::VariableModel(QObject *parent) |
|
41 | VariableModel::VariableModel(QObject *parent) | |
29 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} |
|
42 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | |
30 | { |
|
43 | { | |
31 | } |
|
44 | } | |
32 |
|
45 | |||
33 | std::shared_ptr<Variable> |
|
46 | std::shared_ptr<Variable> | |
34 | VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime, |
|
47 | VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime, | |
35 | std::unique_ptr<IDataSeries> defaultDataSeries) noexcept |
|
48 | std::unique_ptr<IDataSeries> defaultDataSeries) noexcept | |
36 | { |
|
49 | { | |
37 | auto insertIndex = rowCount(); |
|
50 | auto insertIndex = rowCount(); | |
38 | beginInsertRows({}, insertIndex, insertIndex); |
|
51 | beginInsertRows({}, insertIndex, insertIndex); | |
39 |
|
52 | |||
40 | /// @todo For the moment, the other data of the variable is initialized with default values |
|
53 | /// @todo For the moment, the other data of the variable is initialized with default values | |
41 | auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"), |
|
54 | auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"), | |
42 | QStringLiteral("mission"), dateTime); |
|
55 | QStringLiteral("mission"), dateTime); | |
43 | variable->setDataSeries(std::move(defaultDataSeries)); |
|
56 | variable->setDataSeries(std::move(defaultDataSeries)); | |
44 |
|
57 | |||
45 | impl->m_Variables.push_back(variable); |
|
58 | impl->m_Variables.push_back(variable); | |
46 |
|
59 | |||
47 | endInsertRows(); |
|
60 | endInsertRows(); | |
48 |
|
61 | |||
49 | return variable; |
|
62 | return variable; | |
50 | } |
|
63 | } | |
51 |
|
64 | |||
52 | std::shared_ptr<Variable> VariableModel::variable(int index) const |
|
65 | std::shared_ptr<Variable> VariableModel::variable(int index) const | |
53 | { |
|
66 | { | |
54 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; |
|
67 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; | |
55 | } |
|
68 | } | |
56 |
|
69 | |||
57 | int VariableModel::columnCount(const QModelIndex &parent) const |
|
70 | int VariableModel::columnCount(const QModelIndex &parent) const | |
58 | { |
|
71 | { | |
59 | Q_UNUSED(parent); |
|
72 | Q_UNUSED(parent); | |
60 |
|
73 | |||
61 | return NB_COLUMNS; |
|
74 | return NB_COLUMNS; | |
62 | } |
|
75 | } | |
63 |
|
76 | |||
64 | int VariableModel::rowCount(const QModelIndex &parent) const |
|
77 | int VariableModel::rowCount(const QModelIndex &parent) const | |
65 | { |
|
78 | { | |
66 | Q_UNUSED(parent); |
|
79 | Q_UNUSED(parent); | |
67 |
|
80 | |||
68 | return impl->m_Variables.size(); |
|
81 | return impl->m_Variables.size(); | |
69 | } |
|
82 | } | |
70 |
|
83 | |||
71 | QVariant VariableModel::data(const QModelIndex &index, int role) const |
|
84 | QVariant VariableModel::data(const QModelIndex &index, int role) const | |
72 | { |
|
85 | { | |
73 | if (!index.isValid()) { |
|
86 | if (!index.isValid()) { | |
74 | return QVariant{}; |
|
87 | return QVariant{}; | |
75 | } |
|
88 | } | |
76 |
|
89 | |||
77 | if (index.row() < 0 || index.row() >= rowCount()) { |
|
90 | if (index.row() < 0 || index.row() >= rowCount()) { | |
78 | return QVariant{}; |
|
91 | return QVariant{}; | |
79 | } |
|
92 | } | |
80 |
|
93 | |||
81 | if (role == Qt::DisplayRole) { |
|
94 | if (role == Qt::DisplayRole) { | |
82 | if (auto variable = impl->m_Variables.at(index.row()).get()) { |
|
95 | if (auto variable = impl->m_Variables.at(index.row()).get()) { | |
83 | /// Lambda function that builds the variant to return for a time value |
|
96 | /// Lambda function that builds the variant to return for a time value | |
84 | auto dateTimeVariant = [](double time) { |
|
97 | auto dateTimeVariant = [](double time) { | |
85 | auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.); |
|
98 | auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.); | |
86 | return dateTime.toString(DATETIME_FORMAT); |
|
99 | return dateTime.toString(DATETIME_FORMAT); | |
87 | }; |
|
100 | }; | |
88 |
|
101 | |||
89 | switch (index.column()) { |
|
102 | switch (index.column()) { | |
90 | case NAME_COLUMN: |
|
103 | case NAME_COLUMN: | |
91 | return variable->name(); |
|
104 | return variable->name(); | |
92 | case TSTART_COLUMN: |
|
105 | case TSTART_COLUMN: | |
93 | return dateTimeVariant(variable->dateTime().m_TStart); |
|
106 | return dateTimeVariant(variable->dateTime().m_TStart); | |
94 | case TEND_COLUMN: |
|
107 | case TEND_COLUMN: | |
95 | return dateTimeVariant(variable->dateTime().m_TEnd); |
|
108 | return dateTimeVariant(variable->dateTime().m_TEnd); | |
96 | default: |
|
109 | default: | |
97 | // No action |
|
110 | // No action | |
98 | break; |
|
111 | break; | |
99 | } |
|
112 | } | |
100 |
|
113 | |||
101 | qWarning(LOG_VariableModel()) |
|
114 | qWarning(LOG_VariableModel()) | |
102 | << tr("Can't get data (unknown column %1)").arg(index.column()); |
|
115 | << tr("Can't get data (unknown column %1)").arg(index.column()); | |
103 | } |
|
116 | } | |
104 | else { |
|
117 | else { | |
105 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); |
|
118 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); | |
106 | } |
|
119 | } | |
107 | } |
|
120 | } | |
108 |
|
121 | |||
109 | return QVariant{}; |
|
122 | return QVariant{}; | |
110 | } |
|
123 | } | |
111 |
|
124 | |||
112 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
125 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | |
113 | { |
|
126 | { | |
114 | if (role != Qt::DisplayRole) { |
|
127 | if (role != Qt::DisplayRole) { | |
115 | return QVariant{}; |
|
128 | return QVariant{}; | |
116 | } |
|
129 | } | |
117 |
|
130 | |||
118 | if (orientation == Qt::Horizontal) { |
|
131 | if (orientation == Qt::Horizontal) { | |
119 | switch (section) { |
|
132 | auto propertiesIt = COLUMN_PROPERTIES.find(section); | |
120 | case NAME_COLUMN: |
|
133 | if (propertiesIt != COLUMN_PROPERTIES.cend()) { | |
121 | return tr("Name"); |
|
134 | // Role is either DisplayRole or SizeHintRole | |
122 | case UNIT_COLUMN: |
|
135 | return QVariant{propertiesIt->m_Name}; | |
123 | return tr("tStart"); |
|
|||
124 | case MISSION_COLUMN: |
|
|||
125 | return tr("tEnd"); |
|
|||
126 | default: |
|
|||
127 | // No action |
|
|||
128 | break; |
|
|||
129 | } |
|
136 | } | |
130 |
|
137 | else { | ||
131 | qWarning(LOG_VariableModel()) |
|
138 | qWarning(LOG_VariableModel()) | |
132 | << tr("Can't get header data (unknown column %1)").arg(section); |
|
139 | << tr("Can't get header data (unknown column %1)").arg(section); | |
133 | } |
|
140 | } | |
|
141 | } | |||
134 |
|
142 | |||
135 | return QVariant{}; |
|
143 | return QVariant{}; | |
136 | } |
|
144 | } |
General Comments 0
You need to be logged in to leave comments.
Login now