diff --git a/src/ExtendedTableWidget.cpp b/src/ExtendedTableWidget.cpp index d55b5da0..ff728250 100644 --- a/src/ExtendedTableWidget.cpp +++ b/src/ExtendedTableWidget.cpp @@ -157,6 +157,7 @@ ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) : QAction* nullAction = new QAction(tr("Set to NULL"), m_contextMenu); QAction* copyAction = new QAction(QIcon(":/icons/copy"), tr("Copy"), m_contextMenu); QAction* copyWithHeadersAction = new QAction(QIcon(":/icons/special_copy"), tr("Copy with Headers"), m_contextMenu); + QAction* copyAsSQLAction = new QAction(QIcon(":/icons/sql_copy"), tr("Copy as SQL"), m_contextMenu); QAction* pasteAction = new QAction(QIcon(":/icons/paste"), tr("Paste"), m_contextMenu); QAction* filterAction = new QAction(tr("Use as Filter"), m_contextMenu); m_contextMenu->addAction(filterAction); @@ -165,6 +166,7 @@ ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) : m_contextMenu->addSeparator(); m_contextMenu->addAction(copyAction); m_contextMenu->addAction(copyWithHeadersAction); + m_contextMenu->addAction(copyAsSQLAction); m_contextMenu->addAction(pasteAction); setContextMenuPolicy(Qt::CustomContextMenu); @@ -177,6 +179,7 @@ ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) : nullAction->setShortcut(QKeySequence(tr("Alt+Del"))); copyAction->setShortcut(QKeySequence::Copy); copyWithHeadersAction->setShortcut(QKeySequence(tr("Ctrl+Shift+C"))); + copyAsSQLAction->setShortcut(QKeySequence(tr("Ctrl+Alt+C"))); pasteAction->setShortcut(QKeySequence::Paste); // Set up context menu actions @@ -188,6 +191,7 @@ ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) : filterAction->setEnabled(enabled); copyAction->setEnabled(enabled); copyWithHeadersAction->setEnabled(enabled); + copyAsSQLAction->setEnabled(enabled); // Try to find out whether the current view is editable and (de)activate menu options according to that bool editable = editTriggers() != QAbstractItemView::NoEditTriggers; @@ -205,10 +209,13 @@ ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) : model()->setData(index, QVariant()); }); connect(copyAction, &QAction::triggered, [&]() { - copy(false); + copy(false, false); }); connect(copyWithHeadersAction, &QAction::triggered, [&]() { - copy(true); + copy(true, false); + }); + connect(copyAsSQLAction, &QAction::triggered, [&]() { + copy(false, true); }); connect(pasteAction, &QAction::triggered, [&]() { paste(); @@ -226,7 +233,7 @@ void ExtendedTableWidget::reloadSettings() verticalHeader()->setDefaultSectionSize(verticalHeader()->fontMetrics().height()+10); } -void ExtendedTableWidget::copy(const bool withHeaders) +void ExtendedTableWidget::copy(const bool withHeaders, const bool inSQL ) { QModelIndexList indices = selectionModel()->selectedIndexes(); @@ -249,7 +256,7 @@ void ExtendedTableWidget::copy(const bool withHeaders) m_buffer.clear(); // If a single cell is selected, copy it to clipboard - if (!withHeaders && indices.size() == 1) { + if (!inSQL && !withHeaders && indices.size() == 1) { QImage img; QVariant data = m->data(indices.first(), Qt::EditRole); @@ -294,6 +301,7 @@ void ExtendedTableWidget::copy(const bool withHeaders) } m_buffer.push_back(lst); + QString sqlResult; QString result; QString htmlResult = ""; htmlResult.append("
"); @@ -314,8 +322,9 @@ void ExtendedTableWidget::copy(const bool withHeaders) const QString fieldSepText = "\t"; const QString rowSepText = "\r\n"; + QString sqlInsertStatement = QString("INSERT INTO %1 ( '").arg(sqlb::escapeIdentifier(m->currentTableName().toString())); // Table headers - if (withHeaders) { + if (withHeaders || inSQL) { htmlResult.append("