Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the SVG selector collapsible (+ remove duplicated code) #39421

Merged
merged 5 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsfilecontentsourcelineedit.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Returns the current file source.
%Docstring
Sets a specific settings ``key`` to use when storing the last
used path for the file source.
%End

QgsPropertyOverrideButton *fileToolButton() const;
%Docstring
Returns the file tool button

.. versionadded:: 3.16
%End

public slots:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ Constructor for QgsSvgSelectorWidget

QString currentSvgPath() const;

QgsSvgSourceLineEdit *sourceLineEdit() const;
%Docstring
Returns the source line edit

.. versionadded:: 3.16
%End

public slots:
void setSvgPath( const QString &svgPath );
%Docstring
Expand Down
14 changes: 13 additions & 1 deletion python/gui/auto_generated/symbology/qgssymbollayerwidget.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,21 @@ Creates a new QgsSvgMarkerSymbolLayerWidget.
virtual void setContext( const QgsSymbolWidgetContext &context );


public slots:
void setSvgPath( const QString &name );
%Docstring
Sets the SVG path
%End


protected:

void populateList();
void populateList() /Deprecated/;
%Docstring
This method does nothing anymore, the loading is automatic

.. deprecated:: QGIS 3.16
%End

void setGuiForSvg( const QgsSvgMarkerSymbolLayer *layer, bool skipDefaultColors = false );
%Docstring
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsfilecontentsourcelineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "qgssettings.h"
#include "qgsmessagebar.h"
#include "qgsfilterlineedit.h"
#include "qgspropertyoverridebutton.h"
#include <QMenu>
#include <QLineEdit>
#include <QToolButton>
Expand All @@ -35,7 +36,7 @@ QgsAbstractFileContentSourceLineEdit::QgsAbstractFileContentSourceLineEdit( QWid
layout->setContentsMargins( 0, 0, 0, 0 );
mFileLineEdit = new QgsFilterLineEdit( this );
mFileLineEdit->setShowClearButton( true );
mFileToolButton = new QToolButton( this );
mFileToolButton = new QgsPropertyOverrideButton( this );
mFileToolButton->setText( QString( QChar( 0x2026 ) ) );
layout->addWidget( mFileLineEdit, 1 );
layout->addWidget( mFileToolButton );
Expand Down
14 changes: 8 additions & 6 deletions src/gui/qgsfilecontentsourcelineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class QgsFilterLineEdit;
class QToolButton;
class QgsMessageBar;
class QgsPropertyOverrideButton;

/**
* \ingroup gui
Expand All @@ -36,13 +37,8 @@ class QgsMessageBar;
*
* \since QGIS 3.6
*/
#ifndef SIP_RUN
class GUI_EXPORT QgsAbstractFileContentSourceLineEdit : public QWidget
{
#else
class GUI_EXPORT QgsAbstractFileContentSourceLineEdit : public QWidget SIP_ABSTRACT
{
#endif
Q_OBJECT
Q_PROPERTY( QString source READ source WRITE setSource NOTIFY sourceChanged )

Expand Down Expand Up @@ -79,6 +75,12 @@ class GUI_EXPORT QgsAbstractFileContentSourceLineEdit : public QWidget SIP_ABSTR
*/
void setLastPathSettingsKey( const QString &key );

/**
* Returns the file tool button
* \since QGIS 3.16
*/
QgsPropertyOverrideButton *fileToolButton() const {return mFileToolButton;}

public slots:

/**
Expand Down Expand Up @@ -156,7 +158,7 @@ class GUI_EXPORT QgsAbstractFileContentSourceLineEdit : public QWidget SIP_ABSTR
Mode mMode = ModeFile;

QgsFilterLineEdit *mFileLineEdit = nullptr;
QToolButton *mFileToolButton = nullptr;
QgsPropertyOverrideButton *mFileToolButton = nullptr;
QString mLastPathKey;
QString mBase64;
QgsMessageBar *mMessageBar = nullptr;
Expand Down
6 changes: 6 additions & 0 deletions src/gui/symbology/qgssvgselectorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ class GUI_EXPORT QgsSvgSelectorWidget : public QWidget, private Ui::WidgetSvgSel

QString currentSvgPath() const;

/**
* Returns the source line edit
* \since QGIS 3.16
*/
QgsSvgSourceLineEdit *sourceLineEdit() const {return mSvgSourceLineEdit;}

public slots:
//! Accepts absolute paths
void setSvgPath( const QString &svgPath );
Expand Down
76 changes: 11 additions & 65 deletions src/gui/symbology/qgssymbollayerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2314,9 +2314,9 @@ QgsSvgMarkerSymbolLayerWidget::QgsSvgMarkerSymbolLayerWidget( QgsVectorLayer *vl

setupUi( this );

mSvgSourceLineEdit->setLastPathSettingsKey( QStringLiteral( "/UI/lastSVGMarkerDir" ) );
mSvgSelectorWidget->sourceLineEdit()->setLastPathSettingsKey( QStringLiteral( "/UI/lastSVGMarkerDir" ) );

connect( mSvgSourceLineEdit, &QgsSvgSourceLineEdit::sourceChanged, this, &QgsSvgMarkerSymbolLayerWidget::svgSourceChanged );
connect( mSvgSelectorWidget->sourceLineEdit(), &QgsSvgSourceLineEdit::sourceChanged, this, &QgsSvgMarkerSymbolLayerWidget::svgSourceChanged );
connect( mChangeColorButton, &QgsColorButton::colorChanged, this, &QgsSvgMarkerSymbolLayerWidget::mChangeColorButton_colorChanged );
connect( mChangeStrokeColorButton, &QgsColorButton::colorChanged, this, &QgsSvgMarkerSymbolLayerWidget::mChangeStrokeColorButton_colorChanged );
connect( mStrokeWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsSvgMarkerSymbolLayerWidget::mStrokeWidthSpinBox_valueChanged );
Expand All @@ -2331,7 +2331,6 @@ QgsSvgMarkerSymbolLayerWidget::QgsSvgMarkerSymbolLayerWidget( QgsVectorLayer *vl
<< QgsUnitTypes::RenderPoints << QgsUnitTypes::RenderInches );
mOffsetUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << QgsUnitTypes::RenderMillimeters << QgsUnitTypes::RenderMetersInMapUnits << QgsUnitTypes::RenderMapUnits << QgsUnitTypes::RenderPixels
<< QgsUnitTypes::RenderPoints << QgsUnitTypes::RenderInches );
viewGroups->setHeaderHidden( true );
mChangeColorButton->setAllowOpacity( true );
mChangeColorButton->setColorDialogTitle( tr( "Select Fill color" ) );
mChangeColorButton->setContext( QStringLiteral( "symbology" ) );
Expand All @@ -2346,17 +2345,6 @@ QgsSvgMarkerSymbolLayerWidget::QgsSvgMarkerSymbolLayerWidget( QgsVectorLayer *vl
spinOffsetY->setClearValue( 0.0 );
spinAngle->setClearValue( 0.0 );

#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
mIconSize = std::max( 30, static_cast< int >( std::round( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 4 ) ) );
#else
mIconSize = std::max( 30, static_cast< int >( std::round( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 4 ) ) );
#endif
viewImages->setGridSize( QSize( mIconSize * 1.2, mIconSize * 1.2 ) );

populateList();

connect( viewImages->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsSvgMarkerSymbolLayerWidget::setName );
connect( viewGroups->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsSvgMarkerSymbolLayerWidget::populateIcons );
connect( spinWidth, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsSvgMarkerSymbolLayerWidget::setWidth );
connect( spinHeight, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsSvgMarkerSymbolLayerWidget::setHeight );
connect( mLockAspectRatio, static_cast < void ( QgsRatioLockButton::* )( bool ) > ( &QgsRatioLockButton::lockChanged ), this, &QgsSvgMarkerSymbolLayerWidget::lockAspectRatioChanged );
Expand All @@ -2365,6 +2353,8 @@ QgsSvgMarkerSymbolLayerWidget::QgsSvgMarkerSymbolLayerWidget( QgsVectorLayer *vl
connect( spinOffsetY, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsSvgMarkerSymbolLayerWidget::setOffset );
connect( this, &QgsSymbolLayerWidget::changed, this, &QgsSvgMarkerSymbolLayerWidget::updateAssistantSymbol );

connect( mSvgSelectorWidget, &QgsSvgSelectorWidget::svgSelected, this, &QgsSvgMarkerSymbolLayerWidget::setSvgPath );


//make a temporary symbol for the size assistant preview
mAssistantPreviewSymbol.reset( new QgsMarkerSymbol() );
Expand All @@ -2382,38 +2372,7 @@ QgsSvgMarkerSymbolLayerWidget::QgsSvgMarkerSymbolLayerWidget( QgsVectorLayer *vl
#include <QStyle>


void QgsSvgMarkerSymbolLayerWidget::populateList()
{
QAbstractItemModel *oldModel = viewGroups->model();
QgsSvgSelectorGroupsModel *g = new QgsSvgSelectorGroupsModel( viewGroups );
viewGroups->setModel( g );
delete oldModel;

// Set the tree expanded at the first level
int rows = g->rowCount( g->indexFromItem( g->invisibleRootItem() ) );
for ( int i = 0; i < rows; i++ )
{
viewGroups->setExpanded( g->indexFromItem( g->item( i ) ), true );
}

// Initially load the icons in the List view without any grouping
oldModel = viewImages->model();
QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( viewImages, mIconSize );
viewImages->setModel( m );
delete oldModel;
}

void QgsSvgMarkerSymbolLayerWidget::populateIcons( const QModelIndex &idx )
{
QString path = idx.data( Qt::UserRole + 1 ).toString();

QAbstractItemModel *oldModel = viewImages->model();
QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( viewImages, path, mIconSize );
viewImages->setModel( m );
delete oldModel;

connect( viewImages->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsSvgMarkerSymbolLayerWidget::setName );
}

void QgsSvgMarkerSymbolLayerWidget::setGuiForSvg( const QgsSvgMarkerSymbolLayer *layer, bool skipDefaultColors )
{
Expand Down Expand Up @@ -2461,7 +2420,7 @@ void QgsSvgMarkerSymbolLayerWidget::setGuiForSvg( const QgsSvgMarkerSymbolLayer
mChangeStrokeColorButton->setColor( stroke );
}

whileBlocking( mSvgSourceLineEdit )->setSource( layer->path() );
whileBlocking( mSvgSelectorWidget->sourceLineEdit() )->setSource( layer->path() );

mStrokeWidthSpinBox->blockSignals( true );
mStrokeWidthSpinBox->setValue( hasDefaultStrokeWidth ? defaultStrokeWidth : layer->strokeWidth() );
Expand Down Expand Up @@ -2509,20 +2468,7 @@ void QgsSvgMarkerSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer *layer )
mLayer = static_cast<QgsSvgMarkerSymbolLayer *>( layer );

// set values

QAbstractItemModel *m = viewImages->model();
QItemSelectionModel *selModel = viewImages->selectionModel();
for ( int i = 0; i < m->rowCount(); i++ )
{
QModelIndex idx( m->index( i, 0 ) );
if ( m->data( idx ).toString() == mLayer->path() )
{
selModel->select( idx, QItemSelectionModel::SelectCurrent );
selModel->setCurrentIndex( idx, QItemSelectionModel::SelectCurrent );
setName( idx );
break;
}
}
mSvgSelectorWidget->setSvgPath( mLayer->path() );

spinWidth->blockSignals( true );
spinWidth->setValue( mLayer->size() );
Expand Down Expand Up @@ -2567,12 +2513,13 @@ void QgsSvgMarkerSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer *layer )
registerDataDefinedButton( mStrokeWidthDDBtn, QgsSymbolLayer::PropertyStrokeWidth );
registerDataDefinedButton( mAngleDDBtn, QgsSymbolLayer::PropertyAngle );
registerDataDefinedButton( mOffsetDDBtn, QgsSymbolLayer::PropertyOffset );
registerDataDefinedButton( mFilenameDDBtn, QgsSymbolLayer::PropertyName );
registerDataDefinedButton( mFillColorDDBtn, QgsSymbolLayer::PropertyFillColor );
registerDataDefinedButton( mStrokeColorDDBtn, QgsSymbolLayer::PropertyStrokeColor );
registerDataDefinedButton( mHorizontalAnchorDDBtn, QgsSymbolLayer::PropertyHorizontalAnchor );
registerDataDefinedButton( mVerticalAnchorDDBtn, QgsSymbolLayer::PropertyVerticalAnchor );

registerDataDefinedButton( mSvgSelectorWidget->sourceLineEdit()->fileToolButton(), QgsSymbolLayer::PropertyVerticalAnchor );
3nids marked this conversation as resolved.
Show resolved Hide resolved

updateAssistantSymbol();
}

Expand All @@ -2584,14 +2531,13 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayerWidget::symbolLayer()
void QgsSvgMarkerSymbolLayerWidget::setContext( const QgsSymbolWidgetContext &context )
{
QgsSymbolLayerWidget::setContext( context );
mSvgSourceLineEdit->setMessageBar( context.messageBar() );
mSvgSelectorWidget->sourceLineEdit()->setMessageBar( context.messageBar() );
}

void QgsSvgMarkerSymbolLayerWidget::setName( const QModelIndex &idx )
void QgsSvgMarkerSymbolLayerWidget::setSvgPath( const QString &name )
{
QString name = idx.data( Qt::UserRole ).toString();
mLayer->setPath( name );
whileBlocking( mSvgSourceLineEdit )->setSource( name );
whileBlocking( mSvgSelectorWidget->sourceLineEdit() )->setSource( name );

setGuiForSvg( mLayer );
emit changed();
Expand Down
15 changes: 11 additions & 4 deletions src/gui/symbology/qgssymbollayerwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,19 @@ class GUI_EXPORT QgsSvgMarkerSymbolLayerWidget : public QgsSymbolLayerWidget, pr

void setContext( const QgsSymbolWidgetContext &context ) override;

public slots:
//! Sets the SVG path
void setSvgPath( const QString &name );


protected:
// TODO QGIS 4: remove

void populateList();
/**
* This method does nothing anymore, the loading is automatic
* \deprecated since QGIS 3.16
*/
Q_DECL_DEPRECATED void populateList() SIP_DEPRECATED {}

/**
* Updates the GUI to reflect the SVG marker symbol \a layer.
Expand All @@ -613,8 +623,6 @@ class GUI_EXPORT QgsSvgMarkerSymbolLayerWidget : public QgsSymbolLayerWidget, pr
QgsSvgMarkerSymbolLayer *mLayer = nullptr;

private slots:
void setName( const QModelIndex &idx );
void populateIcons( const QModelIndex &idx );
void svgSourceChanged( const QString &text );
void mChangeColorButton_colorChanged( const QColor &color );
void mChangeStrokeColorButton_colorChanged( const QColor &color );
Expand All @@ -634,7 +642,6 @@ class GUI_EXPORT QgsSvgMarkerSymbolLayerWidget : public QgsSymbolLayerWidget, pr
private:

std::shared_ptr< QgsMarkerSymbol > mAssistantPreviewSymbol;
int mIconSize = 30;

};

Expand Down
Loading