Add context menu to all table views

Add a context menu to all table view widgets in the program (Browse Data
tab and Execute SQL tab) which opens when right clicking a table item.

Currently the menu only allows you to copy and paste the selected
item(s).

Only activate the paste action when the current view is writable.
This commit is contained in:
Martin Kleusberg
2017-06-28 16:30:03 +02:00
parent 4de01c1e73
commit 2421fef219
4 changed files with 31 additions and 0 deletions

View File

@@ -12,6 +12,7 @@
#include <QHeaderView>
#include <QMessageBox>
#include <QBuffer>
#include <QMenu>
namespace
{
@@ -75,7 +76,34 @@ ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) :
m_tableHeader = new FilterTableHeader(this);
setHorizontalHeader(m_tableHeader);
// Set up vertical header context menu
verticalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
// Set up table view context menu
m_contextMenu = new QMenu(this);
QAction* copyAction = new QAction(QIcon(":/icons/copy"), "Copy", m_contextMenu);
QAction* pasteAction = new QAction(QIcon(":/icons/paste"), "Paste", m_contextMenu);
m_contextMenu->addAction(copyAction);
m_contextMenu->addAction(pasteAction);
setContextMenuPolicy(Qt::CustomContextMenu);
// Set up context menu actions
connect(this, static_cast<void(QTableView::*)(const QPoint&)>(&QTableView::customContextMenuRequested),
[=](const QPoint& pos)
{
// Try to find out whether the current view is editable and (de)activate menu options according to that
bool editable = editTriggers() != QAbstractItemView::NoEditTriggers;
pasteAction->setEnabled(editable);
// Show menu
m_contextMenu->popup(viewport()->mapToGlobal(pos));
});
connect(copyAction, &QAction::triggered, [&]() {
copy();
});
connect(pasteAction, &QAction::triggered, [&]() {
paste();
});
}
void ExtendedTableWidget::reloadSettings()