Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add an option to set default symbols for QgsSymbolButton
  • Loading branch information
nyalldawson committed Jan 17, 2023
1 parent 2fe201d commit d6b6147
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 33 deletions.
75 changes: 58 additions & 17 deletions python/gui/auto_generated/qgssymbolbutton.sip.in
Expand Up @@ -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:
Expand Down Expand Up @@ -194,37 +242,30 @@ Sets whether a set to null (clear) option is shown in the button's drop-down men
.. versionadded:: 3.26
%End

bool showNull() const;
void setToNull();
%Docstring
Returns whether the set to null (clear) option is shown in the button's drop-down menu.
Sets symbol to to null.

.. 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:`setToDefaultSymbol`

.. seealso:: :py:func:`showNull`

.. versionadded:: 3.26
%End

void setToNull();
void setToDefaultSymbol();
%Docstring
Sets symbol to to null.
Sets symbol to the button's default symbol, if set.

.. seealso:: :py:func:`setShowNull`
.. seealso:: :py:func:`setDefaultSymbol`

.. seealso:: :py:func:`showNull`
.. seealso:: :py:func:`defaultSymbol`

.. versionadded:: 3.26
.. seealso:: :py:func:`setToNull`

.. versionadded:: 3.30
%End

signals:
Expand Down
32 changes: 31 additions & 1 deletion src/gui/qgssymbolbutton.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -831,3 +851,13 @@ void QgsSymbolButton::setToNull()
{
setSymbol( nullptr );
}

void QgsSymbolButton::setToDefaultSymbol()
{
if ( !mDefaultSymbol )
{
return;
}

setSymbol( mDefaultSymbol->clone() );
}
64 changes: 49 additions & 15 deletions src/gui/qgssymbolbutton.h
Expand Up @@ -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:

/**
Expand Down Expand Up @@ -214,28 +250,24 @@ 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.
* Sets symbol to to null.
* \see setShowNull()
* \see setToDefaultSymbol()
* \see showNull()
* \since QGIS 3.26
*/
bool isNull() const;
void setToNull();

/**
* Sets symbol to to null.
* \see setShowNull()
* \see showNull()
* \since QGIS 3.26
* Sets symbol to the button's default symbol, if set.
*
* \see setDefaultSymbol()
* \see defaultSymbol()
* \see setToNull()
*
* \since QGIS 3.30
*/
void setToNull();
void setToDefaultSymbol();

signals:

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d6b6147

Please sign in to comment.