[callouts] Allow callout types to have icons, and use them in the type combobox

...and pick ugly icons to prompt @nirvn into action ;)
This commit is contained in:
Nyall Dawson 2019-07-23 09:57:24 +10:00
parent ab4d8c4500
commit 45b23fd5b2
8 changed files with 100 additions and 10 deletions

View File

@ -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();
@ -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/;

View File

@ -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

View File

@ -16,6 +16,7 @@
#include "qgscalloutsregistry.h"
#include "qgscallout.h"
#include "qgsxmlutils.h"
#include "qgsapplication.h"
//
// QgsCalloutAbstractMetadata
@ -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()

View File

@ -19,6 +19,7 @@
#include "qgis_core.h"
#include "qgis.h"
#include "qgsreadwritecontext.h"
#include <QIcon>
class QgsPathResolver;
class QgsVectorLayer;
@ -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;
@ -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.
*
@ -80,6 +96,7 @@ class CORE_EXPORT QgsCalloutAbstractMetadata
protected:
QString mName;
QString mVisibleName;
QIcon mIcon;
};
typedef QgsCallout *( *QgsCalloutCreateFunc )( const QVariantMap &, const QgsReadWriteContext & ) SIP_SKIP;
@ -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 )
{}

View File

@ -55,6 +55,13 @@ 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 )
@ -62,10 +69,28 @@ class CORE_EXPORT QgsRendererAbstractMetadata
{}
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; }
/**

View File

@ -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;" ) );

View File

@ -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 + '/';

View File

@ -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" ) );
@ -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;