Skip to content

Commit f10f4a5

Browse files
committedAug 19, 2016
[feature] Allow controlling labels for individual edit widgets
In the drag and drop designer, a double click on an item will allow controlling if the label should be shown for each item individually. Fix #15450
1 parent 78845c6 commit f10f4a5

File tree

6 files changed

+225
-105
lines changed

6 files changed

+225
-105
lines changed
 

‎python/core/qgseditformconfig.sip

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* *
1616
***************************************************************************/
1717

18-
class QgsAttributeEditorElement : QObject
18+
class QgsAttributeEditorElement : QObject /Abstract/
1919
{
2020
%TypeHeaderCode
2121
#include "qgsvectorlayer.h"
@@ -74,13 +74,28 @@ class QgsAttributeEditorElement : QObject
7474
AttributeEditorType type() const;
7575

7676
/**
77-
* Is reimplemented in classes inheriting from this to serialize it.
77+
* Get the XML Dom element to save this element.
7878
*
7979
* @param doc The QDomDocument which is used to create new XML elements
8080
*
81-
* @return An DOM element which represents this element
81+
* @return A DOM element to serialize this element
8282
*/
83-
virtual QDomElement toDomElement( QDomDocument& doc ) const = 0;
83+
QDomElement toDomElement( QDomDocument& doc ) const;
84+
85+
/**
86+
* Controls if this element should be labeled with a title (field, relation or groupname).
87+
*
88+
* @note Added in QGIS 2.18
89+
*/
90+
bool showLabel() const;
91+
92+
/**
93+
* Controls if this element should be labeled with a title (field, relation or groupname).
94+
*
95+
* @note Added in QGIS 2.18
96+
*/
97+
void setShowLabel(bool showLabel);
98+
8499
};
85100

86101
class QgsAttributeEditorContainer : QgsAttributeEditorElement

‎src/app/qgsfieldsproperties.cpp

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,26 @@ QTreeWidgetItem *QgsFieldsProperties::loadAttributeEditorTreeItem( QgsAttributeE
161161
switch ( widgetDef->type() )
162162
{
163163
case QgsAttributeEditorElement::AeTypeField:
164-
newWidget = mDesignerTree->addItem( parent, DesignerTreeItemData( DesignerTreeItemData::Field, widgetDef->name() ) );
164+
{
165+
DesignerTreeItemData itemData = DesignerTreeItemData( DesignerTreeItemData::Field, widgetDef->name() );
166+
itemData.setShowLabel( widgetDef->showLabel() );
167+
newWidget = mDesignerTree->addItem( parent, itemData );
165168
break;
169+
}
166170

167171
case QgsAttributeEditorElement::AeTypeRelation:
168-
newWidget = mDesignerTree->addItem( parent, DesignerTreeItemData( DesignerTreeItemData::Relation, widgetDef->name() ) );
172+
{
173+
DesignerTreeItemData itemData = DesignerTreeItemData( DesignerTreeItemData::Relation, widgetDef->name() );
174+
itemData.setShowLabel( widgetDef->showLabel() );
175+
176+
newWidget = mDesignerTree->addItem( parent, itemData );
169177
break;
178+
}
170179

171180
case QgsAttributeEditorElement::AeTypeContainer:
172181
{
173182
DesignerTreeItemData itemData( DesignerTreeItemData::Container, widgetDef->name() );
183+
itemData.setShowLabel( widgetDef->showLabel() );
174184

175185
const QgsAttributeEditorContainer* container = dynamic_cast<const QgsAttributeEditorContainer*>( widgetDef );
176186
if ( !container )
@@ -900,6 +910,8 @@ QgsAttributeEditorElement* QgsFieldsProperties::createAttributeEditorWidget( QTr
900910
}
901911
}
902912

913+
widgetDef->setShowLabel( itemData.showLabel() );
914+
903915
return widgetDef;
904916
}
905917

@@ -1268,12 +1280,23 @@ void DesignerTree::onItemDoubleClicked( QTreeWidgetItem* item, int column )
12681280
Q_UNUSED( column )
12691281
QgsFieldsProperties::DesignerTreeItemData itemData = item->data( 0, QgsFieldsProperties::DesignerTreeRole ).value<QgsFieldsProperties::DesignerTreeItemData>();
12701282

1283+
QGroupBox* baseData = new QGroupBox( tr( "Base configuration" ) );
1284+
1285+
QFormLayout* baseLayout = new QFormLayout();
1286+
baseData->setLayout( baseLayout );
1287+
QCheckBox* showLabelCheckbox = new QCheckBox( "Show label" );
1288+
showLabelCheckbox->setChecked( itemData.showLabel() );
1289+
baseLayout->addWidget( showLabelCheckbox );
1290+
QWidget* baseWidget = new QWidget();
1291+
baseWidget->setLayout( baseLayout );
1292+
12711293
if ( itemData.type() == QgsFieldsProperties::DesignerTreeItemData::Container )
12721294
{
12731295
QDialog dlg;
12741296
dlg.setWindowTitle( tr( "Configure container" ) );
12751297
QFormLayout* layout = new QFormLayout() ;
12761298
dlg.setLayout( layout );
1299+
layout->addWidget( baseWidget );
12771300

12781301
QCheckBox* showAsGroupBox = nullptr;
12791302
QLineEdit* title = new QLineEdit( itemData.name() );
@@ -1304,10 +1327,34 @@ void DesignerTree::onItemDoubleClicked( QTreeWidgetItem* item, int column )
13041327
itemData.setColumnCount( columnCount->value() );
13051328
itemData.setShowAsGroupBox( showAsGroupBox ? showAsGroupBox->isChecked() : true );
13061329
itemData.setName( title->text() );
1330+
itemData.setShowLabel( showLabelCheckbox->isChecked() );
1331+
13071332
item->setData( 0, QgsFieldsProperties::DesignerTreeRole, itemData.asQVariant() );
13081333
item->setText( 0, title->text() );
13091334
}
13101335
}
1336+
else
1337+
{
1338+
QDialog dlg;
1339+
dlg.setWindowTitle( tr( "Configure container" ) );
1340+
dlg.setLayout( new QGridLayout() );
1341+
dlg.layout()->addWidget( baseWidget );
1342+
1343+
QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok
1344+
| QDialogButtonBox::Cancel );
1345+
1346+
connect( buttonBox, SIGNAL( accepted() ), &dlg, SLOT( accept() ) );
1347+
connect( buttonBox, SIGNAL( rejected() ), &dlg, SLOT( reject() ) );
1348+
1349+
dlg.layout()->addWidget( buttonBox );
1350+
1351+
if ( dlg.exec() )
1352+
{
1353+
itemData.setShowLabel( showLabelCheckbox->isChecked() );
1354+
1355+
item->setData( 0, QgsFieldsProperties::DesignerTreeRole, itemData.asQVariant() );
1356+
}
1357+
}
13111358
}
13121359

13131360
/*
@@ -1342,3 +1389,13 @@ void QgsFieldsProperties::DesignerTreeItemData::setShowAsGroupBox( bool showAsGr
13421389
{
13431390
mShowAsGroupBox = showAsGroupBox;
13441391
}
1392+
1393+
bool QgsFieldsProperties::DesignerTreeItemData::showLabel() const
1394+
{
1395+
return mShowLabel;
1396+
}
1397+
1398+
void QgsFieldsProperties::DesignerTreeItemData::setShowLabel( bool showLabel )
1399+
{
1400+
mShowLabel = showLabel;
1401+
}

‎src/app/qgsfieldsproperties.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
7979
bool showAsGroupBox() const;
8080
void setShowAsGroupBox( bool showAsGroupBox );
8181

82+
bool showLabel() const;
83+
void setShowLabel( bool showLabel );
84+
8285
private:
8386
Type mType;
8487
QString mName;
8588
int mColumnCount;
8689
bool mShowAsGroupBox;
90+
bool mShowLabel;
8791
};
8892

8993
/**

‎src/core/qgseditformconfig.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ QgsAttributeEditorElement* QgsEditFormConfig::attributeEditorElementFromDomEleme
452452
QString name = elem.attribute( "name" );
453453
newElement = new QgsAttributeEditorRelation( name, elem.attribute( "relation", "[None]" ), parent );
454454
}
455+
456+
if ( elem.hasAttribute( "showLabel" ) )
457+
newElement->setShowLabel( elem.attribute( "showLabel" ).toInt() );
458+
else
459+
newElement->setShowLabel( true );
460+
455461
return newElement;
456462
}
457463

@@ -487,3 +493,98 @@ void QgsAttributeEditorContainer::setColumnCount( int columnCount )
487493
{
488494
mColumnCount = columnCount;
489495
}
496+
497+
void QgsAttributeEditorContainer::saveConfiguration( QDomElement& elem ) const
498+
{
499+
elem.setAttribute( "columnCount", mColumnCount );
500+
elem.setAttribute( "groupBox", mIsGroupBox ? 1 : 0 );
501+
502+
Q_FOREACH ( QgsAttributeEditorElement* child, mChildren )
503+
{
504+
QDomDocument doc = elem.ownerDocument();
505+
elem.appendChild( child->toDomElement( doc ) );
506+
}
507+
}
508+
509+
QString QgsAttributeEditorContainer::typeIdentifier() const
510+
{
511+
return "attributeEditorContainer";
512+
}
513+
514+
void QgsAttributeEditorContainer::addChildElement( QgsAttributeEditorElement *widget )
515+
{
516+
mChildren.append( widget );
517+
}
518+
519+
void QgsAttributeEditorContainer::setName( const QString& name )
520+
{
521+
mName = name;
522+
}
523+
524+
QList<QgsAttributeEditorElement*> QgsAttributeEditorContainer::findElements( QgsAttributeEditorElement::AttributeEditorType type ) const
525+
{
526+
QList<QgsAttributeEditorElement*> results;
527+
528+
Q_FOREACH ( QgsAttributeEditorElement* elem, mChildren )
529+
{
530+
if ( elem->type() == type )
531+
{
532+
results.append( elem );
533+
}
534+
535+
if ( elem->type() == AeTypeContainer )
536+
{
537+
QgsAttributeEditorContainer* cont = dynamic_cast<QgsAttributeEditorContainer*>( elem );
538+
if ( cont )
539+
results += cont->findElements( type );
540+
}
541+
}
542+
543+
return results;
544+
}
545+
546+
void QgsAttributeEditorField::saveConfiguration( QDomElement &elem ) const
547+
{
548+
elem.setAttribute( "index", mIdx );
549+
}
550+
551+
QString QgsAttributeEditorField::typeIdentifier() const
552+
{
553+
return "attributeEditorField";
554+
}
555+
556+
QDomElement QgsAttributeEditorElement::toDomElement( QDomDocument& doc ) const
557+
{
558+
QDomElement elem = doc.createElement( typeIdentifier() );
559+
elem.setAttribute( "name", mName );
560+
elem.setAttribute( "showLabel", mShowLabel );
561+
562+
saveConfiguration( elem );
563+
return elem;
564+
}
565+
566+
bool QgsAttributeEditorElement::showLabel() const
567+
{
568+
return mShowLabel;
569+
}
570+
571+
void QgsAttributeEditorElement::setShowLabel( bool showLabel )
572+
{
573+
mShowLabel = showLabel;
574+
}
575+
576+
void QgsAttributeEditorRelation::saveConfiguration( QDomElement& elem ) const
577+
{
578+
elem.setAttribute( "relation", mRelation.id() );
579+
}
580+
581+
QString QgsAttributeEditorRelation::typeIdentifier() const
582+
{
583+
return "attributeEditorField";
584+
}
585+
586+
bool QgsAttributeEditorRelation::init( QgsRelationManager* relationManager )
587+
{
588+
mRelation = relationManager->relation( mRelationId );
589+
return mRelation.isValid();
590+
}

‎src/core/qgseditformconfig.h

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CORE_EXPORT QgsAttributeEditorElement : public QObject
5656
: QObject( parent )
5757
, mType( type )
5858
, mName( name )
59+
, mShowLabel( true )
5960
{}
6061

6162
//! Destructor
@@ -76,17 +77,48 @@ class CORE_EXPORT QgsAttributeEditorElement : public QObject
7677
AttributeEditorType type() const { return mType; }
7778

7879
/**
79-
* Is reimplemented in classes inheriting from this to serialize it.
80+
* Get the XML Dom element to save this element.
8081
*
8182
* @param doc The QDomDocument which is used to create new XML elements
8283
*
83-
* @return An DOM element which represents this element
84+
* @return A DOM element to serialize this element
8485
*/
85-
virtual QDomElement toDomElement( QDomDocument& doc ) const = 0;
86+
QDomElement toDomElement( QDomDocument& doc ) const;
87+
88+
/**
89+
* Controls if this element should be labeled with a title (field, relation or groupname).
90+
*
91+
* @note Added in QGIS 2.18
92+
*/
93+
bool showLabel() const;
94+
95+
/**
96+
* Controls if this element should be labeled with a title (field, relation or groupname).
97+
*
98+
* @note Added in QGIS 2.18
99+
*/
100+
void setShowLabel( bool showLabel );
86101

87102
protected:
88103
AttributeEditorType mType;
89104
QString mName;
105+
bool mShowLabel;
106+
107+
private:
108+
/**
109+
* Should be implemented by subclasses to save type specific configuration.
110+
*
111+
* @note Added in QGIS 2.18
112+
*/
113+
virtual void saveConfiguration( QDomElement& elem ) const = 0;
114+
115+
/**
116+
* All subclasses need to overwrite this method and return a type specific identifier.
117+
* Needs to be XML key compatible.
118+
*
119+
* @note Added in QGIS 2.18
120+
*/
121+
virtual QString typeIdentifier() const = 0;
90122
};
91123

92124

@@ -114,15 +146,6 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
114146
//! Destructor
115147
virtual ~QgsAttributeEditorContainer() {}
116148

117-
/**
118-
* Will serialize this containers information into a QDomElement for saving it in an XML file.
119-
*
120-
* @param doc The QDomDocument used to generate the QDomElement
121-
*
122-
* @return The XML element
123-
*/
124-
virtual QDomElement toDomElement( QDomDocument& doc ) const override;
125-
126149
/**
127150
* Add a child element to this container. This may be another container, a field or a relation.
128151
*
@@ -176,6 +199,9 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement
176199
void setColumnCount( int columnCount );
177200

178201
private:
202+
virtual void saveConfiguration( QDomElement& elem ) const override;
203+
virtual QString typeIdentifier() const override;
204+
179205
bool mIsGroupBox;
180206
QList<QgsAttributeEditorElement*> mChildren;
181207
int mColumnCount;
@@ -204,22 +230,15 @@ class CORE_EXPORT QgsAttributeEditorField : public QgsAttributeEditorElement
204230
//! Destructor
205231
virtual ~QgsAttributeEditorField() {}
206232

207-
/**
208-
* Will serialize this elements information into a QDomElement for saving it in an XML file.
209-
*
210-
* @param doc The QDomDocument used to generate the QDomElement
211-
*
212-
* @return The XML element
213-
*/
214-
virtual QDomElement toDomElement( QDomDocument& doc ) const override;
215-
216233
/**
217234
* Return the index of the field
218235
* @return
219236
*/
220237
int idx() const { return mIdx; }
221238

222239
private:
240+
virtual void saveConfiguration( QDomElement& elem ) const override;
241+
virtual QString typeIdentifier() const override;
223242
int mIdx;
224243
};
225244

@@ -257,15 +276,6 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement
257276
//! Destructor
258277
virtual ~QgsAttributeEditorRelation() {}
259278

260-
/**
261-
* Will serialize this elements information into a QDomElement for saving it in an XML file.
262-
*
263-
* @param doc The QDomDocument used to generate the QDomElement
264-
*
265-
* @return The XML element
266-
*/
267-
virtual QDomElement toDomElement( QDomDocument& doc ) const override;
268-
269279
/**
270280
* Get the id of the relation which shall be embedded
271281
*
@@ -282,6 +292,8 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement
282292
bool init( QgsRelationManager *relManager );
283293

284294
private:
295+
virtual void saveConfiguration( QDomElement& elem ) const override;
296+
virtual QString typeIdentifier() const override;
285297
QString mRelationId;
286298
QgsRelation mRelation;
287299
};

‎src/core/qgsvectorlayer.cpp

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,60 +4131,6 @@ QList<QgsRelation> QgsVectorLayer::referencingRelations( int idx )
41314131
return QgsProject::instance()->relationManager()->referencingRelations( this, idx );
41324132
}
41334133

4134-
QDomElement QgsAttributeEditorContainer::toDomElement( QDomDocument& doc ) const
4135-
{
4136-
QDomElement elem = doc.createElement( "attributeEditorContainer" );
4137-
elem.setAttribute( "name", mName );
4138-
elem.setAttribute( "columnCount", mColumnCount );
4139-
elem.setAttribute( "groupBox", mIsGroupBox ? 1 : 0 );
4140-
4141-
Q_FOREACH ( QgsAttributeEditorElement* child, mChildren )
4142-
{
4143-
elem.appendChild( child->toDomElement( doc ) );
4144-
}
4145-
return elem;
4146-
}
4147-
4148-
void QgsAttributeEditorContainer::addChildElement( QgsAttributeEditorElement *widget )
4149-
{
4150-
mChildren.append( widget );
4151-
}
4152-
4153-
void QgsAttributeEditorContainer::setName( const QString& name )
4154-
{
4155-
mName = name;
4156-
}
4157-
4158-
QList<QgsAttributeEditorElement*> QgsAttributeEditorContainer::findElements( QgsAttributeEditorElement::AttributeEditorType type ) const
4159-
{
4160-
QList<QgsAttributeEditorElement*> results;
4161-
4162-
Q_FOREACH ( QgsAttributeEditorElement* elem, mChildren )
4163-
{
4164-
if ( elem->type() == type )
4165-
{
4166-
results.append( elem );
4167-
}
4168-
4169-
if ( elem->type() == AeTypeContainer )
4170-
{
4171-
QgsAttributeEditorContainer* cont = dynamic_cast<QgsAttributeEditorContainer*>( elem );
4172-
if ( cont )
4173-
results += cont->findElements( type );
4174-
}
4175-
}
4176-
4177-
return results;
4178-
}
4179-
4180-
QDomElement QgsAttributeEditorField::toDomElement( QDomDocument& doc ) const
4181-
{
4182-
QDomElement elem = doc.createElement( "attributeEditorField" );
4183-
elem.setAttribute( "name", mName );
4184-
elem.setAttribute( "index", mIdx );
4185-
return elem;
4186-
}
4187-
41884134
int QgsVectorLayer::listStylesInDatabase( QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError )
41894135
{
41904136
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
@@ -4311,21 +4257,6 @@ bool QgsVectorLayer::applyNamedStyle( const QString& namedStyle, QString& errorM
43114257
return importNamedStyle( myDocument, errorMsg );
43124258
}
43134259

4314-
4315-
QDomElement QgsAttributeEditorRelation::toDomElement( QDomDocument& doc ) const
4316-
{
4317-
QDomElement elem = doc.createElement( "attributeEditorRelation" );
4318-
elem.setAttribute( "name", mName );
4319-
elem.setAttribute( "relation", mRelation.id() );
4320-
return elem;
4321-
}
4322-
4323-
bool QgsAttributeEditorRelation::init( QgsRelationManager* relationManager )
4324-
{
4325-
mRelation = relationManager->relation( mRelationId );
4326-
return mRelation.isValid();
4327-
}
4328-
43294260
QSet<QString> QgsVectorLayer::layerDependencies() const
43304261
{
43314262
if ( mDataProvider )

0 commit comments

Comments
 (0)
Please sign in to comment.