Skip to content
Permalink
Browse files
If a text format with the name "Default" is found in the style database,
use this as the text format for newly created layer labels

Instead of defaulting to a random font, this gives us a mechanism
to supply a better default label font to users via the default
style database. And users can always modify this "default" text
format if they'd like to change the default font!

For now this is the underlying code logic changes only -- we
don't yet include a 'Default' text format in the default style database
to take advantage of this functionality.
  • Loading branch information
nyalldawson committed Apr 29, 2021
1 parent 8f7ecd9 commit 3c06b6d42574126f4c62cfee1153eebf0f53cbd2
@@ -0,0 +1,5 @@
# The following has been generated automatically from src/core/symbology/qgsstyle.h
# monkey patching scoped based enum
QgsStyle.TextFormatContext.Labeling.__doc__ = "Text format used in labeling"
QgsStyle.TextFormatContext.__doc__ = 'Text format context.\n\n.. versionadded:: 3.20\n\n' + '* ``Labeling``: ' + QgsStyle.TextFormatContext.Labeling.__doc__
# --
@@ -701,6 +701,18 @@ Returns the default patch geometry for the given symbol ``type`` and ``size`` as
.. seealso:: :py:func:`defaultPatch`

.. versionadded:: 3.14
%End

enum class TextFormatContext
{
Labeling,
};

QgsTextFormat defaultTextFormat( QgsStyle::TextFormatContext context = QgsStyle::TextFormatContext::Labeling ) const;
%Docstring
Returns the default text format to use for new text based objects in the specified ``context``.

.. versionadded:: 3.20
%End

bool saveSymbol3D( const QString &name, QgsAbstract3DSymbol *symbol /Transfer/, bool favorite, const QStringList &tags );
@@ -20,6 +20,7 @@
#include "qgsunittypes.h"
#include "qgsexception.h"
#include "qgsapplication.h"
#include "qgsstyle.h"

#include <list>

@@ -272,6 +273,8 @@ QgsPalLayerSettings::QgsPalLayerSettings()
, mCallout( QgsApplication::calloutRegistry()->defaultCallout() )
{
initPropertyDefinitions();

mFormat = QgsStyle::defaultStyle()->defaultTextFormat( QgsStyle::TextFormatContext::Labeling );
}
Q_NOWARN_DEPRECATED_POP

@@ -1222,6 +1222,11 @@ QList<QList<QPolygonF> > QgsStyle::defaultPatchAsQPolygonF( QgsSymbol::SymbolTyp
return res;
}

QgsTextFormat QgsStyle::defaultTextFormat( QgsStyle::TextFormatContext ) const
{
return textFormat( QStringLiteral( "Default" ) );
}

bool QgsStyle::saveSymbol3D( const QString &name, QgsAbstract3DSymbol *symbol, bool favorite, const QStringList &tags )
{
// insert it into the database
@@ -722,6 +722,23 @@ class CORE_EXPORT QgsStyle : public QObject
*/
QList< QList< QPolygonF > > defaultPatchAsQPolygonF( QgsSymbol::SymbolType type, QSizeF size ) const;

/**
* Text format context.
*
* \since QGIS 3.20
*/
enum class TextFormatContext : int
{
Labeling, //!< Text format used in labeling
};

/**
* Returns the default text format to use for new text based objects in the specified \a context.
*
* \since QGIS 3.20
*/
QgsTextFormat defaultTextFormat( QgsStyle::TextFormatContext context = QgsStyle::TextFormatContext::Labeling ) const;

/**
* Adds a 3d \a symbol to the database.
*
@@ -1139,6 +1156,7 @@ class CORE_EXPORT QgsStyle : public QObject
static QString tagmapEntityIdFieldName( StyleEntity type );

friend class Qgs3D;
friend class TestStyle;

Q_DISABLE_COPY( QgsStyle )
};
@@ -332,7 +332,7 @@ void QgsLabelingGui::setLayer( QgsMapLayer *mapLayer )
mGeometryGeneratorGroupBox->setCollapsed( true );
mGeometryGeneratorType->setCurrentIndex( mGeometryGeneratorType->findData( mSettings.geometryGeneratorType ) );

updateWidgetForFormat( mSettings.format() );
updateWidgetForFormat( mSettings.format().isValid() ? mSettings.format() : QgsStyle::defaultStyle()->defaultTextFormat( QgsStyle::TextFormatContext::Labeling ) );

mFieldExpressionWidget->setRow( -1 );
mFieldExpressionWidget->setField( mSettings.fieldName );
@@ -48,7 +48,7 @@ QgsTextFormatWidget::QgsTextFormatWidget( const QgsTextFormat &format, QgsMapCan
initWidget();
setWidgetMode( Text );
populateDataDefinedButtons();
updateWidgetForFormat( format );
updateWidgetForFormat( format.isValid() ? format : QgsStyle::defaultStyle()->defaultTextFormat() );
}

QgsTextFormatWidget::QgsTextFormatWidget( QgsMapCanvas *mapCanvas, QWidget *parent, Mode mode, QgsVectorLayer *layer )
@@ -117,6 +117,7 @@ class TestStyle : public QObject
void testVisitor();
void testColorRampShaderClassificationEqualInterval();
void testColorRampShaderClassificationContinius();
void testDefaultLabelTextFormat();
};


@@ -158,6 +159,9 @@ void TestStyle::initTestCase()
mStyle = new QgsStyle();
mStyle->createMemoryDatabase();

// now cheat!
QgsStyle::sDefaultStyle = mStyle;

// cpt-city ramp, small selection available in <testdir>/cpt-city
QgsCptCityArchive::initArchives();

@@ -171,7 +175,9 @@ void TestStyle::cleanupTestCase()
{
// don't save
// mStyle->save();
delete mStyle;

// don't delete -- it's handled by exitQgis, cos we've set mStyle as the static default style instance
// delete mStyle;

QgsCptCityArchive::clearArchives();
QgsApplication::exitQgis();
@@ -1811,5 +1817,27 @@ void TestStyle::testColorRampShaderClassificationContinius()
}
}

void TestStyle::testDefaultLabelTextFormat()
{
// no "Default" text format yet
QVERIFY( !QgsStyle::defaultStyle()->textFormat( QStringLiteral( "Default" ) ).isValid() );

QgsPalLayerSettings settings;
// should be app-wide default font (gross!)
QCOMPARE( settings.format().font().family(), QFont().family() );

// now add a default text format
QgsTextFormat format;
format.setFont( QgsFontUtils::getStandardTestFont() );
format.buffer().setEnabled( true );
QVERIFY( QgsStyle::defaultStyle()->addTextFormat( QStringLiteral( "Default" ), format ) );

// re-create default label settings
QgsPalLayerSettings settings2;
// should be default text format now, not app default font
QCOMPARE( settings2.format().font().family(), QgsFontUtils::getStandardTestFont().family() );
QVERIFY( settings2.format().buffer().enabled() );
}

QGSTEST_MAIN( TestStyle )
#include "testqgsstyle.moc"

0 comments on commit 3c06b6d

Please sign in to comment.