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