QGIS API Documentation  2.11.0-Master
qgsattributeformlegacyinterface.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsattributeformlegacyinterface.cpp
3  --------------------------------------
4  Date : 13.5.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias dot kuhn at gmx dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgspythonrunner.h"
19 #include "qgsattributeform.h"
20 
21 #include <QString>
22 #include <QDateTime>
23 #include <QRegExp>
24 
27  , mPyFunctionName( function )
28  , mPyFormVarName( pyFormName )
29 {
30  static int sLayerCounter = 0;
31  mPyLayerVarName = QString( "_qgis_layer_%1_%2" ).arg( form->layer()->id() ).arg( sLayerCounter++ );
32  mPyLayerVarName.replace( QRegExp( "[^a-zA-Z0-9_]" ), "_" ); // clean identifier
33 
34  QString initLayer = QString( "%1 = sip.wrapinstance( %2, qgis.core.QgsVectorLayer )" )
35  .arg( mPyLayerVarName )
36  .arg(( unsigned long ) form->layer() );
37 
38  QgsPythonRunner::run( initLayer );
39 }
40 
42 {
43  QString delLayer = QString( "del %1" ).arg( mPyLayerVarName );
44  QgsPythonRunner::run( delLayer );
45 }
46 
48 {
49  QDialogButtonBox* buttonBox = form()->findChild<QDialogButtonBox*>();
50  if ( buttonBox )
51  {
52  // If the init function did not call disconnect, we do it here before reconnecting
53  // If it did call disconnect, then the call will just do nothing
54  QObject::disconnect( buttonBox, SIGNAL( accepted() ), form(), SLOT( accept() ) );
55  QObject::connect( buttonBox, SIGNAL( accepted() ), form(), SLOT( accept() ) );
56  }
57 
58  // Generate the unique ID of this feature. We used to use feature ID but some providers
59  // return a ID that is an invalid python variable when we have new unsaved features.
61  QString pyFeatureVarName = QString( "_qgis_feature_%1" ).arg( dt.toString( "yyyyMMddhhmmsszzz" ) );
62  QString initFeature = QString( "%1 = sip.wrapinstance( %2, qgis.core.QgsFeature )" )
63  .arg( pyFeatureVarName )
64  .arg(( unsigned long ) & form()->feature() );
65 
66  QgsPythonRunner::run( initFeature );
67 
68  QString expr = QString( "%1( %2, %3, %4)" )
69  .arg( mPyFunctionName )
70  .arg( mPyFormVarName )
71  .arg( mPyLayerVarName )
72  .arg( pyFeatureVarName );
73 
74  QgsPythonRunner::run( expr );
75 
76  QString delFeature = QString( "del %1" ).arg( pyFeatureVarName );
77  QgsPythonRunner::run( delFeature );
78 }
QString toString(Qt::DateFormat format) const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
static bool run(QString command, QString messageOnError=QString())
Execute a python statement.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
Definition: qgsmaplayer.cpp:99
QgsAttributeFormLegacyInterface(const QString &function, const QString &pyFormName, QgsAttributeForm *form)
QString & replace(int position, int n, QChar after)
QDateTime currentDateTime()
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
T findChild(const QString &name) const