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 class QgsFontButton : QToolButton
{ {
%Docstring %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; QgsTextFormat format() const;
%Docstring %Docstring
Returns the current formatting settings defined by the widget. 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 %End


void setContext( const QgsSymbolWidgetContext &context ); 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 ); QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this );
if ( panel && panel->dockMode() ) if ( panel && panel->dockMode() )
{ {
QgsTextFormatPanelWidget *formatWidget = new QgsTextFormatPanelWidget( mFormat, mMapCanvas, this, mLayer.data() ); mActivePanel = new QgsTextFormatPanelWidget( mFormat, mMapCanvas, this, mLayer.data() );
formatWidget->setPanelTitle( mDialogTitle ); mActivePanel->setPanelTitle( mDialogTitle );
formatWidget->setContext( symbolContext ); mActivePanel->setContext( symbolContext );


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


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


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

mFormat = format; mFormat = format;
updatePreview(); updatePreview();

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


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



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


/** /**
* Attempts to parse \a mimeData as a text format. * 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 ) : QgsPanelWidgetWrapper( new QgsTextFormatWidget( format, mapCanvas, nullptr, layer ), parent )
{ {
mFormatWidget = qobject_cast< QgsTextFormatWidget * >( widget() ); 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 QgsTextFormat QgsTextFormatPanelWidget::format() const
{ {
return mFormatWidget->format(); return mFormatWidget->format();
} }


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

void QgsTextFormatPanelWidget::setContext( const QgsSymbolWidgetContext &context ) void QgsTextFormatPanelWidget::setContext( const QgsSymbolWidgetContext &context )
{ {
mFormatWidget->setContext( 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; 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. * Sets the \a context in which the widget is shown, e.g., the associated map canvas and expression contexts.
* \since QGIS 3.10 * \since QGIS 3.10
Expand All @@ -406,6 +413,7 @@ class GUI_EXPORT QgsTextFormatPanelWidget : public QgsPanelWidgetWrapper
private: private:


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


#endif //QGSTEXTFORMATWIDGET_H #endif //QGSTEXTFORMATWIDGET_H
Expand Down

0 comments on commit 466a01c

Please sign in to comment.