Skip to content

Commit

Permalink
[FEATURE] Port editor widgets to new API
Browse files Browse the repository at this point in the history
Create a new widget for for attribute form

fix #10281
fix #7319
fix #7013
fix #9335
fix #4417
  • Loading branch information
m-kuhn committed May 22, 2014
1 parent 373ec56 commit ea91b6f
Show file tree
Hide file tree
Showing 147 changed files with 8,066 additions and 4,923 deletions.
16 changes: 5 additions & 11 deletions python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -860,12 +860,6 @@ class QgsVectorLayer : QgsMapLayer
/** set string representing 'true' for a checkbox (added in 1.4) */
void setCheckedState( int idx, QString checked, QString notChecked );

/** return string representing 'true' for a checkbox (added in 1.4)
* @note not available in python bindings
* FIXME: need SIP binding for QPair<QString, QString>
*/
// QPair<QString, QString> checkedState( int idx );

/** get edit form (added in 1.4) */
QString editForm();

Expand Down Expand Up @@ -893,15 +887,15 @@ class QgsVectorLayer : QgsMapLayer
void setEditFormInit( QString function );

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

/**access range */
RangeData &range( int idx );
RangeData range( int idx ) /Deprecated/;

/**access relations
* @note added in 1.8
**/
ValueRelationData &valueRelation( int idx );
ValueRelationData valueRelation( int idx ) /Deprecated/;

/**
* Get relations, where the foreign key is on this layer
Expand All @@ -914,12 +908,12 @@ class QgsVectorLayer : QgsMapLayer
/**access date format
* @note added in 1.9
*/
QString &dateFormat( int idx );
QString dateFormat( int idx ) /Deprecated/;

/**access widget size for photo and webview widget
* @note added in 1.9
*/
QSize &widgetSize( int idx );
QSize widgetSize( int idx ) /Deprecated/;

/**is edit widget editable
* @note added in 1.9
Expand Down
5 changes: 0 additions & 5 deletions python/gui/attributetable/qgsdualview.sip
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ class QgsDualView : QStackedWidget
*/
bool saveEditChanges();

/**
* Update the shown feature if an attribute changed
*/
void reloadAttribute( const int& attribute );


signals:
/**
Expand Down
118 changes: 114 additions & 4 deletions python/gui/editorwidgets/core/qgseditorwidgetfactory.sip
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,122 @@ class QgsEditorWidgetFactory
%End

public:
/**
* Constructor
*
* @param name A human readable name for this widget type
*/
QgsEditorWidgetFactory( const QString& name );

virtual ~QgsEditorWidgetFactory();
virtual QgsEditorWidgetWrapper* create( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent ) const = 0 /Factory/;
virtual QgsEditorConfigWidget* configWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) const = 0 /Factory/;

/**
* Override this in your implementation.
* Create a new editor widget wrapper. Call {@link QgsEditorWidgetRegistry::create()}
* instead of calling this method directly.
*
* @param vl The vector layer on which this widget will act
* @param fieldIdx The field index on which this widget will act
* @param editor An editor widget if already existent. If NULL is provided, a new widget will be created.
* @param parent The parent for the wrapper class and any created widget.
*
* @return A new widget wrapper
*/
virtual QgsEditorWidgetWrapper* create( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent ) const = 0;

/**
* Return The human readable identifier name of this widget type
*
* @return a name
*/
QString name();

/**
* Override this in your implementation.
* Create a new configuration widget for this widget type.
*
* @param vl The layer for which the widget will be created
* @param fieldIdx The field index for which the widget will be created
* @param parent The parent widget of the created config widget
*
* @return A configuration widget
*/
virtual QgsEditorConfigWidget* configWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) const = 0;

/**
* Read the config from an XML file and map it to a proper {@link QgsEditorWidgetConfig}.
*
* @param configElement The configuration element from the project file
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*
* @return A configuration object. This will be passed to your widget wrapper later on
*/
QgsEditorWidgetConfig readEditorConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx );

/**
* Serialize your configuration and save it in a xml doc.
*
* @param config The configuration to serialize
* @param configElement The element, where you can write your configuration into
* @param doc The document. You can use this to create new nodes
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*/
virtual void writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx );

/**
* Check if this editor widget type supports a certain field.
*
* @param vl The layer
* @param fieldIdx The field index
* @return True if the type is supported for this field
*/
bool supportsField( QgsVectorLayer* vl, int fieldIdx );

/**
* Create a pretty String representation of the value.
*
* @param vl The vector layer.
* @param fieldIdx The index of the field.
* @param config The editor widget config.
* @param value The value to represent.
*
* @return By default the string representation of the provided value.
*/
virtual QString representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const;

/**
* Create a cache for a given field.
*
* @param vl The vector layer.
* @param fieldIdx The index of the field.
* @param config The editor widget config.
*
* @return The default implementation returns an invalid QVariant
*/
virtual QVariant createCache( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config );

private:
/**
* Read the config from an XML file and map it to a proper {@link QgsEditorWidgetConfig}.
*
* @param configElement The configuration element from the project file
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*
* @return A configuration object. This will be passed to your widget wrapper later on
*/
virtual QgsEditorWidgetConfig readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx );
virtual void writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, const QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx );

virtual QString name();
/**
* This method allows to disable this editor widget type for a certain field.
* By default, it returns true for all fields.
* Reimplement this if you only support certain fields.
*
* @param vl
* @param fieldIdx
* @return True if the field is supported.
*/
virtual bool isFieldSupported( QgsVectorLayer* vl, int fieldIdx );
};
3 changes: 2 additions & 1 deletion python/gui/editorwidgets/core/qgseditorwidgetwrapper.sip
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class QgsEditorWidgetWrapper : QObject
QVariant config( QString key );

QgsVectorLayer* layer();
int field();
QgsField field();
int fieldIdx();

protected:
virtual QWidget* createWidget( QWidget* parent ) = 0 /Factory/;
Expand Down
2 changes: 2 additions & 0 deletions python/gui/gui.sip
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
%Include qgsattributeeditor.sip
%Include qgsattributeeditorcontext.sip
%Include qgsattributedialog.sip
%Include qgsattributeform.sip
%Include qgsattributeforminterface.sip
%Include qgsbusyindicatordialog.sip
%Include qgscollapsiblegroupbox.sip
%Include qgscolorbutton.sip
Expand Down
1 change: 0 additions & 1 deletion python/gui/qgsattributedialog.sip
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,4 @@ class QgsAttributeDialog : QObject
int exec();
void show();

void dialogDestroyed();
};
42 changes: 0 additions & 42 deletions python/gui/qgsattributeeditor.sip
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,6 @@ class QgsAttributeEditor : QObject
*/
static QWidget* createAttributeEditor( QWidget* parent, QWidget* editor, QgsVectorLayer* vl, int idx, const QVariant& value, QgsAttributeEditorContext& context ) /Factory/;

/**
* Creates a widget form a QgsAttributeEditorElement definition. Will recursively generate containers and widgets.
* @param widgetDef The definition for the widget
* @param parent The parent object
* @param vl The vector layer to use as data source
* @param feat The feature to create the widget for
* @param context the context used for the created attribute editor
* @param [out] labelText An optional label text will be written into the referenced QString. It will be set to
* a QString::null value if no label should be shown
* @param [out] labelOnTop Will be set to true if the label should be placed on top of the field.
* If set to false, the label should be shown left or right of the field
*
*/
static QWidget *createWidgetFromDef( const QgsAttributeEditorElement* widgetDef, QWidget* parent, QgsVectorLayer* vl, const QgsFeature &feat, QgsAttributeEditorContext& context, QString& labelText, bool& labelOnTop ) /Factory/;

static bool retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value );
static bool setValue( QWidget *widget, QgsVectorLayer *vl, int idx, const QVariant &value );

public slots:
void selectFileName();
void selectDate();
void loadUrl( const QString & );
void loadPixmap( const QString & );
void updateUrl();
void openUrl();
void updateColor();
};

class QgsStringRelay : QObject
{
%TypeHeaderCode
#include "qgsattributeeditor.h"
%End
public:
QgsStringRelay( QObject* parent = 0 );

void appendProxy( QWidget* proxy );

public slots:
void changeText();
void changeText( QString str );

signals:
void textChanged( QString );
};
5 changes: 0 additions & 5 deletions python/gui/qgsattributeeditorcontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class QgsAttributeEditorContext
public:
QgsAttributeEditorContext();

QWidget* proxyWidget( QgsVectorLayer* vl, int fieldIdx );
//! @note not available in python bindings
// void addProxyWidgets( QgsVectorLayer* vl, QMap<int, QWidget*> proxyWidgets );
void addProxyWidget( QgsVectorLayer* vl, int idx, QWidget* widget );

void setDistanceArea( const QgsDistanceArea& distanceArea );
const QgsDistanceArea& distanceArea();

Expand Down
47 changes: 47 additions & 0 deletions python/gui/qgsattributeform.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/***************************************************************************
qgsattributeform.h
--------------------------------------
Date : 3.5.2014
Copyright : (C) 2014 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

class QgsAttributeForm : QWidget
{
%TypeHeaderCode
#include <qgsattributeform.h>
%End

public:
explicit QgsAttributeForm( QgsVectorLayer* vl, const QgsFeature feature = QgsFeature(), QgsAttributeEditorContext context = QgsAttributeEditorContext(), QWidget *parent = 0 );
~QgsAttributeForm();

const QgsFeature& feature();

void hideButtonBox();

void showButtonBox();

signals:
/**
* Notifies about changes of attributes
*
* @param attribute The name of the attribute that changed.
* @param value The new value of the attribute.
*/
void attributeChanged( QString attribute, const QVariant& value );

public slots:
void changeAttribute( const QString& field, const QVariant& value );
void setFeature( const QgsFeature& feature );
bool save();
void accept();
void resetValues();
};
33 changes: 33 additions & 0 deletions python/gui/qgsattributeforminterface.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/***************************************************************************
qgsattributeforminterface.sip
--------------------------------------
Date : 12.5.2014
Copyright : (C) 2014 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

class QgsAttributeFormInterface
{
%TypeHeaderCode
#include <qgsattributeforminterface.h>
%End
public:
explicit QgsAttributeFormInterface( QgsAttributeForm* form );

virtual bool acceptChanges( const QgsFeature& feat );

virtual void initForm();

virtual void featureChanged();

QgsAttributeForm* form();

const QgsFeature& feature();
};
2 changes: 0 additions & 2 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ SET(QGIS_APP_SRCS
qgsannotationwidget.cpp
qgsattributeactiondialog.cpp
qgsattributetypedialog.cpp
qgsattributetypeloaddialog.cpp
qgsattributetabledialog.cpp
qgsbookmarks.cpp
qgsbrowserdockwidget.cpp
Expand Down Expand Up @@ -164,7 +163,6 @@ SET (QGIS_APP_MOC_HDRS
qgsannotationwidget.h
qgsattributeactiondialog.h
qgsattributetypedialog.h
qgsattributetypeloaddialog.h
qgsattributetabledialog.h
qgsbookmarks.h
qgsbrowserdockwidget.h
Expand Down

3 comments on commit ea91b6f

@nirvn
Copy link
Contributor

@nirvn nirvn commented on ea91b6f May 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-kuhn could this have caused the following regression: http://hub.qgis.org/issues/10337 ?

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on ea91b6f May 25, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NathanW2
Copy link
Member

@NathanW2 NathanW2 commented on ea91b6f May 25, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.