[QGIS Commit] r12639 - in trunk/qgis/src/app: . attributetable

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Dec 28 16:32:10 EST 2009


Author: jef
Date: 2009-12-28 16:32:09 -0500 (Mon, 28 Dec 2009)
New Revision: 12639

Modified:
   trunk/qgis/src/app/CMakeLists.txt
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.h
   trunk/qgis/src/app/attributetable/qgsattributetableview.cpp
   trunk/qgis/src/app/attributetable/qgsattributetableview.h
Log:
fix #2019

Modified: trunk/qgis/src/app/CMakeLists.txt
===================================================================
--- trunk/qgis/src/app/CMakeLists.txt	2009-12-28 20:29:34 UTC (rev 12638)
+++ trunk/qgis/src/app/CMakeLists.txt	2009-12-28 21:32:09 UTC (rev 12639)
@@ -192,6 +192,7 @@
   ogr/qgsopenvectorlayerdialog.h
   ogr/qgsnewogrconnection.h
   
+  attributetable/qgsattributetableview.h
   attributetable/qgsattributetablemodel.h
   attributetable/qgsattributetablememorymodel.h
   attributetable/qgsattributetabledialog.h

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2009-12-28 20:29:34 UTC (rev 12638)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2009-12-28 21:32:09 UTC (rev 12639)
@@ -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 @@
       return QVariant( attributeName );
     }
   }
-  else return QVariant();
+  else
+  {
+    return QVariant();
+  }
 }
 
 void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
@@ -487,3 +492,22 @@
   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 );
+}

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.h	2009-12-28 20:29:34 UTC (rev 12638)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.h	2009-12-28 21:32:09 UTC (rev 12639)
@@ -125,6 +125,9 @@
      */
     QgsVectorLayer* layer() const { return mLayer; }
 
+    /** Execute an action */
+    void executeAction( int action, const QModelIndex &idx ) const;
+
   signals:
     /**
      * Model has been changed

Modified: trunk/qgis/src/app/attributetable/qgsattributetableview.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetableview.cpp	2009-12-28 20:29:34 UTC (rev 12638)
+++ trunk/qgis/src/app/attributetable/qgsattributetableview.cpp	2009-12-28 21:32:09 UTC (rev 12639)
@@ -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 @@
   setSelectionBehavior( QAbstractItemView::SelectRows );
   setSelectionMode( QAbstractItemView::NoSelection );
   setSortingEnabled( true );
-
 }
 
 void QgsAttributeTableView::setLayer( QgsVectorLayer* layer )
@@ -64,6 +64,7 @@
 {
   delete mModel;
   delete mFilterModel;
+  delete mActionPopup;
 }
 
 void QgsAttributeTableView::closeEvent( QCloseEvent *event )
@@ -71,3 +72,47 @@
   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 );
+}

Modified: trunk/qgis/src/app/attributetable/qgsattributetableview.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetableview.h	2009-12-28 20:29:34 UTC (rev 12638)
+++ trunk/qgis/src/app/attributetable/qgsattributetableview.h	2009-12-28 21:32:09 UTC (rev 12639)
@@ -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 @@
      */
     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



More information about the QGIS-commit mailing list