Add an option to set default symbols for QgsSymbolButton

This commit is contained in:
Nyall Dawson 2023-01-16 09:57:53 +10:00
parent 2fe201d846
commit d6b61470ca
3 changed files with 144 additions and 39 deletions

View File

@ -131,6 +131,54 @@ such as correctly populating data defined override buttons.
%Docstring
Register an expression context generator class that will be used to retrieve
an expression context for the button when required.
%End
void setDefaultSymbol( QgsSymbol *symbol /Transfer/ );
%Docstring
Sets the default symbol for the button, which is shown in the button's drop-down menu for the
"default symbol" option.
:param symbol: default symbol for the button. Set to ``None`` to disable the default symbol
option. Ownership of ``symbol`` is transferred to the button.
.. seealso:: :py:func:`defaultSymbol`
.. versionadded:: 3.30
%End
const QgsSymbol *defaultSymbol() const;
%Docstring
Returns the default symbol for the button, which is shown in the button's drop-down menu for the
"default symbol" option.
:return: default symbol for the button. Returns ``None`` if the default symbol
option is disabled.
.. seealso:: :py:func:`setDefaultSymbol`
.. versionadded:: 3.30
%End
bool showNull() const;
%Docstring
Returns whether the set to null (clear) option is shown in the button's drop-down menu.
.. seealso:: :py:func:`setShowNull`
.. seealso:: :py:func:`isNull`
.. versionadded:: 3.26
%End
bool isNull() const;
%Docstring
Returns ``True`` if the current symbol is null.
.. seealso:: :py:func:`setShowNull`
.. seealso:: :py:func:`showNull`
.. versionadded:: 3.26
%End
public slots:
@ -191,28 +239,6 @@ Sets whether a set to null (clear) option is shown in the button's drop-down men
.. seealso:: :py:func:`isNull`
.. versionadded:: 3.26
%End
bool showNull() const;
%Docstring
Returns whether the set to null (clear) option is shown in the button's drop-down menu.
.. seealso:: :py:func:`setShowNull`
.. seealso:: :py:func:`isNull`
.. versionadded:: 3.26
%End
bool isNull() const;
%Docstring
Returns ``True`` if the current symbol is null.
.. seealso:: :py:func:`setShowNull`
.. seealso:: :py:func:`showNull`
.. versionadded:: 3.26
%End
@ -222,9 +248,24 @@ Sets symbol to to null.
.. seealso:: :py:func:`setShowNull`
.. seealso:: :py:func:`setToDefaultSymbol`
.. seealso:: :py:func:`showNull`
.. versionadded:: 3.26
%End
void setToDefaultSymbol();
%Docstring
Sets symbol to the button's default symbol, if set.
.. seealso:: :py:func:`setDefaultSymbol`
.. seealso:: :py:func:`defaultSymbol`
.. seealso:: :py:func:`setToNull`
.. versionadded:: 3.30
%End
signals:

View File

@ -246,6 +246,16 @@ void QgsSymbolButton::registerExpressionContextGenerator( QgsExpressionContextGe
mExpressionContextGenerator = generator;
}
void QgsSymbolButton::setDefaultSymbol( QgsSymbol *symbol )
{
mDefaultSymbol.reset( symbol );
}
const QgsSymbol *QgsSymbolButton::defaultSymbol() const
{
return mDefaultSymbol.get();
}
void QgsSymbolButton::setSymbol( QgsSymbol *symbol )
{
mSymbol.reset( symbol );
@ -489,13 +499,14 @@ void QgsSymbolButton::prepareMenu()
mMenu->addAction( copySymbolAction );
connect( copySymbolAction, &QAction::triggered, this, &QgsSymbolButton::copySymbol );
const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
QAction *pasteSymbolAction = new QAction( tr( "Paste Symbol" ), this );
//enable or disable paste action based on current clipboard contents. We always show the paste
//action, even if it's disabled, to give hint to the user that pasting symbols is possible
std::unique_ptr< QgsSymbol > tempSymbol( QgsSymbolLayerUtils::symbolFromMimeData( QApplication::clipboard()->mimeData() ) );
if ( tempSymbol && tempSymbol->type() == mType )
{
const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
pasteSymbolAction->setIcon( QgsSymbolLayerUtils::symbolPreviewIcon( tempSymbol.get(), QSize( iconSize, iconSize ), 1 ) );
}
else
@ -513,6 +524,15 @@ void QgsSymbolButton::prepareMenu()
connect( nullAction, &QAction::triggered, this, &QgsSymbolButton::setToNull );
}
//show default symbol option if set
if ( mDefaultSymbol )
{
QAction *defaultSymbolAction = new QAction( tr( "Default Symbol" ), this );
defaultSymbolAction->setIcon( QgsSymbolLayerUtils::symbolPreviewIcon( mDefaultSymbol.get(), QSize( iconSize, iconSize ), 1 ) );
mMenu->addAction( defaultSymbolAction );
connect( defaultSymbolAction, &QAction::triggered, this, &QgsSymbolButton::setToDefaultSymbol );
}
if ( mSymbol )
{
mMenu->addSeparator();
@ -831,3 +851,13 @@ void QgsSymbolButton::setToNull()
{
setSymbol( nullptr );
}
void QgsSymbolButton::setToDefaultSymbol()
{
if ( !mDefaultSymbol )
{
return;
}
setSymbol( mDefaultSymbol->clone() );
}

View File

@ -162,6 +162,42 @@ class GUI_EXPORT QgsSymbolButton : public QToolButton
*/
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
/**
* Sets the default symbol for the button, which is shown in the button's drop-down menu for the
* "default symbol" option.
* \param symbol default symbol for the button. Set to NULLPTR to disable the default symbol
* option. Ownership of \a symbol is transferred to the button.
* \see defaultSymbol()
* \since QGIS 3.30
*/
void setDefaultSymbol( QgsSymbol *symbol SIP_TRANSFER );
/**
* Returns the default symbol for the button, which is shown in the button's drop-down menu for the
* "default symbol" option.
* \returns default symbol for the button. Returns NULLPTR if the default symbol
* option is disabled.
* \see setDefaultSymbol()
* \since QGIS 3.30
*/
const QgsSymbol *defaultSymbol() const;
/**
* Returns whether the set to null (clear) option is shown in the button's drop-down menu.
* \see setShowNull()
* \see isNull()
* \since QGIS 3.26
*/
bool showNull() const;
/**
* Returns TRUE if the current symbol is null.
* \see setShowNull()
* \see showNull()
* \since QGIS 3.26
*/
bool isNull() const;
public slots:
/**
@ -213,30 +249,26 @@ class GUI_EXPORT QgsSymbolButton : public QToolButton
*/
void setShowNull( bool showNull );
/**
* Returns whether the set to null (clear) option is shown in the button's drop-down menu.
* \see setShowNull()
* \see isNull()
* \since QGIS 3.26
*/
bool showNull() const;
/**
* Returns TRUE if the current symbol is null.
* \see setShowNull()
* \see showNull()
* \since QGIS 3.26
*/
bool isNull() const;
/**
* Sets symbol to to null.
* \see setShowNull()
* \see setToDefaultSymbol()
* \see showNull()
* \since QGIS 3.26
*/
void setToNull();
/**
* Sets symbol to the button's default symbol, if set.
*
* \see setDefaultSymbol()
* \see defaultSymbol()
* \see setToNull()
*
* \since QGIS 3.30
*/
void setToDefaultSymbol();
signals:
/**
@ -314,6 +346,8 @@ class GUI_EXPORT QgsSymbolButton : public QToolButton
bool mShowNull = false;
std::unique_ptr< QgsSymbol > mDefaultSymbol;
/**
* Regenerates the text preview. If \a color is specified, a temporary color preview
* is shown instead.