VariableInspectorWidget.cpp
232 lines
| 9.0 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r637 | #include <Variable/RenameVariableDialog.h> | ||
#include <Variable/Variable.h> | ||||
Alexandre Leroux
|
r143 | #include <Variable/VariableController.h> | ||
Alexandre Leroux
|
r105 | #include <Variable/VariableInspectorWidget.h> | ||
Alexandre Leroux
|
r268 | #include <Variable/VariableMenuHeaderWidget.h> | ||
Alexandre Leroux
|
r143 | #include <Variable/VariableModel.h> | ||
Alexandre Leroux
|
r105 | |||
#include <ui_VariableInspectorWidget.h> | ||||
r388 | #include <QMouseEvent> | |||
Alexandre Leroux
|
r143 | #include <QSortFilterProxyModel> | ||
r369 | #include <QStyledItemDelegate> | |||
Alexandre Leroux
|
r268 | #include <QWidgetAction> | ||
Alexandre Leroux
|
r143 | |||
#include <SqpApplication.h> | ||||
Alexandre Leroux
|
r234 | Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget") | ||
r369 | ||||
class QProgressBarItemDelegate : public QStyledItemDelegate { | ||||
public: | ||||
r370 | QProgressBarItemDelegate(QObject *parent) : QStyledItemDelegate{parent} {} | |||
r369 | ||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, | ||||
const QModelIndex &index) const | ||||
{ | ||||
auto data = index.data(Qt::DisplayRole); | ||||
r370 | auto progressData = index.data(VariableRoles::ProgressRole); | |||
r369 | if (data.isValid() && progressData.isValid()) { | |||
auto name = data.value<QString>(); | ||||
auto progress = progressData.value<double>(); | ||||
r388 | if (progress > 0) { | |||
auto cancelButtonWidth = 20; | ||||
r369 | auto progressBarOption = QStyleOptionProgressBar{}; | |||
r388 | auto progressRect = option.rect; | |||
progressRect.setWidth(progressRect.width() - cancelButtonWidth); | ||||
progressBarOption.rect = progressRect; | ||||
r369 | progressBarOption.minimum = 0; | |||
progressBarOption.maximum = 100; | ||||
progressBarOption.progress = progress; | ||||
progressBarOption.text | ||||
= QString("%1 %2").arg(name).arg(QString::number(progress, 'f', 2) + "%"); | ||||
progressBarOption.textVisible = true; | ||||
progressBarOption.textAlignment = Qt::AlignCenter; | ||||
r388 | ||||
r369 | QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, | |||
painter); | ||||
r388 | ||||
// Cancel button | ||||
auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, | ||||
option.rect.height()); | ||||
auto buttonOption = QStyleOptionButton{}; | ||||
buttonOption.rect = buttonRect; | ||||
buttonOption.text = "X"; | ||||
QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter); | ||||
} | ||||
else { | ||||
QStyledItemDelegate::paint(painter, option, index); | ||||
r369 | } | |||
} | ||||
else { | ||||
QStyledItemDelegate::paint(painter, option, index); | ||||
} | ||||
} | ||||
r388 | ||||
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, | ||||
const QModelIndex &index) | ||||
{ | ||||
if (event->type() == QEvent::MouseButtonRelease) { | ||||
auto data = index.data(Qt::DisplayRole); | ||||
auto progressData = index.data(VariableRoles::ProgressRole); | ||||
if (data.isValid() && progressData.isValid()) { | ||||
auto cancelButtonWidth = 20; | ||||
auto progressRect = option.rect; | ||||
progressRect.setWidth(progressRect.width() - cancelButtonWidth); | ||||
// Cancel button | ||||
auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth, | ||||
option.rect.height()); | ||||
r413 | auto e = (QMouseEvent *)event; | |||
auto clickX = e->x(); | ||||
auto clickY = e->y(); | ||||
r388 | ||||
auto x = buttonRect.left(); // the X coordinate | ||||
auto y = buttonRect.top(); // the Y coordinate | ||||
auto w = buttonRect.width(); // button width | ||||
auto h = buttonRect.height(); // button height | ||||
if (clickX > x && clickX < x + w) { | ||||
if (clickY > y && clickY < y + h) { | ||||
auto variableModel = sqpApp->variableController().variableModel(); | ||||
variableModel->abortProgress(index); | ||||
} | ||||
r705 | return true; | |||
r388 | } | |||
else { | ||||
r705 | return QStyledItemDelegate::editorEvent(event, model, option, index); | |||
r388 | } | |||
} | ||||
else { | ||||
r705 | return QStyledItemDelegate::editorEvent(event, model, option, index); | |||
r388 | } | |||
} | ||||
else { | ||||
r705 | return QStyledItemDelegate::editorEvent(event, model, option, index); | |||
r388 | } | |||
r705 | ||||
return QStyledItemDelegate::editorEvent(event, model, option, index); | ||||
r388 | } | |||
r369 | }; | |||
Alexandre Leroux
|
r105 | VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) | ||
r369 | : QWidget{parent}, | |||
ui{new Ui::VariableInspectorWidget}, | ||||
r370 | m_ProgressBarItemDelegate{new QProgressBarItemDelegate{this}} | |||
Alexandre Leroux
|
r105 | { | ||
ui->setupUi(this); | ||||
Alexandre Leroux
|
r143 | |||
// Sets model for table | ||||
r281 | // auto sortFilterModel = new QSortFilterProxyModel{this}; | |||
// sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); | ||||
Alexandre Leroux
|
r143 | |||
Alexandre Leroux
|
r340 | 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())); | ||||
r281 | ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel()); | |||
r369 | ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate); | |||
Alexandre Leroux
|
r228 | |||
Alexandre Leroux
|
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()); | ||||
} | ||||
Alexandre Leroux
|
r264 | // Sets selection options | ||
ui->tableView->setSelectionBehavior(QTableView::SelectRows); | ||||
ui->tableView->setSelectionMode(QTableView::ExtendedSelection); | ||||
Alexandre Leroux
|
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); | ||||
Alexandre Leroux
|
r105 | } | ||
VariableInspectorWidget::~VariableInspectorWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
Alexandre Leroux
|
r228 | |||
void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept | ||||
{ | ||||
Alexandre Leroux
|
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); | ||||
Alexandre Leroux
|
r229 | } | ||
} | ||||
Alexandre Leroux
|
r265 | |||
QMenu tableMenu{}; | ||||
// Emits a signal so that potential receivers can populate the menu before displaying it | ||||
emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables); | ||||
Alexandre Leroux
|
r269 | // Adds menu-specific actions | ||
if (!selectedVariables.isEmpty()) { | ||||
Alexandre Leroux
|
r635 | tableMenu.addSeparator(); | ||
Alexandre Leroux
|
r650 | // 'Rename' and 'Duplicate' actions (only if one variable selected) | ||
Alexandre Leroux
|
r635 | if (selectedVariables.size() == 1) { | ||
auto selectedVariable = selectedVariables.front(); | ||||
Alexandre Leroux
|
r650 | auto duplicateFun = [&selectedVariable]() { | ||
sqpApp->variableController().cloneVariable(selectedVariable); | ||||
}; | ||||
tableMenu.addAction(tr("Duplicate"), duplicateFun); | ||||
Alexandre Leroux
|
r635 | auto renameFun = [&selectedVariable, &model, this]() { | ||
Alexandre Leroux
|
r637 | // Generates forbidden names (names associated to existing variables) | ||
auto allVariables = model->variables(); | ||||
auto forbiddenNames = QVector<QString>(allVariables.size()); | ||||
std::transform(allVariables.cbegin(), allVariables.cend(), forbiddenNames.begin(), | ||||
[](const auto &variable) { return variable->name(); }); | ||||
RenameVariableDialog dialog{selectedVariable->name(), forbiddenNames, this}; | ||||
if (dialog.exec() == QDialog::Accepted) { | ||||
selectedVariable->setName(dialog.name()); | ||||
} | ||||
Alexandre Leroux
|
r635 | }; | ||
tableMenu.addAction(tr("Rename..."), renameFun); | ||||
} | ||||
Alexandre Leroux
|
r269 | // 'Delete' action | ||
Alexandre Leroux
|
r310 | auto deleteFun = [&selectedVariables]() { | ||
sqpApp->variableController().deleteVariables(selectedVariables); | ||||
Alexandre Leroux
|
r269 | }; | ||
tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun); | ||||
} | ||||
Alexandre Leroux
|
r265 | if (!tableMenu.isEmpty()) { | ||
Alexandre Leroux
|
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); | ||||
Alexandre Leroux
|
r265 | // Displays menu | ||
Alexandre Leroux
|
r612 | tableMenu.exec(QCursor::pos()); | ||
Alexandre Leroux
|
r234 | } | ||
Alexandre Leroux
|
r228 | } | ||
Alexandre Leroux
|
r340 | |||
void VariableInspectorWidget::refresh() noexcept | ||||
{ | ||||
ui->tableView->viewport()->update(); | ||||
} | ||||