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