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