QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
24 QgsAttributeFormLegacyInterface::QgsAttributeFormLegacyInterface( const QString& function, const QString& pyFormName, QgsAttributeForm* form )
26  , mPyFunctionName( function )
27  , mPyFormVarName( pyFormName )
28 {
29  mPyLayerVarName = QString( "_qgis_layer_%1" ).arg( form->layer()->id() );
30 
31  QString initLayer = QString( "%1 = sip.wrapinstance( %2, qgis.core.QgsVectorLayer )" )
32  .arg( mPyLayerVarName )
33  .arg(( unsigned long ) form->layer() );
34 
35  QgsPythonRunner::run( initLayer );
36 }
37 
39 {
40  QString delLayer = QString( "del %1" ).arg( mPyLayerVarName );
41  QgsPythonRunner::run( delLayer );
42 }
43 
45 {
46  QDialogButtonBox* buttonBox = form()->findChild<QDialogButtonBox*>();
47  if ( buttonBox )
48  {
49  QObject::connect( buttonBox, SIGNAL( accepted() ), form(), SLOT( accept() ) );
50  }
51 
52  // Generate the unique ID of this feature. We used to use feature ID but some providers
53  // return a ID that is an invalid python variable when we have new unsaved features.
54  QDateTime dt = QDateTime::currentDateTime();
55  QString pyFeatureVarName = QString( "_qgis_feature_%1" ).arg( dt.toString( "yyyyMMddhhmmsszzz" ) );
56  QString initFeature = QString( "%1 = sip.wrapinstance( %2, qgis.core.QgsFeature )" )
57  .arg( pyFeatureVarName )
58  .arg(( unsigned long ) & form()->feature() );
59 
60  QgsPythonRunner::run( initFeature );
61 
62  QString expr = QString( "%1( %2, %3, %4)" )
63  .arg( mPyFunctionName )
64  .arg( mPyFormVarName )
65  .arg( mPyLayerVarName )
66  .arg( pyFeatureVarName );
67 
68  QgsPythonRunner::run( expr );
69 
70  QString delFeature = QString( "del %1" ).arg( pyFeatureVarName );
71  QgsPythonRunner::run( delFeature );
72 }
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:92
QgsAttributeFormLegacyInterface(const QString &function, const QString &pyFormName, QgsAttributeForm *form)