From d28f6519b43eedebfb004fd2a8bd95d1f06ed4a2 Mon Sep 17 00:00:00 2001 From: Ernie Pasveer Date: Mon, 16 Oct 2023 16:28:36 -0500 Subject: [PATCH] Add 'copy to clipboard' to Logger, Tracker, and Registers. --- src/SeerRegisterValuesBrowserWidget.cpp | 91 ++++++++++++++++++++---- src/SeerVariableLoggerBrowserWidget.cpp | 11 ++- src/SeerVariableTrackerBrowserWidget.cpp | 71 ++++++++++++++++-- src/SeerVariableTrackerBrowserWidget.h | 1 + 4 files changed, 147 insertions(+), 27 deletions(-) diff --git a/src/SeerRegisterValuesBrowserWidget.cpp b/src/SeerRegisterValuesBrowserWidget.cpp index ed440ea..45b41ea 100644 --- a/src/SeerRegisterValuesBrowserWidget.cpp +++ b/src/SeerRegisterValuesBrowserWidget.cpp @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include #include SeerRegisterValuesBrowserWidget::SeerRegisterValuesBrowserWidget (QWidget* parent) : QWidget(parent) { @@ -237,35 +239,96 @@ void SeerRegisterValuesBrowserWidget::handleItemEntered (QTreeWidgetItem* item, void SeerRegisterValuesBrowserWidget::handleContextMenu (const QPoint& pos) { + // Get the item at the cursor. QTreeWidgetItem* item = registersTreeWidget->itemAt(pos); - // Bring up the register edit dialog. - SeerRegisterEditValueDialog dlg(this); - dlg.set(item->text(1), item->text(2)); + // Construct the menu. + QMenu* menu = new QMenu("Options", this); + QAction* editAction = menu->addAction("Edit selected"); + QAction* copyAction = menu->addAction("Copy selected"); + QAction* copyAllAction = menu->addAction("Copy all"); - int ret = dlg.exec(); + // If no selected item, disable 'selected' copy but allow 'all'. + if (item == 0) { + editAction->setEnabled(false); + copyAction->setEnabled(false); + } - if (ret == 0) { + // Execute the menu. Return if nothing. + QAction* action = menu->exec(registersTreeWidget->mapToGlobal(pos)); + + if (action == 0) { return; } - // The register name could be changed, as well as the value. - QString name = dlg.nameText(); - QString value = dlg.valueText(); + // Do register edit. + if (action == editAction) { + + // Bring up the register edit dialog. + SeerRegisterEditValueDialog dlg(this); + + dlg.set(item->text(1), item->text(2)); + + int ret = dlg.exec(); + + if (ret == 0) { + return; + } + + // The register name could be changed, as well as the value. + QString name = dlg.nameText(); + QString value = dlg.valueText(); + + if (name == "") { + return; + } + + if (value == "") { + return; + } + + // Get the format. + QString fmt = registerFormatComboBox->currentData().toString(); + + // Emit the signal to change the register to the new value. + emit setRegisterValue(fmt, name, value); - if (name == "") { return; } - if (value == "") { + // Get selected tree items. + QList items; + + // Get list of 'select' items. + if (action == copyAction) { + items = registersTreeWidget->selectedItems(); + } + + // Get list of 'all' items. + if (action == copyAllAction) { + items = registersTreeWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard); + } + + // Populate the clipboard. + if (items.size() == 0) { return; } - // Get the format. - QString fmt = registerFormatComboBox->currentData().toString(); + QClipboard* clipboard = QGuiApplication::clipboard(); - // Emit the signal to change the register to the new value. - emit setRegisterValue(fmt, name, value); + QString text; + + for (int i=0; itext(1) + ":" + items[i]->text(2); + } + + clipboard->setText(text, QClipboard::Clipboard); + clipboard->setText(text, QClipboard::Selection); } void SeerRegisterValuesBrowserWidget::handleIndexEditingFinished (const QModelIndex& index) { diff --git a/src/SeerVariableLoggerBrowserWidget.cpp b/src/SeerVariableLoggerBrowserWidget.cpp index 36ee7d0..1f8f663 100644 --- a/src/SeerVariableLoggerBrowserWidget.cpp +++ b/src/SeerVariableLoggerBrowserWidget.cpp @@ -216,12 +216,9 @@ void SeerVariableLoggerBrowserWidget::handleContextMenu (const QPoint& pos) { QTreeWidgetItem* item = variablesTreeWidget->itemAt(pos); // Construct the menu. - QMenu* clipboardMenu = new QMenu("Clipboard"); - QAction* copyAction = clipboardMenu->addAction("Copy selected"); - QAction* copyAllAction = clipboardMenu->addAction("Copy all"); - QMenu* menu = new QMenu(this); - - menu->addMenu(clipboardMenu); + QMenu* menu = new QMenu("Options", this); + QAction* copyAction = menu->addAction("Copy selected"); + QAction* copyAllAction = menu->addAction("Copy all"); // If no selected item, disable 'selected' copy but allow 'all'. if (item == 0) { @@ -257,7 +254,7 @@ void SeerVariableLoggerBrowserWidget::handleContextMenu (const QPoint& pos) { QString text; - for (int i=0; i #include #include +#include #include +#include #include #include #include @@ -16,20 +18,20 @@ SeerVariableTrackerBrowserWidget::SeerVariableTrackerBrowserWidget (QWidget* par // Setup the widgets variablesTreeWidget->setMouseTracking(true); variablesTreeWidget->setSortingEnabled(false); + variablesTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu); variablesTreeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); variablesTreeWidget->resizeColumnToContents(0); // id variablesTreeWidget->resizeColumnToContents(1); // name variablesTreeWidget->resizeColumnToContents(2); // value - variablesTreeWidget->setColumnHidden(0, true); // Hide the 'number' column. - variablesTreeWidget->clear(); // Connect things. - QObject::connect(variableAddLineEdit, &QLineEdit::returnPressed, this, &SeerVariableTrackerBrowserWidget::handleAddLineEdit); - QObject::connect(variableDeleteToolButton, &QToolButton::clicked, this, &SeerVariableTrackerBrowserWidget::handleDeleteToolButton); - QObject::connect(variableDeleteAllToolButton, &QToolButton::clicked, this, &SeerVariableTrackerBrowserWidget::handleDeleteAllToolButton); - QObject::connect(variablesTreeWidget, &QTreeWidget::itemEntered, this, &SeerVariableTrackerBrowserWidget::handleItemEntered); + QObject::connect(variableAddLineEdit, &QLineEdit::returnPressed, this, &SeerVariableTrackerBrowserWidget::handleAddLineEdit); + QObject::connect(variableDeleteToolButton, &QToolButton::clicked, this, &SeerVariableTrackerBrowserWidget::handleDeleteToolButton); + QObject::connect(variableDeleteAllToolButton, &QToolButton::clicked, this, &SeerVariableTrackerBrowserWidget::handleDeleteAllToolButton); + QObject::connect(variablesTreeWidget, &QTreeWidget::itemEntered, this, &SeerVariableTrackerBrowserWidget::handleItemEntered); + QObject::connect(variablesTreeWidget, &QTreeWidget::customContextMenuRequested, this, &SeerVariableTrackerBrowserWidget::handleContextMenu); } SeerVariableTrackerBrowserWidget::~SeerVariableTrackerBrowserWidget () { @@ -258,3 +260,60 @@ void SeerVariableTrackerBrowserWidget::showEvent (QShowEvent* event) { refresh(); } +void SeerVariableTrackerBrowserWidget::handleContextMenu (const QPoint& pos) { + + // Get the item at the cursor. + QTreeWidgetItem* item = variablesTreeWidget->itemAt(pos); + + // Construct the menu. + QMenu* menu = new QMenu("Options", this); + QAction* copyAction = menu->addAction("Copy selected"); + QAction* copyAllAction = menu->addAction("Copy all"); + + // If no selected item, disable 'selected' copy but allow 'all'. + if (item == 0) { + copyAction->setEnabled(false); + } + + // Execute the menu. Return if nothing. + QAction* action = menu->exec(variablesTreeWidget->mapToGlobal(pos)); + + if (action == 0) { + return; + } + + // Get selected tree items. + QList items; + + // Get list of 'select' items. + if (action == copyAction) { + items = variablesTreeWidget->selectedItems(); + } + + // Get list of 'all' items. + if (action == copyAllAction) { + items = variablesTreeWidget->findItems(QString("*"), Qt::MatchWrap | Qt::MatchWildcard); + } + + // Populate the clipboard. + if (items.size() == 0) { + return; + } + + QClipboard* clipboard = QGuiApplication::clipboard(); + + QString text; + + for (int i=0; itext(1) + ":" + items[i]->text(2); + } + + clipboard->setText(text, QClipboard::Clipboard); + clipboard->setText(text, QClipboard::Selection); +} + diff --git a/src/SeerVariableTrackerBrowserWidget.h b/src/SeerVariableTrackerBrowserWidget.h index b350314..cfaad8c 100644 --- a/src/SeerVariableTrackerBrowserWidget.h +++ b/src/SeerVariableTrackerBrowserWidget.h @@ -23,6 +23,7 @@ class SeerVariableTrackerBrowserWidget : public QWidget, protected Ui::SeerVaria void handleDeleteToolButton (); void handleDeleteAllToolButton (); void handleItemEntered (QTreeWidgetItem* item, int column); + void handleContextMenu (const QPoint& pos); signals: void refreshVariableTrackerNames ();