##// END OF EJS Templates
Changes Variable from struct to class
Alexandre Leroux -
r163:8de0b7c96d22
parent child
Show More
@@ -0,0 +1,32
1 #include "Variable/Variable.h"
2
3 struct Variable::VariablePrivate {
4 explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission)
5 : m_Name{name}, m_Unit{unit}, m_Mission{mission}
6 {
7 }
8
9 QString m_Name;
10 QString m_Unit;
11 QString m_Mission;
12 };
13
14 Variable::Variable(const QString &name, const QString &unit, const QString &mission)
15 : impl{spimpl::make_unique_impl<VariablePrivate>(name, unit, mission)}
16 {
17 }
18
19 QString Variable::name() const noexcept
20 {
21 return impl->m_Name;
22 }
23
24 QString Variable::mission() const noexcept
25 {
26 return impl->m_Mission;
27 }
28
29 QString Variable::unit() const noexcept
30 {
31 return impl->m_Unit;
32 }
@@ -1,20 +1,24
1 1 #ifndef SCIQLOP_VARIABLE_H
2 2 #define SCIQLOP_VARIABLE_H
3 3
4 #include <QString>
4 #include <Common/spimpl.h>
5
6 class QString;
5 7
6 8 /**
7 * @brief The Variable struct represents a variable in SciQlop.
9 * @brief The Variable class represents a variable in SciQlop.
8 10 */
9 struct Variable {
10 explicit Variable(const QString &name, const QString &unit, const QString &mission)
11 : m_Name{name}, m_Unit{unit}, m_Mission{mission}
12 {
13 }
11 class Variable {
12 public:
13 explicit Variable(const QString &name, const QString &unit, const QString &mission);
14
15 QString name() const noexcept;
16 QString mission() const noexcept;
17 QString unit() const noexcept;
14 18
15 QString m_Name;
16 QString m_Unit;
17 QString m_Mission;
19 private:
20 class VariablePrivate;
21 spimpl::unique_impl_ptr<VariablePrivate> impl;
18 22 };
19 23
20 24 #endif // SCIQLOP_VARIABLE_H
@@ -1,115 +1,115
1 #include <Variable/Variable.h>
1 2 #include <Variable/VariableModel.h>
2 3
3 #include <Variable/Variable.h>
4 4
5 5 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
6 6
7 7 namespace {
8 8
9 9 // Column indexes
10 10 const auto NAME_COLUMN = 0;
11 11 const auto UNIT_COLUMN = 1;
12 12 const auto MISSION_COLUMN = 2;
13 13 const auto NB_COLUMNS = 3;
14 14
15 15 } // namespace
16 16
17 17 struct VariableModel::VariableModelPrivate {
18 18 /// Variables created in SciQlop
19 19 std::vector<std::unique_ptr<Variable> > m_Variables;
20 20 };
21 21
22 22 VariableModel::VariableModel(QObject *parent)
23 23 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
24 24 {
25 25 }
26 26
27 27 Variable *VariableModel::createVariable(const QString &name) noexcept
28 28 {
29 29 auto insertIndex = rowCount();
30 30 beginInsertRows({}, insertIndex, insertIndex);
31 31
32 32 /// @todo For the moment, the other data of the variable is initialized with default values
33 33 auto variable
34 34 = std::make_unique<Variable>(name, QStringLiteral("unit"), QStringLiteral("mission"));
35 35 impl->m_Variables.push_back(std::move(variable));
36 36
37 37 endInsertRows();
38 38
39 39 return impl->m_Variables.at(insertIndex).get();
40 40 }
41 41
42 42 int VariableModel::columnCount(const QModelIndex &parent) const
43 43 {
44 44 Q_UNUSED(parent);
45 45
46 46 return NB_COLUMNS;
47 47 }
48 48
49 49 int VariableModel::rowCount(const QModelIndex &parent) const
50 50 {
51 51 Q_UNUSED(parent);
52 52
53 53 return impl->m_Variables.size();
54 54 }
55 55
56 56 QVariant VariableModel::data(const QModelIndex &index, int role) const
57 57 {
58 58 if (!index.isValid()) {
59 59 return QVariant{};
60 60 }
61 61
62 62 if (index.row() < 0 || index.row() >= rowCount()) {
63 63 return QVariant{};
64 64 }
65 65
66 66 if (role == Qt::DisplayRole) {
67 67 if (auto variable = impl->m_Variables.at(index.row()).get()) {
68 68 switch (index.column()) {
69 69 case NAME_COLUMN:
70 return variable->m_Name;
70 return variable->name();
71 71 case UNIT_COLUMN:
72 return variable->m_Unit;
72 return variable->unit();
73 73 case MISSION_COLUMN:
74 return variable->m_Mission;
74 return variable->mission();
75 75 default:
76 76 // No action
77 77 break;
78 78 }
79 79
80 80 qWarning(LOG_VariableModel())
81 81 << tr("Can't get data (unknown column %1)").arg(index.column());
82 82 }
83 83 else {
84 84 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
85 85 }
86 86 }
87 87
88 88 return QVariant{};
89 89 }
90 90
91 91 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
92 92 {
93 93 if (role != Qt::DisplayRole) {
94 94 return QVariant{};
95 95 }
96 96
97 97 if (orientation == Qt::Horizontal) {
98 98 switch (section) {
99 99 case NAME_COLUMN:
100 100 return tr("Name");
101 101 case UNIT_COLUMN:
102 102 return tr("Unit");
103 103 case MISSION_COLUMN:
104 104 return tr("Mission");
105 105 default:
106 106 // No action
107 107 break;
108 108 }
109 109
110 110 qWarning(LOG_VariableModel())
111 111 << tr("Can't get header data (unknown column %1)").arg(section);
112 112 }
113 113
114 114 return QVariant{};
115 115 }
General Comments 0
You need to be logged in to leave comments. Login now