Skip to content

Commit c122111

Browse files
author
jef
committed
fix #2019
git-svn-id: http://svn.osgeo.org/qgis/trunk@12639 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent cd20513 commit c122111

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

src/app/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ SET (QGIS_APP_MOC_HDRS
192192
ogr/qgsopenvectorlayerdialog.h
193193
ogr/qgsnewogrconnection.h
194194

195+
attributetable/qgsattributetableview.h
195196
attributetable/qgsattributetablemodel.h
196197
attributetable/qgsattributetablememorymodel.h
197198
attributetable/qgsattributetabledialog.h

src/app/attributetable/qgsattributetablemodel.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "qgsfield.h"
2020
#include "qgsvectorlayer.h"
2121
#include "qgslogger.h"
22+
#include "qgisapp.h"
23+
#include "qgsattributeaction.h"
2224

2325
#include <QtGui>
2426
#include <QVariant>
@@ -317,7 +319,10 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
317319
return QVariant( attributeName );
318320
}
319321
}
320-
else return QVariant();
322+
else
323+
{
324+
return QVariant();
325+
}
321326
}
322327

323328
void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
@@ -487,3 +492,22 @@ void QgsAttributeTableModel::incomingChangeLayout()
487492
emit layoutAboutToBeChanged();
488493
}
489494

495+
static void _runPythonString( const QString &expr )
496+
{
497+
QgisApp::instance()->runPythonString( expr );
498+
}
499+
500+
void QgsAttributeTableModel::executeAction( int action, const QModelIndex &idx ) const
501+
{
502+
QList< QPair<QString, QString> > attributes;
503+
504+
for ( int i = 0; i < mAttributes.size(); i++ )
505+
{
506+
attributes << QPair<QString, QString>(
507+
mLayer->pendingFields()[ mAttributes[i] ].name(),
508+
data( index( idx.row(), i ), Qt::EditRole ).toString()
509+
);
510+
}
511+
512+
mLayer->actions()->doAction( action, attributes, fieldIdx( idx.column() ), _runPythonString );
513+
}

src/app/attributetable/qgsattributetablemodel.h

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ class QgsAttributeTableModel: public QAbstractTableModel
125125
*/
126126
QgsVectorLayer* layer() const { return mLayer; }
127127

128+
/** Execute an action */
129+
void executeAction( int action, const QModelIndex &idx ) const;
130+
128131
signals:
129132
/**
130133
* Model has been changed

src/app/attributetable/qgsattributetableview.cpp

+48-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <QKeyEvent>
1717
#include <QSettings>
1818
#include <QHeaderView>
19+
#include <QMenu>
1920

2021
#include "qgsattributetableview.h"
2122
#include "qgsattributetablemodel.h"
@@ -25,10 +26,10 @@
2526

2627
#include "qgsvectorlayer.h"
2728
#include "qgsvectordataprovider.h"
28-
29+
#include "qgsattributeaction.h"
2930

3031
QgsAttributeTableView::QgsAttributeTableView( QWidget* parent )
31-
: QTableView( parent )
32+
: QTableView( parent ), mActionPopup( 0 )
3233
{
3334
QSettings settings;
3435
restoreGeometry( settings.value( "/BetterTable/geometry" ).toByteArray() );
@@ -41,7 +42,6 @@ QgsAttributeTableView::QgsAttributeTableView( QWidget* parent )
4142
setSelectionBehavior( QAbstractItemView::SelectRows );
4243
setSelectionMode( QAbstractItemView::NoSelection );
4344
setSortingEnabled( true );
44-
4545
}
4646

4747
void QgsAttributeTableView::setLayer( QgsVectorLayer* layer )
@@ -64,10 +64,55 @@ QgsAttributeTableView::~QgsAttributeTableView()
6464
{
6565
delete mModel;
6666
delete mFilterModel;
67+
delete mActionPopup;
6768
}
6869

6970
void QgsAttributeTableView::closeEvent( QCloseEvent *event )
7071
{
7172
QSettings settings;
7273
settings.setValue( "/BetterAttributeTable/geometry", QVariant( saveGeometry() ) );
7374
}
75+
76+
void QgsAttributeTableView::contextMenuEvent( QContextMenuEvent *event )
77+
{
78+
if ( mActionPopup )
79+
{
80+
delete mActionPopup;
81+
mActionPopup = 0;
82+
}
83+
84+
QModelIndex idx = indexAt( event->pos() );
85+
if ( !idx.isValid() )
86+
{
87+
return;
88+
}
89+
90+
QgsVectorLayer *vlayer = mModel->layer();
91+
if ( !vlayer || vlayer->actions()->size() == 0 )
92+
{
93+
return;
94+
}
95+
96+
mActionPopup = new QMenu();
97+
98+
QAction *a = mActionPopup->addAction( tr( "Run action" ) );
99+
a->setEnabled( false );
100+
101+
for ( int i = 0; i < vlayer->actions()->size(); i++ )
102+
{
103+
const QgsAction &action = vlayer->actions()->at( i );
104+
105+
if ( !action.runable() )
106+
continue;
107+
108+
QgsAttributeTableAction *a = new QgsAttributeTableAction( action.name(), this, mModel, i, idx );
109+
mActionPopup->addAction( action.name(), a, SLOT( execute() ) );
110+
}
111+
112+
mActionPopup->popup( event->globalPos() );
113+
}
114+
115+
void QgsAttributeTableAction::execute()
116+
{
117+
mModel->executeAction( mAction, mFieldIdx );
118+
}

src/app/attributetable/qgsattributetableview.h

+24
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
#define QGSATTRIBUTETABLEVIEW_H
1818

1919
#include <QTableView>
20+
#include <QAction>
2021

2122
class QgsAttributeTableModel;
2223
class QgsAttributeTableFilterModel;
2324

2425
class QgsVectorLayer;
26+
class QMenu;
2527

2628

2729
class QgsAttributeTableView: public QTableView
@@ -42,9 +44,31 @@ class QgsAttributeTableView: public QTableView
4244
*/
4345
void closeEvent( QCloseEvent *event );
4446

47+
void contextMenuEvent( QContextMenuEvent* );
48+
4549
private:
4650
QgsAttributeTableModel* mModel;
4751
QgsAttributeTableFilterModel* mFilterModel;
52+
QMenu *mActionPopup;
53+
};
54+
55+
class QgsAttributeTableAction : public QAction
56+
{
57+
Q_OBJECT
58+
59+
public:
60+
QgsAttributeTableAction( const QString &name, QgsAttributeTableView *view, QgsAttributeTableModel *model, int action, const QModelIndex &fieldIdx ) :
61+
QAction( name, view ), mModel( model ), mAction( action ), mFieldIdx( fieldIdx )
62+
{}
63+
64+
public slots:
65+
void execute();
66+
67+
private:
68+
QgsAttributeTableModel *mModel;
69+
int mAction;
70+
QModelIndex mFieldIdx;
4871
};
4972

73+
5074
#endif

0 commit comments

Comments
 (0)