QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 at opengis 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  static int sLayerCounter = 0;
31  mPyLayerVarName = QStringLiteral( "_qgis_layer_%1_%2" ).arg( form->layer()->id() ).arg( sLayerCounter++ );
32  mPyLayerVarName.replace( QRegExp( "[^a-zA-Z0-9_]" ), QStringLiteral( "_" ) ); // clean identifier
33 
34  QString initLayer = QStringLiteral( "%1 = sip.wrapinstance( %2, qgis.core.QgsVectorLayer )" )
35  .arg( mPyLayerVarName )
36  .arg( ( quint64 ) form->layer() );
37 
38  QgsPythonRunner::run( initLayer );
39 }
40 
42 {
43  QString delLayer = QStringLiteral( "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, &QDialogButtonBox::accepted, form(), &QgsAttributeForm::save );
55  QObject::connect( buttonBox, &QDialogButtonBox::accepted, form(), &QgsAttributeForm::save );
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.
60  QDateTime dt = QDateTime::currentDateTime();
61  QString pyFeatureVarName = QStringLiteral( "_qgis_feature_%1" ).arg( dt.toString( QStringLiteral( "yyyyMMddhhmmsszzz" ) ) );
62  QString initFeature = QStringLiteral( "%1 = sip.wrapinstance( %2, qgis.core.QgsFeature )" )
63  .arg( pyFeatureVarName )
64  .arg( ( quint64 ) & form()->feature() );
65 
66  QgsPythonRunner::run( initFeature );
67 
68  QString expr = QStringLiteral( "%1( %2, %3, %4)" )
69  .arg( mPyFunctionName,
70  mPyFormVarName,
71  mPyLayerVarName,
72  pyFeatureVarName );
73 
74  QgsPythonRunner::run( expr );
75 
76  QString delFeature = QStringLiteral( "del %1" ).arg( pyFeatureVarName );
77  QgsPythonRunner::run( delFeature );
78 }
QgsAttributeFormInterface
Definition: qgsattributeforminterface.h:29
qgsattributeform.h
QgsAttributeFormLegacyInterface::featureChanged
void featureChanged() override
Definition: qgsattributeformlegacyinterface.cpp:47
QgsAttributeFormLegacyInterface::~QgsAttributeFormLegacyInterface
~QgsAttributeFormLegacyInterface() override
Definition: qgsattributeformlegacyinterface.cpp:41
QgsAttributeForm::save
bool save()
Save all the values from the editors to the layer.
Definition: qgsattributeform.cpp:738
qgsattributeformlegacyinterface.h
QgsAttributeForm::layer
QgsVectorLayer * layer()
Returns the layer for which this form is shown.
Definition: qgsattributeform.h:129
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:148
QgsAttributeFormInterface::feature
const QgsFeature & feature()
Definition: qgsattributeforminterface.cpp:46
QgsPythonRunner::run
static bool run(const QString &command, const QString &messageOnError=QString())
Execute a Python statement.
Definition: qgspythonrunner.cpp:28
QgsAttributeFormLegacyInterface::QgsAttributeFormLegacyInterface
QgsAttributeFormLegacyInterface(const QString &function, const QString &pyFormName, QgsAttributeForm *form)
Definition: qgsattributeformlegacyinterface.cpp:25
QgsAttributeFormInterface::form
QgsAttributeForm * form()
Definition: qgsattributeforminterface.cpp:41
QgsAttributeForm
Definition: qgsattributeform.h:45
qgspythonrunner.h