Skip to content

Commit

Permalink
When setting a new text format for a QgsFontButton, automatically
Browse files Browse the repository at this point in the history
update any open configuration panel to reflect the new format
(or dismiss it automatically if the new format is invalid)

Avoids UI going out of sync with text formats when a panel is already
open
  • Loading branch information
nyalldawson committed Jul 27, 2020
1 parent 7cfa476 commit 466a01c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions python/gui/auto_generated/qgsfontbutton.sip.in
Expand Up @@ -9,6 +9,7 @@




class QgsFontButton : QToolButton
{
%Docstring
Expand Down
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgstextformatwidget.sip.in
Expand Up @@ -277,6 +277,13 @@ Constructor for QgsTextFormatPanelWidget.
QgsTextFormat format() const;
%Docstring
Returns the current formatting settings defined by the widget.
%End

void setFormat( const QgsTextFormat &format );
%Docstring
Sets the ``format`` to show in the widget.

.. versionadded:: 3.16
%End

void setContext( const QgsSymbolWidgetContext &context );
Expand Down
16 changes: 11 additions & 5 deletions src/gui/qgsfontbutton.cpp
Expand Up @@ -94,12 +94,12 @@ void QgsFontButton::showSettingsDialog()
QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
if ( panel && panel->dockMode() )
{
QgsTextFormatPanelWidget *formatWidget = new QgsTextFormatPanelWidget( mFormat, mMapCanvas, this, mLayer.data() );
formatWidget->setPanelTitle( mDialogTitle );
formatWidget->setContext( symbolContext );
mActivePanel = new QgsTextFormatPanelWidget( mFormat, mMapCanvas, this, mLayer.data() );
mActivePanel->setPanelTitle( mDialogTitle );
mActivePanel->setContext( symbolContext );

connect( formatWidget, &QgsTextFormatPanelWidget::widgetChanged, this, [ this, formatWidget ] { this->setTextFormat( formatWidget->format() ); } );
panel->openPanel( formatWidget );
connect( mActivePanel, &QgsTextFormatPanelWidget::widgetChanged, this, [ this ] { setTextFormat( mActivePanel->format() ); } );
panel->openPanel( mActivePanel );
return;
}

Expand Down Expand Up @@ -154,8 +154,14 @@ QgsMessageBar *QgsFontButton::messageBar() const

void QgsFontButton::setTextFormat( const QgsTextFormat &format )
{
if ( mActivePanel && !format.isValid() )
mActivePanel->acceptPanel();

mFormat = format;
updatePreview();

if ( mActivePanel && format.isValid() )
mActivePanel->setFormat( format );
emit changed();
}

Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsfontbutton.h
Expand Up @@ -24,6 +24,8 @@
class QgsExpressionContextGenerator;
class QgsMapCanvas;
class QgsMessageBar;
class QgsTextFormatPanelWidget;


/**
* \ingroup gui
Expand Down Expand Up @@ -329,6 +331,7 @@ class GUI_EXPORT QgsFontButton : public QToolButton
bool mShowNoFormat = false;
QString mNullFormatString;
QPointer< QAction > mNullFormatAction;
QPointer< QgsTextFormatPanelWidget > mActivePanel;

/**
* Attempts to parse \a mimeData as a text format.
Expand Down
13 changes: 12 additions & 1 deletion src/gui/qgstextformatwidget.cpp
Expand Up @@ -2036,14 +2036,25 @@ QgsTextFormatPanelWidget::QgsTextFormatPanelWidget( const QgsTextFormat &format,
: QgsPanelWidgetWrapper( new QgsTextFormatWidget( format, mapCanvas, nullptr, layer ), parent )
{
mFormatWidget = qobject_cast< QgsTextFormatWidget * >( widget() );
connect( mFormatWidget, &QgsTextFormatWidget::widgetChanged, this, &QgsPanelWidget::widgetChanged );
connect( mFormatWidget, &QgsTextFormatWidget::widgetChanged, this, [ = ]
{
if ( !mBlockSignals )
emit widgetChanged();
} );
}

QgsTextFormat QgsTextFormatPanelWidget::format() const
{
return mFormatWidget->format();
}

void QgsTextFormatPanelWidget::setFormat( const QgsTextFormat &format )
{
mBlockSignals = true;
mFormatWidget->setFormat( format );
mBlockSignals = false;
}

void QgsTextFormatPanelWidget::setContext( const QgsSymbolWidgetContext &context )
{
mFormatWidget->setContext( context );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgstextformatwidget.h
Expand Up @@ -395,6 +395,13 @@ class GUI_EXPORT QgsTextFormatPanelWidget : public QgsPanelWidgetWrapper
*/
QgsTextFormat format() const;

/**
* Sets the \a format to show in the widget.
*
* \since QGIS 3.16
*/
void setFormat( const QgsTextFormat &format );

/**
* Sets the \a context in which the widget is shown, e.g., the associated map canvas and expression contexts.
* \since QGIS 3.10
Expand All @@ -406,6 +413,7 @@ class GUI_EXPORT QgsTextFormatPanelWidget : public QgsPanelWidgetWrapper
private:

QgsTextFormatWidget *mFormatWidget = nullptr;
bool mBlockSignals = false;
};

#endif //QGSTEXTFORMATWIDGET_H
Expand Down

0 comments on commit 466a01c

Please sign in to comment.