##// END OF EJS Templates
Implements data() method
Alexandre Leroux -
r151:c81827035288
parent child
Show More
@@ -1,80 +1,112
1 1 #include <Variable/VariableModel.h>
2 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 /// @todo For the moment, the other data of the variable is initialized with default values
30 30 auto variable
31 31 = std::make_unique<Variable>(name, QStringLiteral("unit"), QStringLiteral("mission"));
32 32 impl->m_Variables.push_back(std::move(variable));
33 33
34 34 return impl->m_Variables.back().get();
35 35 }
36 36
37 37 int VariableModel::columnCount(const QModelIndex &parent) const
38 38 {
39 39 Q_UNUSED(parent);
40 40
41 41 return NB_COLUMNS;
42 42 }
43 43
44 44 int VariableModel::rowCount(const QModelIndex &parent) const
45 45 {
46 46 Q_UNUSED(parent);
47 47
48 48 return impl->m_Variables.size();
49 49 }
50 50
51 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 85 return QVariant{};
54 86 }
55 87
56 88 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
57 89 {
58 90 if (role != Qt::DisplayRole) {
59 91 return QVariant{};
60 92 }
61 93
62 94 if (orientation == Qt::Horizontal) {
63 95 switch (section) {
64 96 case NAME_COLUMN:
65 97 return tr("Name");
66 98 case UNIT_COLUMN:
67 99 return tr("Unit");
68 100 case MISSION_COLUMN:
69 101 return tr("Mission");
70 102 default:
71 103 // No action
72 104 break;
73 105 }
74 106
75 107 qWarning(LOG_VariableModel())
76 108 << tr("Can't get header data (unknown column %1)").arg(section);
77 109 }
78 110
79 111 return QVariant{};
80 112 }
General Comments 0
You need to be logged in to leave comments. Login now