Skip to content
Permalink
Browse files

Use registry to create 3d symbol widgets, avoid hardcoded widgets

  • Loading branch information
nyalldawson committed Jul 29, 2020
1 parent 2ad4337 commit b1d02619f5f170ab2c7c805321142527eceb0d7b
@@ -37,6 +37,18 @@ Sets the ``symbol`` to show in the widget.
Returns a new instance of the symbol defined by the widget.

Caller takes ownership of the returned symbol.
%End

virtual QString symbolType() const = 0;
%Docstring
Returns the symbol type handled by the widget.
%End

signals:

void changed();
%Docstring
Emitted when the symbol is changed.
%End
};

@@ -75,6 +75,11 @@ QgsAbstract3DSymbol *QgsLine3DSymbolWidget::symbol()
return sym.release();
}

QString QgsLine3DSymbolWidget::symbolType() const
{
return QStringLiteral( "line" );
}

void QgsLine3DSymbolWidget::updateGuiState()
{
bool simple = chkSimpleLines->isChecked();
@@ -35,14 +35,11 @@ class QgsLine3DSymbolWidget : public Qgs3DSymbolWidget, private Ui::Line3DSymbol

void setSymbol( const QgsAbstract3DSymbol *symbol, QgsVectorLayer *layer ) override;
QgsAbstract3DSymbol *symbol() override;
QString symbolType() const override;

private slots:
void updateGuiState();

signals:
void changed();

public slots:
};

#endif // QGSLINE3DSYMBOLWIDGET_H
@@ -232,6 +232,11 @@ QgsAbstract3DSymbol *QgsPoint3DSymbolWidget::symbol()
return sym.release();
}

QString QgsPoint3DSymbolWidget::symbolType() const
{
return QStringLiteral( "point" );
}

void QgsPoint3DSymbolWidget::onShapeChanged()
{
QList<QWidget *> allWidgets;
@@ -33,9 +33,7 @@ class QgsPoint3DSymbolWidget : public Qgs3DSymbolWidget, private Ui::Point3DSymb

void setSymbol( const QgsAbstract3DSymbol *symbol, QgsVectorLayer *layer ) override;
QgsAbstract3DSymbol *symbol() override;

signals:
void changed();
QString symbolType() const override;

private slots:
void onShapeChanged();
@@ -100,3 +100,8 @@ QgsAbstract3DSymbol *QgsPolygon3DSymbolWidget::symbol()

return sym.release();
}

QString QgsPolygon3DSymbolWidget::symbolType() const
{
return QStringLiteral( "polygon" );
}
@@ -32,11 +32,8 @@ class QgsPolygon3DSymbolWidget : public Qgs3DSymbolWidget, private Ui::Polygon3D

void setSymbol( const QgsAbstract3DSymbol *symbol, QgsVectorLayer *layer ) override;
QgsAbstract3DSymbol *symbol() override;
QString symbolType() const override;

signals:
void changed();

public slots:
};

#endif // QGSPOLYGON3DSYMBOLWIDGET_H
@@ -14,19 +14,14 @@
***************************************************************************/

#include "qgssymbol3dwidget.h"

#include "qgsline3dsymbol.h"
#include "qgspoint3dsymbol.h"
#include "qgspolygon3dsymbol.h"

#include "qgsline3dsymbolwidget.h"
#include "qgspoint3dsymbolwidget.h"
#include "qgspolygon3dsymbolwidget.h"
#include "qgsabstractmaterialsettings.h"
#include "qgsstyleitemslistwidget.h"
#include "qgsstylesavedialog.h"
#include "qgsvectorlayer.h"

#include "qgsapplication.h"
#include "qgs3dsymbolregistry.h"
#include "qgs3dsymbolwidget.h"
#include "qgsabstract3dsymbol.h"
#include <QStackedWidget>
#include <QMessageBox>

@@ -36,15 +31,9 @@ QgsSymbol3DWidget::QgsSymbol3DWidget( QgsVectorLayer *layer, QWidget *parent )
, mLayer( layer )
{
widgetUnsupported = new QLabel( tr( "Sorry, this symbol is not supported." ), this );
widgetLine = new QgsLine3DSymbolWidget( this );
widgetPoint = new QgsPoint3DSymbolWidget( this );
widgetPolygon = new QgsPolygon3DSymbolWidget( this );

widgetStack = new QStackedWidget( this );
widgetStack->addWidget( widgetUnsupported );
widgetStack->addWidget( widgetLine );
widgetStack->addWidget( widgetPoint );
widgetStack->addWidget( widgetPolygon );

QVBoxLayout *layout = new QVBoxLayout( this );
layout->setContentsMargins( 0, 0, 0, 0 );
@@ -59,80 +48,33 @@ QgsSymbol3DWidget::QgsSymbol3DWidget( QgsVectorLayer *layer, QWidget *parent )
connect( mStyleWidget, &QgsStyleItemsListWidget::saveEntity, this, &QgsSymbol3DWidget::saveSymbol );

layout->addWidget( mStyleWidget );

connect( widgetLine, &QgsLine3DSymbolWidget::changed, this, &QgsSymbol3DWidget::widgetChanged );
connect( widgetPoint, &QgsPoint3DSymbolWidget::changed, this, &QgsSymbol3DWidget::widgetChanged );
connect( widgetPolygon, &QgsPolygon3DSymbolWidget::changed, this, &QgsSymbol3DWidget::widgetChanged );
}

std::unique_ptr<QgsAbstract3DSymbol> QgsSymbol3DWidget::symbol()
{
int pageIndex = widgetStack->currentIndex();
if ( pageIndex == 1 || pageIndex == 2 || pageIndex == 3 )
if ( Qgs3DSymbolWidget *w = qobject_cast< Qgs3DSymbolWidget * >( widgetStack->currentWidget() ) )
{
std::unique_ptr< QgsAbstract3DSymbol > sym;
if ( pageIndex == 1 )
sym.reset( widgetLine->symbol() );
else if ( pageIndex == 2 )
sym.reset( widgetPoint->symbol() );
else
sym.reset( widgetPolygon->symbol() );
return sym;
return std::unique_ptr< QgsAbstract3DSymbol >( w->symbol() );
}
return nullptr;
}

void QgsSymbol3DWidget::setSymbol( const QgsAbstract3DSymbol *symbol, QgsVectorLayer *vlayer )
{
int pageIndex;
mLayer = vlayer;
mStyleWidget->setLayerType( mLayer->geometryType() );
switch ( mLayer->geometryType() )

if ( Qgs3DSymbolWidget *w = qobject_cast< Qgs3DSymbolWidget * >( widgetStack->currentWidget() ) )
{
case QgsWkbTypes::PointGeometry:
pageIndex = 2;
if ( symbol && symbol->type() == QLatin1String( "point" ) )
{
whileBlocking( widgetPoint )->setSymbol( symbol, vlayer );
}
else
{
QgsPoint3DSymbol defaultSymbol;
whileBlocking( widgetPoint )->setSymbol( &defaultSymbol, vlayer );
}
break;

case QgsWkbTypes::LineGeometry:
pageIndex = 1;
if ( symbol && symbol->type() == QLatin1String( "line" ) )
{
whileBlocking( widgetLine )->setSymbol( symbol, vlayer );
}
else
{
QgsLine3DSymbol defaultSymbol;
whileBlocking( widgetLine )->setSymbol( &defaultSymbol, vlayer );
}
break;

case QgsWkbTypes::PolygonGeometry:
pageIndex = 3;
if ( symbol && symbol->type() == QLatin1String( "polygon" ) )
{
whileBlocking( widgetPolygon )->setSymbol( symbol, vlayer );
}
else
{
QgsPolygon3DSymbol defaultSymbol;
whileBlocking( widgetPolygon )->setSymbol( &defaultSymbol, vlayer );
}
break;

default:
pageIndex = 0; // unsupported
break;
if ( w->symbolType() == symbol->type() )
{
// we can reuse the existing widget
w->setSymbol( symbol, mLayer );
return;
}
}
widgetStack->setCurrentIndex( pageIndex );

updateSymbolWidget( symbol );
}

void QgsSymbol3DWidget::setSymbolFromStyle( const QString &name )
@@ -142,12 +84,7 @@ void QgsSymbol3DWidget::setSymbolFromStyle( const QString &name )
if ( !s )
return;

if ( s->type() == QStringLiteral( "point" ) )
widgetPoint->setSymbol( s.release(), mLayer );
else if ( s->type() == QStringLiteral( "line" ) )
widgetLine->setSymbol( s.release(), mLayer );
else if ( s->type() == QStringLiteral( "polygon" ) )
widgetPolygon->setSymbol( s.release(), mLayer );
setSymbol( s.get(), mLayer );
}

void QgsSymbol3DWidget::saveSymbol()
@@ -185,3 +122,30 @@ void QgsSymbol3DWidget::saveSymbol()
// make sure the symbol is stored
QgsStyle::defaultStyle()->saveSymbol3D( saveDlg.name(), s, saveDlg.isFavorite(), symbolTags );
}

void QgsSymbol3DWidget::updateSymbolWidget( const QgsAbstract3DSymbol *newSymbol )
{
if ( widgetStack->currentWidget() != widgetUnsupported )
{
// stop updating from the original widget
if ( Qgs3DSymbolWidget *w = qobject_cast< Qgs3DSymbolWidget * >( widgetStack->currentWidget() ) )
disconnect( w, &Qgs3DSymbolWidget::changed, this, &QgsSymbol3DWidget::widgetChanged );
widgetStack->removeWidget( widgetStack->currentWidget() );
}

const QString symbolType = newSymbol->type();
if ( Qgs3DSymbolAbstractMetadata *am = QgsApplication::symbol3DRegistry()->symbolMetadata( symbolType ) )
{
if ( Qgs3DSymbolWidget *w = am->createSymbolWidget( mLayer ) )
{
w->setSymbol( newSymbol, mLayer );
widgetStack->addWidget( w );
widgetStack->setCurrentWidget( w );
// start receiving updates from widget
connect( w, &Qgs3DSymbolWidget::changed, this, &QgsSymbol3DWidget::widgetChanged );
return;
}
}
// When anything is not right
widgetStack->setCurrentWidget( widgetUnsupported );
}
@@ -56,6 +56,8 @@ class QgsSymbol3DWidget : public QWidget

private:

void updateSymbolWidget( const QgsAbstract3DSymbol *newSymbol );

QStackedWidget *widgetStack = nullptr;
QgsLine3DSymbolWidget *widgetLine = nullptr;
QgsPoint3DSymbolWidget *widgetPoint = nullptr;
@@ -53,6 +53,18 @@ class GUI_EXPORT Qgs3DSymbolWidget : public QWidget
* Caller takes ownership of the returned symbol.
*/
virtual QgsAbstract3DSymbol *symbol() = 0 SIP_FACTORY;

/**
* Returns the symbol type handled by the widget.
*/
virtual QString symbolType() const = 0;

signals:

/**
* Emitted when the symbol is changed.
*/
void changed();
};


0 comments on commit b1d0261

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