Skip to content

Commit 4acb12d

Browse files
authored
Merge pull request #8281 from m-kuhn/copyValueMapToClipboard
Allow copying value map definitions to clipboard
2 parents 6a9cff3 + 7a984c3 commit 4acb12d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/gui/editorwidgets/qgsvaluemapconfigdlg.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <QFileDialog>
2424
#include <QMessageBox>
2525
#include <QTextStream>
26+
#include <QClipboard>
27+
#include <QKeyEvent>
2628

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

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

202+
bool QgsValueMapConfigDlg::eventFilter( QObject *watched, QEvent *event )
203+
{
204+
Q_UNUSED( watched )
205+
if ( event->type() == QEvent::KeyRelease )
206+
{
207+
QKeyEvent *keyEvent = static_cast<QKeyEvent *>( event );
208+
if ( keyEvent->matches( QKeySequence::Copy ) )
209+
{
210+
copySelectionToClipboard();
211+
return true;
212+
}
213+
}
214+
return false;
215+
}
216+
199217
void QgsValueMapConfigDlg::setRow( int row, const QString &value, const QString &description )
200218
{
201219
QgsSettings settings;
@@ -219,6 +237,33 @@ void QgsValueMapConfigDlg::setRow( int row, const QString &value, const QString
219237
tableWidget->setItem( row, 1, descriptionCell );
220238
}
221239

240+
void QgsValueMapConfigDlg::copySelectionToClipboard()
241+
{
242+
QAbstractItemModel *model = tableWidget->model();
243+
QItemSelectionModel *selection = tableWidget->selectionModel();
244+
const QModelIndexList indexes = selection->selectedIndexes();
245+
246+
QString clipboardText;
247+
QModelIndex previous = indexes.first();
248+
std::unique_ptr<QMimeData> mimeData = qgis::make_unique<QMimeData>();
249+
for ( const QModelIndex &current : indexes )
250+
{
251+
const QString text = model->data( current ).toString();
252+
if ( current.row() != previous.row() )
253+
{
254+
clipboardText.append( '\n' );
255+
}
256+
else if ( current.column() != previous.column() )
257+
{
258+
clipboardText.append( '\t' );
259+
}
260+
clipboardText.append( text );
261+
previous = current;
262+
}
263+
mimeData->setData( QStringLiteral( "text/plain" ), clipboardText.toUtf8() );
264+
QApplication::clipboard()->setMimeData( mimeData.release() );
265+
}
266+
222267
void QgsValueMapConfigDlg::addNullButtonPushed()
223268
{
224269
setRow( tableWidget->rowCount() - 1, QgsValueMapFieldFormatter::NULL_VALUE, QStringLiteral( "<NULL>" ) );

src/gui/editorwidgets/qgsvaluemapconfigdlg.h

+3
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ class GUI_EXPORT QgsValueMapConfigDlg : public QgsEditorConfigWidget, private Ui
5151
*/
5252
static void populateComboBox( QComboBox *comboBox, const QVariantMap &configuration, bool skipNull );
5353

54+
bool eventFilter( QObject *watched, QEvent *event ) override;
55+
5456
private:
5557
void setRow( int row, const QString &value, const QString &description );
5658

5759
private slots:
60+
void copySelectionToClipboard();
5861
void vCellChanged( int row, int column );
5962
void addNullButtonPushed();
6063
void removeSelectedButtonPushed();

0 commit comments

Comments
 (0)