QGIS API Documentation  2.8.2-Wien
 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 #include <QRegExp>
24 
25 QgsAttributeFormLegacyInterface::QgsAttributeFormLegacyInterface( const QString& function, const QString& pyFormName, QgsAttributeForm* form )
27  , mPyFunctionName( function )
28  , mPyFormVarName( pyFormName )
29 {
30  mPyLayerVarName = QString( "_qgis_layer_%1" ).arg( form->layer()->id() );
31  mPyLayerVarName.replace( QRegExp( "[^a-zA-Z0-9_]" ), "_" ); // clean identifier
32 
33  QString initLayer = QString( "%1 = sip.wrapinstance( %2, qgis.core.QgsVectorLayer )" )
34  .arg( mPyLayerVarName )
35  .arg(( unsigned long ) form->layer() );
36 
37  QgsPythonRunner::run( initLayer );
38 }
39 
41 {
42  QString delLayer = QString( "del %1" ).arg( mPyLayerVarName );
43  QgsPythonRunner::run( delLayer );
44 }
45 
47 {
48  QDialogButtonBox* buttonBox = form()->findChild<QDialogButtonBox*>();
49  if ( buttonBox )
50  {
51  // If the init function did not call disconnect, we do it here before reconnecting
52  // If it did call disconnect, then the call will just do nothing
53  QObject::disconnect( buttonBox, SIGNAL( accepted() ), form(), SLOT( accept() ) );
54  QObject::connect( buttonBox, SIGNAL( accepted() ), form(), SLOT( accept() ) );
55  }
56 
57  // Generate the unique ID of this feature. We used to use feature ID but some providers
58  // return a ID that is an invalid python variable when we have new unsaved features.
59  QDateTime dt = QDateTime::currentDateTime();
60  QString pyFeatureVarName = QString( "_qgis_feature_%1" ).arg( dt.toString( "yyyyMMddhhmmsszzz" ) );
61  QString initFeature = QString( "%1 = sip.wrapinstance( %2, qgis.core.QgsFeature )" )
62  .arg( pyFeatureVarName )
63  .arg(( unsigned long ) & form()->feature() );
64 
65  QgsPythonRunner::run( initFeature );
66 
67  QString expr = QString( "%1( %2, %3, %4)" )
68  .arg( mPyFunctionName )
69  .arg( mPyFormVarName )
70  .arg( mPyLayerVarName )
71  .arg( pyFeatureVarName );
72 
73  QgsPythonRunner::run( expr );
74 
75  QString delFeature = QString( "del %1" ).arg( pyFeatureVarName );
76  QgsPythonRunner::run( delFeature );
77 }