Skip to content
Permalink
Browse files

Merge pull request #8281 from m-kuhn/copyValueMapToClipboard

Allow copying value map definitions to clipboard
  • Loading branch information
m-kuhn committed Oct 25, 2018
2 parents 6a9cff3 + 7a984c3 commit 4acb12d18e15c0cc7f6bd6ca2ae7ad5dba5dd5ed
Showing with 48 additions and 0 deletions.
  1. +45 −0 src/gui/editorwidgets/qgsvaluemapconfigdlg.cpp
  2. +3 −0 src/gui/editorwidgets/qgsvaluemapconfigdlg.h
@@ -23,6 +23,8 @@
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QClipboard>
#include <QKeyEvent>

QgsValueMapConfigDlg::QgsValueMapConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
@@ -39,6 +41,7 @@ QgsValueMapConfigDlg::QgsValueMapConfigDlg( QgsVectorLayer *vl, int fieldIdx, QW
connect( loadFromLayerButton, &QAbstractButton::clicked, this, &QgsValueMapConfigDlg::loadFromLayerButtonPushed );
connect( loadFromCSVButton, &QAbstractButton::clicked, this, &QgsValueMapConfigDlg::loadFromCSVButtonPushed );
connect( tableWidget, &QTableWidget::cellChanged, this, &QgsValueMapConfigDlg::vCellChanged );
tableWidget->installEventFilter( this );
}

QVariantMap QgsValueMapConfigDlg::config()
@@ -196,6 +199,21 @@ void QgsValueMapConfigDlg::populateComboBox( QComboBox *comboBox, const QVariant
}
}

bool QgsValueMapConfigDlg::eventFilter( QObject *watched, QEvent *event )
{
Q_UNUSED( watched )
if ( event->type() == QEvent::KeyRelease )
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>( event );
if ( keyEvent->matches( QKeySequence::Copy ) )
{
copySelectionToClipboard();
return true;
}
}
return false;
}

void QgsValueMapConfigDlg::setRow( int row, const QString &value, const QString &description )
{
QgsSettings settings;
@@ -219,6 +237,33 @@ void QgsValueMapConfigDlg::setRow( int row, const QString &value, const QString
tableWidget->setItem( row, 1, descriptionCell );
}

void QgsValueMapConfigDlg::copySelectionToClipboard()
{
QAbstractItemModel *model = tableWidget->model();
QItemSelectionModel *selection = tableWidget->selectionModel();
const QModelIndexList indexes = selection->selectedIndexes();

QString clipboardText;
QModelIndex previous = indexes.first();
std::unique_ptr<QMimeData> mimeData = qgis::make_unique<QMimeData>();
for ( const QModelIndex &current : indexes )
{
const QString text = model->data( current ).toString();
if ( current.row() != previous.row() )
{
clipboardText.append( '\n' );
}
else if ( current.column() != previous.column() )
{
clipboardText.append( '\t' );
}
clipboardText.append( text );
previous = current;
}
mimeData->setData( QStringLiteral( "text/plain" ), clipboardText.toUtf8() );
QApplication::clipboard()->setMimeData( mimeData.release() );
}

void QgsValueMapConfigDlg::addNullButtonPushed()
{
setRow( tableWidget->rowCount() - 1, QgsValueMapFieldFormatter::NULL_VALUE, QStringLiteral( "<NULL>" ) );
@@ -51,10 +51,13 @@ class GUI_EXPORT QgsValueMapConfigDlg : public QgsEditorConfigWidget, private Ui
*/
static void populateComboBox( QComboBox *comboBox, const QVariantMap &configuration, bool skipNull );

bool eventFilter( QObject *watched, QEvent *event ) override;

private:
void setRow( int row, const QString &value, const QString &description );

private slots:
void copySelectionToClipboard();
void vCellChanged( int row, int column );
void addNullButtonPushed();
void removeSelectedButtonPushed();

0 comments on commit 4acb12d

Please sign in to comment.
You can’t perform that action at this time.