QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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 "qgsattributeform.h"
19#include "qgspythonrunner.h"
20
21#include <QDateTime>
22#include <QRegularExpression>
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
27QgsAttributeFormLegacyInterface::QgsAttributeFormLegacyInterface( const QString &function, const QString &pyFormName, QgsAttributeForm *form )
29 , mPyFunctionName( function )
30 , mPyFormVarName( pyFormName )
31{
32 static int sLayerCounter = 0;
33 mPyLayerVarName = u"_qgis_layer_%1_%2"_s.arg( form->layer()->id() ).arg( sLayerCounter++ );
34 const thread_local QRegularExpression reClean( QRegularExpression( "[^a-zA-Z0-9_]" ) );
35 mPyLayerVarName.replace( reClean, u"_"_s ); // clean identifier
36
37 const QString initLayer = u"%1 = sip.wrapinstance( %2, qgis.core.QgsVectorLayer )"_s
38 .arg( mPyLayerVarName )
39 .arg( ( quint64 ) form->layer() );
40
41 QgsPythonRunner::run( initLayer );
42}
43
45{
46 const QString delLayer = u"del %1"_s.arg( mPyLayerVarName );
47 QgsPythonRunner::run( delLayer );
48}
49
51{
52 QDialogButtonBox *buttonBox = form()->findChild<QDialogButtonBox *>();
53 if ( buttonBox )
54 {
55 // If the init function did not call disconnect, we do it here before reconnecting
56 // If it did call disconnect, then the call will just do nothing
57 QObject::disconnect( buttonBox, &QDialogButtonBox::accepted, form(), &QgsAttributeForm::save );
58 QObject::connect( buttonBox, &QDialogButtonBox::accepted, form(), &QgsAttributeForm::save );
59 }
60
61 // Generate the unique ID of this feature. We used to use feature ID but some providers
62 // return a ID that is an invalid python variable when we have new unsaved features.
63 const QDateTime dt = QDateTime::currentDateTime();
64 const QString pyFeatureVarName = u"_qgis_feature_%1"_s.arg( dt.toString( u"yyyyMMddhhmmsszzz"_s ) );
65 const QString initFeature = u"%1 = sip.wrapinstance( %2, qgis.core.QgsFeature )"_s
66 .arg( pyFeatureVarName )
67 .arg( ( quint64 ) &form()->feature() );
68
69 QgsPythonRunner::run( initFeature );
70
71 const QString expr = u"%1( %2, %3, %4)"_s
72 .arg( mPyFunctionName, mPyFormVarName, mPyLayerVarName, pyFeatureVarName );
73
75
76 const QString delFeature = u"del %1"_s.arg( pyFeatureVarName );
77 QgsPythonRunner::run( delFeature );
78}
QgsAttributeForm * form() const
Returns the attribute form.
QgsAttributeFormInterface(QgsAttributeForm *form)
const QgsFeature & feature() const
Returns the current feature for the attribute form.
QgsAttributeFormLegacyInterface(const QString &function, const QString &pyFormName, QgsAttributeForm *form)
The attribute form widget for vector layer features.
bool save()
Save all the values from the editors to the layer.
static bool run(const QString &command, const QString &messageOnError=QString())
Execute a Python statement.