Skip to content
Permalink
Browse files

fix #2019

git-svn-id: http://svn.osgeo.org/qgis/trunk@12639 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Dec 28, 2009
1 parent cd20513 commit c122111ec8de17b699e2a2b0545cc6492e8a3c5c
@@ -192,6 +192,7 @@ SET (QGIS_APP_MOC_HDRS
ogr/qgsopenvectorlayerdialog.h
ogr/qgsnewogrconnection.h

attributetable/qgsattributetableview.h
attributetable/qgsattributetablemodel.h
attributetable/qgsattributetablememorymodel.h
attributetable/qgsattributetabledialog.h
@@ -19,6 +19,8 @@
#include "qgsfield.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include "qgisapp.h"
#include "qgsattributeaction.h"

#include <QtGui>
#include <QVariant>
@@ -317,7 +319,10 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
return QVariant( attributeName );
}
}
else return QVariant();
else
{
return QVariant();
}
}

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

static void _runPythonString( const QString &expr )
{
QgisApp::instance()->runPythonString( expr );
}

void QgsAttributeTableModel::executeAction( int action, const QModelIndex &idx ) const
{
QList< QPair<QString, QString> > attributes;

for ( int i = 0; i < mAttributes.size(); i++ )
{
attributes << QPair<QString, QString>(
mLayer->pendingFields()[ mAttributes[i] ].name(),
data( index( idx.row(), i ), Qt::EditRole ).toString()
);
}

mLayer->actions()->doAction( action, attributes, fieldIdx( idx.column() ), _runPythonString );
}
@@ -125,6 +125,9 @@ class QgsAttributeTableModel: public QAbstractTableModel
*/
QgsVectorLayer* layer() const { return mLayer; }

/** Execute an action */
void executeAction( int action, const QModelIndex &idx ) const;

signals:
/**
* Model has been changed
@@ -16,6 +16,7 @@
#include <QKeyEvent>
#include <QSettings>
#include <QHeaderView>
#include <QMenu>

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

#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"

#include "qgsattributeaction.h"

QgsAttributeTableView::QgsAttributeTableView( QWidget* parent )
: QTableView( parent )
: QTableView( parent ), mActionPopup( 0 )
{
QSettings settings;
restoreGeometry( settings.value( "/BetterTable/geometry" ).toByteArray() );
@@ -41,7 +42,6 @@ QgsAttributeTableView::QgsAttributeTableView( QWidget* parent )
setSelectionBehavior( QAbstractItemView::SelectRows );
setSelectionMode( QAbstractItemView::NoSelection );
setSortingEnabled( true );

}

void QgsAttributeTableView::setLayer( QgsVectorLayer* layer )
@@ -64,10 +64,55 @@ QgsAttributeTableView::~QgsAttributeTableView()
{
delete mModel;
delete mFilterModel;
delete mActionPopup;
}

void QgsAttributeTableView::closeEvent( QCloseEvent *event )
{
QSettings settings;
settings.setValue( "/BetterAttributeTable/geometry", QVariant( saveGeometry() ) );
}

void QgsAttributeTableView::contextMenuEvent( QContextMenuEvent *event )
{
if ( mActionPopup )
{
delete mActionPopup;
mActionPopup = 0;
}

QModelIndex idx = indexAt( event->pos() );
if ( !idx.isValid() )
{
return;
}

QgsVectorLayer *vlayer = mModel->layer();
if ( !vlayer || vlayer->actions()->size() == 0 )
{
return;
}

mActionPopup = new QMenu();

QAction *a = mActionPopup->addAction( tr( "Run action" ) );
a->setEnabled( false );

for ( int i = 0; i < vlayer->actions()->size(); i++ )
{
const QgsAction &action = vlayer->actions()->at( i );

if ( !action.runable() )
continue;

QgsAttributeTableAction *a = new QgsAttributeTableAction( action.name(), this, mModel, i, idx );
mActionPopup->addAction( action.name(), a, SLOT( execute() ) );
}

mActionPopup->popup( event->globalPos() );
}

void QgsAttributeTableAction::execute()
{
mModel->executeAction( mAction, mFieldIdx );
}
@@ -17,11 +17,13 @@
#define QGSATTRIBUTETABLEVIEW_H

#include <QTableView>
#include <QAction>

class QgsAttributeTableModel;
class QgsAttributeTableFilterModel;

class QgsVectorLayer;
class QMenu;


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

void contextMenuEvent( QContextMenuEvent* );

private:
QgsAttributeTableModel* mModel;
QgsAttributeTableFilterModel* mFilterModel;
QMenu *mActionPopup;
};

class QgsAttributeTableAction : public QAction
{
Q_OBJECT

public:
QgsAttributeTableAction( const QString &name, QgsAttributeTableView *view, QgsAttributeTableModel *model, int action, const QModelIndex &fieldIdx ) :
QAction( name, view ), mModel( model ), mAction( action ), mFieldIdx( fieldIdx )
{}

public slots:
void execute();

private:
QgsAttributeTableModel *mModel;
int mAction;
QModelIndex mFieldIdx;
};


#endif

0 comments on commit c122111

Please sign in to comment.
You can’t perform that action at this time.