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