2323#include < QFileDialog>
2424#include < QMessageBox>
2525#include < QTextStream>
26+ #include < QClipboard>
27+ #include < QKeyEvent>
2628
2729QgsValueMapConfigDlg::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
4447QVariantMap 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+
199217void 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 ¤t : 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+
222267void QgsValueMapConfigDlg::addNullButtonPushed ()
223268{
224269 setRow ( tableWidget->rowCount () - 1 , QgsValueMapFieldFormatter::NULL_VALUE, QStringLiteral ( " <NULL>" ) );
0 commit comments