##// END OF EJS Templates
Fixes refresh problem in Variable widget
Alexandre Leroux -
r368:fc12ad933c3b
parent child
Show More
@@ -1,55 +1,63
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/MetaTypes.h>
10 #include <Common/spimpl.h>
11 #include <Common/spimpl.h>
11
12
12 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
13 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
13
14
14 class IDataSeries;
15 class IDataSeries;
15 class Variable;
16 class Variable;
16
17
17 /**
18 /**
18 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
19 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
19 */
20 */
20 class VariableModel : public QAbstractTableModel {
21 class VariableModel : public QAbstractTableModel {
21 public:
22 public:
22 explicit VariableModel(QObject *parent = nullptr);
23 explicit VariableModel(QObject *parent = nullptr);
23
24
24 /**
25 /**
25 * Creates a new variable in the model
26 * Creates a new variable in the model
26 * @param name the name of the new variable
27 * @param name the name of the new variable
27 * @param dateTime the dateTime of the new variable
28 * @param dateTime the dateTime of the new variable
28 * @return the pointer to the new variable
29 * @return the pointer to the new variable
29 */
30 */
30 std::shared_ptr<Variable> createVariable(const QString &name,
31 std::shared_ptr<Variable> createVariable(const QString &name,
31 const SqpDateTime &dateTime) noexcept;
32 const SqpDateTime &dateTime) noexcept;
32
33
33 /**
34 /**
34 * Deletes a variable from the model, if it exists
35 * Deletes a variable from the model, if it exists
35 * @param variable the variable to delete
36 * @param variable the variable to delete
36 */
37 */
37 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
38 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
38
39
39 std::shared_ptr<Variable> variable(int index) const;
40 std::shared_ptr<Variable> variable(int index) const;
40
41
41 // /////////////////////////// //
42 // /////////////////////////// //
42 // QAbstractTableModel methods //
43 // QAbstractTableModel methods //
43 // /////////////////////////// //
44 // /////////////////////////// //
44 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
45 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
45 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
46 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
46 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
47 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
47 virtual QVariant headerData(int section, Qt::Orientation orientation,
48 virtual QVariant headerData(int section, Qt::Orientation orientation,
48 int role = Qt::DisplayRole) const override;
49 int role = Qt::DisplayRole) const override;
49
50
50 private:
51 private:
51 class VariableModelPrivate;
52 class VariableModelPrivate;
52 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
53 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
54
55 private slots:
56 /// Slot called when data of a variable has been updated
57 void onVariableUpdated() noexcept;
53 };
58 };
54
59
60 // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal
61 SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>)
62
55 #endif // SCIQLOP_VARIABLEMODEL_H
63 #endif // SCIQLOP_VARIABLEMODEL_H
@@ -1,179 +1,198
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 #include <QDateTime>
6 #include <QDateTime>
7 #include <QSize>
7 #include <QSize>
8
8
9 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
9 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
10
10
11 namespace {
11 namespace {
12
12
13 // Column indexes
13 // Column indexes
14 const auto NAME_COLUMN = 0;
14 const auto NAME_COLUMN = 0;
15 const auto TSTART_COLUMN = 1;
15 const auto TSTART_COLUMN = 1;
16 const auto TEND_COLUMN = 2;
16 const auto TEND_COLUMN = 2;
17 const auto NB_COLUMNS = 3;
17 const auto NB_COLUMNS = 3;
18
18
19 // Column properties
19 // Column properties
20 const auto DEFAULT_HEIGHT = 25;
20 const auto DEFAULT_HEIGHT = 25;
21 const auto DEFAULT_WIDTH = 100;
21 const auto DEFAULT_WIDTH = 100;
22
22
23 struct ColumnProperties {
23 struct ColumnProperties {
24 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
24 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
25 int height = DEFAULT_HEIGHT)
25 int height = DEFAULT_HEIGHT)
26 : m_Name{name}, m_Width{width}, m_Height{height}
26 : m_Name{name}, m_Width{width}, m_Height{height}
27 {
27 {
28 }
28 }
29
29
30 QString m_Name;
30 QString m_Name;
31 int m_Width;
31 int m_Width;
32 int m_Height;
32 int m_Height;
33 };
33 };
34
34
35 const auto COLUMN_PROPERTIES
35 const auto COLUMN_PROPERTIES
36 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
36 = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}},
37 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
37 {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
38 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
38 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}};
39
39
40 /// Format for datetimes
40 /// Format for datetimes
41 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
41 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
42
42
43 } // namespace
43 } // namespace
44
44
45 struct VariableModel::VariableModelPrivate {
45 struct VariableModel::VariableModelPrivate {
46 /// Variables created in SciQlop
46 /// Variables created in SciQlop
47 std::vector<std::shared_ptr<Variable> > m_Variables;
47 std::vector<std::shared_ptr<Variable> > m_Variables;
48 };
48 };
49
49
50 VariableModel::VariableModel(QObject *parent)
50 VariableModel::VariableModel(QObject *parent)
51 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
51 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
52 {
52 {
53 }
53 }
54
54
55 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
55 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
56 const SqpDateTime &dateTime) noexcept
56 const SqpDateTime &dateTime) noexcept
57 {
57 {
58 auto insertIndex = rowCount();
58 auto insertIndex = rowCount();
59 beginInsertRows({}, insertIndex, insertIndex);
59 beginInsertRows({}, insertIndex, insertIndex);
60
60
61 /// @todo For the moment, the other data of the variable is initialized with default values
61 /// @todo For the moment, the other data of the variable is initialized with default values
62 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
62 auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"),
63 QStringLiteral("mission"), dateTime);
63 QStringLiteral("mission"), dateTime);
64
64
65 impl->m_Variables.push_back(variable);
65 impl->m_Variables.push_back(variable);
66 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
66
67
67 endInsertRows();
68 endInsertRows();
68
69
69 return variable;
70 return variable;
70 }
71 }
71
72
72 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
73 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
73 {
74 {
74 if (!variable) {
75 if (!variable) {
75 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
76 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
76 return;
77 return;
77 }
78 }
78
79
79 // Finds variable in the model
80 // Finds variable in the model
80 auto begin = impl->m_Variables.cbegin();
81 auto begin = impl->m_Variables.cbegin();
81 auto end = impl->m_Variables.cend();
82 auto end = impl->m_Variables.cend();
82 auto it = std::find(begin, end, variable);
83 auto it = std::find(begin, end, variable);
83 if (it != end) {
84 if (it != end) {
84 auto removeIndex = std::distance(begin, it);
85 auto removeIndex = std::distance(begin, it);
85
86
86 // Deletes variable
87 // Deletes variable
87 beginRemoveRows({}, removeIndex, removeIndex);
88 beginRemoveRows({}, removeIndex, removeIndex);
88 impl->m_Variables.erase(it);
89 impl->m_Variables.erase(it);
89 endRemoveRows();
90 endRemoveRows();
90 }
91 }
91 else {
92 else {
92 qCritical(LOG_VariableModel())
93 qCritical(LOG_VariableModel())
93 << tr("Can't delete variable %1 from the model: the variable is not in the model")
94 << tr("Can't delete variable %1 from the model: the variable is not in the model")
94 .arg(variable->name());
95 .arg(variable->name());
95 }
96 }
96 }
97 }
97
98
98 std::shared_ptr<Variable> VariableModel::variable(int index) const
99 std::shared_ptr<Variable> VariableModel::variable(int index) const
99 {
100 {
100 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
101 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
101 }
102 }
102
103
103 int VariableModel::columnCount(const QModelIndex &parent) const
104 int VariableModel::columnCount(const QModelIndex &parent) const
104 {
105 {
105 Q_UNUSED(parent);
106 Q_UNUSED(parent);
106
107
107 return NB_COLUMNS;
108 return NB_COLUMNS;
108 }
109 }
109
110
110 int VariableModel::rowCount(const QModelIndex &parent) const
111 int VariableModel::rowCount(const QModelIndex &parent) const
111 {
112 {
112 Q_UNUSED(parent);
113 Q_UNUSED(parent);
113
114
114 return impl->m_Variables.size();
115 return impl->m_Variables.size();
115 }
116 }
116
117
117 QVariant VariableModel::data(const QModelIndex &index, int role) const
118 QVariant VariableModel::data(const QModelIndex &index, int role) const
118 {
119 {
119 if (!index.isValid()) {
120 if (!index.isValid()) {
120 return QVariant{};
121 return QVariant{};
121 }
122 }
122
123
123 if (index.row() < 0 || index.row() >= rowCount()) {
124 if (index.row() < 0 || index.row() >= rowCount()) {
124 return QVariant{};
125 return QVariant{};
125 }
126 }
126
127
127 if (role == Qt::DisplayRole) {
128 if (role == Qt::DisplayRole) {
128 if (auto variable = impl->m_Variables.at(index.row()).get()) {
129 if (auto variable = impl->m_Variables.at(index.row()).get()) {
129 /// Lambda function that builds the variant to return for a time value
130 /// Lambda function that builds the variant to return for a time value
130 auto dateTimeVariant = [](double time) {
131 auto dateTimeVariant = [](double time) {
131 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
132 auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.);
132 return dateTime.toString(DATETIME_FORMAT);
133 return dateTime.toString(DATETIME_FORMAT);
133 };
134 };
134
135
135 switch (index.column()) {
136 switch (index.column()) {
136 case NAME_COLUMN:
137 case NAME_COLUMN:
137 return variable->name();
138 return variable->name();
138 case TSTART_COLUMN:
139 case TSTART_COLUMN:
139 return dateTimeVariant(variable->dateTime().m_TStart);
140 return dateTimeVariant(variable->dateTime().m_TStart);
140 case TEND_COLUMN:
141 case TEND_COLUMN:
141 return dateTimeVariant(variable->dateTime().m_TEnd);
142 return dateTimeVariant(variable->dateTime().m_TEnd);
142 default:
143 default:
143 // No action
144 // No action
144 break;
145 break;
145 }
146 }
146
147
147 qWarning(LOG_VariableModel())
148 qWarning(LOG_VariableModel())
148 << tr("Can't get data (unknown column %1)").arg(index.column());
149 << tr("Can't get data (unknown column %1)").arg(index.column());
149 }
150 }
150 else {
151 else {
151 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
152 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
152 }
153 }
153 }
154 }
154
155
155 return QVariant{};
156 return QVariant{};
156 }
157 }
157
158
158 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
159 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
159 {
160 {
160 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
161 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
161 return QVariant{};
162 return QVariant{};
162 }
163 }
163
164
164 if (orientation == Qt::Horizontal) {
165 if (orientation == Qt::Horizontal) {
165 auto propertiesIt = COLUMN_PROPERTIES.find(section);
166 auto propertiesIt = COLUMN_PROPERTIES.find(section);
166 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
167 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
167 // Role is either DisplayRole or SizeHintRole
168 // Role is either DisplayRole or SizeHintRole
168 return (role == Qt::DisplayRole)
169 return (role == Qt::DisplayRole)
169 ? QVariant{propertiesIt->m_Name}
170 ? QVariant{propertiesIt->m_Name}
170 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
171 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
171 }
172 }
172 else {
173 else {
173 qWarning(LOG_VariableModel())
174 qWarning(LOG_VariableModel())
174 << tr("Can't get header data (unknown column %1)").arg(section);
175 << tr("Can't get header data (unknown column %1)").arg(section);
175 }
176 }
176 }
177 }
177
178
178 return QVariant{};
179 return QVariant{};
179 }
180 }
181
182 void VariableModel::onVariableUpdated() noexcept
183 {
184 // Finds variable that has been updated in the model
185 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
186 auto begin = std::cbegin(impl->m_Variables);
187 auto end = std::cend(impl->m_Variables);
188 auto it = std::find_if(begin, end, [updatedVariable](const auto &variable) {
189 return variable.get() == updatedVariable;
190 });
191
192 if (it != end) {
193 auto updateVariableIndex = std::distance(begin, it);
194 emit dataChanged(createIndex(updateVariableIndex, 0),
195 createIndex(updateVariableIndex, columnCount() - 1));
196 }
197 }
198 }
@@ -1,50 +1,52
1 #ifndef SCIQLOP_VARIABLEINSPECTORWIDGET_H
1 #ifndef SCIQLOP_VARIABLEINSPECTORWIDGET_H
2 #define SCIQLOP_VARIABLEINSPECTORWIDGET_H
2 #define SCIQLOP_VARIABLEINSPECTORWIDGET_H
3
3
4 #include <QLoggingCategory>
4 #include <QLoggingCategory>
5 #include <QMenu>
5 #include <QMenu>
6 #include <QWidget>
6 #include <QWidget>
7
7
8 #include <memory>
8 #include <memory>
9
9
10 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableInspectorWidget)
10 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableInspectorWidget)
11
11
12 class Variable;
12 class Variable;
13
13
14 namespace Ui {
14 namespace Ui {
15 class VariableInspectorWidget;
15 class VariableInspectorWidget;
16 } // Ui
16 } // Ui
17
17
18 /**
18 /**
19 * @brief The VariableInspectorWidget class representes represents the variable inspector, from
19 * @brief The VariableInspectorWidget class representes represents the variable inspector, from
20 * which it is possible to view the loaded variables, handle them or trigger their display in
20 * which it is possible to view the loaded variables, handle them or trigger their display in
21 * visualization
21 * visualization
22 */
22 */
23 class VariableInspectorWidget : public QWidget {
23 class VariableInspectorWidget : public QWidget {
24 Q_OBJECT
24 Q_OBJECT
25
25
26 public:
26 public:
27 explicit VariableInspectorWidget(QWidget *parent = 0);
27 explicit VariableInspectorWidget(QWidget *parent = 0);
28 virtual ~VariableInspectorWidget();
28 virtual ~VariableInspectorWidget();
29
29
30 signals:
30 signals:
31 /**
31 /**
32 * Signal emitted before a menu concerning variables is displayed. It is used for other widgets
32 * Signal emitted before a menu concerning variables is displayed. It is used for other widgets
33 * to complete the menu.
33 * to complete the menu.
34 * @param tableMenu the menu to be completed
34 * @param tableMenu the menu to be completed
35 * @param variables the variables concerned by the menu
35 * @param variables the variables concerned by the menu
36 * @remarks To make the dynamic addition of menus work, the connections to this signal must be
36 * @remarks To make the dynamic addition of menus work, the connections to this signal must be
37 * in Qt :: DirectConnection
37 * in Qt :: DirectConnection
38 */
38 */
39 void tableMenuAboutToBeDisplayed(QMenu *tableMenu,
39 void tableMenuAboutToBeDisplayed(QMenu *tableMenu,
40 const QVector<std::shared_ptr<Variable> > &variables);
40 const QVector<std::shared_ptr<Variable> > &variables);
41
41
42 private:
42 private:
43 Ui::VariableInspectorWidget *ui;
43 Ui::VariableInspectorWidget *ui;
44
44
45 private slots:
45 private slots:
46 /// Slot called when right clicking on an variable in the table (displays a menu)
46 /// Slot called when right clicking on an variable in the table (displays a menu)
47 void onTableMenuRequested(const QPoint &pos) noexcept;
47 void onTableMenuRequested(const QPoint &pos) noexcept;
48 /// Refreshes instantly the variable view
49 void refresh() noexcept;
48 };
50 };
49
51
50 #endif // SCIQLOP_VARIABLEINSPECTORWIDGET_H
52 #endif // SCIQLOP_VARIABLEINSPECTORWIDGET_H
@@ -1,89 +1,101
1 #include <Variable/VariableController.h>
1 #include <Variable/VariableController.h>
2 #include <Variable/VariableInspectorWidget.h>
2 #include <Variable/VariableInspectorWidget.h>
3 #include <Variable/VariableMenuHeaderWidget.h>
3 #include <Variable/VariableMenuHeaderWidget.h>
4 #include <Variable/VariableModel.h>
4 #include <Variable/VariableModel.h>
5
5
6 #include <ui_VariableInspectorWidget.h>
6 #include <ui_VariableInspectorWidget.h>
7
7
8 #include <QSortFilterProxyModel>
8 #include <QSortFilterProxyModel>
9 #include <QWidgetAction>
9 #include <QWidgetAction>
10
10
11 #include <SqpApplication.h>
11 #include <SqpApplication.h>
12
12
13 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
13 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
14
14
15 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
15 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
16 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
16 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
17 {
17 {
18 ui->setupUi(this);
18 ui->setupUi(this);
19
19
20 // Sets model for table
20 // Sets model for table
21 // auto sortFilterModel = new QSortFilterProxyModel{this};
21 // auto sortFilterModel = new QSortFilterProxyModel{this};
22 // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
22 // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
23
23
24 ui->tableView->setModel(sqpApp->variableController().variableModel());
24 auto variableModel = sqpApp->variableController().variableModel();
25 ui->tableView->setModel(variableModel);
26
27 // Adds extra signal/slot between view and model, so the view can be updated instantly when
28 // there is a change of data in the model
29 connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this,
30 SLOT(refresh()));
31
25 ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel());
32 ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel());
26
33
27 // Fixes column sizes
34 // Fixes column sizes
28 auto model = ui->tableView->model();
35 auto model = ui->tableView->model();
29 const auto count = model->columnCount();
36 const auto count = model->columnCount();
30 for (auto i = 0; i < count; ++i) {
37 for (auto i = 0; i < count; ++i) {
31 ui->tableView->setColumnWidth(
38 ui->tableView->setColumnWidth(
32 i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
39 i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
33 }
40 }
34
41
35 // Sets selection options
42 // Sets selection options
36 ui->tableView->setSelectionBehavior(QTableView::SelectRows);
43 ui->tableView->setSelectionBehavior(QTableView::SelectRows);
37 ui->tableView->setSelectionMode(QTableView::ExtendedSelection);
44 ui->tableView->setSelectionMode(QTableView::ExtendedSelection);
38
45
39 // Connection to show a menu when right clicking on the tree
46 // Connection to show a menu when right clicking on the tree
40 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
47 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
41 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
48 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
42 &VariableInspectorWidget::onTableMenuRequested);
49 &VariableInspectorWidget::onTableMenuRequested);
43 }
50 }
44
51
45 VariableInspectorWidget::~VariableInspectorWidget()
52 VariableInspectorWidget::~VariableInspectorWidget()
46 {
53 {
47 delete ui;
54 delete ui;
48 }
55 }
49
56
50 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
57 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
51 {
58 {
52 auto selectedRows = ui->tableView->selectionModel()->selectedRows();
59 auto selectedRows = ui->tableView->selectionModel()->selectedRows();
53
60
54 // Gets the model to retrieve the underlying selected variables
61 // Gets the model to retrieve the underlying selected variables
55 auto model = sqpApp->variableController().variableModel();
62 auto model = sqpApp->variableController().variableModel();
56 auto selectedVariables = QVector<std::shared_ptr<Variable> >{};
63 auto selectedVariables = QVector<std::shared_ptr<Variable> >{};
57 for (const auto &selectedRow : qAsConst(selectedRows)) {
64 for (const auto &selectedRow : qAsConst(selectedRows)) {
58 if (auto selectedVariable = model->variable(selectedRow.row())) {
65 if (auto selectedVariable = model->variable(selectedRow.row())) {
59 selectedVariables.push_back(selectedVariable);
66 selectedVariables.push_back(selectedVariable);
60 }
67 }
61 }
68 }
62
69
63 QMenu tableMenu{};
70 QMenu tableMenu{};
64
71
65 // Emits a signal so that potential receivers can populate the menu before displaying it
72 // Emits a signal so that potential receivers can populate the menu before displaying it
66 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables);
73 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables);
67
74
68 // Adds menu-specific actions
75 // Adds menu-specific actions
69 if (!selectedVariables.isEmpty()) {
76 if (!selectedVariables.isEmpty()) {
70 // 'Delete' action
77 // 'Delete' action
71 auto deleteFun = [&selectedVariables]() {
78 auto deleteFun = [&selectedVariables]() {
72 sqpApp->variableController().deleteVariables(selectedVariables);
79 sqpApp->variableController().deleteVariables(selectedVariables);
73 };
80 };
74
81
75 tableMenu.addSeparator();
82 tableMenu.addSeparator();
76 tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun);
83 tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun);
77 }
84 }
78
85
79 if (!tableMenu.isEmpty()) {
86 if (!tableMenu.isEmpty()) {
80 // Generates menu header (inserted before first action)
87 // Generates menu header (inserted before first action)
81 auto firstAction = tableMenu.actions().first();
88 auto firstAction = tableMenu.actions().first();
82 auto headerAction = new QWidgetAction{&tableMenu};
89 auto headerAction = new QWidgetAction{&tableMenu};
83 headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu});
90 headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu});
84 tableMenu.insertAction(firstAction, headerAction);
91 tableMenu.insertAction(firstAction, headerAction);
85
92
86 // Displays menu
93 // Displays menu
87 tableMenu.exec(mapToGlobal(pos));
94 tableMenu.exec(mapToGlobal(pos));
88 }
95 }
89 }
96 }
97
98 void VariableInspectorWidget::refresh() noexcept
99 {
100 ui->tableView->viewport()->update();
101 }
General Comments 0
You need to be logged in to leave comments. Login now