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