QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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.arg( mPyLayerVarName ).arg( ( quint64 ) form->layer() );
38
39 QgsPythonRunner::run( initLayer );
40}
41
43{
44 const QString delLayer = u"del %1"_s.arg( mPyLayerVarName );
45 QgsPythonRunner::run( delLayer );
46}
47
49{
50 QDialogButtonBox *buttonBox = form()->findChild<QDialogButtonBox *>();
51 if ( buttonBox )
52 {
53 // If the init function did not call disconnect, we do it here before reconnecting
54 // If it did call disconnect, then the call will just do nothing
55 QObject::disconnect( buttonBox, &QDialogButtonBox::accepted, form(), &QgsAttributeForm::save );
56 QObject::connect( buttonBox, &QDialogButtonBox::accepted, form(), &QgsAttributeForm::save );
57 }
58
59 // Generate the unique ID of this feature. We used to use feature ID but some providers
60 // return a ID that is an invalid python variable when we have new unsaved features.
61 const QDateTime dt = QDateTime::currentDateTime();
62 const QString pyFeatureVarName = u"_qgis_feature_%1"_s.arg( dt.toString( u"yyyyMMddhhmmsszzz"_s ) );
63 const QString initFeature = u"%1 = sip.wrapinstance( %2, qgis.core.QgsFeature )"_s.arg( pyFeatureVarName ).arg( ( quint64 ) &form()->feature() );
64
65 QgsPythonRunner::run( initFeature );
66
67 const QString expr = u"%1( %2, %3, %4)"_s.arg( mPyFunctionName, mPyFormVarName, mPyLayerVarName, pyFeatureVarName );
68
70
71 const QString delFeature = u"del %1"_s.arg( pyFeatureVarName );
72 QgsPythonRunner::run( delFeature );
73}
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.