Skip to content

Commit

Permalink
[callouts] Allow callout types to have icons, and use them in the typ…
Browse files Browse the repository at this point in the history
…e combobox

...and pick ugly icons to prompt @nirvn into action ;)
  • Loading branch information
nyalldawson committed Jul 23, 2019
1 parent ab4d8c4 commit 45b23fd
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 10 deletions.
18 changes: 17 additions & 1 deletion python/core/auto_generated/callouts/qgscalloutsregistry.sip.in
Expand Up @@ -28,11 +28,13 @@ Stores metadata about one callout renderer class.
%End
public:

QgsCalloutAbstractMetadata( const QString &name, const QString &visibleName );
QgsCalloutAbstractMetadata( const QString &name, const QString &visibleName, const QIcon &icon = QIcon() );
%Docstring
Constructor for QgsCalloutAbstractMetadata, with the specified ``name``.

The ``visibleName`` argument gives a translated, user friendly string identifying the callout type.

The ``icon`` argument can be used to specify an icon representing the callout.
%End

virtual ~QgsCalloutAbstractMetadata();
Expand All @@ -49,6 +51,20 @@ Returns the unique name of the callout type. This value is not translated.
Returns a friendly display name of the callout type. This value is translated.

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

QIcon icon() const;
%Docstring
Returns an icon representing the callout.

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

void setIcon( const QIcon &icon );
%Docstring
Sets an ``icon`` representing the callout.

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

virtual QgsCallout *createCallout( const QVariantMap &properties, const QgsReadWriteContext &context ) = 0 /Factory/;
Expand Down
29 changes: 29 additions & 0 deletions python/core/auto_generated/symbology/qgsrendererregistry.sip.in
Expand Up @@ -37,13 +37,42 @@ Stores metadata about one renderer class.


QgsRendererAbstractMetadata( const QString &name, const QString &visibleName, const QIcon &icon = QIcon() );
%Docstring
Constructor for QgsRendererAbstractMetadata, with the specified ``name``.

The ``visibleName`` argument gives a translated, user friendly string identifying the renderer type.

The ``icon`` argument can be used to specify an icon representing the renderer.
%End
virtual ~QgsRendererAbstractMetadata();

QString name() const;
%Docstring
Returns the unique name of the renderer. This value is not translated.

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

QString visibleName() const;
%Docstring
Returns a friendly display name of the renderer. This value is translated.

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

QIcon icon() const;
%Docstring
Returns an icon representing the renderer.

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

void setIcon( const QIcon &icon );
%Docstring
Sets an ``icon`` representing the renderer.

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

virtual QgsRendererAbstractMetadata::LayerTypes compatibleLayerTypes() const;
%Docstring
Expand Down
5 changes: 3 additions & 2 deletions src/core/callouts/qgscalloutsregistry.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgscalloutsregistry.h"
#include "qgscallout.h"
#include "qgsxmlutils.h"
#include "qgsapplication.h"

//
// QgsCalloutAbstractMetadata
Expand Down Expand Up @@ -48,8 +49,8 @@ QgsCalloutWidget *QgsCalloutMetadata::createCalloutWidget( QgsVectorLayer *vl )
QgsCalloutRegistry::QgsCalloutRegistry()
{
// init registry with known callouts
addCalloutType( new QgsCalloutMetadata( QStringLiteral( "simple" ), QObject::tr( "Simple lines" ), QgsSimpleLineCallout::create ) );
addCalloutType( new QgsCalloutMetadata( QStringLiteral( "manhattan" ), QObject::tr( "Manhattan lines" ), QgsManhattanLineCallout::create ) );
addCalloutType( new QgsCalloutMetadata( QStringLiteral( "simple" ), QObject::tr( "Simple lines" ), QgsApplication::getThemeIcon( QStringLiteral( "mIconSnappingSegment.svg" ) ), QgsSimpleLineCallout::create ) );
addCalloutType( new QgsCalloutMetadata( QStringLiteral( "manhattan" ), QObject::tr( "Manhattan lines" ), QgsApplication::getThemeIcon( QStringLiteral( "mIconSnappingSegment.svg" ) ), QgsManhattanLineCallout::create ) );
}

QgsCalloutRegistry::~QgsCalloutRegistry()
Expand Down
22 changes: 20 additions & 2 deletions src/core/callouts/qgscalloutsregistry.h
Expand Up @@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgsreadwritecontext.h"
#include <QIcon>

class QgsPathResolver;
class QgsVectorLayer;
Expand All @@ -43,10 +44,13 @@ class CORE_EXPORT QgsCalloutAbstractMetadata
* Constructor for QgsCalloutAbstractMetadata, with the specified \a name.
*
* The \a visibleName argument gives a translated, user friendly string identifying the callout type.
*
* The \a icon argument can be used to specify an icon representing the callout.
*/
QgsCalloutAbstractMetadata( const QString &name, const QString &visibleName )
QgsCalloutAbstractMetadata( const QString &name, const QString &visibleName, const QIcon &icon = QIcon() )
: mName( name )
, mVisibleName( visibleName )
, mIcon( icon )
{}

virtual ~QgsCalloutAbstractMetadata() = default;
Expand All @@ -63,6 +67,18 @@ class CORE_EXPORT QgsCalloutAbstractMetadata
*/
QString visibleName() const { return mVisibleName; }

/**
* Returns an icon representing the callout.
* \see setIcon()
*/
QIcon icon() const { return mIcon; }

/**
* Sets an \a icon representing the callout.
* \see icon()
*/
void setIcon( const QIcon &icon ) { mIcon = icon; }

/**
* Create a callout of this type given the map of \a properties.
*
Expand All @@ -80,6 +96,7 @@ class CORE_EXPORT QgsCalloutAbstractMetadata
protected:
QString mName;
QString mVisibleName;
QIcon mIcon;
};

typedef QgsCallout *( *QgsCalloutCreateFunc )( const QVariantMap &, const QgsReadWriteContext & ) SIP_SKIP;
Expand All @@ -96,9 +113,10 @@ class CORE_EXPORT QgsCalloutMetadata : public QgsCalloutAbstractMetadata

//! \note not available in Python bindings
QgsCalloutMetadata( const QString &name, const QString &visibleName,
const QIcon &icon,
QgsCalloutCreateFunc pfCreate,
QgsCalloutWidgetFunc pfWidget = nullptr ) SIP_SKIP
: QgsCalloutAbstractMetadata( name, visibleName )
: QgsCalloutAbstractMetadata( name, visibleName, icon )
, mCreateFunc( pfCreate )
, mWidgetFunc( pfWidget )
{}
Expand Down
25 changes: 25 additions & 0 deletions src/core/symbology/qgsrendererregistry.h
Expand Up @@ -55,17 +55,42 @@ class CORE_EXPORT QgsRendererAbstractMetadata
};
Q_DECLARE_FLAGS( LayerTypes, LayerType )

/**
* Constructor for QgsRendererAbstractMetadata, with the specified \a name.
*
* The \a visibleName argument gives a translated, user friendly string identifying the renderer type.
*
* The \a icon argument can be used to specify an icon representing the renderer.
*/
QgsRendererAbstractMetadata( const QString &name, const QString &visibleName, const QIcon &icon = QIcon() )
: mName( name )
, mVisibleName( visibleName )
, mIcon( icon )
{}
virtual ~QgsRendererAbstractMetadata() = default;

/**
* Returns the unique name of the renderer. This value is not translated.
* \see visibleName()
*/
QString name() const { return mName; }

/**
* Returns a friendly display name of the renderer. This value is translated.
* \see name()
*/
QString visibleName() const { return mVisibleName; }

/**
* Returns an icon representing the renderer.
* \see setIcon()
*/
QIcon icon() const { return mIcon; }

/**
* Sets an \a icon representing the renderer.
* \see icon()
*/
void setIcon( const QIcon &icon ) { mIcon = icon; }

/**
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgslabelinggui.cpp
Expand Up @@ -155,7 +155,8 @@ QgsLabelingGui::QgsLabelingGui( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas,
const QStringList calloutTypes = QgsApplication::calloutRegistry()->calloutTypes();
for ( const QString &type : calloutTypes )
{
mCalloutStyleComboBox->addItem( QgsApplication::calloutRegistry()->calloutMetadata( type )->visibleName(), type );
mCalloutStyleComboBox->addItem( QgsApplication::calloutRegistry()->calloutMetadata( type )->icon(),
QgsApplication::calloutRegistry()->calloutMetadata( type )->visibleName(), type );
}

mGeometryGeneratorWarningLabel->setStyleSheet( QStringLiteral( "color: #FFC107;" ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgscallout.cpp
Expand Up @@ -157,7 +157,7 @@ void TestQgsCallout::initTestCase()
mReport += QLatin1String( "<h1>Callout Tests</h1>\n" );

QgsCalloutRegistry *registry = QgsApplication::calloutRegistry();
registry->addCalloutType( new QgsCalloutMetadata( QStringLiteral( "Dummy" ), QStringLiteral( "Dummy callout" ), DummyCallout::create ) );
registry->addCalloutType( new QgsCalloutMetadata( QStringLiteral( "Dummy" ), QStringLiteral( "Dummy callout" ), QIcon(), DummyCallout::create ) );

QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
mTestDataDir = myDataDir + '/';
Expand Down
6 changes: 3 additions & 3 deletions tests/src/core/testqgscalloutregistry.cpp
Expand Up @@ -79,7 +79,7 @@ void TestQgsCalloutRegistry::cleanup()

void TestQgsCalloutRegistry::metadata()
{
QgsCalloutMetadata metadata = QgsCalloutMetadata( QStringLiteral( "name" ), QStringLiteral( "display name" ), DummyCallout::create );
QgsCalloutMetadata metadata = QgsCalloutMetadata( QStringLiteral( "name" ), QStringLiteral( "display name" ), QIcon(), DummyCallout::create );
QCOMPARE( metadata.name(), QString( "name" ) );
QCOMPARE( metadata.visibleName(), QString( "display name" ) );

Expand Down Expand Up @@ -111,10 +111,10 @@ void TestQgsCalloutRegistry::addCallout()
QgsCalloutRegistry *registry = QgsApplication::calloutRegistry();
int previousCount = registry->calloutTypes().length();

registry->addCalloutType( new QgsCalloutMetadata( QStringLiteral( "Dummy" ), QStringLiteral( "Dummy callout" ), DummyCallout::create ) );
registry->addCalloutType( new QgsCalloutMetadata( QStringLiteral( "Dummy" ), QStringLiteral( "Dummy callout" ), QIcon(), DummyCallout::create ) );
QCOMPARE( registry->calloutTypes().length(), previousCount + 1 );
//try adding again, should have no effect
QgsCalloutMetadata *dupe = new QgsCalloutMetadata( QStringLiteral( "Dummy" ), QStringLiteral( "Dummy callout" ), DummyCallout::create );
QgsCalloutMetadata *dupe = new QgsCalloutMetadata( QStringLiteral( "Dummy" ), QStringLiteral( "Dummy callout" ), QIcon(), DummyCallout::create );
QVERIFY( ! registry->addCalloutType( dupe ) );
QCOMPARE( registry->calloutTypes().length(), previousCount + 1 );
delete dupe;
Expand Down

0 comments on commit 45b23fd

Please sign in to comment.