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