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