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