QGIS API Documentation  2.5.0-Master
 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  // If the init function did not call disconnect, we do it here before reconnecting
50  // If it did call disconnect, then the call will just do nothing
51  QObject::disconnect( buttonBox, SIGNAL( accepted() ), form(), SLOT( accept() ) );
52  QObject::connect( buttonBox, SIGNAL( accepted() ), form(), SLOT( accept() ) );
53  }
54 
55  // Generate the unique ID of this feature. We used to use feature ID but some providers
56  // return a ID that is an invalid python variable when we have new unsaved features.
57  QDateTime dt = QDateTime::currentDateTime();
58  QString pyFeatureVarName = QString( "_qgis_feature_%1" ).arg( dt.toString( "yyyyMMddhhmmsszzz" ) );
59  QString initFeature = QString( "%1 = sip.wrapinstance( %2, qgis.core.QgsFeature )" )
60  .arg( pyFeatureVarName )
61  .arg(( unsigned long ) & form()->feature() );
62 
63  QgsPythonRunner::run( initFeature );
64 
65  QString expr = QString( "%1( %2, %3, %4)" )
66  .arg( mPyFunctionName )
67  .arg( mPyFormVarName )
68  .arg( mPyLayerVarName )
69  .arg( pyFeatureVarName );
70 
71  QgsPythonRunner::run( expr );
72 
73  QString delFeature = QString( "del %1" ).arg( pyFeatureVarName );
74  QgsPythonRunner::run( delFeature );
75 }
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:95
QgsAttributeFormLegacyInterface(const QString &function, const QString &pyFormName, QgsAttributeForm *form)