Skip to content
Permalink
Browse files

Use first feature in layer to generate symbol preview icon in widget

Uses the first feature found in a layer in order to create a better
preview for the symbol in the symbol selector dialog. This allows
data defined settings to be evaluated correctly (at least, for the
first feature) and avoids missing previews due to missing
attribute values when data defined settings are present.

Fixes #17061
  • Loading branch information
nyalldawson committed Feb 19, 2018
1 parent 7701ea6 commit 23dc1c8f696c541e5101e3c0aadbfc90819de04f
Showing with 28 additions and 1 deletion.
  1. +25 −1 src/gui/symbology/qgssymbolselectordialog.cpp
  2. +3 −0 src/gui/symbology/qgssymbolselectordialog.h
@@ -31,6 +31,8 @@
#include "qgslogger.h"
#include "qgsapplication.h"
#include "qgssettings.h"
#include "qgsfeatureiterator.h"
#include "qgsvectorlayer.h"

#include <QColorDialog>
#include <QPainter>
@@ -247,6 +249,19 @@ QgsSymbolSelectorWidget::QgsSymbolSelectorWidget( QgsSymbol *symbol, QgsStyle *s
layersTree->setModel( model );
layersTree->setHeaderHidden( true );

//get first feature from layer for previews
if ( mVectorLayer )
{
QgsFeatureIterator it = mVectorLayer->getFeatures( QgsFeatureRequest().setLimit( 1 ) );
it.nextFeature( mPreviewFeature );
mPreviewExpressionContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mVectorLayer ) );
mPreviewExpressionContext.setFeature( mPreviewFeature );
}
else
{
mPreviewExpressionContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( nullptr ) );
}

QItemSelectionModel *selModel = layersTree->selectionModel();
connect( selModel, &QItemSelectionModel::currentChanged, this, &QgsSymbolSelectorWidget::layerChanged );

@@ -285,6 +300,15 @@ void QgsSymbolSelectorWidget::setContext( const QgsSymbolWidgetContext &context
{
mContext = context;

if ( mContext.expressionContext() )
{
mPreviewExpressionContext = *mContext.expressionContext();
if ( mVectorLayer )
mPreviewExpressionContext.appendScope( QgsExpressionContextUtils::layerScope( mVectorLayer ) );

mPreviewExpressionContext.setFeature( mPreviewFeature );
}

QWidget *widget = stackedWidget->currentWidget();
QgsLayerPropertiesWidget *layerProp = dynamic_cast< QgsLayerPropertiesWidget * >( widget );
QgsSymbolsListWidget *listWidget = dynamic_cast< QgsSymbolsListWidget * >( widget );
@@ -362,7 +386,7 @@ void QgsSymbolSelectorWidget::updateUi()

void QgsSymbolSelectorWidget::updatePreview()
{
QImage preview = mSymbol->bigSymbolPreviewImage( mContext.expressionContext() );
QImage preview = mSymbol->bigSymbolPreviewImage( &mPreviewExpressionContext );
lblPreview->setPixmap( QPixmap::fromImage( preview ) );
// Hope this is a appropriate place
emit symbolModified();
@@ -250,6 +250,9 @@ class GUI_EXPORT QgsSymbolSelectorWidget: public QgsPanelWidget, private Ui::Qgs
private:
std::unique_ptr<DataDefinedRestorer> mDataDefineRestorer;
QgsSymbolWidgetContext mContext;
QgsFeature mPreviewFeature;
QgsExpressionContext mPreviewExpressionContext;

};

/**

0 comments on commit 23dc1c8

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