Skip to content

Commit

Permalink
fix #2019
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@12639 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Dec 28, 2009
1 parent cd20513 commit c122111
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion src/app/attributetable/qgsattributetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "qgsfield.h"
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include "qgisapp.h"
#include "qgsattributeaction.h"

#include <QtGui>
#include <QVariant>
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
}
3 changes: 3 additions & 0 deletions src/app/attributetable/qgsattributetablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 48 additions & 3 deletions src/app/attributetable/qgsattributetableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <QKeyEvent>
#include <QSettings>
#include <QHeaderView>
#include <QMenu>

#include "qgsattributetableview.h"
#include "qgsattributetablemodel.h"
Expand All @@ -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() );
Expand All @@ -41,7 +42,6 @@ QgsAttributeTableView::QgsAttributeTableView( QWidget* parent )
setSelectionBehavior( QAbstractItemView::SelectRows );
setSelectionMode( QAbstractItemView::NoSelection );
setSortingEnabled( true );

}

void QgsAttributeTableView::setLayer( QgsVectorLayer* layer )
Expand All @@ -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 );
}
24 changes: 24 additions & 0 deletions src/app/attributetable/qgsattributetableview.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
#define QGSATTRIBUTETABLEVIEW_H

#include <QTableView>
#include <QAction>

class QgsAttributeTableModel;
class QgsAttributeTableFilterModel;

class QgsVectorLayer;
class QMenu;


class QgsAttributeTableView: public QTableView
Expand All @@ -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.