Skip to content
Permalink
Browse files

[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
  • Loading branch information
m-kuhn committed Aug 19, 2016
1 parent 78845c6 commit f10f4a5a12bd2bc23cd8f98eacdf42ab5caca18d
@@ -15,7 +15,7 @@
* *
***************************************************************************/

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

/**
* Is reimplemented in classes inheriting from this to serialize it.
* Get the XML Dom element to save this element.
*
* @param doc The QDomDocument which is used to create new XML elements
*
* @return An DOM element which represents this element
* @return A DOM element to serialize this element
*/
virtual QDomElement toDomElement( QDomDocument& doc ) const = 0;
QDomElement toDomElement( QDomDocument& doc ) const;

/**
* Controls if this element should be labeled with a title (field, relation or groupname).
*
* @note Added in QGIS 2.18
*/
bool showLabel() const;

/**
* Controls if this element should be labeled with a title (field, relation or groupname).
*
* @note Added in QGIS 2.18
*/
void setShowLabel(bool showLabel);

};

class QgsAttributeEditorContainer : QgsAttributeEditorElement
@@ -161,16 +161,26 @@ QTreeWidgetItem *QgsFieldsProperties::loadAttributeEditorTreeItem( QgsAttributeE
switch ( widgetDef->type() )
{
case QgsAttributeEditorElement::AeTypeField:
newWidget = mDesignerTree->addItem( parent, DesignerTreeItemData( DesignerTreeItemData::Field, widgetDef->name() ) );
{
DesignerTreeItemData itemData = DesignerTreeItemData( DesignerTreeItemData::Field, widgetDef->name() );
itemData.setShowLabel( widgetDef->showLabel() );
newWidget = mDesignerTree->addItem( parent, itemData );
break;
}

case QgsAttributeEditorElement::AeTypeRelation:
newWidget = mDesignerTree->addItem( parent, DesignerTreeItemData( DesignerTreeItemData::Relation, widgetDef->name() ) );
{
DesignerTreeItemData itemData = DesignerTreeItemData( DesignerTreeItemData::Relation, widgetDef->name() );
itemData.setShowLabel( widgetDef->showLabel() );

newWidget = mDesignerTree->addItem( parent, itemData );
break;
}

case QgsAttributeEditorElement::AeTypeContainer:
{
DesignerTreeItemData itemData( DesignerTreeItemData::Container, widgetDef->name() );
itemData.setShowLabel( widgetDef->showLabel() );

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

widgetDef->setShowLabel( itemData.showLabel() );

return widgetDef;
}

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

QGroupBox* baseData = new QGroupBox( tr( "Base configuration" ) );

QFormLayout* baseLayout = new QFormLayout();
baseData->setLayout( baseLayout );
QCheckBox* showLabelCheckbox = new QCheckBox( "Show label" );
showLabelCheckbox->setChecked( itemData.showLabel() );
baseLayout->addWidget( showLabelCheckbox );
QWidget* baseWidget = new QWidget();
baseWidget->setLayout( baseLayout );

if ( itemData.type() == QgsFieldsProperties::DesignerTreeItemData::Container )
{
QDialog dlg;
dlg.setWindowTitle( tr( "Configure container" ) );
QFormLayout* layout = new QFormLayout() ;
dlg.setLayout( layout );
layout->addWidget( baseWidget );

QCheckBox* showAsGroupBox = nullptr;
QLineEdit* title = new QLineEdit( itemData.name() );
@@ -1304,10 +1327,34 @@ void DesignerTree::onItemDoubleClicked( QTreeWidgetItem* item, int column )
itemData.setColumnCount( columnCount->value() );
itemData.setShowAsGroupBox( showAsGroupBox ? showAsGroupBox->isChecked() : true );
itemData.setName( title->text() );
itemData.setShowLabel( showLabelCheckbox->isChecked() );

item->setData( 0, QgsFieldsProperties::DesignerTreeRole, itemData.asQVariant() );
item->setText( 0, title->text() );
}
}
else
{
QDialog dlg;
dlg.setWindowTitle( tr( "Configure container" ) );
dlg.setLayout( new QGridLayout() );
dlg.layout()->addWidget( baseWidget );

QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok
| QDialogButtonBox::Cancel );

connect( buttonBox, SIGNAL( accepted() ), &dlg, SLOT( accept() ) );
connect( buttonBox, SIGNAL( rejected() ), &dlg, SLOT( reject() ) );

dlg.layout()->addWidget( buttonBox );

if ( dlg.exec() )
{
itemData.setShowLabel( showLabelCheckbox->isChecked() );

item->setData( 0, QgsFieldsProperties::DesignerTreeRole, itemData.asQVariant() );
}
}
}

/*
@@ -1342,3 +1389,13 @@ void QgsFieldsProperties::DesignerTreeItemData::setShowAsGroupBox( bool showAsGr
{
mShowAsGroupBox = showAsGroupBox;
}

bool QgsFieldsProperties::DesignerTreeItemData::showLabel() const
{
return mShowLabel;
}

void QgsFieldsProperties::DesignerTreeItemData::setShowLabel( bool showLabel )
{
mShowLabel = showLabel;
}
@@ -79,11 +79,15 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
bool showAsGroupBox() const;
void setShowAsGroupBox( bool showAsGroupBox );

bool showLabel() const;
void setShowLabel( bool showLabel );

private:
Type mType;
QString mName;
int mColumnCount;
bool mShowAsGroupBox;
bool mShowLabel;
};

/**
@@ -452,6 +452,12 @@ QgsAttributeEditorElement* QgsEditFormConfig::attributeEditorElementFromDomEleme
QString name = elem.attribute( "name" );
newElement = new QgsAttributeEditorRelation( name, elem.attribute( "relation", "[None]" ), parent );
}

if ( elem.hasAttribute( "showLabel" ) )
newElement->setShowLabel( elem.attribute( "showLabel" ).toInt() );
else
newElement->setShowLabel( true );

return newElement;
}

@@ -487,3 +493,98 @@ void QgsAttributeEditorContainer::setColumnCount( int columnCount )
{
mColumnCount = columnCount;
}

void QgsAttributeEditorContainer::saveConfiguration( QDomElement& elem ) const
{
elem.setAttribute( "columnCount", mColumnCount );
elem.setAttribute( "groupBox", mIsGroupBox ? 1 : 0 );

Q_FOREACH ( QgsAttributeEditorElement* child, mChildren )
{
QDomDocument doc = elem.ownerDocument();
elem.appendChild( child->toDomElement( doc ) );
}
}

QString QgsAttributeEditorContainer::typeIdentifier() const
{
return "attributeEditorContainer";
}

void QgsAttributeEditorContainer::addChildElement( QgsAttributeEditorElement *widget )
{
mChildren.append( widget );
}

void QgsAttributeEditorContainer::setName( const QString& name )
{
mName = name;
}

QList<QgsAttributeEditorElement*> QgsAttributeEditorContainer::findElements( QgsAttributeEditorElement::AttributeEditorType type ) const
{
QList<QgsAttributeEditorElement*> results;

Q_FOREACH ( QgsAttributeEditorElement* elem, mChildren )
{
if ( elem->type() == type )
{
results.append( elem );
}

if ( elem->type() == AeTypeContainer )
{
QgsAttributeEditorContainer* cont = dynamic_cast<QgsAttributeEditorContainer*>( elem );
if ( cont )
results += cont->findElements( type );
}
}

return results;
}

void QgsAttributeEditorField::saveConfiguration( QDomElement &elem ) const
{
elem.setAttribute( "index", mIdx );
}

QString QgsAttributeEditorField::typeIdentifier() const
{
return "attributeEditorField";
}

QDomElement QgsAttributeEditorElement::toDomElement( QDomDocument& doc ) const
{
QDomElement elem = doc.createElement( typeIdentifier() );
elem.setAttribute( "name", mName );
elem.setAttribute( "showLabel", mShowLabel );

saveConfiguration( elem );
return elem;
}

bool QgsAttributeEditorElement::showLabel() const
{
return mShowLabel;
}

void QgsAttributeEditorElement::setShowLabel( bool showLabel )
{
mShowLabel = showLabel;
}

void QgsAttributeEditorRelation::saveConfiguration( QDomElement& elem ) const
{
elem.setAttribute( "relation", mRelation.id() );
}

QString QgsAttributeEditorRelation::typeIdentifier() const
{
return "attributeEditorField";
}

bool QgsAttributeEditorRelation::init( QgsRelationManager* relationManager )
{
mRelation = relationManager->relation( mRelationId );
return mRelation.isValid();
}

0 comments on commit f10f4a5

Please sign in to comment.
You can’t perform that action at this time.