QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgslayoutitemregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemregistry.cpp
3  -------------------------
4  begin : June 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgslayoutitemregistry.h"
18 #include "qgslayoutitemshape.h"
19 #include "qgslayoutitemmap.h"
20 #include "qgslayoutitemlabel.h"
21 #include "qgslayoutitemlegend.h"
22 #include "qgslayoutitempolygon.h"
23 #include "qgslayoutitempolyline.h"
24 #include "qgslayoutitempage.h"
25 #include "qgslayoutitempicture.h"
26 #include "qgslayoutitemgroup.h"
27 #include "qgslayoutitemhtml.h"
28 #include "qgslayoutitemscalebar.h"
31 #include "qgslayoutitemtexttable.h"
32 #include "qgslayoutframe.h"
33 #include "qgslayoutitemmarker.h"
34 #include "qgsgloweffect.h"
35 #include "qgseffectstack.h"
36 #include "qgsvectorlayer.h"
37 #include "qgssymbol.h"
38 
39 #include <QPainter>
40 
42  : QObject( parent )
43 {
44 }
45 
47 {
48  qDeleteAll( mMetadata );
49  qDeleteAll( mMultiFrameMetadata );
50 }
51 
53 {
54  if ( !mMetadata.isEmpty() )
55  return false;
56 
57 #if 0
58  // add temporary item to register
59  auto createTemporaryItem = []( QgsLayout * layout )->QgsLayoutItem *
60  {
61  return new TestLayoutItem( layout );
62  };
63 
64  addLayoutItemType( new QgsLayoutItemMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, QStringLiteral( "temp type" ), createTemporaryItem ) );
65 #endif
66 
67  addLayoutItemType( new QgsLayoutItemMetadata( LayoutGroup, QObject::tr( "Group" ), QObject::tr( "Groups" ), QgsLayoutItemGroup::create ) );
68  addLayoutItemType( new QgsLayoutItemMetadata( LayoutFrame, QObject::tr( "Frame" ), QObject::tr( "Frames" ), QgsLayoutFrame::create ) );
69  addLayoutItemType( new QgsLayoutItemMetadata( LayoutPage, QObject::tr( "Page" ), QObject::tr( "Pages" ), QgsLayoutItemPage::create ) );
70  addLayoutItemType( new QgsLayoutItemMetadata( LayoutMap, QObject::tr( "Map" ), QObject::tr( "Maps" ), QgsLayoutItemMap::create ) );
71  addLayoutItemType( new QgsLayoutItemMetadata( LayoutPicture, QObject::tr( "Picture" ), QObject::tr( "Pictures" ), QgsLayoutItemPicture::create ) );
72  addLayoutItemType( new QgsLayoutItemMetadata( LayoutLabel, QObject::tr( "Label" ), QObject::tr( "Labels" ), QgsLayoutItemLabel::create ) );
73  addLayoutItemType( new QgsLayoutItemMetadata( LayoutLegend, QObject::tr( "Legend" ), QObject::tr( "Legends" ), QgsLayoutItemLegend::create ) );
74  addLayoutItemType( new QgsLayoutItemMetadata( LayoutScaleBar, QObject::tr( "Scalebar" ), QObject::tr( "Scalebars" ), QgsLayoutItemScaleBar::create ) );
75  addLayoutItemType( new QgsLayoutItemMetadata( LayoutShape, QObject::tr( "Shape" ), QObject::tr( "Shapes" ), []( QgsLayout * layout )
76  {
77  QgsLayoutItemShape *shape = new QgsLayoutItemShape( layout );
79  return shape;
80  } ) );
81  addLayoutItemType( new QgsLayoutItemMetadata( LayoutMarker, QObject::tr( "Marker" ), QObject::tr( "Markers" ), QgsLayoutItemMarker::create ) );
82  addLayoutItemType( new QgsLayoutItemMetadata( LayoutPolygon, QObject::tr( "Polygon" ), QObject::tr( "Polygons" ), QgsLayoutItemPolygon::create ) );
83  addLayoutItemType( new QgsLayoutItemMetadata( LayoutPolyline, QObject::tr( "Polyline" ), QObject::tr( "Polylines" ), QgsLayoutItemPolyline::create ) );
84 
89 
90  return true;
91 }
92 
94 {
95  return mMetadata.value( type );
96 }
97 
99 {
100  return mMultiFrameMetadata.value( type );
101 }
102 
104 {
105  if ( !metadata || mMetadata.contains( metadata->type() ) )
106  return false;
107 
108  mMetadata[metadata->type()] = metadata;
109  emit typeAdded( metadata->type(), metadata->visibleName() );
110  return true;
111 }
112 
114 {
115  if ( !metadata || mMultiFrameMetadata.contains( metadata->type() ) )
116  return false;
117 
118  mMultiFrameMetadata[metadata->type()] = metadata;
119  emit multiFrameTypeAdded( metadata->type(), metadata->visibleName() );
120  return true;
121 }
122 
124 {
125  if ( !mMetadata.contains( type ) )
126  return nullptr;
127 
128  return mMetadata[type]->createItem( layout );
129 }
130 
132 {
133  if ( !mMultiFrameMetadata.contains( type ) )
134  return nullptr;
135 
136  return mMultiFrameMetadata[type]->createMultiFrame( layout );
137 }
138 
139 void QgsLayoutItemRegistry::resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const
140 {
141  if ( mMetadata.contains( type ) )
142  {
143  mMetadata[type]->resolvePaths( properties, pathResolver, saving );
144  }
145  else if ( mMultiFrameMetadata.contains( type ) )
146  {
147  mMultiFrameMetadata[type]->resolvePaths( properties, pathResolver, saving );
148  }
149 }
150 
151 QMap<int, QString> QgsLayoutItemRegistry::itemTypes() const
152 {
153  QMap<int, QString> types;
154  for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
155  {
156  types.insert( it.key(), it.value()->visibleName() );
157  }
158  for ( auto it = mMultiFrameMetadata.constBegin(); it != mMultiFrameMetadata.constEnd(); ++it )
159  {
160  types.insert( it.key(), it.value()->visibleName() );
161  }
162 
163  return types;
164 }
165 
167 #if 0
168 TestLayoutItem::TestLayoutItem( QgsLayout *layout )
169  : QgsLayoutItem( layout )
170 {
171  int h = static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
172  int s = ( qrand() % ( 200 - 100 + 1 ) ) + 100;
173  int v = ( qrand() % ( 130 - 255 + 1 ) ) + 130;
174  mColor = QColor::fromHsv( h, s, v );
175 
176  QgsStringMap properties;
177  properties.insert( QStringLiteral( "color" ), mColor.name() );
178  properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
179  properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
180  properties.insert( QStringLiteral( "color_border" ), QStringLiteral( "black" ) );
181  properties.insert( QStringLiteral( "width_border" ), QStringLiteral( "0.3" ) );
182  properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
183  mShapeStyleSymbol = QgsFillSymbol::createSimple( properties );
184 
185 }
186 
187 void TestLayoutItem::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
188 {
189  Q_UNUSED( itemStyle )
190 
191  QgsEffectStack stack;
192  stack.appendEffect( new QgsDrawSourceEffect() );
193  stack.appendEffect( new QgsInnerGlowEffect() );
194  stack.begin( context );
195 
196  QPainter *painter = context.painter();
197 
198  painter->save();
199  painter->setRenderHint( QPainter::Antialiasing, false );
200  painter->setPen( Qt::NoPen );
201  painter->setBrush( mColor );
202 
203  double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
204 
205  QPolygonF shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
206  QList<QPolygonF> rings; //empty list
207 
208  mShapeStyleSymbol->startRender( context );
209  mShapeStyleSymbol->renderPolygon( shapePolygon, &rings, nullptr, context );
210  mShapeStyleSymbol->stopRender( context );
211 
212 // painter->drawRect( r );
213  painter->restore();
214  stack.end( context );
215 }
216 #endif
217 
QgsLayoutItemRegistry::LayoutPolygon
@ LayoutPolygon
Polygon shape item.
Definition: qgslayoutitemregistry.h:352
qgslayoutitemgroup.h
QgsLayoutMultiFrameAbstractMetadata::visibleName
QString visibleName() const
Returns a translated, user visible name for the layout multiframe class.
Definition: qgslayoutitemregistry.h:217
qgslayoutitempolygon.h
QgsLayoutItemShape
Layout item for basic filled shapes (e.g. rectangles, ellipses).
Definition: qgslayoutitemshape.h:31
qgslayoutitemattributetable.h
QgsLayoutItemRegistry::resolvePaths
void resolvePaths(int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) const
Resolve paths in properties of a particular symbol layer.
Definition: qgslayoutitemregistry.cpp:139
QgsLayoutMultiFrameMetadata
Convenience metadata class that uses static functions to create layout multiframes and their configur...
Definition: qgslayoutitemregistry.h:273
QgsPaintEffect::begin
virtual void begin(QgsRenderContext &context)
Begins intercepting paint operations to a render context.
Definition: qgspainteffect.cpp:90
QgsLayoutItemPolyline::create
static QgsLayoutItemPolyline * create(QgsLayout *layout)
Returns a new polyline item for the specified layout.
Definition: qgslayoutitempolyline.cpp:48
QgsDrawSourceEffect
A paint effect which draws the source picture with minor or no alterations.
Definition: qgspainteffect.h:327
QgsLayoutItemRegistry::createItem
QgsLayoutItem * createItem(int type, QgsLayout *layout) const
Creates a new instance of a layout item given the item type, and target layout.
Definition: qgslayoutitemregistry.cpp:123
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
qgslayoutitemlabel.h
QgsLayoutMultiFrame
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Definition: qgslayoutmultiframe.h:48
QgsLayoutItemAbstractMetadata::type
int type() const
Returns the unique item type code for the layout item class.
Definition: qgslayoutitemregistry.h:66
QgsLayoutItemRegistry::QgsLayoutItemRegistry
QgsLayoutItemRegistry(QObject *parent=nullptr)
Creates a new empty item registry.
Definition: qgslayoutitemregistry.cpp:41
QgsLayoutItemMetadata
Convenience metadata class that uses static functions to create layout items and their configuration ...
Definition: qgslayoutitemregistry.h:135
QgsLayoutItemRegistry::typeAdded
void typeAdded(int type, const QString &name)
Emitted whenever a new item type is added to the registry, with the specified type and visible name.
QgsLayoutItemRegistry::itemMetadata
QgsLayoutItemAbstractMetadata * itemMetadata(int type) const
Returns the metadata for the specified item type.
Definition: qgslayoutitemregistry.cpp:93
QgsLayoutItemRegistry::LayoutGroup
@ LayoutGroup
Grouped item.
Definition: qgslayoutitemregistry.h:339
qgslayoutframe.h
QgsLayoutItemAbstractMetadata
Stores metadata about one layout item class.
Definition: qgslayoutitemregistry.h:45
QgsLayoutItemGroup::create
static QgsLayoutItemGroup * create(QgsLayout *layout)
Returns a new group item for the specified layout.
Definition: qgslayoutitemgroup.cpp:64
QgsLayoutMultiFrameAbstractMetadata
Stores metadata about one layout multiframe class.
Definition: qgslayoutitemregistry.h:189
QgsLayoutItemLegend::create
static QgsLayoutItemLegend * create(QgsLayout *layout)
Returns a new legend item for the specified layout.
Definition: qgslayoutitemlegend.cpp:74
QgsLayoutItemMarker::create
static QgsLayoutItemMarker * create(QgsLayout *layout)
Returns a new marker item for the specified layout.
Definition: qgslayoutitemmarker.cpp:51
QgsLayoutItemRegistry::~QgsLayoutItemRegistry
~QgsLayoutItemRegistry() override
Definition: qgslayoutitemregistry.cpp:46
QgsLayoutItemTextTable::create
static QgsLayoutItemTextTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemTextTable for the specified parent layout.
Definition: qgslayoutitemtexttable.cpp:39
QgsLayoutItemRegistry::addLayoutMultiFrameType
bool addLayoutMultiFrameType(QgsLayoutMultiFrameAbstractMetadata *metadata)
Registers a new layout multiframe type.
Definition: qgslayoutitemregistry.cpp:113
QgsLayoutItemRegistry::addLayoutItemType
bool addLayoutItemType(QgsLayoutItemAbstractMetadata *metadata)
Registers a new layout item type.
Definition: qgslayoutitemregistry.cpp:103
QgsLayoutItemRegistry::LayoutPolyline
@ LayoutPolyline
Polyline shape item.
Definition: qgslayoutitemregistry.h:353
qgsgloweffect.h
QgsLayoutItemPicture::create
static QgsLayoutItemPicture * create(QgsLayout *layout)
Returns a new picture item for the specified layout.
Definition: qgslayoutitempicture.cpp:83
QgsLayoutFrame::create
static QgsLayoutFrame * create(QgsLayout *layout)
Creates a new QgsLayoutFrame belonging to the specified layout.
Definition: qgslayoutframe.cpp:44
QgsLayoutItemMap::create
static QgsLayoutItemMap * create(QgsLayout *layout)
Returns a new map item for the specified layout.
Definition: qgslayoutitemmap.cpp:156
QgsPaintEffect::end
virtual void end(QgsRenderContext &context)
Ends interception of paint operations to a render context, and draws the result to the render context...
Definition: qgspainteffect.cpp:105
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsLayoutItemRegistry::populate
bool populate()
Populates the registry with standard item types.
Definition: qgslayoutitemregistry.cpp:52
qgseffectstack.h
qgslayoutitemmarker.h
QgsFillSymbol::createSimple
static QgsFillSymbol * createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
Definition: qgsfillsymbol.cpp:20
QgsLayoutItemRegistry::LayoutScaleBar
@ LayoutScaleBar
Scale bar item.
Definition: qgslayoutitemregistry.h:354
QgsInnerGlowEffect
A paint effect which draws a glow within a picture.
Definition: qgsgloweffect.h:332
QgsLayoutItemShape::Rectangle
@ Rectangle
Rectangle shape.
Definition: qgslayoutitemshape.h:41
QgsLayoutItemPage::create
static QgsLayoutItemPage * create(QgsLayout *layout)
Returns a new page item for the specified layout.
Definition: qgslayoutitempage.cpp:57
qgsvectorlayer.h
QgsRenderContext::convertToPainterUnits
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
Definition: qgsrendercontext.cpp:367
qgslayoutitemmanualtable.h
qgslayoutitemlegend.h
QgsLayoutItemAttributeTable::create
static QgsLayoutItemAttributeTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemAttributeTable for the specified parent layout.
Definition: qgslayoutitemattributetable.cpp:66
qgslayoutitempage.h
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:2781
QgsLayoutItemRegistry::LayoutLabel
@ LayoutLabel
Label item.
Definition: qgslayoutitemregistry.h:349
QgsLayoutItemRegistry::multiFrameMetadata
QgsLayoutMultiFrameAbstractMetadata * multiFrameMetadata(int type) const
Returns the metadata for the specified multiframe type.
Definition: qgslayoutitemregistry.cpp:98
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutItemLabel::create
static QgsLayoutItemLabel * create(QgsLayout *layout)
Returns a new label item for the specified layout.
Definition: qgslayoutitemlabel.cpp:102
QgsLayoutItemRegistry::LayoutPicture
@ LayoutPicture
Picture item.
Definition: qgslayoutitemregistry.h:348
QgsEffectStack::appendEffect
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
Definition: qgseffectstack.cpp:215
QgsLayoutItemRegistry::LayoutMarker
@ LayoutMarker
Marker item.
Definition: qgslayoutitemregistry.h:369
QgsLayoutItemShape::setShapeType
void setShapeType(QgsLayoutItemShape::Shape type)
Sets the type of shape (e.g.
Definition: qgslayoutitemshape.cpp:105
QgsLayoutItemRegistry::LayoutMap
@ LayoutMap
Map item.
Definition: qgslayoutitemregistry.h:347
qgslayoutitempicture.h
qgslayoutitempolyline.h
QgsLayoutItemRegistry::LayoutTextTable
@ LayoutTextTable
Preset text table.
Definition: qgslayoutitemregistry.h:364
qgslayoutitemshape.h
QgsLayoutItemRegistry::itemTypes
QMap< int, QString > itemTypes() const
Returns a map of available item types to translated name.
Definition: qgslayoutitemregistry.cpp:151
QgsLayoutItemPolygon::create
static QgsLayoutItemPolygon * create(QgsLayout *layout)
Returns a new polygon item for the specified layout.
Definition: qgslayoutitempolygon.cpp:45
QgsLayoutItemRegistry::LayoutHtml
@ LayoutHtml
Html multiframe item.
Definition: qgslayoutitemregistry.h:362
qgslayoutitemtexttable.h
QgsLayoutItemRegistry::createMultiFrame
QgsLayoutMultiFrame * createMultiFrame(int type, QgsLayout *layout) const
Creates a new instance of a layout multiframe given the multiframe type, and target layout.
Definition: qgslayoutitemregistry.cpp:131
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:112
QgsLayoutItemRegistry::LayoutFrame
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
Definition: qgslayoutitemregistry.h:355
QgsLayoutItemRegistry::LayoutShape
@ LayoutShape
Shape item.
Definition: qgslayoutitemregistry.h:351
QgsLayoutItemHtml::create
static QgsLayoutItemHtml * create(QgsLayout *layout)
Returns a new QgsLayoutItemHtml for the specified parent layout.
Definition: qgslayoutitemhtml.cpp:95
QgsLayoutItemAbstractMetadata::visibleName
QString visibleName() const
Returns a translated, user visible name for the layout item class.
Definition: qgslayoutitemregistry.h:72
QgsLayoutItemRegistry::LayoutItem
@ LayoutItem
Base class for items.
Definition: qgslayoutitemregistry.h:338
QgsLayoutItemManualTable::create
static QgsLayoutItemManualTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemManualTable for the specified parent layout.
Definition: qgslayoutitemmanualtable.cpp:51
qgslayoutitemregistry.h
QgsLayoutItemRegistry::LayoutAttributeTable
@ LayoutAttributeTable
Attribute table.
Definition: qgslayoutitemregistry.h:363
QgsLayoutItemRegistry::LayoutManualTable
@ LayoutManualTable
Manual (fixed) table.
Definition: qgslayoutitemregistry.h:368
QgsLayoutItemRegistry::LayoutLegend
@ LayoutLegend
Legend item.
Definition: qgslayoutitemregistry.h:350
qgslayoutitemscalebar.h
qgssymbol.h
QgsPathResolver
Resolves relative paths into absolute paths and vice versa. Used for writing.
Definition: qgspathresolver.h:31
QgsLayoutItemRegistry::LayoutPage
@ LayoutPage
Page items.
Definition: qgslayoutitemregistry.h:346
qgslayoutitemhtml.h
QgsLayoutItemRegistry::multiFrameTypeAdded
void multiFrameTypeAdded(int type, const QString &name)
Emitted whenever a new multiframe type is added to the registry, with the specified type and visible ...
qgslayoutitemmap.h
QgsLayoutItemScaleBar::create
static QgsLayoutItemScaleBar * create(QgsLayout *layout)
Returns a new scale bar item for the specified layout.
Definition: qgslayoutitemscalebar.cpp:66
QgsEffectStack
A paint effect which consists of a stack of other chained paint effects.
Definition: qgseffectstack.h:44
QgsLayoutMultiFrameAbstractMetadata::type
int type() const
Returns the unique item type code for the layout multiframe class.
Definition: qgslayoutitemregistry.h:207