VariableInspectorWidget.cpp
88 lines
| 3.0 KiB
| text/x-c
|
CppLexer
|
r143 | #include <Variable/VariableController.h> | ||
|
r105 | #include <Variable/VariableInspectorWidget.h> | ||
|
r268 | #include <Variable/VariableMenuHeaderWidget.h> | ||
|
r143 | #include <Variable/VariableModel.h> | ||
|
r105 | |||
#include <ui_VariableInspectorWidget.h> | ||||
|
r143 | #include <QSortFilterProxyModel> | ||
|
r268 | #include <QWidgetAction> | ||
|
r143 | |||
#include <SqpApplication.h> | ||||
|
r234 | Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget") | ||
|
r105 | VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) | ||
: QWidget{parent}, ui{new Ui::VariableInspectorWidget} | ||||
{ | ||||
ui->setupUi(this); | ||||
|
r143 | |||
// Sets model for table | ||||
auto sortFilterModel = new QSortFilterProxyModel{this}; | ||||
sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); | ||||
ui->tableView->setModel(sortFilterModel); | ||||
|
r228 | |||
|
r258 | // 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()); | ||||
} | ||||
|
r264 | // Sets selection options | ||
ui->tableView->setSelectionBehavior(QTableView::SelectRows); | ||||
ui->tableView->setSelectionMode(QTableView::ExtendedSelection); | ||||
|
r228 | // Connection to show a menu when right clicking on the tree | ||
ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu); | ||||
connect(ui->tableView, &QTableView::customContextMenuRequested, this, | ||||
&VariableInspectorWidget::onTableMenuRequested); | ||||
|
r105 | } | ||
VariableInspectorWidget::~VariableInspectorWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
|
r228 | |||
void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept | ||||
{ | ||||
|
r265 | 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); | ||||
|
r229 | } | ||
} | ||||
|
r265 | |||
QMenu tableMenu{}; | ||||
// Emits a signal so that potential receivers can populate the menu before displaying it | ||||
emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables); | ||||
|
r269 | // Adds menu-specific actions | ||
if (!selectedVariables.isEmpty()) { | ||||
// 'Delete' action | ||||
auto deleteFun = []() { | ||||
/// @todo ALX : call variable deletion | ||||
}; | ||||
tableMenu.addSeparator(); | ||||
tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun); | ||||
} | ||||
|
r265 | if (!tableMenu.isEmpty()) { | ||
|
r268 | // 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); | ||||
|
r265 | // Displays menu | ||
tableMenu.exec(mapToGlobal(pos)); | ||||
|
r234 | } | ||
|
r228 | } | ||