Skip to content
Permalink
Browse files
Code cleanup and modernization
  • Loading branch information
nyalldawson committed May 26, 2021
1 parent f3f4c17 commit 7a8f35fc259156edfc69e285bd3cf6e8b7fd9272
@@ -35,29 +35,28 @@ QgsRendererWidget *QgsSingleSymbolRendererWidget::create( QgsVectorLayer *layer,

QgsSingleSymbolRendererWidget::QgsSingleSymbolRendererWidget( QgsVectorLayer *layer, QgsStyle *style, QgsFeatureRenderer *renderer )
: QgsRendererWidget( layer, style )

{
// try to recognize the previous renderer
// (null renderer means "no previous renderer")

if ( renderer )
{
mRenderer = QgsSingleSymbolRenderer::convertFromRenderer( renderer );
mRenderer.reset( QgsSingleSymbolRenderer::convertFromRenderer( renderer ) );
}
if ( !mRenderer )
{
QgsSymbol *symbol = QgsSymbol::defaultSymbol( mLayer->geometryType() );

if ( symbol )
mRenderer = new QgsSingleSymbolRenderer( symbol );
mRenderer = std::make_unique< QgsSingleSymbolRenderer >( symbol );
}

// load symbol from it
if ( mRenderer )
mSingleSymbol = mRenderer->symbol()->clone();
mSingleSymbol.reset( mRenderer->symbol()->clone() );

// setup ui
mSelector = new QgsSymbolSelectorWidget( mSingleSymbol, mStyle, mLayer, nullptr );
mSelector = new QgsSymbolSelectorWidget( mSingleSymbol.get(), mStyle, mLayer, nullptr );
connect( mSelector, &QgsSymbolSelectorWidget::symbolModified, this, &QgsSingleSymbolRendererWidget::changeSingleSymbol );
connect( mSelector, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );

@@ -73,24 +72,21 @@ QgsSingleSymbolRendererWidget::QgsSingleSymbolRendererWidget( QgsVectorLayer *la
if ( mSingleSymbol && mSingleSymbol->type() == Qgis::SymbolType::Marker )
{
QAction *actionDdsLegend = advMenu->addAction( tr( "Data-defined Size Legend…" ) );
// only from Qt 5.6 there is convenience addAction() with new style connection
connect( actionDdsLegend, &QAction::triggered, this, &QgsSingleSymbolRendererWidget::dataDefinedSizeLegend );
}
}

QgsSingleSymbolRendererWidget::~QgsSingleSymbolRendererWidget()
{
delete mSingleSymbol;

delete mRenderer;
mSingleSymbol.reset();
mRenderer.reset();

delete mSelector;
}


QgsFeatureRenderer *QgsSingleSymbolRendererWidget::renderer()
{
return mRenderer;
return mRenderer.get();
}

void QgsSingleSymbolRendererWidget::setContext( const QgsSymbolWidgetContext &context )
@@ -125,12 +121,12 @@ void QgsSingleSymbolRendererWidget::changeSingleSymbol()

void QgsSingleSymbolRendererWidget::showSymbolLevels()
{
showSymbolLevelsDialog( mRenderer );
showSymbolLevelsDialog( mRenderer.get() );
}

void QgsSingleSymbolRendererWidget::dataDefinedSizeLegend()
{
QgsMarkerSymbol *s = static_cast<QgsMarkerSymbol *>( mSingleSymbol ); // this should be only enabled for marker symbols
QgsMarkerSymbol *s = static_cast<QgsMarkerSymbol *>( mSingleSymbol.get() ); // this should be only enabled for marker symbols
QgsDataDefinedSizeLegendWidget *panel = createDataDefinedSizeLegendWidget( s, mRenderer->dataDefinedSizeLegend() );
if ( panel )
{
@@ -61,9 +61,9 @@ class GUI_EXPORT QgsSingleSymbolRendererWidget : public QgsRendererWidget

private:

QgsSingleSymbolRenderer *mRenderer = nullptr;
std::unique_ptr< QgsSingleSymbolRenderer > mRenderer;
QgsSymbolSelectorWidget *mSelector = nullptr;
QgsSymbol *mSingleSymbol = nullptr;
std::unique_ptr< QgsSymbol > mSingleSymbol;
};


@@ -234,14 +234,13 @@ class SymbolLayerItem : public QStandardItem

QgsSymbolSelectorWidget::QgsSymbolSelectorWidget( QgsSymbol *symbol, QgsStyle *style, QgsVectorLayer *vl, QWidget *parent )
: QgsPanelWidget( parent )
, mStyle( style )
, mSymbol( symbol )
, mVectorLayer( vl )
{
#ifdef Q_OS_MAC
setWindowModality( Qt::WindowModal );
#endif
mStyle = style;
mSymbol = symbol;
mPresentWidget = nullptr;

setupUi( this );
this->layout()->setContentsMargins( 0, 0, 0, 0 );
@@ -263,9 +262,9 @@ QgsSymbolSelectorWidget::QgsSymbolSelectorWidget( QgsSymbol *symbol, QgsStyle *s
btnUp->setIcon( QIcon( QgsApplication::iconPath( "mActionArrowUp.svg" ) ) );
btnDown->setIcon( QIcon( QgsApplication::iconPath( "mActionArrowDown.svg" ) ) );

model = new QStandardItemModel( layersTree );
mSymbolLayersModel = new QStandardItemModel( layersTree );
// Set the symbol
layersTree->setModel( model );
layersTree->setModel( mSymbolLayersModel );
layersTree->setHeaderHidden( true );

//get first feature from layer for previews
@@ -289,7 +288,7 @@ QgsSymbolSelectorWidget::QgsSymbolSelectorWidget( QgsSymbol *symbol, QgsStyle *s
QItemSelectionModel *selModel = layersTree->selectionModel();
connect( selModel, &QItemSelectionModel::currentChanged, this, &QgsSymbolSelectorWidget::layerChanged );

loadSymbol( symbol, static_cast<SymbolLayerItem *>( model->invisibleRootItem() ) );
loadSymbol( mSymbol, static_cast<SymbolLayerItem *>( mSymbolLayersModel->invisibleRootItem() ) );
updatePreview();

connect( btnUp, &QAbstractButton::clicked, this, &QgsSymbolSelectorWidget::moveLayerUp );
@@ -354,12 +353,9 @@ void QgsSymbolSelectorWidget::setContext( const QgsSymbolWidgetContext &context
}

QWidget *widget = stackedWidget->currentWidget();
QgsLayerPropertiesWidget *layerProp = dynamic_cast< QgsLayerPropertiesWidget * >( widget );
QgsSymbolsListWidget *listWidget = dynamic_cast< QgsSymbolsListWidget * >( widget );

if ( layerProp )
if ( QgsLayerPropertiesWidget *layerProp = qobject_cast< QgsLayerPropertiesWidget * >( widget ) )
layerProp->setContext( context );
if ( listWidget )
else if ( QgsSymbolsListWidget *listWidget = qobject_cast< QgsSymbolsListWidget * >( widget ) )
listWidget->setContext( context );

layerChanged();
@@ -379,8 +375,8 @@ void QgsSymbolSelectorWidget::loadSymbol( QgsSymbol *symbol, SymbolLayerItem *pa
if ( !parent )
{
mSymbol = symbol;
model->clear();
parent = static_cast<SymbolLayerItem *>( model->invisibleRootItem() );
mSymbolLayersModel->clear();
parent = static_cast<SymbolLayerItem *>( mSymbolLayersModel->invisibleRootItem() );
}

SymbolLayerItem *symbolItem = new SymbolLayerItem( symbol );
@@ -402,7 +398,6 @@ void QgsSymbolSelectorWidget::loadSymbol( QgsSymbol *symbol, SymbolLayerItem *pa
layersTree->setExpanded( layerItem->index(), true );
}
layersTree->setExpanded( symbolItem->index(), true );
}

if ( mSymbol == symbol && !layersTree->currentIndex().isValid() )
{
@@ -413,8 +408,8 @@ void QgsSymbolSelectorWidget::loadSymbol( QgsSymbol *symbol, SymbolLayerItem *pa

void QgsSymbolSelectorWidget::reloadSymbol()
{
model->clear();
loadSymbol( mSymbol, static_cast<SymbolLayerItem *>( model->invisibleRootItem() ) );
mSymbolLayersModel->clear();
loadSymbol( mSymbol, static_cast<SymbolLayerItem *>( mSymbolLayersModel->invisibleRootItem() ) );
}

void QgsSymbolSelectorWidget::updateUi()
@@ -423,7 +418,7 @@ void QgsSymbolSelectorWidget::updateUi()
if ( !currentIdx.isValid() )
return;

SymbolLayerItem *item = static_cast<SymbolLayerItem *>( model->itemFromIndex( currentIdx ) );
SymbolLayerItem *item = static_cast<SymbolLayerItem *>( mSymbolLayersModel->itemFromIndex( currentIdx ) );
if ( !item->isLayer() )
{
btnUp->setEnabled( false );
@@ -473,7 +468,7 @@ SymbolLayerItem *QgsSymbolSelectorWidget::currentLayerItem()
if ( !idx.isValid() )
return nullptr;

SymbolLayerItem *item = static_cast<SymbolLayerItem *>( model->itemFromIndex( idx ) );
SymbolLayerItem *item = static_cast<SymbolLayerItem *>( mSymbolLayersModel->itemFromIndex( idx ) );
if ( !item->isLayer() )
return nullptr;

@@ -486,7 +481,7 @@ QgsSymbolLayer *QgsSymbolSelectorWidget::currentLayer()
if ( !idx.isValid() )
return nullptr;

SymbolLayerItem *item = static_cast<SymbolLayerItem *>( model->itemFromIndex( idx ) );
SymbolLayerItem *item = static_cast<SymbolLayerItem *>( mSymbolLayersModel->itemFromIndex( idx ) );
if ( item->isLayer() )
return item->layer();

@@ -497,7 +492,7 @@ void QgsSymbolSelectorWidget::layerChanged()
{
updateUi();

SymbolLayerItem *currentItem = static_cast<SymbolLayerItem *>( model->itemFromIndex( layersTree->currentIndex() ) );
SymbolLayerItem *currentItem = static_cast<SymbolLayerItem *>( mSymbolLayersModel->itemFromIndex( layersTree->currentIndex() ) );
if ( !currentItem )
return;

@@ -518,8 +513,8 @@ void QgsSymbolSelectorWidget::layerChanged()
}
else
{
mDataDefineRestorer.reset();
// then it must be a symbol
mDataDefineRestorer.reset();
Q_NOWARN_DEPRECATED_PUSH
currentItem->symbol()->setLayer( mVectorLayer );
Q_NOWARN_DEPRECATED_POP
@@ -535,7 +530,7 @@ void QgsSymbolSelectorWidget::layerChanged()

void QgsSymbolSelectorWidget::symbolChanged()
{
SymbolLayerItem *currentItem = static_cast<SymbolLayerItem *>( model->itemFromIndex( layersTree->currentIndex() ) );
SymbolLayerItem *currentItem = static_cast<SymbolLayerItem *>( mSymbolLayersModel->itemFromIndex( layersTree->currentIndex() ) );
if ( !currentItem || currentItem->isLayer() )
return;
// disconnect to avoid recreating widget
@@ -586,7 +581,7 @@ void QgsSymbolSelectorWidget::addLayer()
return;

int insertIdx = -1;
SymbolLayerItem *item = static_cast<SymbolLayerItem *>( model->itemFromIndex( idx ) );
SymbolLayerItem *item = static_cast<SymbolLayerItem *>( mSymbolLayersModel->itemFromIndex( idx ) );
if ( item->isLayer() )
{
insertIdx = item->row();
@@ -624,7 +619,7 @@ void QgsSymbolSelectorWidget::addLayer()
item->insertRow( insertIdx == -1 ? 0 : insertIdx, newLayerItem );
item->updatePreview();

layersTree->setCurrentIndex( model->indexFromItem( newLayerItem ) );
layersTree->setCurrentIndex( mSymbolLayersModel->indexFromItem( newLayerItem ) );
updateUi();
updatePreview();
}
@@ -702,7 +697,7 @@ void QgsSymbolSelectorWidget::duplicateLayer()
if ( !idx.isValid() )
return;

SymbolLayerItem *item = static_cast<SymbolLayerItem *>( model->itemFromIndex( idx ) );
SymbolLayerItem *item = static_cast<SymbolLayerItem *>( mSymbolLayersModel->itemFromIndex( idx ) );
if ( !item->isLayer() )
return;

@@ -728,7 +723,7 @@ void QgsSymbolSelectorWidget::duplicateLayer()
}
item->updatePreview();

layersTree->setCurrentIndex( model->indexFromItem( newLayerItem ) );
layersTree->setCurrentIndex( mSymbolLayersModel->indexFromItem( newLayerItem ) );
updateUi();
updatePreview();
}
@@ -258,7 +258,7 @@ class GUI_EXPORT QgsSymbolSelectorWidget: public QgsPanelWidget, private Ui::Qgs
QMenu *mAdvancedMenu = nullptr;
QgsVectorLayer *mVectorLayer = nullptr;

QStandardItemModel *model = nullptr;
QStandardItemModel *mSymbolLayersModel = nullptr;
QWidget *mPresentWidget = nullptr;

std::unique_ptr<DataDefinedRestorer> mDataDefineRestorer;

0 comments on commit 7a8f35f

Please sign in to comment.