Skip to content

Commit

Permalink
add support for a python feature form initialization function
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@12215 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 21, 2009
1 parent c59f11c commit 865fce9
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 28 deletions.
10 changes: 10 additions & 0 deletions python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ public:
*/
void setEditForm( QString ui );

/** get edit form init function
@note added in 1.4
*/
QString editFormInit();

/** set edit form init function
@note added in 1.4
*/
void setEditFormInit( QString function );

/**access value map*/
QMap<QString, QVariant> &valueMap(int idx);

Expand Down
61 changes: 55 additions & 6 deletions src/app/qgsattributedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "qgssymbol.h"
#include "qgsattributeeditor.h"

#include "qgisapp.h"

#include <QTableWidgetItem>
#include <QSettings>
#include <QLabel>
Expand Down Expand Up @@ -187,11 +189,34 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
}

connect( buttonBox, SIGNAL( rejected() ), mDialog, SLOT( reject() ) );
connect( buttonBox, SIGNAL( rejected() ), this, SLOT( rejected() ) );
}

QMetaObject::connectSlotsByName( mDialog );

connect( mDialog, SIGNAL( destroyed() ), this, SLOT( dialogDestroyed() ) );

if ( !vl->editFormInit().isEmpty() )
{
#if 0
// would be nice if only PyQt's QVariant.toPyObject() wouldn't take ownership
vl->setProperty( "featureForm.dialog", QVariant::fromValue( qobject_cast<QObject*>( mDialog ) ) );
vl->setProperty( "featureForm.id", QVariant( mpFeature->id() ) );
#endif

QString module = vl->editFormInit();
int pos = module.lastIndexOf( "." );
if ( pos >= 0 )
{
QgisApp::instance()->runPythonString( QString( "import %1" ).arg( module.left( pos ) ) );
}

QgisApp::instance()->runPythonString( QString( "_qgis_featureform_%1 = wrapinstance( %2, QtGui.QDialog )" ).arg( mLayer->getLayerID() ).arg(( unsigned long ) mDialog ) );

QString expr = QString( "%1(_qgis_featureform_%2,'%2',%3)" ).arg( vl->editFormInit() ).arg( vl->getLayerID() ).arg( mpFeature->id() );
QgsDebugMsg( QString( "running featureForm init: %1" ).arg( expr ) );
QgisApp::instance()->runPythonString( expr );
}

restoreGeometry();
}

Expand Down Expand Up @@ -223,17 +248,41 @@ void QgsAttributeDialog::accept()

int QgsAttributeDialog::exec()
{
return mDialog->exec();
if ( mDialog )
{
return mDialog->exec();
}
else
{
QgsDebugMsg( "No dialog" );
return QDialog::Rejected;
}
}

void QgsAttributeDialog::saveGeometry()
{
QSettings settings;
settings.setValue( mSettingsPath + "geometry", mDialog->saveGeometry() );
if ( mDialog )
{
QSettings settings;
settings.setValue( mSettingsPath + "geometry", mDialog->saveGeometry() );
}
}

void QgsAttributeDialog::restoreGeometry()
{
QSettings settings;
mDialog->restoreGeometry( settings.value( mSettingsPath + "geometry" ).toByteArray() );
if ( mDialog )
{
QSettings settings;
mDialog->restoreGeometry( settings.value( mSettingsPath + "geometry" ).toByteArray() );
}
}

void QgsAttributeDialog::dialogDestroyed()
{
#if 0
mLayer->setProperty( "featureForm.dialog", QVariant() );
mLayer->setProperty( "featureForm.id", QVariant() );
#endif
QgisApp::instance()->runPythonString( QString( "del _qgis_featureform_%1" ).arg( mLayer->getLayerID() ) );
mDialog = NULL;
}
2 changes: 2 additions & 0 deletions src/app/qgsattributedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class QgsAttributeDialog : public QObject

int exec();

void dialogDestroyed();

private:

QDialog *mDialog;
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsattributeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectFileName() ) );
}
break;

case QgsVectorLayer::Immutable:
return NULL;

}

if ( editType == QgsVectorLayer::Immutable )
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
leSpatialRefSys->setCursorPosition( 0 );

leEditForm->setText( layer->editForm() );
leEditFormInit->setText( layer->editFormInit() );

connect( sliderTransparency, SIGNAL( valueChanged( int ) ), this, SLOT( sliderTransparency_valueChanged( int ) ) );

Expand Down Expand Up @@ -568,6 +569,7 @@ void QgsVectorLayerProperties::apply()
layer->setDisplayField( displayFieldComboBox->currentText() );

layer->setEditForm( leEditForm->text() );
layer->setEditFormInit( leEditFormInit->text() );

actionDialog->apply();

Expand Down
29 changes: 25 additions & 4 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
mEditable( false ),
mModified( false ),
mMaxUpdatedIndex( -1 ),
mActiveCommand( NULL ),
mRenderer( 0 ),
mRendererV2( NULL ),
mUsingRendererV2( false ),
mLabel( 0 ),
mLabelOn( false ),
mActiveCommand( NULL ),
mVertexMarkerOnlyForSelection( false ),
mFetching( false ),
mRendererV2( NULL ),
mUsingRendererV2( false )
mFetching( false )
{
mActions = new QgsAttributeAction;

Expand Down Expand Up @@ -2687,6 +2687,12 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
mEditForm = QgsProject::instance()->readPath( e.text() );
}

QDomNode editFormInitNode = node.namedItem( "editforminit" );
if ( !editFormInitNode.isNull() )
{
mEditFormInit = editFormInitNode.toElement().text();
}

mAttributeAliasMap.clear();
QDomNode aliasesNode = node.namedItem( "aliases" );
if ( !aliasesNode.isNull() )
Expand Down Expand Up @@ -2828,6 +2834,11 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
efField.appendChild( efText );
node.appendChild( efField );

QDomElement efiField = doc.createElement( "editforminit" );
QDomText efiText = doc.createTextNode( mEditFormInit );
efiField.appendChild( efiText );
node.appendChild( efiField );

//attribute aliases
if ( mAttributeAliasMap.size() > 0 )
{
Expand Down Expand Up @@ -4026,6 +4037,16 @@ void QgsVectorLayer::setEditForm( QString ui )
mEditForm = ui;
}

QString QgsVectorLayer::editFormInit()
{
return mEditFormInit;
}

void QgsVectorLayer::setEditFormInit( QString function )
{
mEditFormInit = function;
}

QMap< QString, QVariant > &QgsVectorLayer::valueMap( int idx )
{
const QgsFieldMap &fields = pendingFields();
Expand Down
9 changes: 8 additions & 1 deletion src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** set edit form (added in 1.4) */
void setEditForm( QString ui );

/** get python function for edit form initialization (added in 1.4) */
QString editFormInit();

/** set python function for edit form initialization (added in 1.4) */
void setEditFormInit( QString function );

/**access value map*/
QMap<QString, QVariant> &valueMap( int idx );

Expand Down Expand Up @@ -754,7 +760,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
QMap< QString, QMap<QString, QVariant> > mValueMaps;
QMap< QString, RangeData > mRanges;
QMap< QString, QPair<QString, QString> > mCheckedStates;
QString mEditForm;

QString mEditForm, mEditFormInit;

bool mFetching;
QgsRectangle mFetchRect;
Expand Down
43 changes: 26 additions & 17 deletions src/ui/qgsvectorlayerpropertiesbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<item row="0" column="2" colspan="3">
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>0</number>
<number>3</number>
</property>
<widget class="QWidget" name="page_3">
<layout class="QGridLayout" name="gridLayout_4">
Expand Down Expand Up @@ -172,7 +172,7 @@
<enum>QFrame::Sunken</enum>
</property>
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="page"/>
<widget class="QWidget" name="page_2"/>
Expand Down Expand Up @@ -355,8 +355,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>407</width>
<height>394</height>
<width>554</width>
<height>397</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_3">
Expand Down Expand Up @@ -409,7 +409,7 @@
</property>
</widget>
</item>
<item row="1" column="1">
<item row="1" column="1" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="leEditForm"/>
Expand All @@ -423,7 +423,24 @@
</item>
</layout>
</item>
<item row="1" column="2">
<item row="1" column="3">
<widget class="QPushButton" name="pbnIndex">
<property name="text">
<string>Create Spatial Index</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="4">
<widget class="QLineEdit" name="leSpatialRefSys">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QLineEdit" name="leEditFormInit"/>
</item>
<item row="2" column="3">
<widget class="QPushButton" name="pbnChangeSpatialRefSys">
<property name="toolTip">
<string>Specify the coordinate reference system of the layer's geometry.</string>
Expand All @@ -436,17 +453,10 @@
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QPushButton" name="pbnIndex">
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Create Spatial Index</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="4">
<widget class="QLineEdit" name="leSpatialRefSys">
<property name="readOnly">
<bool>true</bool>
<string>Init function</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -650,7 +660,6 @@
<tabstop>displayFieldComboBox</tabstop>
<tabstop>leSpatialRefSys</tabstop>
<tabstop>pbnIndex</tabstop>
<tabstop>pbnChangeSpatialRefSys</tabstop>
<tabstop>spinMinimumScale</tabstop>
<tabstop>spinMaximumScale</tabstop>
<tabstop>txtSubsetSQL</tabstop>
Expand Down

0 comments on commit 865fce9

Please sign in to comment.