Skip to content

Commit ab3ff17

Browse files
committed
Pass attribute editor context for widgets on the attribute table
Fixes relation reference widget in attribute table
1 parent d05d83e commit ab3ff17

File tree

9 files changed

+98
-29
lines changed

9 files changed

+98
-29
lines changed

python/gui/qgsattributeeditorcontext.sip

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,26 @@ class QgsAttributeEditorContext
1515
*/
1616
enum RelationMode
1717
{
18-
Undefined, //!< This context is not defined by a relation
19-
EmbedMultiple, //!< When embedding a list of features (e.g. houses as an embedded form in a district form)
20-
EmbedSingle, //!< When embedding a single feature (e.g. district information when looking at the form of a house)
21-
StandaloneSingle //!< When showing a new dialog for a single feature (e.g. district information when looking at the form of a house)
18+
Undefined, //!< This context is not defined by a relation
19+
Multiple, //!< When showing a list of features (e.g. houses as an embedded form in a district form)
20+
Single //!< When showing a single feature (e.g. district information when looking at the form of a house)
21+
};
22+
23+
enum FormMode
24+
{
25+
Embed,
26+
StandaloneDialog,
27+
Popup
2228
};
2329

2430
QgsAttributeEditorContext();
2531

26-
QgsAttributeEditorContext( const QgsAttributeEditorContext& parentContext, const QgsRelation& relation, RelationMode mode );
2732

28-
void setDistanceArea( const QgsDistanceArea& distanceArea );
33+
QgsAttributeEditorContext( const QgsAttributeEditorContext& parentContext, FormMode formMode );
2934

30-
const QgsDistanceArea& distanceArea();
35+
QgsAttributeEditorContext( const QgsAttributeEditorContext& parentContext, const QgsRelation& relation, RelationMode relationMode, FormMode formMode );
36+
37+
void setDistanceArea( const QgsDistanceArea& distanceArea );
3138

3239
void setVectorLayerTools( QgsVectorLayerTools* vlTools );
3340
const QgsVectorLayerTools* vectorLayerTools() const;
@@ -36,5 +43,7 @@ class QgsAttributeEditorContext
3643
const QgsRelation& relation() const;
3744
RelationMode relationMode() const;
3845

46+
FormMode formMode() const;
47+
3948
const QgsAttributeEditorContext* parentContext() const;
4049
};

src/app/qgsattributetabledialog.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class QSignalMapper;
4040

4141
class QgsAttributeTableModel;
4242
class QgsAttributeTableFilterModel;
43-
class QgsAttributeTableView;
4443

4544
class APP_EXPORT QgsAttributeTableDialog : public QDialog, private Ui::QgsAttributeTableDialog
4645
{

src/gui/attributetable/qgsattributetabledelegate.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
#include "qgsvectordataprovider.h"
3030

3131

32-
33-
// TODO: Remove this casting orgy
34-
35-
QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *model ) const
32+
QgsVectorLayer* QgsAttributeTableDelegate::layer( const QAbstractItemModel *model )
3633
{
3734
const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
3835
if ( tm )
@@ -42,7 +39,20 @@ QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *mode
4239
if ( fm )
4340
return fm->layer();
4441

45-
return NULL;
42+
return 0;
43+
}
44+
45+
const QgsAttributeTableModel* QgsAttributeTableDelegate::masterModel( const QAbstractItemModel* model )
46+
{
47+
const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
48+
if ( tm )
49+
return tm;
50+
51+
const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( model );
52+
if ( fm )
53+
return fm->masterModel();
54+
55+
return 0;
4656
}
4757

4858
QWidget *QgsAttributeTableDelegate::createEditor(
@@ -59,7 +69,8 @@ QWidget *QgsAttributeTableDelegate::createEditor(
5969

6070
QString widgetType = vl->editorWidgetV2( fieldIdx );
6171
QgsEditorWidgetConfig cfg = vl->editorWidgetV2Config( fieldIdx );
62-
QgsEditorWidgetWrapper* eww = QgsEditorWidgetRegistry::instance()->create( widgetType, vl, fieldIdx, cfg, 0, parent );
72+
QgsAttributeEditorContext context( masterModel( index.model() )->editorContext(), QgsAttributeEditorContext::Popup );
73+
QgsEditorWidgetWrapper* eww = QgsEditorWidgetRegistry::instance()->create( widgetType, vl, fieldIdx, cfg, 0, parent, context );
6374
QWidget* w = eww->widget();
6475

6576
w->setAutoFillBackground( true );

src/gui/attributetable/qgsattributetabledelegate.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class QgsFeatureSelectionModel;
2323
class QPainter;
2424
class QgsVectorLayer;
25-
class QgsAttributeTableView;
25+
class QgsAttributeTableModel;
2626

2727
/** \ingroup app
2828
* A delegate item class for QgsAttributeTable (see Qt documentation for
@@ -31,16 +31,18 @@ class QgsAttributeTableView;
3131

3232
class GUI_EXPORT QgsAttributeTableDelegate : public QItemDelegate
3333
{
34-
Q_OBJECT;
34+
Q_OBJECT
3535

36-
QgsVectorLayer *layer( const QAbstractItemModel *model ) const;
36+
static QgsVectorLayer* layer( const QAbstractItemModel* model );
37+
static const QgsAttributeTableModel* masterModel( const QAbstractItemModel* model );
3738

3839
public:
3940
/** Constructor
4041
* @param parent parent object
4142
*/
4243
QgsAttributeTableDelegate( QObject* parent = NULL ) :
43-
QItemDelegate( parent ) {};
44+
QItemDelegate( parent ) {}
45+
4446
/** Used to create an editor for when the user tries to
4547
* change the contents of a cell */
4648
QWidget * createEditor(

src/gui/attributetable/qgsattributetablemodel.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "qgsvectorlayer.h" // QgsAttributeList
2828
#include "qgsvectorlayercache.h"
29+
#include "qgsattributeeditorcontext.h"
2930

3031
class QgsMapCanvas;
3132
class QgsMapLayerAction;
@@ -193,8 +194,31 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
193194
*/
194195
void prefetchColumnData( int column );
195196

197+
/**
198+
* Set a request that will be used to fill this attribute table model.
199+
* In contrast to a filter, the request will constrain the data shown without the possibility
200+
* to dynamically adjust it.
201+
*
202+
* @param request The request to use to fill this table model.
203+
*/
196204
void setRequest( const QgsFeatureRequest& request );
197205

206+
/**
207+
* Sets the context in which this table is shown.
208+
* Will be forwarded to any editor widget created when editing data on this model.
209+
*
210+
* @param context The context
211+
*/
212+
void setEditorContext( const QgsAttributeEditorContext& context ) { mEditorContext = context; }
213+
214+
/**
215+
* Returns the context in which this table is shown.
216+
* Will be forwarded to any editor widget created when editing data on this model.
217+
*
218+
* @return The context
219+
*/
220+
const QgsAttributeEditorContext& editorContext() const { return mEditorContext; }
221+
198222
signals:
199223
/**
200224
* Model has been changed
@@ -291,6 +315,8 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
291315
* right = max column
292316
*/
293317
QRect mChangedCellBounds;
318+
319+
QgsAttributeEditorContext mEditorContext;
294320
};
295321

296322

src/gui/attributetable/qgsdualview.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void QgsDualView::columnBoxInit()
114114
// ... If there are primary key(s) defined
115115
QStringList pkFields;
116116

117-
Q_FOREACH ( int attr, pkAttrs )
117+
Q_FOREACH( int attr, pkAttrs )
118118
{
119119
pkFields.append( "COALESCE(\"" + fields[attr].name() + "\", '<NULL>')" );
120120
}
@@ -224,6 +224,7 @@ void QgsDualView::initModels( QgsMapCanvas* mapCanvas, const QgsFeatureRequest&
224224
{
225225
mMasterModel = new QgsAttributeTableModel( mLayerCache, this );
226226
mMasterModel->setRequest( request );
227+
mMasterModel->setEditorContext( mEditorContext );
227228

228229
connect( mMasterModel, SIGNAL( progress( int, bool & ) ), this, SLOT( progress( int, bool & ) ) );
229230
connect( mMasterModel, SIGNAL( finished() ), this, SLOT( finished() ) );

src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void QgsRelationReferenceWidget::setRelation( QgsRelation relation, bool allowNu
152152
mFkeyFieldIdx = mReferencedLayer->fieldNameIndex( relation.fieldPairs().first().second );
153153

154154

155-
QgsAttributeEditorContext context( mEditorContext, relation, QgsAttributeEditorContext::EmbedSingle );
155+
QgsAttributeEditorContext context( mEditorContext, relation, QgsAttributeEditorContext::Single, QgsAttributeEditorContext::Embed );
156156

157157
if ( mEmbedForm )
158158
{
@@ -430,7 +430,7 @@ void QgsRelationReferenceWidget::openForm()
430430
if ( !feat.isValid() )
431431
return;
432432

433-
QgsAttributeEditorContext context( mEditorContext, mRelation, QgsAttributeEditorContext::StandaloneSingle );
433+
QgsAttributeEditorContext context( mEditorContext, mRelation, QgsAttributeEditorContext::Single, QgsAttributeEditorContext::StandaloneDialog );
434434
QgsAttributeDialog attributeDialog( mReferencedLayer, new QgsFeature( feat ), true, this, true, context );
435435
attributeDialog.exec();
436436
}

src/gui/editorwidgets/qgsrelationwidgetwrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ void QgsRelationWidgetWrapper::initWidget( QWidget* editor )
4848
w = new QgsRelationEditorWidget( editor );
4949
}
5050

51-
QgsAttributeEditorContext myContext( QgsAttributeEditorContext( context(), mRelation, QgsAttributeEditorContext::EmbedMultiple ) );
51+
QgsAttributeEditorContext myContext( QgsAttributeEditorContext( context(), mRelation, QgsAttributeEditorContext::Multiple, QgsAttributeEditorContext::Embed ) );
5252

5353
w->setEditorContext( myContext );
5454

5555
// If this widget is already embedded by the same relation, reduce functionality
5656
const QgsAttributeEditorContext* ctx = &context();
5757
do
5858
{
59-
if ( ctx->relation().name() == mRelation.name() && ctx->relationMode() == QgsAttributeEditorContext::EmbedMultiple )
59+
if ( ctx->relation().name() == mRelation.name() && ctx->relationMode() == QgsAttributeEditorContext::Multiple )
6060
{
6161
w->setSaveCollapsedState( false );
6262
w->setCollapsed( true );

src/gui/qgsattributeeditorcontext.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ class GUI_EXPORT QgsAttributeEditorContext
3838
*/
3939
enum RelationMode
4040
{
41-
Undefined, //!< This context is not defined by a relation
42-
EmbedMultiple, //!< When embedding a list of features (e.g. houses as an embedded form in a district form)
43-
EmbedSingle, //!< When embedding a single feature (e.g. district information when looking at the form of a house)
44-
StandaloneSingle //!< When showing a new dialog for a single feature (e.g. district information when looking at the form of a house)
41+
Undefined, //!< This context is not defined by a relation
42+
Multiple, //!< When showing a list of features (e.g. houses as an embedded form in a district form)
43+
Single //!< When showing a single feature (e.g. district information when looking at the form of a house)
44+
};
45+
46+
enum FormMode
47+
{
48+
Embed, //!< A form was embedded as a widget on another form
49+
StandaloneDialog, //!< A form was opened as a new dialog
50+
Popup //!< A widget was opened as a popup (e.g. attribute table editor widget)
4551
};
4652

4753
QgsAttributeEditorContext()
@@ -51,13 +57,25 @@ class GUI_EXPORT QgsAttributeEditorContext
5157
, mRelationMode( Undefined )
5258
{}
5359

54-
QgsAttributeEditorContext( const QgsAttributeEditorContext& parentContext, const QgsRelation& relation, RelationMode mode )
60+
QgsAttributeEditorContext( const QgsAttributeEditorContext& parentContext, FormMode formMode )
61+
: mParentContext( &parentContext )
62+
, mLayer( 0 )
63+
, mVectorLayerTools( parentContext.mVectorLayerTools )
64+
, mDistanceArea( parentContext.mDistanceArea )
65+
, mRelationMode( Undefined )
66+
, mFormMode( formMode )
67+
{
68+
Q_ASSERT( parentContext.vectorLayerTools() );
69+
}
70+
71+
QgsAttributeEditorContext( const QgsAttributeEditorContext& parentContext, const QgsRelation& relation, RelationMode relationMode, FormMode widgetMode )
5572
: mParentContext( &parentContext )
5673
, mLayer( 0 )
5774
, mVectorLayerTools( parentContext.mVectorLayerTools )
5875
, mDistanceArea( parentContext.mDistanceArea )
5976
, mRelation( relation )
60-
, mRelationMode( mode )
77+
, mRelationMode( relationMode )
78+
, mFormMode( widgetMode )
6179
{
6280
Q_ASSERT( parentContext.vectorLayerTools() );
6381
}
@@ -80,6 +98,8 @@ class GUI_EXPORT QgsAttributeEditorContext
8098
inline const QgsRelation& relation() const { return mRelation; }
8199
inline RelationMode relationMode() const { return mRelationMode; }
82100

101+
inline FormMode formMode() const { return mFormMode; }
102+
83103
inline const QgsAttributeEditorContext* parentContext() const { return mParentContext; }
84104

85105
private:
@@ -89,6 +109,7 @@ class GUI_EXPORT QgsAttributeEditorContext
89109
QgsDistanceArea mDistanceArea;
90110
QgsRelation mRelation;
91111
RelationMode mRelationMode;
112+
FormMode mFormMode;
92113
};
93114

94115
#endif // QGSATTRIBUTEEDITORCONTEXT_H

0 commit comments

Comments
 (0)