/
qgsrelationadddlg.cpp
284 lines (241 loc) · 10.4 KB
/
qgsrelationadddlg.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/***************************************************************************
qgsrelationadddlg.cpp - QgsRelationAddDlg
---------------------------------
begin : 4.10.2013
copyright : (C) 2013 by Matthias Kuhn
email : matthias@opengis.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 <QDialogButtonBox>
#include <QLabel>
#include <QToolButton>
#include <QPushButton>
#include <QComboBox>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "qgsrelationadddlg.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayercombobox.h"
#include "qgsfieldcombobox.h"
#include "qgsmaplayerproxymodel.h"
#include "qgsapplication.h"
#include "qgshelp.h"
QgsFieldPairWidget::QgsFieldPairWidget( int index, QWidget *parent )
: QWidget( parent )
, mIndex( index )
, mEnabled( index == 0 )
{
mLayout = new QHBoxLayout();
mLayout->setContentsMargins( 0, 0, 0, 0 );
mAddButton = new QToolButton( this );
mAddButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/symbologyAdd.svg" ) ) );
mAddButton->setMinimumWidth( 30 );
mAddButton->setToolTip( tr( "Add new field pair as part of a composite foreign key" ) );
mLayout->addWidget( mAddButton, 0, Qt::AlignLeft );
mRemoveButton = new QToolButton( this );
mRemoveButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/symbologyRemove.svg" ) ) );
mRemoveButton->setMinimumWidth( 30 );
mRemoveButton->setToolTip( tr( "Remove the last pair of fields" ) );
mLayout->addWidget( mRemoveButton, 0, Qt::AlignLeft );
mSpacerItem = new QSpacerItem( 30, 30, QSizePolicy::Minimum, QSizePolicy::Maximum );
mLayout->addSpacerItem( mSpacerItem );
mReferencedFieldCombobox = new QgsFieldComboBox( this );
connect( mReferencedFieldCombobox, &QgsFieldComboBox::fieldChanged, this, &QgsFieldPairWidget::configChanged );
mLayout->addWidget( mReferencedFieldCombobox, 1 );
mReferencingFieldCombobox = new QgsFieldComboBox( this );
connect( mReferencingFieldCombobox, &QgsFieldComboBox::fieldChanged, this, &QgsFieldPairWidget::configChanged );
mLayout->addWidget( mReferencingFieldCombobox, 1 );
setLayout( mLayout );
updateWidgetVisibility();
connect( mAddButton, &QToolButton::clicked, this, &QgsFieldPairWidget::changeEnable );
connect( mRemoveButton, &QToolButton::clicked, this, &QgsFieldPairWidget::changeEnable );
connect( mAddButton, &QToolButton::clicked, this, &QgsFieldPairWidget::pairEnabled );
connect( mRemoveButton, &QToolButton::clicked, this, &QgsFieldPairWidget::pairDisabled );
}
void QgsFieldPairWidget::setReferencingLayer( QgsMapLayer *layer )
{
mReferencingFieldCombobox->setLayer( layer );
}
void QgsFieldPairWidget::setReferencedLayer( QgsMapLayer *layer )
{
mReferencedFieldCombobox->setLayer( layer );
}
QString QgsFieldPairWidget::referencingField() const
{
return mReferencingFieldCombobox->currentField();
}
QString QgsFieldPairWidget::referencedField() const
{
return mReferencedFieldCombobox->currentField();
}
bool QgsFieldPairWidget::isPairEnabled() const
{
return mEnabled;
}
void QgsFieldPairWidget::updateWidgetVisibility()
{
mAddButton->setVisible( !mEnabled );
mRemoveButton->setVisible( mIndex > 0 && mEnabled );
mReferencingFieldCombobox->setVisible( mEnabled );
mReferencedFieldCombobox->setVisible( mEnabled );
int spacerSize = 0;
if ( !mRemoveButton->isVisible() && !mAddButton->isVisible() )
spacerSize = mRemoveButton->minimumWidth() + mLayout->spacing();
mSpacerItem->changeSize( spacerSize, spacerSize );
}
void QgsFieldPairWidget::changeEnable()
{
mEnabled = !mEnabled || ( mIndex == 0 );
updateWidgetVisibility();
emit configChanged();
}
////////////////////
// QgsRelationAddDlg
QgsRelationAddDlg::QgsRelationAddDlg( QWidget *parent )
: QDialog( parent )
{
setWindowTitle( tr( "Add New Relation" ) );
QGridLayout *layout = new QGridLayout(); // column 0 is kept free for alignment purpose
// row 0: name
// col 1
QLabel *nameLabel = new QLabel( tr( "Name" ), this );
layout->addWidget( nameLabel, 0, 1 );
// col 2
mNameLineEdit = new QLineEdit( this );
layout->addWidget( mNameLineEdit, 0, 2 );
// row 1: labels
// col 1
QLabel *referencedLabel = new QLabel( tr( "Referenced layer (parent)" ), this );
layout->addWidget( referencedLabel, 1, 1 );
// col 2
QLabel *referencingLabel = new QLabel( tr( "Referencing layer (child)" ), this );
layout->addWidget( referencingLabel, 1, 2 );
// row 2: layer comboboxes
// col 1
mReferencedLayerCombobox = new QgsMapLayerComboBox( this );
mReferencedLayerCombobox->setFilters( QgsMapLayerProxyModel::VectorLayer );
layout->addWidget( mReferencedLayerCombobox, 2, 1 );
// col 2
mReferencingLayerCombobox = new QgsMapLayerComboBox( this );
mReferencingLayerCombobox->setFilters( QgsMapLayerProxyModel::VectorLayer );
layout->addWidget( mReferencingLayerCombobox, 2, 2 );
// row 3: field pairs layout
mFieldPairsLayout = new QVBoxLayout();
layout->addLayout( mFieldPairsLayout, 3, 0, 1, 3 );
// row 4: id
// row 1
QLabel *idLabel = new QLabel( tr( "Id" ), this );
layout->addWidget( idLabel, 4, 1 );
// col 2
mIdLineEdit = new QLineEdit( this );
mIdLineEdit->setPlaceholderText( tr( "[Generated automatically]" ) );
layout->addWidget( mIdLineEdit, 4, 2 );
// row 5: strength
QLabel *strengthLabel = new QLabel( tr( "Relationship strength" ), this );
layout->addWidget( strengthLabel, 5, 1 );
// col 2
mStrengthCombobox = new QComboBox( this );
mStrengthCombobox->addItem( tr( "Association" ), QVariant::fromValue( QgsRelation::RelationStrength::Association ) );
mStrengthCombobox->addItem( tr( "Composition" ), QVariant::fromValue( QgsRelation::RelationStrength::Composition ) );
mStrengthCombobox->setToolTip( tr( "When composition is selected the child features will be duplicated too.\n"
"Duplications are made by the feature duplication action.\n"
"The default actions are activated in the Action section of the layer properties." ) );
layout->addWidget( mStrengthCombobox, 5, 2 );
// row 6: button box
mButtonBox = new QDialogButtonBox( this );
mButtonBox->setOrientation( Qt::Horizontal );
mButtonBox->setStandardButtons( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok );
connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsRelationAddDlg::accept );
connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsRelationAddDlg::reject );
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, [ = ]
{
QgsHelp::openHelp( QStringLiteral( "working_with_vector/attribute_table.html#defining-1-n-relations" ) );
} );
layout->addWidget( mButtonBox, 7, 1, 1, 2 );
// set layout
layout->setColumnMinimumWidth( 0, 30 );
layout->setColumnStretch( 1, 1 );
layout->setColumnStretch( 2, 1 );
layout->setRowStretch( 6, 2 );
setLayout( layout );
addFieldPairWidget();
addFieldPairWidget();
checkDefinitionValid();
connect( mReferencingLayerCombobox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
connect( mReferencedLayerCombobox, &QgsMapLayerComboBox::layerChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
}
void QgsRelationAddDlg::addFieldPairWidget()
{
int index = mFieldPairWidgets.count();
QgsFieldPairWidget *w = new QgsFieldPairWidget( index, this );
w->setReferencingLayer( mReferencingLayerCombobox->currentLayer() );
w->setReferencedLayer( mReferencedLayerCombobox->currentLayer() );
mFieldPairsLayout->addWidget( w );
mFieldPairWidgets << w;
connect( w, &QgsFieldPairWidget::configChanged, this, &QgsRelationAddDlg::checkDefinitionValid );
connect( w, &QgsFieldPairWidget::pairDisabled, this, [ = ]() {emit fieldPairRemoved( w );} );
connect( w, &QgsFieldPairWidget::pairEnabled, this, &QgsRelationAddDlg::addFieldPairWidget );
connect( mReferencingLayerCombobox, &QgsMapLayerComboBox::layerChanged, w, &QgsFieldPairWidget::setReferencingLayer );
connect( mReferencedLayerCombobox, &QgsMapLayerComboBox::layerChanged, w, &QgsFieldPairWidget::setReferencedLayer );
}
void QgsRelationAddDlg::fieldPairRemoved( QgsFieldPairWidget *fieldPairWidget )
{
// remove widget only if not the first one, last one should always be disabled
int index = mFieldPairWidgets.indexOf( fieldPairWidget );
if ( index > 0 && index < mFieldPairWidgets.count() )
{
mFieldPairWidgets.removeAt( index );
fieldPairWidget->deleteLater();
}
}
QString QgsRelationAddDlg::referencingLayerId()
{
return mReferencingLayerCombobox->currentLayer()->id();
}
QString QgsRelationAddDlg::referencedLayerId()
{
return mReferencedLayerCombobox->currentLayer()->id();
}
QList< QPair< QString, QString > > QgsRelationAddDlg::references()
{
QList< QPair< QString, QString > > references;
for ( int i = 0; i < mFieldPairWidgets.count(); i++ )
{
if ( !mFieldPairWidgets.at( i )->isPairEnabled() )
continue;
QString referencingField = mFieldPairWidgets.at( i )->referencingField();
QString referencedField = mFieldPairWidgets.at( i )->referencedField();
references << qMakePair( referencingField, referencedField );
}
return references;
}
QString QgsRelationAddDlg::relationId()
{
return mIdLineEdit->text();
}
QString QgsRelationAddDlg::relationName()
{
return mNameLineEdit->text();
}
QgsRelation::RelationStrength QgsRelationAddDlg::relationStrength()
{
return mStrengthCombobox->currentData().value<QgsRelation::RelationStrength>();
}
void QgsRelationAddDlg::checkDefinitionValid()
{
bool valid = mReferencingLayerCombobox->currentLayer() && mReferencedLayerCombobox->currentLayer();
for ( const QgsFieldPairWidget *fieldPairWidget : qgis::as_const( mFieldPairWidgets ) )
{
if ( !fieldPairWidget->isPairEnabled() )
continue;
valid &= !fieldPairWidget->referencingField().isNull() && !fieldPairWidget->referencedField().isNull();
}
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( valid );
}