Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make Ctrl+C in sql results window table always copy selection instead…
… of single cells
  • Loading branch information
nyalldawson committed Apr 18, 2023
1 parent 7a0c9a3 commit f2c23ad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/gui/qgsqueryresultwidget.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgsapplication.h"

#include <QClipboard>
#include <QShortcut>

QgsQueryResultWidget::QgsQueryResultWidget( QWidget *parent, QgsAbstractDatabaseProviderConnection *connection )
: QWidget( parent )
Expand Down Expand Up @@ -103,6 +104,9 @@ QgsQueryResultWidget::QgsQueryResultWidget( QWidget *parent, QgsAbstractDatabase
}
} );

QShortcut *copySelection = new QShortcut( QKeySequence::Copy, mQueryResultsTableView );
connect( copySelection, &QShortcut::activated, this, &QgsQueryResultWidget::copySelection );

setConnection( connection );
}

Expand Down Expand Up @@ -228,39 +232,47 @@ void QgsQueryResultWidget::showCellContextMenu( QPoint point )

menu->addAction( QgsApplication::getThemeIcon( "mActionEditCopy.svg" ), tr( "Copy" ), this, [ = ]
{
const QModelIndexList selection = mQueryResultsTableView->selectionModel()->selectedIndexes();
int minRow = -1;
int maxRow = -1;
int minCol = -1;
int maxCol = -1;
for ( const QModelIndex &index : selection )
{
if ( minRow == -1 || index.row() < minRow )
minRow = index.row();
if ( maxRow == -1 || index.row() > maxRow )
maxRow = index.row();
if ( minCol == -1 || index.column() < minCol )
minCol = index.column();
if ( maxCol == -1 || index.column() > maxCol )
maxCol = index.column();
}

if ( minRow == maxRow && minCol == maxCol )
{
// copy only one cell
const QString text = mModel->data( modelIndex, Qt::DisplayRole ).toString();
QApplication::clipboard()->setText( text );
}
else
{
copyResults( minRow, maxRow, minCol, maxCol );
}
copySelection();
}, QKeySequence::Copy );

menu->exec( mQueryResultsTableView->viewport()->mapToGlobal( point ) );
}
}

void QgsQueryResultWidget::copySelection()
{
const QModelIndexList selection = mQueryResultsTableView->selectionModel()->selectedIndexes();
if ( selection.empty() )
return;

int minRow = -1;
int maxRow = -1;
int minCol = -1;
int maxCol = -1;
for ( const QModelIndex &index : selection )
{
if ( minRow == -1 || index.row() < minRow )
minRow = index.row();
if ( maxRow == -1 || index.row() > maxRow )
maxRow = index.row();
if ( minCol == -1 || index.column() < minCol )
minCol = index.column();
if ( maxCol == -1 || index.column() > maxCol )
maxCol = index.column();
}

if ( minRow == maxRow && minCol == maxCol )
{
// copy only one cell
const QString text = mModel->data( selection.at( 0 ), Qt::DisplayRole ).toString();
QApplication::clipboard()->setText( text );
}
else
{
copyResults( minRow, maxRow, minCol, maxCol );
}
}

void QgsQueryResultWidget::updateSqlLayerColumns( )
{
// Precondition
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsqueryresultwidget.h
Expand Up @@ -209,6 +209,8 @@ class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResul

void showCellContextMenu( QPoint point );

void copySelection();

private:

std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
Expand Down

0 comments on commit f2c23ad

Please sign in to comment.