at methode verify the avaibility of the index but return an execption in case of error. 1) You can do : a try catch (and return the null ptr in case of out_of_range. 2) Keep your verification and use [] instead of at
@@ -1,49 +1,51 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLEMODEL_H |
|
2 | 2 | #define SCIQLOP_VARIABLEMODEL_H |
|
3 | 3 | |
|
4 | 4 | |
|
5 | 5 | #include <Data/SqpDateTime.h> |
|
6 | 6 | |
|
7 | 7 | #include <QAbstractTableModel> |
|
8 | 8 | #include <QLoggingCategory> |
|
9 | 9 | |
|
10 | 10 | #include <Common/spimpl.h> |
|
11 | 11 | |
|
12 | 12 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel) |
|
13 | 13 | |
|
14 | 14 | class IDataSeries; |
|
15 | 15 | class Variable; |
|
16 | 16 | |
|
17 | 17 | /** |
|
18 | 18 | * @brief The VariableModel class aims to hold the variables that have been created in SciQlop |
|
19 | 19 | */ |
|
20 | 20 | class VariableModel : public QAbstractTableModel { |
|
21 | 21 | public: |
|
22 | 22 | explicit VariableModel(QObject *parent = nullptr); |
|
23 | 23 | |
|
24 | 24 | /** |
|
25 | 25 | * Creates a new variable in the model |
|
26 | 26 | * @param name the name of the new variable |
|
27 | 27 | * @param dateTime the dateTime of the new variable |
|
28 | 28 | * @param defaultDataSeries the default data of the new variable |
|
29 | 29 | * @return the pointer to the new variable |
|
30 | 30 | */ |
|
31 | 31 | std::shared_ptr<Variable> |
|
32 | 32 | createVariable(const QString &name, const SqpDateTime &dateTime, |
|
33 | 33 | std::unique_ptr<IDataSeries> defaultDataSeries) noexcept; |
|
34 | 34 | |
|
35 | std::shared_ptr<Variable> variable(int index) const; | |
|
36 | ||
|
35 | 37 | // /////////////////////////// // |
|
36 | 38 | // QAbstractTableModel methods // |
|
37 | 39 | // /////////////////////////// // |
|
38 | 40 | virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override; |
|
39 | 41 | virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override; |
|
40 | 42 | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
|
41 | 43 | virtual QVariant headerData(int section, Qt::Orientation orientation, |
|
42 | 44 | int role = Qt::DisplayRole) const override; |
|
43 | 45 | |
|
44 | 46 | private: |
|
45 | 47 | class VariableModelPrivate; |
|
46 | 48 | spimpl::unique_impl_ptr<VariableModelPrivate> impl; |
|
47 | 49 | }; |
|
48 | 50 | |
|
49 | 51 | #endif // SCIQLOP_VARIABLEMODEL_H |
@@ -1,120 +1,125 | |||
|
1 | 1 | #include <Variable/Variable.h> |
|
2 | 2 | #include <Variable/VariableModel.h> |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataSeries.h> |
|
5 | 5 | |
|
6 | 6 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") |
|
7 | 7 | |
|
8 | 8 | namespace { |
|
9 | 9 | |
|
10 | 10 | // Column indexes |
|
11 | 11 | const auto NAME_COLUMN = 0; |
|
12 | 12 | const auto UNIT_COLUMN = 1; |
|
13 | 13 | const auto MISSION_COLUMN = 2; |
|
14 | 14 | const auto NB_COLUMNS = 3; |
|
15 | 15 | |
|
16 | 16 | } // namespace |
|
17 | 17 | |
|
18 | 18 | struct VariableModel::VariableModelPrivate { |
|
19 | 19 | /// Variables created in SciQlop |
|
20 | 20 | std::vector<std::shared_ptr<Variable> > m_Variables; |
|
21 | 21 | }; |
|
22 | 22 | |
|
23 | 23 | VariableModel::VariableModel(QObject *parent) |
|
24 | 24 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} |
|
25 | 25 | { |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | std::shared_ptr<Variable> |
|
29 | 29 | VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime, |
|
30 | 30 | std::unique_ptr<IDataSeries> defaultDataSeries) noexcept |
|
31 | 31 | { |
|
32 | 32 | auto insertIndex = rowCount(); |
|
33 | 33 | beginInsertRows({}, insertIndex, insertIndex); |
|
34 | 34 | |
|
35 | 35 | /// @todo For the moment, the other data of the variable is initialized with default values |
|
36 | 36 | auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"), |
|
37 | 37 | QStringLiteral("mission"), dateTime); |
|
38 | 38 | variable->setDataSeries(std::move(defaultDataSeries)); |
|
39 | 39 | |
|
40 | 40 | impl->m_Variables.push_back(variable); |
|
41 | 41 | |
|
42 | 42 | endInsertRows(); |
|
43 | 43 | |
|
44 | 44 | return variable; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | std::shared_ptr<Variable> VariableModel::variable(int index) const | |
|
48 | { | |
|
49 |
return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables.at(index) : nullptr;
|
|
|
50 | } | |
|
51 | ||
|
47 | 52 | int VariableModel::columnCount(const QModelIndex &parent) const |
|
48 | 53 | { |
|
49 | 54 | Q_UNUSED(parent); |
|
50 | 55 | |
|
51 | 56 | return NB_COLUMNS; |
|
52 | 57 | } |
|
53 | 58 | |
|
54 | 59 | int VariableModel::rowCount(const QModelIndex &parent) const |
|
55 | 60 | { |
|
56 | 61 | Q_UNUSED(parent); |
|
57 | 62 | |
|
58 | 63 | return impl->m_Variables.size(); |
|
59 | 64 | } |
|
60 | 65 | |
|
61 | 66 | QVariant VariableModel::data(const QModelIndex &index, int role) const |
|
62 | 67 | { |
|
63 | 68 | if (!index.isValid()) { |
|
64 | 69 | return QVariant{}; |
|
65 | 70 | } |
|
66 | 71 | |
|
67 | 72 | if (index.row() < 0 || index.row() >= rowCount()) { |
|
68 | 73 | return QVariant{}; |
|
69 | 74 | } |
|
70 | 75 | |
|
71 | 76 | if (role == Qt::DisplayRole) { |
|
72 | 77 | if (auto variable = impl->m_Variables.at(index.row()).get()) { |
|
73 | 78 | switch (index.column()) { |
|
74 | 79 | case NAME_COLUMN: |
|
75 | 80 | return variable->name(); |
|
76 | 81 | case UNIT_COLUMN: |
|
77 | 82 | return variable->unit(); |
|
78 | 83 | case MISSION_COLUMN: |
|
79 | 84 | return variable->mission(); |
|
80 | 85 | default: |
|
81 | 86 | // No action |
|
82 | 87 | break; |
|
83 | 88 | } |
|
84 | 89 | |
|
85 | 90 | qWarning(LOG_VariableModel()) |
|
86 | 91 | << tr("Can't get data (unknown column %1)").arg(index.column()); |
|
87 | 92 | } |
|
88 | 93 | else { |
|
89 | 94 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); |
|
90 | 95 | } |
|
91 | 96 | } |
|
92 | 97 | |
|
93 | 98 | return QVariant{}; |
|
94 | 99 | } |
|
95 | 100 | |
|
96 | 101 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
97 | 102 | { |
|
98 | 103 | if (role != Qt::DisplayRole) { |
|
99 | 104 | return QVariant{}; |
|
100 | 105 | } |
|
101 | 106 | |
|
102 | 107 | if (orientation == Qt::Horizontal) { |
|
103 | 108 | switch (section) { |
|
104 | 109 | case NAME_COLUMN: |
|
105 | 110 | return tr("Name"); |
|
106 | 111 | case UNIT_COLUMN: |
|
107 | 112 | return tr("Unit"); |
|
108 | 113 | case MISSION_COLUMN: |
|
109 | 114 | return tr("Mission"); |
|
110 | 115 | default: |
|
111 | 116 | // No action |
|
112 | 117 | break; |
|
113 | 118 | } |
|
114 | 119 | |
|
115 | 120 | qWarning(LOG_VariableModel()) |
|
116 | 121 | << tr("Can't get header data (unknown column %1)").arg(section); |
|
117 | 122 | } |
|
118 | 123 | |
|
119 | 124 | return QVariant{}; |
|
120 | 125 | } |
@@ -1,35 +1,49 | |||
|
1 | 1 | #include <Variable/VariableController.h> |
|
2 | 2 | #include <Variable/VariableInspectorWidget.h> |
|
3 | 3 | #include <Variable/VariableModel.h> |
|
4 | 4 | |
|
5 | 5 | #include <ui_VariableInspectorWidget.h> |
|
6 | 6 | |
|
7 | 7 | #include <QSortFilterProxyModel> |
|
8 | 8 | |
|
9 | 9 | #include <SqpApplication.h> |
|
10 | 10 | |
|
11 | 11 | VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) |
|
12 | 12 | : QWidget{parent}, ui{new Ui::VariableInspectorWidget} |
|
13 | 13 | { |
|
14 | 14 | ui->setupUi(this); |
|
15 | 15 | |
|
16 | 16 | // Sets model for table |
|
17 | 17 | auto sortFilterModel = new QSortFilterProxyModel{this}; |
|
18 | 18 | sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); |
|
19 | 19 | |
|
20 | 20 | ui->tableView->setModel(sortFilterModel); |
|
21 | 21 | |
|
22 | 22 | // Connection to show a menu when right clicking on the tree |
|
23 | 23 | ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); |
|
24 | 24 | connect(ui->tableView, &QTableView::customContextMenuRequested, this, |
|
25 | 25 | &VariableInspectorWidget::onTableMenuRequested); |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | VariableInspectorWidget::~VariableInspectorWidget() |
|
29 | 29 | { |
|
30 | 30 | delete ui; |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept |
|
34 | 34 | { |
|
35 | auto selectedIndex = ui->tableView->indexAt(pos); | |
|
36 | if (selectedIndex.isValid()) { | |
|
37 | // Gets the model to retrieve the underlying selected variable | |
|
38 | auto model = sqpApp->variableController().variableModel(); | |
|
39 | if (auto selectedVariable = model->variable(selectedIndex.row())) { | |
|
40 | QMenu tableMenu{}; | |
|
41 | ||
|
42 | /// @todo ALX : make menu | |
|
43 | ||
|
44 | if (!tableMenu.isEmpty()) { | |
|
45 | tableMenu.exec(mapToGlobal(pos)); | |
|
46 | } | |
|
47 | } | |
|
48 |
}
else log error |
|
|
35 | 49 | } |
General Comments 0
You need to be logged in to leave comments.
Login now