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