34 class QgsAnnotationLayerSpatialIndex :
public RTree<QString, float, 2, float>
38 void insert(
const QString &uuid,
const QgsRectangle &bounds )
40 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
43 scaledBounds[0], scaledBounds[ 1]
46 scaledBounds[2], scaledBounds[3]
57 void remove(
const QString &uuid,
const QgsRectangle &bounds )
59 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
62 scaledBounds[0], scaledBounds[ 1]
65 scaledBounds[2], scaledBounds[3]
75 bool intersects(
const QgsRectangle &bounds,
const std::function<
bool(
const QString &uuid )> &callback )
const
77 std::array< float, 4 > scaledBounds = scaleBounds( bounds );
80 scaledBounds[0], scaledBounds[ 1]
83 scaledBounds[2], scaledBounds[3]
90 std::array<float, 4> scaleBounds(
const QgsRectangle &bounds )
const
94 static_cast< float >( bounds.
xMinimum() ),
95 static_cast< float >( bounds.
yMinimum() ),
96 static_cast< float >( bounds.
xMaximum() ),
97 static_cast< float >( bounds.
yMaximum() )
105 , mTransformContext( options.transformContext )
106 , mSpatialIndex( std::make_unique< QgsAnnotationLayerSpatialIndex >() )
113 mDataProvider =
new QgsAnnotationLayerDataProvider( providerOptions, QgsDataProvider::ReadFlags() );
116 mPaintEffect->setEnabled(
false );
122 qDeleteAll( mItems );
123 delete mDataProvider;
136 const QString uuid = QUuid::createUuid().toString();
137 mItems.insert( uuid,
item );
139 mNonIndexedItems.insert( uuid );
150 std::unique_ptr< QgsAnnotationItem> prevItem( mItems.take(
id ) );
154 auto it = mNonIndexedItems.find(
id );
155 if ( it == mNonIndexedItems.end() )
157 mSpatialIndex->remove(
id, prevItem->boundingBox() );
161 mNonIndexedItems.erase( it );
165 mItems.insert(
id,
item );
167 mNonIndexedItems.insert(
id );
176 if ( !mItems.contains(
id ) )
179 std::unique_ptr< QgsAnnotationItem>
item( mItems.take(
id ) );
181 auto it = mNonIndexedItems.find(
id );
182 if ( it == mNonIndexedItems.end() )
188 mNonIndexedItems.erase( it );
200 qDeleteAll( mItems );
202 mSpatialIndex = std::make_unique< QgsAnnotationLayerSpatialIndex >();
203 mNonIndexedItems.clear();
210 return mItems.empty();
215 return mItems.value(
id );
223 mSpatialIndex->intersects( bounds, [&res, feedback](
const QString & uuid )->
bool
233 QStringList res = queryIndex( bounds, feedback );
235 for (
const QString &uuid : mNonIndexedItems )
237 if ( mItems.value( uuid )->boundingBox( context ).intersects( bounds ) )
250 auto it = mNonIndexedItems.find( operation->
itemId() );
251 if ( it == mNonIndexedItems.end() )
253 mSpatialIndex->remove( operation->
itemId(), targetItem->boundingBox() );
255 res = targetItem->applyEdit( operation );
263 mSpatialIndex->insert( operation->
itemId(), targetItem->boundingBox() );
268 delete mItems.take( operation->
itemId() );
269 mNonIndexedItems.remove( operation->
itemId() );
289 std::unique_ptr< QgsAnnotationLayer > layer = std::make_unique< QgsAnnotationLayer >(
name(), options );
292 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
294 layer->mItems.insert( it.key(), ( *it )->clone() );
296 layer->mNonIndexedItems.insert( it.key() );
298 layer->mSpatialIndex->insert( it.key(), ( *it )->boundingBox() );
302 layer->setPaintEffect( mPaintEffect->clone() );
304 return layer.release();
315 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
319 rect = it.value()->boundingBox();
334 mTransformContext = context;
345 qDeleteAll( mItems );
347 mSpatialIndex = std::make_unique< QgsAnnotationLayerSpatialIndex >();
348 mNonIndexedItems.clear();
350 const QDomNodeList itemsElements = layerNode.toElement().elementsByTagName( QStringLiteral(
"items" ) );
351 if ( itemsElements.size() == 0 )
354 const QDomNodeList
items = itemsElements.at( 0 ).childNodes();
355 for (
int i = 0; i <
items.size(); ++i )
357 const QDomElement itemElement =
items.at( i ).toElement();
358 const QString
id = itemElement.attribute( QStringLiteral(
"id" ) );
359 const QString
type = itemElement.attribute( QStringLiteral(
"type" ) );
365 mNonIndexedItems.insert(
id );
368 mItems.insert(
id,
item.release() );
383 QDomElement mapLayerNode = layer_node.toElement();
385 if ( mapLayerNode.isNull() )
393 QDomElement itemsElement = doc.createElement( QStringLiteral(
"items" ) );
394 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
396 QDomElement itemElement = doc.createElement( QStringLiteral(
"item" ) );
397 itemElement.setAttribute( QStringLiteral(
"type" ), ( *it )->type() );
398 itemElement.setAttribute( QStringLiteral(
"id" ), it.key() );
399 ( *it )->writeXml( itemElement, doc, context );
400 itemsElement.appendChild( itemElement );
402 mapLayerNode.appendChild( itemsElement );
414 QDomElement layerOpacityElem = doc.createElement( QStringLiteral(
"layerOpacity" ) );
415 const QDomText layerOpacityText = doc.createTextNode( QString::number(
opacity() ) );
416 layerOpacityElem.appendChild( layerOpacityText );
417 node.appendChild( layerOpacityElem );
423 QDomElement blendModeElem = doc.createElement( QStringLiteral(
"blendMode" ) );
425 blendModeElem.appendChild( blendModeText );
426 node.appendChild( blendModeElem );
428 QDomElement paintEffectElem = doc.createElement( QStringLiteral(
"paintEffect" ) );
430 mPaintEffect->saveProperties( doc, paintEffectElem );
431 node.appendChild( paintEffectElem );
441 const QDomNode layerOpacityNode = node.namedItem( QStringLiteral(
"layerOpacity" ) );
442 if ( !layerOpacityNode.isNull() )
444 const QDomElement e = layerOpacityNode.toElement();
452 const QDomNode blendModeNode = node.namedItem( QStringLiteral(
"blendMode" ) );
453 if ( !blendModeNode.isNull() )
455 const QDomElement e = blendModeNode.toElement();
460 const QDomNode paintEffectNode = node.namedItem( QStringLiteral(
"paintEffect" ) );
461 if ( !paintEffectNode.isNull() )
463 const QDomElement effectElem = paintEffectNode.firstChildElement( QStringLiteral(
"effect" ) );
464 if ( !effectElem.isNull() )
487 return mDataProvider;
492 return mDataProvider;
497 QString
metadata = QStringLiteral(
"<html>\n<body>\n<h1>" ) + tr(
"General" ) + QStringLiteral(
"</h1>\n<hr>\n" ) + QStringLiteral(
"<table class=\"list-view\">\n" );
499 metadata += QStringLiteral(
"<tr><td class=\"highlight\">" ) + tr(
"Name" ) + QStringLiteral(
"</td><td>" ) +
name() + QStringLiteral(
"</td></tr>\n" );
502 metadata += QStringLiteral(
"<tr><td class=\"highlight\">" ) + tr(
"Extent" ) + QStringLiteral(
"</td><td>" ) +
extent().
toString() + QStringLiteral(
"</td></tr>\n" );
505 QLocale locale = QLocale();
506 locale.setNumberOptions( locale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
507 const int itemCount = mItems.size();
508 metadata += QStringLiteral(
"<tr><td class=\"highlight\">" )
509 + tr(
"Item count" ) + QStringLiteral(
"</td><td>" )
510 + locale.toString(
static_cast<qlonglong
>( itemCount ) )
511 + QStringLiteral(
"</td></tr>\n" );
512 metadata += QLatin1String(
"</table>\n<br><br>" );
518 metadata += QStringLiteral(
"<h1>" ) + tr(
"Items" ) + QStringLiteral(
"</h1>\n<hr>\n" );
520 metadata += QLatin1String(
"<table width=\"100%\" class=\"tabular-view\">\n" );
521 metadata += QLatin1String(
"<tr><th>" ) + tr(
"Type" ) + QLatin1String(
"</th><th>" ) + tr(
"Count" ) + QLatin1String(
"</th></tr>\n" );
523 QMap< QString, int > itemCounts;
524 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
526 itemCounts[ it.value()->type() ]++;
531 for (
auto it = itemTypes.begin(); it != itemTypes.end(); ++it )
535 rowClass = QStringLiteral(
"class=\"odd-row\"" );
536 metadata += QLatin1String(
"<tr " ) + rowClass + QLatin1String(
"><td>" ) + it.value() + QLatin1String(
"</td><td>" ) + locale.toString(
static_cast<qlonglong
>( itemCounts.value( it.key() ) ) ) + QLatin1String(
"</td></tr>\n" );
540 metadata += QLatin1String(
"</table>\n<br><br>" );
542 metadata += QLatin1String(
"\n</body>\n</html>\n" );
548 return mPaintEffect.get();
553 mPaintEffect.reset( effect );
561 QgsAnnotationLayerDataProvider::QgsAnnotationLayerDataProvider(
562 const ProviderOptions &options,
563 QgsDataProvider::ReadFlags flags )
572 QString QgsAnnotationLayerDataProvider::name()
const
574 return QStringLiteral(
"annotation" );
577 QString QgsAnnotationLayerDataProvider::description()
const
582 QgsRectangle QgsAnnotationLayerDataProvider::extent()
const
587 bool QgsAnnotationLayerDataProvider::isValid()
const
@ UsersCannotToggleEditing
Indicates that users are not allowed to toggle editing for this layer. Note that this does not imply ...
@ ScaleDependentBoundingBox
Item's bounding box will vary depending on map scale.
AnnotationItemEditOperationResult
Results from an edit operation on an annotation item.
@ Invalid
Operation has invalid parameters for the item, no change occurred.
@ Success
Item was modified successfully.
@ ItemCleared
The operation results in the item being cleared, and the item should be removed from the layer as a r...
Abstract base class for annotation item edit operations.
QString itemId() const
Returns the associated item ID.
QMap< QString, QString > itemTypes() const
Returns a map of available item types to translated name.
Abstract base class for annotation items which are drawn with QgsAnnotationLayers.
virtual QgsRectangle boundingBox() const =0
Returns the bounding box of the item's geographic location, in the parent layer's coordinate referenc...
virtual bool readXml(const QDomElement &element, const QgsReadWriteContext &context)=0
Reads the item's state from the given DOM element.
virtual Qgis::AnnotationItemFlags flags() const
Returns item flags.
Represents a map layer containing a set of georeferenced annotations, e.g.
QgsRectangle extent() const override
Returns the extent of the layer.
bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &, StyleCategories categories=AllStyleCategories) const override
Write the style for the layer into the document provided.
bool readSymbology(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) override
Read the symbology for the current layer from the DOM node supplied.
void clear()
Removes all items from the layer.
QgsDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
QgsAnnotationItem * item(const QString &id)
Returns the item with the specified id, or nullptr if no matching item was found.
QgsMapLayerRenderer * createMapRenderer(QgsRenderContext &rendererContext) override
Returns new instance of QgsMapLayerRenderer that will be used for rendering of given context.
QMap< QString, QgsAnnotationItem * > items() const
Returns a map of items contained in the layer, by unique item ID.
bool isEditable() const override
Returns true if the layer can be edited.
bool removeItem(const QString &id)
Removes (and deletes) the item with matching id.
QStringList itemsInBounds(const QgsRectangle &bounds, QgsRenderContext &context, QgsFeedback *feedback=nullptr) const
Returns a list of the IDs of all annotation items within the specified bounds (in layer CRS),...
void setTransformContext(const QgsCoordinateTransformContext &context) override
Sets the coordinate transform context to transformContext.
Qgis::AnnotationItemEditOperationResult applyEdit(QgsAbstractAnnotationItemEditOperation *operation)
Applies an edit operation to the layer.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the layer.
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the layer.
void replaceItem(const QString &id, QgsAnnotationItem *item)
Replaces the existing item with matching id with a new item.
QgsAnnotationLayer * clone() const override
Returns a new instance equivalent to this one except for the id which is still unique.
friend class QgsAnnotationLayerRenderer
bool supportsEditing() const override
Returns whether the layer supports editing or not.
void reset()
Resets the annotation layer to a default state, and clears all items from it.
QString addItem(QgsAnnotationItem *item)
Adds an item to the layer.
QString htmlMetadata() const override
Obtain a formatted HTML string containing assorted metadata for this layer.
Qgis::MapLayerProperties properties() const override
Returns the map layer properties of this layer.
bool isEmpty() const
Returns true if the annotation layer is empty and contains no annotations.
~QgsAnnotationLayer() override
bool writeXml(QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context) const override
Called by writeLayerXML(), used by children to write state specific to them to project files.
bool readXml(const QDomNode &layerNode, QgsReadWriteContext &context) override
Called by readLayerXML(), used by children to read state specific to them from project files.
QgsAnnotationLayer(const QString &name, const QgsAnnotationLayer::LayerOptions &options)
Constructor for a new QgsAnnotationLayer with the specified layer name.
static QgsAnnotationItemRegistry * annotationItemRegistry()
Returns the application's annotation item registry, used for annotation item types.
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Abstract base class for spatial data provider implementations.
virtual void setTransformContext(const QgsCoordinateTransformContext &transformContext)
Sets data coordinate transform context to transformContext.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
static QString typeToString(QgsMapLayerType type)
Converts a map layer type to a string value.
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Base class for all map layer types.
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
QString crsHtmlMetadata() const
Returns a HTML fragment containing the layer's CRS metadata, for use in the htmlMetadata() method.
QgsLayerMetadata metadata
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
@ FlagDontResolveLayers
Don't resolve layer paths or create data providers for layers.
QgsMapLayer::ReadFlags mReadFlags
Read flags. It's up to the subclass to respect these when restoring state from XML.
bool mValid
Indicates if the layer is valid and can be drawn.
@ Rendering
Rendering: scale visibility, simplify method, opacity.
void invalidateWgs84Extent()
Invalidates the WGS84 extent.
bool mShouldValidateCrs
true if the layer's CRS should be validated and invalid CRSes are not permitted.
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
static QgsPaintEffect * defaultStack()
Returns a new effect stack consisting of a sensible selection of default effects.
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
Base class for visual effects which can be applied to QPicture drawings.
static QgsPainting::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a BlendMode corresponding to a QPainter::CompositionMode.
static QPainter::CompositionMode getCompositionMode(QgsPainting::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a BlendMode.
BlendMode
Blending modes enum defining the available composition modes that can be used when rendering a layer.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
Contains information about the context of a rendering operation.
QgsMapLayerType
Types of layers that can be added to a map.
@ AnnotationLayer
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
#define QgsDebugMsgLevel(str, level)
const QgsCoordinateReferenceSystem & crs
Setting options for loading annotation layers.
QgsCoordinateTransformContext transformContext
Coordinate transform context.
Setting options for creating vector data providers.
QgsCoordinateTransformContext transformContext
Coordinate transform context.