##// END OF EJS Templates
Retrieves the current selected variable when clicking on the variable widget
Alexandre Leroux -
r229:37254eb43d9f
parent child
Show More
@@ -1,49 +1,51
1 #ifndef SCIQLOP_VARIABLEMODEL_H
1 #ifndef SCIQLOP_VARIABLEMODEL_H
2 #define SCIQLOP_VARIABLEMODEL_H
2 #define SCIQLOP_VARIABLEMODEL_H
3
3
4
4
5 #include <Data/SqpDateTime.h>
5 #include <Data/SqpDateTime.h>
6
6
7 #include <QAbstractTableModel>
7 #include <QAbstractTableModel>
8 #include <QLoggingCategory>
8 #include <QLoggingCategory>
9
9
10 #include <Common/spimpl.h>
10 #include <Common/spimpl.h>
11
11
12 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
12 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
13
13
14 class IDataSeries;
14 class IDataSeries;
15 class Variable;
15 class Variable;
16
16
17 /**
17 /**
18 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
18 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
19 */
19 */
20 class VariableModel : public QAbstractTableModel {
20 class VariableModel : public QAbstractTableModel {
21 public:
21 public:
22 explicit VariableModel(QObject *parent = nullptr);
22 explicit VariableModel(QObject *parent = nullptr);
23
23
24 /**
24 /**
25 * Creates a new variable in the model
25 * Creates a new variable in the model
26 * @param name the name of the new variable
26 * @param name the name of the new variable
27 * @param dateTime the dateTime of the new variable
27 * @param dateTime the dateTime of the new variable
28 * @param defaultDataSeries the default data of the new variable
28 * @param defaultDataSeries the default data of the new variable
29 * @return the pointer to the new variable
29 * @return the pointer to the new variable
30 */
30 */
31 std::shared_ptr<Variable>
31 std::shared_ptr<Variable>
32 createVariable(const QString &name, const SqpDateTime &dateTime,
32 createVariable(const QString &name, const SqpDateTime &dateTime,
33 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept;
33 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept;
34
34
35 std::shared_ptr<Variable> variable(int index) const;
36
35 // /////////////////////////// //
37 // /////////////////////////// //
36 // QAbstractTableModel methods //
38 // QAbstractTableModel methods //
37 // /////////////////////////// //
39 // /////////////////////////// //
38 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
40 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
39 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
41 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
40 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
42 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
41 virtual QVariant headerData(int section, Qt::Orientation orientation,
43 virtual QVariant headerData(int section, Qt::Orientation orientation,
42 int role = Qt::DisplayRole) const override;
44 int role = Qt::DisplayRole) const override;
43
45
44 private:
46 private:
45 class VariableModelPrivate;
47 class VariableModelPrivate;
46 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
48 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
47 };
49 };
48
50
49 #endif // SCIQLOP_VARIABLEMODEL_H
51 #endif // SCIQLOP_VARIABLEMODEL_H
@@ -1,120 +1,125
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 #include <Data/IDataSeries.h>
5
5
6 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
6 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
7
7
8 namespace {
8 namespace {
9
9
10 // Column indexes
10 // Column indexes
11 const auto NAME_COLUMN = 0;
11 const auto NAME_COLUMN = 0;
12 const auto UNIT_COLUMN = 1;
12 const auto UNIT_COLUMN = 1;
13 const auto MISSION_COLUMN = 2;
13 const auto MISSION_COLUMN = 2;
14 const auto NB_COLUMNS = 3;
14 const auto NB_COLUMNS = 3;
15
15
16 } // namespace
16 } // namespace
17
17
18 struct VariableModel::VariableModelPrivate {
18 struct VariableModel::VariableModelPrivate {
19 /// Variables created in SciQlop
19 /// Variables created in SciQlop
20 std::vector<std::shared_ptr<Variable> > m_Variables;
20 std::vector<std::shared_ptr<Variable> > m_Variables;
21 };
21 };
22
22
23 VariableModel::VariableModel(QObject *parent)
23 VariableModel::VariableModel(QObject *parent)
24 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
24 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
25 {
25 {
26 }
26 }
27
27
28 std::shared_ptr<Variable>
28 std::shared_ptr<Variable>
29 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
29 VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime,
30 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept
30 std::unique_ptr<IDataSeries> defaultDataSeries) noexcept
31 {
31 {
32 auto insertIndex = rowCount();
32 auto insertIndex = rowCount();
33 beginInsertRows({}, insertIndex, insertIndex);
33 beginInsertRows({}, insertIndex, insertIndex);
34
34
35 /// @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
36 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
36 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
37 QStringLiteral("mission"), dateTime);
37 QStringLiteral("mission"), dateTime);
38 variable->setDataSeries(std::move(defaultDataSeries));
38 variable->setDataSeries(std::move(defaultDataSeries));
39
39
40 impl->m_Variables.push_back(variable);
40 impl->m_Variables.push_back(variable);
41
41
42 endInsertRows();
42 endInsertRows();
43
43
44 return variable;
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;
note

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

50 }
51
47 int VariableModel::columnCount(const QModelIndex &parent) const
52 int VariableModel::columnCount(const QModelIndex &parent) const
48 {
53 {
49 Q_UNUSED(parent);
54 Q_UNUSED(parent);
50
55
51 return NB_COLUMNS;
56 return NB_COLUMNS;
52 }
57 }
53
58
54 int VariableModel::rowCount(const QModelIndex &parent) const
59 int VariableModel::rowCount(const QModelIndex &parent) const
55 {
60 {
56 Q_UNUSED(parent);
61 Q_UNUSED(parent);
57
62
58 return impl->m_Variables.size();
63 return impl->m_Variables.size();
59 }
64 }
60
65
61 QVariant VariableModel::data(const QModelIndex &index, int role) const
66 QVariant VariableModel::data(const QModelIndex &index, int role) const
62 {
67 {
63 if (!index.isValid()) {
68 if (!index.isValid()) {
64 return QVariant{};
69 return QVariant{};
65 }
70 }
66
71
67 if (index.row() < 0 || index.row() >= rowCount()) {
72 if (index.row() < 0 || index.row() >= rowCount()) {
68 return QVariant{};
73 return QVariant{};
69 }
74 }
70
75
71 if (role == Qt::DisplayRole) {
76 if (role == Qt::DisplayRole) {
72 if (auto variable = impl->m_Variables.at(index.row()).get()) {
77 if (auto variable = impl->m_Variables.at(index.row()).get()) {
73 switch (index.column()) {
78 switch (index.column()) {
74 case NAME_COLUMN:
79 case NAME_COLUMN:
75 return variable->name();
80 return variable->name();
76 case UNIT_COLUMN:
81 case UNIT_COLUMN:
77 return variable->unit();
82 return variable->unit();
78 case MISSION_COLUMN:
83 case MISSION_COLUMN:
79 return variable->mission();
84 return variable->mission();
80 default:
85 default:
81 // No action
86 // No action
82 break;
87 break;
83 }
88 }
84
89
85 qWarning(LOG_VariableModel())
90 qWarning(LOG_VariableModel())
86 << tr("Can't get data (unknown column %1)").arg(index.column());
91 << tr("Can't get data (unknown column %1)").arg(index.column());
87 }
92 }
88 else {
93 else {
89 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
94 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
90 }
95 }
91 }
96 }
92
97
93 return QVariant{};
98 return QVariant{};
94 }
99 }
95
100
96 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
101 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
97 {
102 {
98 if (role != Qt::DisplayRole) {
103 if (role != Qt::DisplayRole) {
99 return QVariant{};
104 return QVariant{};
100 }
105 }
101
106
102 if (orientation == Qt::Horizontal) {
107 if (orientation == Qt::Horizontal) {
103 switch (section) {
108 switch (section) {
104 case NAME_COLUMN:
109 case NAME_COLUMN:
105 return tr("Name");
110 return tr("Name");
106 case UNIT_COLUMN:
111 case UNIT_COLUMN:
107 return tr("Unit");
112 return tr("Unit");
108 case MISSION_COLUMN:
113 case MISSION_COLUMN:
109 return tr("Mission");
114 return tr("Mission");
110 default:
115 default:
111 // No action
116 // No action
112 break;
117 break;
113 }
118 }
114
119
115 qWarning(LOG_VariableModel())
120 qWarning(LOG_VariableModel())
116 << tr("Can't get header data (unknown column %1)").arg(section);
121 << tr("Can't get header data (unknown column %1)").arg(section);
117 }
122 }
118
123
119 return QVariant{};
124 return QVariant{};
120 }
125 }
@@ -1,35 +1,49
1 #include <Variable/VariableController.h>
1 #include <Variable/VariableController.h>
2 #include <Variable/VariableInspectorWidget.h>
2 #include <Variable/VariableInspectorWidget.h>
3 #include <Variable/VariableModel.h>
3 #include <Variable/VariableModel.h>
4
4
5 #include <ui_VariableInspectorWidget.h>
5 #include <ui_VariableInspectorWidget.h>
6
6
7 #include <QSortFilterProxyModel>
7 #include <QSortFilterProxyModel>
8
8
9 #include <SqpApplication.h>
9 #include <SqpApplication.h>
10
10
11 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
11 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
12 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
12 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
13 {
13 {
14 ui->setupUi(this);
14 ui->setupUi(this);
15
15
16 // Sets model for table
16 // Sets model for table
17 auto sortFilterModel = new QSortFilterProxyModel{this};
17 auto sortFilterModel = new QSortFilterProxyModel{this};
18 sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
18 sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
19
19
20 ui->tableView->setModel(sortFilterModel);
20 ui->tableView->setModel(sortFilterModel);
21
21
22 // Connection to show a menu when right clicking on the tree
22 // Connection to show a menu when right clicking on the tree
23 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
23 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
24 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
24 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
25 &VariableInspectorWidget::onTableMenuRequested);
25 &VariableInspectorWidget::onTableMenuRequested);
26 }
26 }
27
27
28 VariableInspectorWidget::~VariableInspectorWidget()
28 VariableInspectorWidget::~VariableInspectorWidget()
29 {
29 {
30 delete ui;
30 delete ui;
31 }
31 }
32
32
33 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
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 }
note

else log error

35 }
49 }
General Comments 0
You need to be logged in to leave comments. Login now