Skip to content
Permalink
Browse files

Add option to QgsFontButton to show a "set to null" action in menu

Allows for inclusion of a "use default" style option for QgsFontButton
  • Loading branch information
nyalldawson committed Jul 14, 2020
1 parent ac5a722 commit cdc91267fc368e6cfcbd7aa9950bc517699ed905
@@ -157,6 +157,51 @@ Register an expression context generator class that will be used to retrieve
an expression context for the button when required.

.. versionadded:: 3.10
%End

void setShowNullFormat( const bool show );
%Docstring
Sets whether the "null format" option should be shown in the button's drop-down menu. This option
is only used for buttons set to the ModeTextRenderer :py:func:`~QgsFontButton.mode`.

If selected, the "null format" option sets the button's format to an invalid :py:class:`QgsTextFormat`. This
can be used to represent a "use default format" state for the button.

By default this option is not shown.

.. seealso:: :py:func:`setNoFormatString`

.. seealso:: :py:func:`showNullFormat`

.. versionadded:: 3.16
%End

void setNoFormatString( const QString &string );
%Docstring
Sets the ``string`` to use for the "null format" option in the button's drop-down menu.

.. note::

The "null format" option is only shown if :py:func:`~QgsFontButton.showNullFormat` is ``True``.

.. seealso:: :py:func:`setShowNullFormat`

.. versionadded:: 3.16
%End

bool showNullFormat() const;
%Docstring
Returns whether the "null format" option will be shown in the button's drop-down menu. This option
is only used for buttons set to the ModeTextRenderer :py:func:`~QgsFontButton.mode`.

If selected, the "null format" option sets the button's format to an invalid :py:class:`QgsTextFormat`. This
can be used to represent a "use default format" state for the button.

By default this option is not shown.

.. seealso:: :py:func:`setShowNullFormat`

.. versionadded:: 3.16
%End

public slots:
@@ -167,6 +212,15 @@ Sets the current text ``format`` to show in the widget.
This is only used when :py:func:`~QgsFontButton.mode` is ModeTextRenderer.

.. seealso:: :py:func:`textFormat`
%End

void setToNullFormat();
%Docstring
Sets the text format to a null (invalid) :py:class:`QgsTextFormat`.

This is only used when :py:func:`~QgsFontButton.mode` is ModeTextRenderer.

.. versionadded:: 3.16
%End

void setCurrentFont( const QFont &font );
@@ -38,7 +38,7 @@
QgsFontButton::QgsFontButton( QWidget *parent, const QString &dialogTitle )
: QToolButton( parent )
, mDialogTitle( dialogTitle.isEmpty() ? tr( "Text Format" ) : dialogTitle )

, mNullFormatString( tr( "No Format" ) )
{
setText( tr( "Font" ) );

@@ -159,11 +159,21 @@ void QgsFontButton::setTextFormat( const QgsTextFormat &format )
emit changed();
}

void QgsFontButton::setToNullFormat()
{
mFormat = QgsTextFormat();
updatePreview();
emit changed();
}

void QgsFontButton::setColor( const QColor &color )
{
QColor opaque = color;
opaque.setAlphaF( 1.0 );

if ( mNullFormatAction )
mNullFormatAction->setChecked( false );

if ( mFormat.color() != opaque )
{
mFormat.setColor( opaque );
@@ -521,6 +531,17 @@ void QgsFontButton::prepareMenu()
//menu is opened, otherwise color schemes like the recent color scheme grid are meaningless
mMenu->clear();

if ( mMode == ModeTextRenderer && mShowNoFormat )
{
mNullFormatAction = new QAction( mNullFormatString, this );
mMenu->addAction( mNullFormatAction );
connect( mNullFormatAction, &QAction::triggered, this, &QgsFontButton::setToNullFormat );
if ( !mFormat.isValid() )
{
mNullFormatAction->setCheckable( true );
mNullFormatAction->setChecked( true );
}
}

QWidgetAction *sizeAction = new QWidgetAction( mMenu );
QWidget *sizeWidget = new QWidget();
@@ -555,6 +576,8 @@ void QgsFontButton::prepareMenu()
switch ( mMode )
{
case ModeTextRenderer:
if ( mNullFormatAction )
mNullFormatAction->setChecked( false );
mFormat.setSize( value );
break;
case ModeQFont:
@@ -672,6 +695,8 @@ void QgsFontButton::prepareMenu()
double opacity = color.alphaF();
mFormat.setOpacity( opacity );
updatePreview();
if ( mNullFormatAction )
mNullFormatAction->setChecked( false );
emit changed();
} );
connect( colorAction, &QgsColorWidgetAction::colorChanged, alphaRamp, [alphaRamp]( const QColor & color ) { alphaRamp->setColor( color, false ); }
@@ -163,6 +163,45 @@ class GUI_EXPORT QgsFontButton : public QToolButton
*/
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );

/**
* Sets whether the "null format" option should be shown in the button's drop-down menu. This option
* is only used for buttons set to the ModeTextRenderer mode().
*
* If selected, the "null format" option sets the button's format to an invalid QgsTextFormat. This
* can be used to represent a "use default format" state for the button.
*
* By default this option is not shown.
*
* \see setNoFormatString()
* \see showNullFormat()
* \since QGIS 3.16
*/
void setShowNullFormat( const bool show ) { mShowNoFormat = show; }

/**
* Sets the \a string to use for the "null format" option in the button's drop-down menu.
*
* \note The "null format" option is only shown if showNullFormat() is TRUE.
*
* \see setShowNullFormat()
* \since QGIS 3.16
*/
void setNoFormatString( const QString &string ) { mNullFormatString = string; }

/**
* Returns whether the "null format" option will be shown in the button's drop-down menu. This option
* is only used for buttons set to the ModeTextRenderer mode().
*
* If selected, the "null format" option sets the button's format to an invalid QgsTextFormat. This
* can be used to represent a "use default format" state for the button.
*
* By default this option is not shown.
*
* \see setShowNullFormat()
* \since QGIS 3.16
*/
bool showNullFormat() const { return mShowNoFormat; }

public slots:

/**
@@ -172,6 +211,15 @@ class GUI_EXPORT QgsFontButton : public QToolButton
*/
void setTextFormat( const QgsTextFormat &format );

/**
* Sets the text format to a null (invalid) QgsTextFormat.
*
* This is only used when mode() is ModeTextRenderer.
*
* \since QGIS 3.16
*/
void setToNullFormat();

/**
* Sets the current text \a font to show in the widget.
* This is only used when mode() is ModeQFont.
@@ -278,6 +326,10 @@ class GUI_EXPORT QgsFontButton : public QToolButton

QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;

bool mShowNoFormat = false;
QString mNullFormatString;
QPointer< QAction > mNullFormatAction;

/**
* Attempts to parse \a mimeData as a text format.
* \returns TRUE if mime data could be intrepreted as a format
@@ -103,6 +103,21 @@ def testSetColor(self):
r = button.textFormat()
self.assertEqual(r.color(), QColor(0, 255, 0))

def testNull(self):
button = QgsFontButton()
self.assertFalse(button.showNullFormat())
button.setShowNullFormat(True)
self.assertTrue(button.showNullFormat())

s = QgsTextFormat()
s.setFont(getTestFont())
button.setTextFormat(s)
self.assertTrue(button.textFormat().isValid())
button.setToNullFormat()
self.assertFalse(button.textFormat().isValid())
button.setTextFormat(s)
self.assertTrue(button.textFormat().isValid())


if __name__ == '__main__':
unittest.main()

0 comments on commit cdc9126

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