-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
307 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/*************************************************************************** | ||
qgsactionmenu.cpp | ||
-------------------------------------- | ||
Date : 11.8.2014 | ||
Copyright : (C) 2014 Matthias Kuhn | ||
Email : matthias dot kuhn at gmx dot ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsactionmenu.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
#include <QMenuItem> | ||
|
||
QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeature* feature, QWidget* parent ) | ||
: QMenu( parent ) | ||
, mLayer( layer ) | ||
, mAttributeActionSignalMapper( 0 ) | ||
, mMapLayerActionSignalMapper( 0 ) | ||
, mActions( 0 ) | ||
, mFeature( feature ) | ||
, mOwnsFeature( false ) | ||
{ | ||
init(); | ||
} | ||
|
||
QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeatureId fid, QWidget* parent ) | ||
: QMenu( parent ) | ||
, mLayer( layer ) | ||
, mAttributeActionSignalMapper( 0 ) | ||
, mMapLayerActionSignalMapper( 0 ) | ||
, mActions( 0 ) | ||
, mFeature( 0 ) | ||
, mFeatureId( fid ) | ||
, mOwnsFeature( false ) | ||
{ | ||
init(); | ||
} | ||
|
||
void QgsActionMenu::init() | ||
{ | ||
setTitle( tr( "&Actions" ) ); | ||
|
||
connect( QgsMapLayerActionRegistry::instance(), SIGNAL(changed()), this, SLOT(reloadActions()) ); | ||
|
||
reloadActions(); | ||
} | ||
|
||
const QgsFeature* QgsActionMenu::feature() | ||
{ | ||
if ( !mFeature ) | ||
{ | ||
QgsFeature* feat = new QgsFeature(); | ||
if ( mActions->layer()->getFeatures( QgsFeatureRequest( mFeatureId ) ).nextFeature( *feat ) ) | ||
{ | ||
mFeature = feat; | ||
mOwnsFeature = true; | ||
} | ||
else | ||
{ | ||
delete feat; | ||
} | ||
} | ||
|
||
return mFeature; | ||
} | ||
|
||
QgsActionMenu::~QgsActionMenu() | ||
{ | ||
delete mActions; | ||
|
||
if ( mOwnsFeature ) | ||
delete mFeature; | ||
} | ||
|
||
void QgsActionMenu::setFeature( QgsFeature* feature ) | ||
{ | ||
if ( mOwnsFeature ) | ||
delete mFeature; | ||
mOwnsFeature = false; | ||
mFeature = feature; | ||
} | ||
|
||
void QgsActionMenu::triggerAttributeAction( int index ) | ||
{ | ||
if ( feature() ) | ||
{ | ||
mActions->doAction( index, *feature() ); | ||
} | ||
else | ||
{ | ||
QgsDebugMsg( QString( "Trying to run an action on a non-existing feature with fid %1" ).arg( mFeatureId ) ); | ||
} | ||
} | ||
|
||
void QgsActionMenu::triggerMapLayerAction( int index ) | ||
{ | ||
if ( feature() ) | ||
{ | ||
QgsMapLayerAction* action = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer ).at( index ); | ||
|
||
action->triggerForFeature( mLayer, feature() ); | ||
} | ||
} | ||
|
||
void QgsActionMenu::reloadActions() | ||
{ | ||
delete mAttributeActionSignalMapper; | ||
mAttributeActionSignalMapper = new QSignalMapper( this ); | ||
delete mMapLayerActionSignalMapper; | ||
mMapLayerActionSignalMapper = new QSignalMapper( this ); | ||
|
||
connect( mAttributeActionSignalMapper, SIGNAL(mapped(int)), this, SLOT(triggerAttributeAction(int)) ); | ||
connect( mMapLayerActionSignalMapper, SIGNAL(mapped(int)), this, SLOT(triggerMapLayerAction(int)) ); | ||
|
||
delete mActions; | ||
mActions = new QgsAttributeAction( *mLayer->actions() ); | ||
|
||
for( int idx = 0; idx < mActions->size(); ++idx ) | ||
{ | ||
const QgsAction& qaction( mActions->at( idx ) ); | ||
|
||
QAction* action = new QAction( qaction.name(), this ); | ||
|
||
// Only enable items on supported platforms | ||
if ( !qaction.runable() ) | ||
{ | ||
action->setEnabled( false ); | ||
action->setToolTip( tr( "Not supported on your platform" ) ); | ||
} | ||
else | ||
{ | ||
action->setToolTip( qaction.action() ); | ||
} | ||
|
||
mAttributeActionSignalMapper->setMapping( action, idx ); | ||
|
||
connect( action, SIGNAL(triggered()), mAttributeActionSignalMapper, SLOT(map()) ); | ||
|
||
addAction( action ); | ||
} | ||
|
||
QList<QgsMapLayerAction*> mapLayerActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer ); | ||
|
||
if ( mapLayerActions.size() > 0 ) | ||
{ | ||
//add a separator between user defined and standard actions | ||
addSeparator(); | ||
|
||
for ( int i = 0; i < mapLayerActions.size(); ++i ) | ||
{ | ||
QgsMapLayerAction* qaction = mapLayerActions.at( i ); | ||
QAction* action = new QAction( qaction->text(), this ); | ||
mMapLayerActionSignalMapper->setMapping( action, i ); | ||
addAction( action ); | ||
connect( action, SIGNAL(triggered()), mMapLayerActionSignalMapper, SLOT(map()) ); | ||
} | ||
} | ||
|
||
emit reinit(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/*************************************************************************** | ||
qgsactionmenu.h | ||
-------------------------------------- | ||
Date : 11.8.2014 | ||
Copyright : (C) 2014 Matthias Kuhn | ||
Email : matthias dot kuhn at gmx dot ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSACTIONMENU_H | ||
#define QGSACTIONMENU_H | ||
|
||
#include <QMenu> | ||
#include <QSignalMapper> | ||
|
||
#include "qgsattributeaction.h" | ||
#include "qgsmaplayeractionregistry.h" | ||
|
||
/** | ||
* This class is a menu that is populated automatically with the actions defined for a given layer. | ||
*/ | ||
|
||
class QgsActionMenu : public QMenu | ||
{ | ||
Q_OBJECT | ||
public: | ||
/** | ||
* Constructs a new QgsActionMenu | ||
* | ||
* @param layer The layer that this action will be run upon. | ||
* @param feature The feature that this action will be run upon. Make sure that this feature is available | ||
* for the lifetime of this object. | ||
* @param parent The usual QWidget parent. | ||
*/ | ||
explicit QgsActionMenu( QgsVectorLayer* layer, const QgsFeature* feature, QWidget* parent = 0 ); | ||
|
||
/** | ||
* Constructs a new QgsActionMenu | ||
* | ||
* @param layer The layer that this action will be run upon. | ||
* @param fid The feature id of the feature for which this action will be run. | ||
* @param parent The usual QWidget parent. | ||
*/ | ||
explicit QgsActionMenu( QgsVectorLayer* layer, const QgsFeatureId fid, QWidget* parent = 0 ); | ||
|
||
/** | ||
* Destructor | ||
*/ | ||
~QgsActionMenu(); | ||
|
||
/** | ||
* Change the feature on which actions are performed | ||
* | ||
* @param feature A feature. Will not take ownership. It's the callers responsibility to keep the feature | ||
* as long as the menu is displayed and the action is running. | ||
*/ | ||
void setFeature( QgsFeature* feature ); | ||
|
||
/** | ||
* @brief setFeature | ||
* @param feature | ||
*/ | ||
void setFeature( QgsFeatureId feature ); | ||
|
||
private slots: | ||
void triggerAttributeAction( int index ); | ||
void triggerMapLayerAction( int index ); | ||
void reloadActions(); | ||
|
||
signals: | ||
void reinit(); | ||
|
||
private: | ||
void init(); | ||
const QgsFeature* feature(); | ||
|
||
QgsVectorLayer* mLayer; | ||
QSignalMapper* mAttributeActionSignalMapper; | ||
QSignalMapper* mMapLayerActionSignalMapper; | ||
QgsAttributeAction* mActions; | ||
const QgsFeature* mFeature; | ||
QgsFeatureId mFeatureId; | ||
bool mOwnsFeature; | ||
}; | ||
|
||
|
||
#endif // QGSACTIONMENU_H |
Oops, something went wrong.