##// END OF EJS Templates
Merge pull request 183 from SCIQLOP-Initialisation develop...
Merge pull request 183 from SCIQLOP-Initialisation develop Develop

File last commit:

r368:fc12ad933c3b
r399:edf6d13f3245 merge
Show More
VariableInspectorWidget.cpp
101 lines | 3.6 KiB | text/x-c | CppLexer
/ gui / src / Variable / VariableInspectorWidget.cpp
#include <Variable/VariableController.h>
#include <Variable/VariableInspectorWidget.h>
#include <Variable/VariableMenuHeaderWidget.h>
#include <Variable/VariableModel.h>
#include <ui_VariableInspectorWidget.h>
#include <QSortFilterProxyModel>
#include <QWidgetAction>
#include <SqpApplication.h>
Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
: QWidget{parent}, ui{new Ui::VariableInspectorWidget}
{
ui->setupUi(this);
// Sets model for table
// auto sortFilterModel = new QSortFilterProxyModel{this};
// sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
auto variableModel = sqpApp->variableController().variableModel();
ui->tableView->setModel(variableModel);
// Adds extra signal/slot between view and model, so the view can be updated instantly when
// there is a change of data in the model
connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this,
SLOT(refresh()));
ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel());
// Fixes column sizes
auto model = ui->tableView->model();
const auto count = model->columnCount();
for (auto i = 0; i < count; ++i) {
ui->tableView->setColumnWidth(
i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
}
// Sets selection options
ui->tableView->setSelectionBehavior(QTableView::SelectRows);
ui->tableView->setSelectionMode(QTableView::ExtendedSelection);
// Connection to show a menu when right clicking on the tree
ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->tableView, &QTableView::customContextMenuRequested, this,
&VariableInspectorWidget::onTableMenuRequested);
}
VariableInspectorWidget::~VariableInspectorWidget()
{
delete ui;
}
void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
{
auto selectedRows = ui->tableView->selectionModel()->selectedRows();
// Gets the model to retrieve the underlying selected variables
auto model = sqpApp->variableController().variableModel();
auto selectedVariables = QVector<std::shared_ptr<Variable> >{};
for (const auto &selectedRow : qAsConst(selectedRows)) {
if (auto selectedVariable = model->variable(selectedRow.row())) {
selectedVariables.push_back(selectedVariable);
}
}
QMenu tableMenu{};
// Emits a signal so that potential receivers can populate the menu before displaying it
emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables);
// Adds menu-specific actions
if (!selectedVariables.isEmpty()) {
// 'Delete' action
auto deleteFun = [&selectedVariables]() {
sqpApp->variableController().deleteVariables(selectedVariables);
};
tableMenu.addSeparator();
tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun);
}
if (!tableMenu.isEmpty()) {
// Generates menu header (inserted before first action)
auto firstAction = tableMenu.actions().first();
auto headerAction = new QWidgetAction{&tableMenu};
headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu});
tableMenu.insertAction(firstAction, headerAction);
// Displays menu
tableMenu.exec(mapToGlobal(pos));
}
}
void VariableInspectorWidget::refresh() noexcept
{
ui->tableView->viewport()->update();
}