-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsrelationreferencewidgetwrapper.cpp
155 lines (131 loc) · 4.92 KB
/
qgsrelationreferencewidgetwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***************************************************************************
qgsrelationreferencewidgetwrapper.cpp
--------------------------------------
Date : 20.4.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias at opengis 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. *
* *
***************************************************************************/
#include "qgsrelationreferencewidgetwrapper.h"
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include "qgsrelationreferencewidget.h"
QgsRelationReferenceWidgetWrapper::QgsRelationReferenceWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QgsMapCanvas* canvas, QgsMessageBar* messageBar, QWidget* parent )
: QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent )
, mWidget( nullptr )
, mCanvas( canvas )
, mMessageBar( messageBar )
, mIndeterminateState( false )
{
}
QWidget* QgsRelationReferenceWidgetWrapper::createWidget( QWidget* parent )
{
QgsRelationReferenceWidget* w = new QgsRelationReferenceWidget( parent );
return w;
}
void QgsRelationReferenceWidgetWrapper::initWidget( QWidget* editor )
{
QgsRelationReferenceWidget* w = dynamic_cast<QgsRelationReferenceWidget*>( editor );
if ( !w )
{
w = new QgsRelationReferenceWidget( editor );
}
mWidget = w;
mWidget->setEditorContext( context(), mCanvas, mMessageBar );
bool showForm = config( QStringLiteral( "ShowForm" ), true ).toBool();
bool mapIdent = config( QStringLiteral( "MapIdentification" ), false ).toBool();
bool readOnlyWidget = config( QStringLiteral( "ReadOnly" ), false ).toBool();
bool orderByValue = config( QStringLiteral( "OrderByValue" ), false ).toBool();
mWidget->setEmbedForm( showForm );
mWidget->setReadOnlySelector( readOnlyWidget );
mWidget->setAllowMapIdentification( mapIdent );
mWidget->setOrderByValue( orderByValue );
if ( config( QStringLiteral( "FilterFields" ), QVariant() ).isValid() )
{
mWidget->setFilterFields( config( QStringLiteral( "FilterFields" ) ).toStringList() );
mWidget->setChainFilters( config( QStringLiteral( "ChainFilters" ) ).toBool() );
}
mWidget->setAllowAddFeatures( config( QStringLiteral( "AllowAddFeatures" ), false ).toBool() );
const QVariant relationName = config( QStringLiteral( "Relation" ) );
QgsRelation relation = relationName.isValid() ?
QgsProject::instance()->relationManager()->relation( relationName.toString() ) :
layer()->referencingRelations( fieldIdx() )[0];
// If this widget is already embedded by the same relation, reduce functionality
const QgsAttributeEditorContext* ctx = &context();
do
{
if ( ctx->relation().name() == relation.name() )
{
mWidget->setEmbedForm( false );
mWidget->setReadOnlySelector( false );
mWidget->setAllowMapIdentification( false );
}
ctx = ctx->parentContext();
}
while ( ctx );
mWidget->setRelation( relation, config( QStringLiteral( "AllowNULL" ) ).toBool() );
connect( mWidget, SIGNAL( foreignKeyChanged( QVariant ) ), this, SLOT( foreignKeyChanged( QVariant ) ) );
}
QVariant QgsRelationReferenceWidgetWrapper::value() const
{
if ( !mWidget )
return QVariant( field().type() );
QVariant v = mWidget->foreignKey();
if ( v.isNull() )
{
return QVariant( field().type() );
}
else
{
return v;
}
}
bool QgsRelationReferenceWidgetWrapper::valid() const
{
return mWidget;
}
void QgsRelationReferenceWidgetWrapper::showIndeterminateState()
{
if ( mWidget )
{
mWidget->showIndeterminateState();
}
mIndeterminateState = true;
}
void QgsRelationReferenceWidgetWrapper::setValue( const QVariant& val )
{
if ( !mWidget || ( !mIndeterminateState && val == value() ) )
return;
mIndeterminateState = false;
mWidget->setForeignKey( val );
}
void QgsRelationReferenceWidgetWrapper::setEnabled( bool enabled )
{
if ( !mWidget )
return;
mWidget->setRelationEditable( enabled );
}
void QgsRelationReferenceWidgetWrapper::foreignKeyChanged( QVariant value )
{
if ( !value.isValid() || value.isNull() )
{
value = QVariant( field().type() );
}
emit valueChanged( value );
}
void QgsRelationReferenceWidgetWrapper::updateConstraintWidgetStatus( bool constraintValid )
{
if ( mWidget )
{
if ( constraintValid )
mWidget->setStyleSheet( QString() );
else
mWidget->setStyleSheet( QStringLiteral( ".QComboBox { background-color: #dd7777; }" ) );
}
}