Skip to content

Commit 81366dd

Browse files
author
jef
committed
[FEATHRE] attribute action improvements
- add python bindings for attribute actions - support actions as context menu in feature form (ui) - add action types: * generic: commands that should work on all platforms * windows, mac, unix: commands that should work and are only shown on one platform respectively (eg. vim on unix, notepad on windows and textmate on Mac) * python: python strings to be executed instead of a command (eg. qgis.utils.plugins['apluginname'].amethod('[%someattribute%]')) [BUGFIXES] - ogr support for relative filenames fixed - relative filename support also for feature form uis git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12113 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 3126e7b commit 81366dd

15 files changed

+407
-254
lines changed

python/core/core.sip

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
%Include qgssymbologyutils.sip
6969
%Include qgstolerance.sip
7070
%Include qgsuniquevaluerenderer.sip
71+
%Include qgsattributeaction.sip
7172
%Include qgsvectordataprovider.sip
7273
%Include qgsvectorfilewriter.sip
7374
%Include qgsvectorlayer.sip

python/core/qgsattributeaction.sip

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class QgsAction
2+
{
3+
%TypeHeaderCode
4+
#include "qgsattributeaction.h"
5+
%End
6+
7+
public:
8+
enum ActionType
9+
{
10+
Generic,
11+
GenericPython,
12+
Mac,
13+
Windows,
14+
Unix,
15+
};
16+
17+
QgsAction( ActionType type, QString name, QString action, bool capture );
18+
19+
//! The name of the action
20+
QString name() const;
21+
22+
//! The action
23+
QString action() const;
24+
25+
//! The action type
26+
ActionType type() const;
27+
28+
//! Whether to capture output for display when this action is run
29+
bool capture() const;
30+
31+
bool runable() const;
32+
};
33+
34+
class QgsAttributeAction
35+
{
36+
%TypeHeaderCode
37+
#include "qgsattributeaction.h"
38+
%End
39+
public:
40+
QgsAttributeAction();
41+
42+
//! Destructor
43+
virtual ~QgsAttributeAction();
44+
45+
//! Add an action with the given name and action details.
46+
// Will happily have duplicate names and actions. If
47+
// capture is true, when running the action using doAction(),
48+
// any stdout from the process will be captured and displayed in a
49+
// dialog box.
50+
void addAction( QgsAction::ActionType type, QString name, QString action, bool capture = false );
51+
52+
/*
53+
//! Does the action using the given values. defaultValueIndex is an
54+
// index into values which indicates which value in the values vector
55+
// is to be used if the action has a default placeholder.
56+
void doAction( int index, const QList< QPair<QString, QString> > &values,
57+
int defaultValueIndex = 0, void *executePython = 0 );
58+
*/
59+
60+
//! Removes all actions
61+
void clearActions();
62+
63+
//! Expands the given action, replacing all %'s with the value as
64+
// given.
65+
static QString expandAction( QString action, const QList< QPair<QString, QString> > &values,
66+
uint defaultValueIndex );
67+
68+
//! Writes the actions out in XML format
69+
bool writeXML( QDomNode& layer_node, QDomDocument& doc ) const;
70+
71+
//! Reads the actions in in XML format
72+
bool readXML( const QDomNode& layer_node );
73+
74+
//! interface to inherited methods from QList<QgsAction>
75+
const QgsAction &at( int idx );
76+
const int size();
77+
};

python/core/qgsvectorlayer.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public:
6363

6464
QgsLabel *label();
6565

66-
// TODO: wrap QgsAttributeAction* actions();
66+
QgsAttributeAction *actions();
6767

6868
/** The number of features that are selected in this layer */
6969
int selectedFeatureCount();

src/app/qgisapp.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -6397,3 +6397,11 @@ void QgisApp::updateUndoActions()
63976397
mActionUndo->setEnabled( canUndo );
63986398
mActionRedo->setEnabled( canRedo );
63996399
}
6400+
6401+
void QgisApp::runPythonString( const QString &expr )
6402+
{
6403+
if ( mPythonUtils )
6404+
{
6405+
mPythonUtils->runStringUnsafe( expr );
6406+
}
6407+
}

src/app/qgisapp.h

+3
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ class QgisApp : public QMainWindow
340340
QToolBar *pluginToolBar() { return mPluginToolBar; }
341341
QToolBar *helpToolBar() { return mHelpToolBar; }
342342

343+
//! run python
344+
void runPythonString( const QString &expr );
345+
343346
public slots:
344347
//! Zoom to full extent
345348
void zoomFull();

src/app/qgsattributeactiondialog.cpp

+25-21
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,26 @@ void QgsAttributeActionDialog::init()
6868
attributeActionTable->setRowCount( 0 );
6969

7070
// Populate with our actions.
71-
QgsAttributeAction::AttributeActions::const_iterator
72-
iter = mActions->begin();
73-
int i = 0;
74-
for ( ; iter != mActions->end(); ++iter, ++i )
71+
for ( int i = 0; i < mActions->size(); i++ )
7572
{
76-
insertRow( i, iter->name(), iter->action(), iter->capture() );
73+
const QgsAction action = ( *mActions )[i];
74+
insertRow( i, action.type(), action.name(), action.action(), action.capture() );
7775
}
7876
}
7977

80-
void QgsAttributeActionDialog::insertRow( int row, const QString &name, const QString &action, bool capture )
78+
void QgsAttributeActionDialog::insertRow( int row, QgsAction::ActionType type, const QString &name, const QString &action, bool capture )
8179
{
80+
QTableWidgetItem* item;
8281
attributeActionTable->insertRow( row );
83-
attributeActionTable->setItem( row, 0, new QTableWidgetItem( name ) );
84-
attributeActionTable->setItem( row, 1, new QTableWidgetItem( action ) );
85-
QTableWidgetItem* item = new QTableWidgetItem();
82+
item = new QTableWidgetItem( actionType->itemText( type ) );
83+
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
84+
attributeActionTable->setItem( row, 0, item );
85+
attributeActionTable->setItem( row, 1, new QTableWidgetItem( name ) );
86+
attributeActionTable->setItem( row, 2, new QTableWidgetItem( action ) );
87+
item = new QTableWidgetItem();
8688
item->setFlags( item->flags() & ~( Qt::ItemIsEditable | Qt::ItemIsUserCheckable ) );
8789
item->setCheckState( capture ? Qt::Checked : Qt::Unchecked );
88-
attributeActionTable->setItem( row, 2, item );
90+
attributeActionTable->setItem( row, 3, item );
8991
}
9092

9193
void QgsAttributeActionDialog::moveUp()
@@ -208,15 +210,15 @@ void QgsAttributeActionDialog::insert( int pos )
208210
if ( pos >= numRows )
209211
{
210212
// Expand the table to have a row with index pos
211-
insertRow( pos, name, actionAction->text(), captureCB->isChecked() );
213+
insertRow( pos, ( QgsAction::ActionType ) actionType->currentIndex(), name, actionAction->text(), captureCB->isChecked() );
212214
}
213215
else
214216
{
215217
// Update existing row
216-
attributeActionTable->item( pos, 0 )->setText( name );
217-
attributeActionTable->item( pos, 1 )->setText( actionAction->text() );
218-
attributeActionTable->item( pos, 2 )->setCheckState(
219-
captureCB->isChecked() ? Qt::Checked : Qt::Unchecked );
218+
attributeActionTable->item( pos, 0 )->setText( actionType->currentText() );
219+
attributeActionTable->item( pos, 1 )->setText( name );
220+
attributeActionTable->item( pos, 2 )->setText( actionAction->text() );
221+
attributeActionTable->item( pos, 3 )->setCheckState( captureCB->isChecked() ? Qt::Checked : Qt::Unchecked );
220222
}
221223
}
222224
}
@@ -253,12 +255,13 @@ void QgsAttributeActionDialog::apply()
253255
mActions->clearActions();
254256
for ( int i = 0; i < attributeActionTable->rowCount(); ++i )
255257
{
256-
const QString &name = attributeActionTable->item( i, 0 )->text();
257-
const QString &action = attributeActionTable->item( i, 1 )->text();
258+
const QgsAction::ActionType type = ( QgsAction::ActionType ) actionType->findText( attributeActionTable->item( i, 0 )->text() );
259+
const QString &name = attributeActionTable->item( i, 1 )->text();
260+
const QString &action = attributeActionTable->item( i, 2 )->text();
258261
if ( !name.isEmpty() && !action.isEmpty() )
259262
{
260-
QTableWidgetItem *item = attributeActionTable->item( i, 2 );
261-
mActions->addAction( name, action, item->checkState() == Qt::Checked );
263+
QTableWidgetItem *item = attributeActionTable->item( i, 3 );
264+
mActions->addAction( type, name, action, item->checkState() == Qt::Checked );
262265
}
263266
}
264267
}
@@ -292,8 +295,9 @@ void QgsAttributeActionDialog::rowSelected( int row )
292295
if ( item )
293296
{
294297
// Only if a populated row was selected
295-
actionName->setText( attributeActionTable->item( row, 0 )->text() );
296-
actionAction->setText( attributeActionTable->item( row, 1 )->text() );
298+
actionType->setCurrentIndex( actionType->findText( attributeActionTable->item( row, 0 )->text() ) );
299+
actionName->setText( attributeActionTable->item( row, 1 )->text() );
300+
actionAction->setText( attributeActionTable->item( row, 2 )->text() );
297301
captureCB->setChecked( item->checkState() == Qt::Checked );
298302
}
299303
}

src/app/qgsattributeactiondialog.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ back to QgsVectorLayer.
2525
#define QGSATTRIBUTEACTIONDIALOG_H
2626

2727
#include "ui_qgsattributeactiondialogbase.h"
28+
#include "qgsattributeaction.h"
2829
#include "qgsfield.h"
2930
#include <QMap>
3031

@@ -56,7 +57,7 @@ class QgsAttributeActionDialog: public QWidget, private Ui::QgsAttributeActionDi
5657

5758
private:
5859

59-
void insertRow( int row, const QString &name, const QString &action, bool capture );
60+
void insertRow( int row, QgsAction::ActionType type, const QString &name, const QString &action, bool capture );
6061
void swapRows( int row1, int row2 );
6162

6263
void insert( int pos );
@@ -66,7 +67,7 @@ class QgsAttributeActionDialog: public QWidget, private Ui::QgsAttributeActionDi
6667
QString uniqueName( QString name );
6768

6869
// Pointer to the QgsAttributeAction in the class that created us.
69-
QgsAttributeAction* mActions;
70+
QgsAttributeAction *mActions;
7071
};
7172

7273
#endif

src/app/qgsattributedialog.h

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class QgsAttributeDialog : public QObject
4545
*/
4646
void restoreGeometry();
4747

48+
QDialog *dialog() { return mDialog; }
49+
4850
public slots:
4951
/** Overloaded accept method which will write the feature field
5052
* values, then delegate to QDialog::accept()

0 commit comments

Comments
 (0)