Skip to content

Commit 24417fd

Browse files
committed
Add python bindings for QgsWidgetWrapper
Allows the usage of `widget.wrapper()` from a form's python init script.
1 parent 5bcf0f5 commit 24417fd

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/***************************************************************************
2+
qgswidgetwrapper.h
3+
--------------------------------------
4+
Date : 14.5.2014
5+
Copyright : (C) 2013 Matthias Kuhn
6+
Email : matthias dot kuhn at gmx 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+
16+
17+
/**
18+
* Manages an editor widget
19+
* Widget and wrapper share the same parent
20+
*
21+
* A wrapper controls one attribute editor widget and is able to create a default
22+
* widget or use a pre-existent widget. It is able to set the widget to the value implied
23+
* by a field of a vector layer, or return the value it currently holds. Every time it is changed
24+
* it has to emit a valueChanged signal. If it fails to do so, there is no guarantee that the
25+
* changed status of the widget will be saved.
26+
*
27+
*/
28+
class QgsWidgetWrapper : QObject
29+
{
30+
%TypeHeaderCode
31+
#include "qgswidgetwrapper.h"
32+
%End
33+
34+
%ConvertToSubClassCode
35+
QgsEditorWidgetWrapper* eww = qobject_cast<QgsEditorWidgetWrapper*>( sipCpp );
36+
if ( eww )
37+
sipType = sipType_QgsEditorWidgetWrapper;
38+
else
39+
sipType = 0;
40+
%End
41+
42+
public:
43+
/**
44+
* Create a new widget wrapper
45+
*
46+
* @param vl The layer on which the field is
47+
* @param editor An editor widget. Can be NULL if one should be autogenerated.
48+
* @param parent A parent widget for this widget wrapper and the created widget.
49+
*/
50+
QgsWidgetWrapper( QgsVectorLayer* vl, QWidget* editor = 0, QWidget* parent = 0 );
51+
52+
/**
53+
* @brief Access the widget managed by this wrapper
54+
*
55+
* @return The widget
56+
*/
57+
QWidget* widget();
58+
59+
60+
/**
61+
* Will set the config of this wrapper to the specified config.
62+
*
63+
* @param config The config for this wrapper
64+
*/
65+
void setConfig( const QgsEditorWidgetConfig& config );
66+
67+
/**
68+
* Set the context in which this widget is shown
69+
*
70+
* @param context context information
71+
*/
72+
void setContext( const QgsAttributeEditorContext& context );
73+
74+
/**
75+
* Use this inside your overriden classes to access the configuration.
76+
*
77+
* @param key The configuration option you want to load
78+
* @param defaultVal Default value
79+
*
80+
* @return the value assigned to this configuration option
81+
*/
82+
QVariant config( QString key, QVariant defaultVal = QVariant() );
83+
84+
/**
85+
* Returns the whole config
86+
*
87+
* @return The configuration
88+
*/
89+
const QgsEditorWidgetConfig config();
90+
91+
/**
92+
* Returns information about the context in which this widget is shown
93+
*
94+
* @return context information
95+
*/
96+
const QgsAttributeEditorContext& context();
97+
98+
/**
99+
* Access the QgsVectorLayer, you are working on
100+
*
101+
* @return The layer
102+
*
103+
* @see field()
104+
*/
105+
QgsVectorLayer* layer();
106+
107+
/**
108+
* Will return a wrapper for a given widget
109+
* @param widget The widget which was created by a wrapper
110+
* @return The wrapper for the widget or NULL
111+
*/
112+
static QgsWidgetWrapper* fromWidget( QWidget* widget );
113+
114+
protected:
115+
/**
116+
* This method should create a new widget with the provided parent. This will only be called
117+
* if the form did not already provide a widget, so it is not guaranteed to be called!
118+
* You should not do initialisation stuff, which also has to be done for custom editor
119+
* widgets inside this method. Things like filling comboboxes and assigning other data which
120+
* will also be used to make widgets on forms created in the QtDesigner usable should be assigned
121+
* in {@link initWidget(QWidget*)}.
122+
*
123+
* @param parent You should set this parent on the created widget.
124+
* @return A new widget
125+
*/
126+
virtual QWidget* createWidget( QWidget* parent ) = 0;
127+
128+
/**
129+
* This method should initialize the editor widget with runtime data. Fill your comboboxes here.
130+
*
131+
* @param editor The widget which will represent this attribute editor in a form.
132+
*/
133+
virtual void initWidget( QWidget* editor );
134+
135+
public slots:
136+
/**
137+
* Is called, when the value of the widget needs to be changed. Update the widget representation
138+
* to reflect the new value.
139+
*
140+
* @param feature The new feature
141+
*/
142+
virtual void setFeature( const QgsFeature& feature ) = 0;
143+
144+
/**
145+
* Is used to enable or disable the edit functionality of the managed widget.
146+
* By default this will not change the enabled state of the widget
147+
*
148+
* @param enabled Enable or Disable?
149+
*/
150+
virtual void setEnabled( bool enabled );
151+
152+
};

python/gui/gui.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
%Include editorwidgets/core/qgseditorwidgetfactory.sip
184184
%Include editorwidgets/core/qgseditorwidgetregistry.sip
185185
%Include editorwidgets/core/qgseditorwidgetwrapper.sip
186+
%Include editorwidgets/core/qgswidgetwrapper.sip
186187
%Include editorwidgets/qgsdoublespinbox.sip
187188
%Include editorwidgets/qgsrelationreferencewidget.sip
188189
%Include editorwidgets/qgsrelationreferencewidgetwrapper.sip

0 commit comments

Comments
 (0)