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