Skip to content

Commit

Permalink
Merge pull request #8281 from m-kuhn/copyValueMapToClipboard
Browse files Browse the repository at this point in the history
Allow copying value map definitions to clipboard
  • Loading branch information
m-kuhn committed Oct 25, 2018
2 parents 6a9cff3 + 7a984c3 commit 4acb12d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/gui/editorwidgets/qgsvaluemapconfigdlg.cpp
Expand Up @@ -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 )
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -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>" ) );
Expand Down
3 changes: 3 additions & 0 deletions src/gui/editorwidgets/qgsvaluemapconfigdlg.h
Expand Up @@ -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();
Expand Down

0 comments on commit 4acb12d

Please sign in to comment.