Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Spacer widget line option
  • Loading branch information
elpaso committed Jan 17, 2023
1 parent 6cd867e commit e3c4db9
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 10 deletions.
Expand Up @@ -31,6 +31,17 @@ Creates a new element which represents a spacer
virtual QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const /Factory/;


bool drawLine() const;
%Docstring
Returns ``True`` if the spacer element will contain an horizontal line.
%End

void setDrawLine( bool drawLine );
%Docstring
Sets a flag to define if the spacer element will contain an horizontal line.

:param drawLine: flag status
%End

};

Expand Down
Expand Up @@ -29,17 +29,29 @@ Create a new widget wrapper
:param parent: A parent widget for this widget wrapper and the created widget.
%End

public:
virtual bool valid() const;


bool drawLine() const;
%Docstring
Returns ``True`` if the spacer element will contain an horizontal line.
%End

void setDrawLine( bool drawLine );
%Docstring
Sets a flag to define if the spacer element will contain an horizontal line.

:param drawLine: flag status
%End

protected:
virtual QWidget *createWidget( QWidget *parent );


public slots:
virtual void setFeature( const QgsFeature &feature );


};

/************************************************************************
Expand Down
19 changes: 16 additions & 3 deletions src/core/editform/qgsattributeeditorspacerelement.cpp
Expand Up @@ -23,21 +23,34 @@ QgsAttributeEditorElement *QgsAttributeEditorSpacerElement::clone( QgsAttributeE

void QgsAttributeEditorSpacerElement::saveConfiguration( QDomElement &elem, QDomDocument &doc ) const
{
// Nothing to save
Q_UNUSED( elem );
Q_UNUSED( doc );
elem.setAttribute( QStringLiteral( "drawLine" ), mDrawLine ? 1 : 0 );
}

void QgsAttributeEditorSpacerElement::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
{
// Nothing to load
Q_UNUSED( element );
Q_UNUSED( layerId );
Q_UNUSED( context );
Q_UNUSED( fields );

bool ok;
const bool drawLine = element.attribute( QStringLiteral( "drawLine" ) ).toInt( &ok );
if ( ok )
mDrawLine = drawLine;
}

QString QgsAttributeEditorSpacerElement::typeIdentifier() const
{
return QStringLiteral( "attributeEditorSpacerElement" );
}

bool QgsAttributeEditorSpacerElement::drawLine() const
{
return mDrawLine;
}

void QgsAttributeEditorSpacerElement::setDrawLine( bool drawLine )
{
mDrawLine = drawLine;
}
12 changes: 11 additions & 1 deletion src/core/editform/qgsattributeeditorspacerelement.h
Expand Up @@ -41,12 +41,22 @@ class CORE_EXPORT QgsAttributeEditorSpacerElement : public QgsAttributeEditorEle

QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const override SIP_FACTORY;

/**
* Returns TRUE if the spacer element will contain an horizontal line.
*/
bool drawLine() const;

/**
* Sets a flag to define if the spacer element will contain an horizontal line.
* \param drawLine flag status
*/
void setDrawLine( bool drawLine );

// QgsAttributeEditorElement interface
private:
void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const override;
void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) override;
QString typeIdentifier() const override;
bool mDrawLine = false;
};

#endif // QGSATTRIBUTEEDITORSPACERELEMENT_H
15 changes: 14 additions & 1 deletion src/gui/editorwidgets/qgsspacerwidgetwrapper.cpp
Expand Up @@ -15,6 +15,7 @@
***************************************************************************/

#include "qgsspacerwidgetwrapper.h"
#include "qframe.h"


QgsSpacerWidgetWrapper::QgsSpacerWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
Expand All @@ -30,10 +31,22 @@ bool QgsSpacerWidgetWrapper::valid() const

QWidget *QgsSpacerWidgetWrapper::createWidget( QWidget *parent )
{
return new QWidget( parent );
QFrame *hFrame = new QFrame( parent );
hFrame->setFrameShape( mDrawLine ? QFrame::HLine : QFrame::NoFrame );
return hFrame;
}

void QgsSpacerWidgetWrapper::setFeature( const QgsFeature &feature )
{
Q_UNUSED( feature );
}

bool QgsSpacerWidgetWrapper::drawLine() const
{
return mDrawLine;
}

void QgsSpacerWidgetWrapper::setDrawLine( bool drawLine )
{
mDrawLine = drawLine;
}
17 changes: 15 additions & 2 deletions src/gui/editorwidgets/qgsspacerwidgetwrapper.h
Expand Up @@ -38,15 +38,28 @@ class GUI_EXPORT QgsSpacerWidgetWrapper : public QgsWidgetWrapper
*/
explicit QgsSpacerWidgetWrapper( QgsVectorLayer *layer, QWidget *editor = nullptr, QWidget *parent = nullptr );

// QgsWidgetWrapper interface
public:
bool valid() const override;

/**
* Returns TRUE if the spacer element will contain an horizontal line.
*/
bool drawLine() const;

/**
* Sets a flag to define if the spacer element will contain an horizontal line.
* \param drawLine flag status
*/
void setDrawLine( bool drawLine );

protected:
QWidget *createWidget( QWidget *parent ) override;

public slots:
void setFeature( const QgsFeature &feature ) override;

private:

bool mDrawLine = false;
};

#endif // QGSSPACERWIDGETWRAPPER_H
3 changes: 3 additions & 0 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -15,6 +15,7 @@

#include "qgsattributeform.h"

#include "qgsattributeeditorspacerelement.h"
#include "qgsattributeforminterface.h"
#include "qgsattributeformlegacyinterface.h"
#include "qgsattributeformrelationeditorwidget.h"
Expand Down Expand Up @@ -2515,7 +2516,9 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt

case QgsAttributeEditorElement::AeTypeSpacerElement:
{
const QgsAttributeEditorSpacerElement *elementDef = static_cast<const QgsAttributeEditorSpacerElement *>( widgetDef );
QgsSpacerWidgetWrapper *spacerWrapper = new QgsSpacerWidgetWrapper( mLayer, nullptr, this );
spacerWrapper->setDrawLine( elementDef->drawLine() );
context.setAttributeFormMode( mMode );
mWidgets.append( spacerWrapper );

Expand Down
47 changes: 46 additions & 1 deletion src/gui/vector/qgsattributesformproperties.cpp
Expand Up @@ -549,11 +549,14 @@ QTreeWidgetItem *QgsAttributesFormProperties::loadAttributeEditorTreeItem( QgsAt

case QgsAttributeEditorElement::AeTypeSpacerElement:
{
const QgsAttributeEditorSpacerElement *spacerElementEditor = static_cast<const QgsAttributeEditorSpacerElement *>( widgetDef );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::SpacerWidget, widgetDef->name(), widgetDef->name() );
itemData.setShowLabel( widgetDef->showLabel() );
SpacerElementEditorConfiguration spacerEdConfig;
spacerEdConfig.drawLine = spacerElementEditor->drawLine();
itemData.setSpacerElementEditorConfiguration( spacerEdConfig );
itemData.setLabelStyle( widgetDef->labelStyle() );
itemData.setShowLabel( false );
newWidget = tree->addItem( parent, itemData );
break;
}
Expand Down Expand Up @@ -823,6 +826,7 @@ QgsAttributeEditorElement *QgsAttributesFormProperties::createAttributeEditorWid
case DnDTreeItemData::SpacerWidget:
{
QgsAttributeEditorSpacerElement *element = new QgsAttributeEditorSpacerElement( item->text( 0 ), parent );
element->setDrawLine( itemData.spacerElementEditorConfiguration().drawLine );
widgetDef = element;
break;
}
Expand Down Expand Up @@ -1283,7 +1287,6 @@ void QgsAttributesDnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int colum
case QgsAttributesFormProperties::DnDTreeItemData::WidgetType:
case QgsAttributesFormProperties::DnDTreeItemData::Relation:
case QgsAttributesFormProperties::DnDTreeItemData::Field:
case QgsAttributesFormProperties::DnDTreeItemData::SpacerWidget:
break;

case QgsAttributesFormProperties::DnDTreeItemData::QmlWidget:
Expand Down Expand Up @@ -1611,6 +1614,48 @@ void QgsAttributesDnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int colum
}
break;
}

case QgsAttributesFormProperties::DnDTreeItemData::SpacerWidget:
{
if ( mType == QgsAttributesDnDTree::Type::Drag )
return;
QDialog dlg;
dlg.setWindowTitle( tr( "Configure Spacer Widget" ) );

QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addWidget( new QLabel( tr( "Title" ) ) );
QLineEdit *title = new QLineEdit( itemData.name() );
mainLayout->addWidget( title );

QHBoxLayout *cbLayout = new QHBoxLayout( );
mainLayout->addLayout( cbLayout );
dlg.setLayout( mainLayout );
QCheckBox *cb = new QCheckBox { &dlg };
cb->setChecked( itemData.spacerElementEditorConfiguration().drawLine );
cbLayout->addWidget( new QLabel( tr( "Draw horizontal line" ), &dlg ) );
cbLayout->addWidget( cb );


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

connect( buttonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
connect( buttonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject );

mainLayout->addWidget( buttonBox );

if ( dlg.exec() )
{
QgsAttributesFormProperties::SpacerElementEditorConfiguration spacerEdCfg;
spacerEdCfg.drawLine = cb->isChecked();
itemData.setSpacerElementEditorConfiguration( spacerEdCfg );
itemData.setShowLabel( false );
itemData.setName( title->text() );
item->setData( 0, QgsAttributesFormProperties::DnDTreeRole, itemData );
item->setText( 0, title->text() );
}

break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/vector/qgsattributesformproperties.h
Expand Up @@ -99,7 +99,7 @@ class GUI_EXPORT QgsAttributesFormProperties : public QWidget, public QgsExpress

struct SpacerElementEditorConfiguration
{
// TODO: not sure yet if we need config for this kind of element
bool drawLine;
};

/**
Expand Down

0 comments on commit e3c4db9

Please sign in to comment.