Skip to content
Permalink
Browse files
Use QgsImageSourceLineEdit and allow placeholder images also for vect…
…or layers
  • Loading branch information
mhugent committed Jul 2, 2021
1 parent b7b9470 commit 6effd6bd753427e4e32c16e85d1f9da3fb84ad3c
@@ -15,6 +15,7 @@

#include "qgsmaplayerlegend.h"
#include "qgsiconutils.h"
#include "qgsimagecache.h"
#include "qgssettings.h"
#include "qgslayertree.h"
#include "qgslayertreemodellegendnode.h"
@@ -359,6 +360,19 @@ QList<QgsLayerTreeModelLegendNode *> QgsDefaultVectorLayerLegend::createLayerTre
{
QList<QgsLayerTreeModelLegendNode *> nodes;

if ( mLayer )
{
QString placeholderImage = mLayer->legendPlaceholderImage();
if ( !placeholderImage.isEmpty() )
{
QgsImageCache ic;
bool fitsInCache;
QImage img = ic.pathAsImage( placeholderImage, QSize(), false, 1.0, fitsInCache );
nodes << new QgsImageLegendNode( nodeLayer, img );
return nodes;
}
}

QgsFeatureRenderer *r = mLayer->renderer();
if ( !r )
return nodes;
@@ -505,7 +519,10 @@ QList<QgsLayerTreeModelLegendNode *> QgsDefaultRasterLayerLegend::createLayerTre
QString placeholderImage = mLayer->legendPlaceholderImage();
if ( !placeholderImage.isEmpty() )
{
nodes << new QgsImageLegendNode( nodeLayer, QImage( placeholderImage ) );
QgsImageCache ic;
bool fitsInCache;
QImage img = ic.pathAsImage( placeholderImage, QSize(), false, 1.0, fitsInCache );
nodes << new QgsImageLegendNode( nodeLayer, img );
}
else if ( mLayer->renderer() )
nodes.append( mLayer->renderer()->createLegendNodes( nodeLayer ) );
@@ -914,7 +914,8 @@ void QgsRasterLayerProperties::sync()
QVariant wmsBackgroundLayer = mRasterLayer->customProperty( QStringLiteral( "WMSBackgroundLayer" ), false );
mBackgroundLayerCheckBox->setChecked( wmsBackgroundLayer.toBool() );

mLegendPlaceholderLineEdit->setText( mRasterLayer->legendPlaceholderImage() );
mLegendPlaceholderWidget->setLastPathSettingsKey( QStringLiteral( "lastLegendPlaceholderDir" ) );
mLegendPlaceholderWidget->setSource( mRasterLayer->legendPlaceholderImage() );
mLegendConfigEmbeddedWidget->setLayer( mRasterLayer );

mTemporalWidget->syncToLayer();
@@ -947,7 +948,7 @@ void QgsRasterLayerProperties::apply()
/*
* Legend Tab
*/
mRasterLayer->setLegendPlaceholderImage( mLegendPlaceholderLineEdit->text() );
mRasterLayer->setLegendPlaceholderImage( mLegendPlaceholderWidget->source() );
mLegendConfigEmbeddedWidget->applyToLayer();

QgsDebugMsgLevel( QStringLiteral( "apply processing symbology tab" ), 3 );
@@ -1277,18 +1278,6 @@ void QgsRasterLayerProperties::urlClicked( const QUrl &url )
QDesktopServices::openUrl( url );
}

void QgsRasterLayerProperties::on_mLegendPlaceholderToolButton_clicked()
{
QgsSettings myQSettings;
QString myLastDir = myQSettings.value( QStringLiteral( "lastPlaceholderDir" ), QDir::homePath() ).toString();
QString myFileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), myLastDir );
if ( !myFileName.isEmpty() )
{
mLegendPlaceholderLineEdit->setText( myFileName );
myQSettings.setValue( QStringLiteral( "lastPlaceholderDir" ), QFileInfo( myFileName ).absolutePath() );
}
}

void QgsRasterLayerProperties::mRenderTypeComboBox_currentIndexChanged( int index )
{
if ( index < 0 || mDisableRenderTypeComboBoxCurrentIndexChanged || ! mRasterLayer->renderer() )
@@ -214,8 +214,6 @@ class GUI_EXPORT QgsRasterLayerProperties : public QgsOptionsDialogBase, private

void urlClicked( const QUrl &url );

void on_mLegendPlaceholderToolButton_clicked();

private:
QPushButton *mBtnStyle = nullptr;
QPushButton *mBtnMetadata = nullptr;
@@ -21,6 +21,7 @@
#include <QTreeWidget>

#include "qgsexpressionbuilderdialog.h"
#include "qgsfilecontentsourcelineedit.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerlegend.h"
#include "qgsrenderer.h"
@@ -71,10 +72,19 @@ QgsVectorLayerLegendWidget::QgsVectorLayerLegendWidget( QWidget *parent )
labelLegendLayout->addWidget( mLabelLegendTreeWidget );
mLabelLegendGroupBox->setLayout( labelLegendLayout );

mPlaceholderImageLabel = new QLabel( tr( "Legend placeholder image" ) );
mImageSourceLineEdit = new QgsImageSourceLineEdit();
mImageSourceLineEdit->setLastPathSettingsKey( QStringLiteral( "lastLegendPlaceholderDir" ) );
if ( mLayer )
{
mImageSourceLineEdit->setSource( mLayer->legendPlaceholderImage() );
}


QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins( 0, 0, 0, 0 );
layout->addWidget( mPlaceholderImageLabel );
layout->addWidget( mImageSourceLineEdit );
layout->addWidget( mShowLabelLegendCheckBox );
layout->addWidget( mLabelLegendGroupBox );
layout->addWidget( mTextOnSymbolGroupBox );
@@ -122,7 +132,10 @@ void QgsVectorLayerLegendWidget::setLayer( QgsVectorLayer *layer )
mTextOnSymbolGroupBox->setChecked( legend->textOnSymbolEnabled() );
mTextOnSymbolFormatButton->setTextFormat( legend->textOnSymbolTextFormat() );
populateLegendTreeView( legend->textOnSymbolContent() );

if ( mLayer )
{
mImageSourceLineEdit->setSource( mLayer->legendPlaceholderImage() );
}
}

void QgsVectorLayerLegendWidget::populateLabelLegendTreeWidget()
@@ -221,6 +234,8 @@ void QgsVectorLayerLegendWidget::applyToLayer()
applyLabelLegend();
}

mLayer->setLegendPlaceholderImage( mImageSourceLineEdit->source() );

mLayer->setLegend( legend );
}

@@ -25,6 +25,7 @@
#include "qgis_gui.h"

class QCheckBox;
class QgsImageSourceLineEdit;
class QLabel;
class QPushButton;
class QTreeView;
@@ -76,6 +77,8 @@ class GUI_EXPORT QgsVectorLayerLegendWidget : public QWidget
QCheckBox *mShowLabelLegendCheckBox = nullptr;
QgsCollapsibleGroupBox *mLabelLegendGroupBox = nullptr;
QTreeWidget *mLabelLegendTreeWidget = nullptr;
QLabel *mPlaceholderImageLabel = nullptr;
QgsImageSourceLineEdit *mImageSourceLineEdit = nullptr;

QgsMapCanvas *mCanvas = nullptr;
QgsVectorLayer *mLayer = nullptr;
@@ -1740,7 +1740,7 @@ p, li { white-space: pre-wrap; }
</widget>
<widget class="QWidget" name="mOptsPage_Legend">
<layout class="QGridLayout" name="gridLayout_15">
<item row="1" column="0">
<item row="2" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
@@ -1758,33 +1758,14 @@ p, li { white-space: pre-wrap; }
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QgsImageSourceLineEdit" name="mLegendPlaceholderWidget" native="true"/>
</item>
<item row="0" column="0">
<widget class="QGroupBox" name="mPlaceholderImageGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<widget class="QLabel" name="mLegendPlaceholderLabel">
<property name="text">
<string>Legend placeholder image</string>
</property>
<layout class="QGridLayout" name="gridLayout_16">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLineEdit" name="mLegendPlaceholderLineEdit"/>
</item>
<item>
<widget class="QToolButton" name="mLegendPlaceholderToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
@@ -2338,6 +2319,23 @@ p, li { white-space: pre-wrap; }
<extends>QComboBox</extends>
<header>qgsblendmodecombobox.h</header>
</customwidget>
<customwidget>
<class>QgsOpacityWidget</class>
<extends>QWidget</extends>
<header>qgsopacitywidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsRasterBandComboBox</class>
<extends>QComboBox</extends>
<header>qgsrasterbandcombobox.h</header>
</customwidget>
<customwidget>
<class>QgsImageSourceLineEdit</class>
<extends>QWidget</extends>
<header>qgsfilecontentsourcelineedit.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mSearchLineEdit</tabstop>

0 comments on commit 6effd6b

Please sign in to comment.