23
23
#include < QFileDialog>
24
24
#include < QMessageBox>
25
25
#include < QTextStream>
26
+ #include < QClipboard>
27
+ #include < QKeyEvent>
26
28
27
29
QgsValueMapConfigDlg::QgsValueMapConfigDlg ( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
28
30
: QgsEditorConfigWidget( vl, fieldIdx, parent )
@@ -39,6 +41,7 @@ QgsValueMapConfigDlg::QgsValueMapConfigDlg( QgsVectorLayer *vl, int fieldIdx, QW
39
41
connect ( loadFromLayerButton, &QAbstractButton::clicked, this , &QgsValueMapConfigDlg::loadFromLayerButtonPushed );
40
42
connect ( loadFromCSVButton, &QAbstractButton::clicked, this , &QgsValueMapConfigDlg::loadFromCSVButtonPushed );
41
43
connect ( tableWidget, &QTableWidget::cellChanged, this , &QgsValueMapConfigDlg::vCellChanged );
44
+ tableWidget->installEventFilter ( this );
42
45
}
43
46
44
47
QVariantMap QgsValueMapConfigDlg::config ()
@@ -196,6 +199,21 @@ void QgsValueMapConfigDlg::populateComboBox( QComboBox *comboBox, const QVariant
196
199
}
197
200
}
198
201
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
+
199
217
void QgsValueMapConfigDlg::setRow ( int row, const QString &value, const QString &description )
200
218
{
201
219
QgsSettings settings;
@@ -219,6 +237,33 @@ void QgsValueMapConfigDlg::setRow( int row, const QString &value, const QString
219
237
tableWidget->setItem ( row, 1 , descriptionCell );
220
238
}
221
239
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
+
222
267
void QgsValueMapConfigDlg::addNullButtonPushed ()
223
268
{
224
269
setRow ( tableWidget->rowCount () - 1 , QgsValueMapFieldFormatter::NULL_VALUE, QStringLiteral ( " <NULL>" ) );
0 commit comments