Skip to content

Commit

Permalink
[FEATURE] add copy cell content action in attributetable context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere authored and nyalldawson committed Jun 2, 2016
1 parent e0c87ff commit 3da3cf1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/gui/attributetable/qgsdualview.sip
Expand Up @@ -189,6 +189,12 @@ class QgsDualView : QStackedWidget
*/
void toggleSearchMode( bool enabled );

/**
* Copy the content of the selected cell in the clipboard.
* @note added in QGIS 1.16
*/
void copyCellContent() const;

signals:
/**
* Is emitted, whenever the display expression is successfully changed
Expand Down
20 changes: 20 additions & 0 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsvectordataprovider.h"
#include "qgsvectorlayercache.h"

#include <QClipboard>
#include <QDialog>
#include <QMenu>
#include <QMessageBox>
Expand Down Expand Up @@ -379,13 +380,32 @@ int QgsDualView::filteredFeatureCount()
return mFilterModel->rowCount();
}

void QgsDualView::copyCellContent() const
{
QAction* action = qobject_cast<QAction*>( sender() );

if ( action && action->data().isValid() && action->data().canConvert<QModelIndex>() )
{
QModelIndex index = action->data().value<QModelIndex>();

QgsFeature f = masterModel()->feature( index );
QVariant var = f.attributes().at( index.column() );
QApplication::clipboard()->setText( var.toString() );
}
}

void QgsDualView::viewWillShowContextMenu( QMenu* menu, const QModelIndex& atIndex )
{
if ( !menu )
{
return;
}

QAction *copyContentAction = new QAction( tr( "Copy cell content" ), this );
copyContentAction->setData( QVariant::fromValue<QModelIndex>( atIndex ) );
menu->addAction( copyContentAction );
connect( copyContentAction, SIGNAL( triggered() ), this, SLOT( copyCellContent() ) );

QgsVectorLayer* vl = mFilterModel->layer();
QgsMapCanvas* canvas = mFilterModel->mapCanvas();
if ( canvas && vl && vl->geometryType() != QGis::NoGeometry )
Expand Down
8 changes: 8 additions & 0 deletions src/gui/attributetable/qgsdualview.h
Expand Up @@ -226,6 +226,12 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
*/
void toggleSearchMode( bool enabled );

/**
* Copy the content of the selected cell in the clipboard.
* @note added in QGIS 1.16
*/
void copyCellContent() const;

signals:
/**
* Is emitted, whenever the display expression is successfully changed
Expand Down Expand Up @@ -358,4 +364,6 @@ class GUI_EXPORT QgsAttributeTableMapLayerAction : public QAction
QModelIndex mFieldIdx;
};

Q_DECLARE_METATYPE( QModelIndex );

#endif // QGSDUALVIEW_H

0 comments on commit 3da3cf1

Please sign in to comment.