QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
18
19#include "qgslayoutframe.h"
21#include "qgslayoutitemchart.h"
23#include "qgslayoutitemgroup.h"
24#include "qgslayoutitemhtml.h"
25#include "qgslayoutitemlabel.h"
26#include "qgslayoutitemlegend.h"
28#include "qgslayoutitemmap.h"
29#include "qgslayoutitemmarker.h"
30#include "qgslayoutitempage.h"
35#include "qgslayoutitemshape.h"
37
38#include <QPainter>
39
40#include "moc_qgslayoutitemregistry.cpp"
41
43 : QObject( parent )
44{
45}
46
48{
49 qDeleteAll( mMetadata );
50 qDeleteAll( mMultiFrameMetadata );
51}
52
54{
55 if ( !mMetadata.isEmpty() )
56 return false;
57
58#if 0
59 // add temporary item to register
60 auto createTemporaryItem = []( QgsLayout * layout )->QgsLayoutItem *
61 {
62 return new TestLayoutItem( layout );
63 };
64
65 addLayoutItemType( new QgsLayoutItemMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, QStringLiteral( "temp type" ), createTemporaryItem ) );
66#endif
67
68 addLayoutItemType( new QgsLayoutItemMetadata( LayoutGroup, QObject::tr( "Group" ), QObject::tr( "Groups" ), QgsLayoutItemGroup::create ) );
69 addLayoutItemType( new QgsLayoutItemMetadata( LayoutFrame, QObject::tr( "Frame" ), QObject::tr( "Frames" ), QgsLayoutFrame::create ) );
70 addLayoutItemType( new QgsLayoutItemMetadata( LayoutPage, QObject::tr( "Page" ), QObject::tr( "Pages" ), QgsLayoutItemPage::create ) );
71 addLayoutItemType( new QgsLayoutItemMetadata( LayoutMap, QObject::tr( "Map" ), QObject::tr( "Maps" ), QgsLayoutItemMap::create ) );
72 addLayoutItemType( new QgsLayoutItemMetadata( LayoutPicture, QObject::tr( "Picture" ), QObject::tr( "Pictures" ), QgsLayoutItemPicture::create ) );
73 addLayoutItemType( new QgsLayoutItemMetadata( LayoutLabel, QObject::tr( "Label" ), QObject::tr( "Labels" ), QgsLayoutItemLabel::create ) );
74 addLayoutItemType( new QgsLayoutItemMetadata( LayoutLegend, QObject::tr( "Legend" ), QObject::tr( "Legends" ), QgsLayoutItemLegend::create ) );
75 addLayoutItemType( new QgsLayoutItemMetadata( LayoutScaleBar, QObject::tr( "Scalebar" ), QObject::tr( "Scalebars" ), QgsLayoutItemScaleBar::create ) );
76 addLayoutItemType( new QgsLayoutItemMetadata( LayoutShape, QObject::tr( "Shape" ), QObject::tr( "Shapes" ), []( QgsLayout * layout )
77 {
78 QgsLayoutItemShape *shape = new QgsLayoutItemShape( layout );
80 return shape;
81 } ) );
82 addLayoutItemType( new QgsLayoutItemMetadata( LayoutMarker, QObject::tr( "Marker" ), QObject::tr( "Markers" ), QgsLayoutItemMarker::create ) );
83 addLayoutItemType( new QgsLayoutItemMetadata( LayoutPolygon, QObject::tr( "Polygon" ), QObject::tr( "Polygons" ), QgsLayoutItemPolygon::create ) );
84 addLayoutItemType( new QgsLayoutItemMetadata( LayoutPolyline, QObject::tr( "Polyline" ), QObject::tr( "Polylines" ), QgsLayoutItemPolyline::create ) );
85
86 addLayoutItemType( new QgsLayoutItemMetadata( LayoutElevationProfile, QObject::tr( "Elevation Profile" ), QObject::tr( "Elevation Profiles" ), QgsLayoutItemElevationProfile::create ) );
87 addLayoutItemType( new QgsLayoutItemMetadata( LayoutChart, QObject::tr( "Chart" ), QObject::tr( "Charts" ), QgsLayoutItemChart::create ) );
88
93
94 return true;
95}
96
98{
99 return mMetadata.value( type );
100}
101
103{
104 return mMultiFrameMetadata.value( type );
105}
106
108{
109 if ( !metadata || mMetadata.contains( metadata->type() ) )
110 return false;
111
112 mMetadata[metadata->type()] = metadata;
113 emit typeAdded( metadata->type(), metadata->visibleName() );
114 return true;
115}
116
118{
119 if ( !mMetadata.contains( typeId ) )
120 return false;
121 mMetadata.remove( typeId );
122 emit typeRemoved( typeId );
123 return true;
124}
125
130
132{
133 if ( !metadata || mMultiFrameMetadata.contains( metadata->type() ) )
134 return false;
135
136 mMultiFrameMetadata[metadata->type()] = metadata;
137 emit multiFrameTypeAdded( metadata->type(), metadata->visibleName() );
138 return true;
139}
140
142{
143 if ( !mMultiFrameMetadata.contains( typeId ) )
144 return false;
145 mMultiFrameMetadata.remove( typeId );
146 emit multiFrameTypeRemoved( typeId );
147 return true;
148}
149
154
156{
157 if ( !mMetadata.contains( type ) )
158 return nullptr;
159
160 return mMetadata[type]->createItem( layout );
161}
162
164{
165 if ( !mMultiFrameMetadata.contains( type ) )
166 return nullptr;
167
168 return mMultiFrameMetadata[type]->createMultiFrame( layout );
169}
170
171void QgsLayoutItemRegistry::resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const
172{
173 if ( mMetadata.contains( type ) )
174 {
175 mMetadata[type]->resolvePaths( properties, pathResolver, saving );
176 }
177 else if ( mMultiFrameMetadata.contains( type ) )
178 {
179 mMultiFrameMetadata[type]->resolvePaths( properties, pathResolver, saving );
180 }
181}
182
183QMap<int, QString> QgsLayoutItemRegistry::itemTypes() const
184{
185 QMap<int, QString> types;
186 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
187 {
188 types.insert( it.key(), it.value()->visibleName() );
189 }
190 for ( auto it = mMultiFrameMetadata.constBegin(); it != mMultiFrameMetadata.constEnd(); ++it )
191 {
192 types.insert( it.key(), it.value()->visibleName() );
193 }
194
195 return types;
196}
197
199#if 0
200TestLayoutItem::TestLayoutItem( QgsLayout *layout )
201 : QgsLayoutItem( layout )
202{
203 int h = static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
204 int s = ( qrand() % ( 200 - 100 + 1 ) ) + 100;
205 int v = ( qrand() % ( 130 - 255 + 1 ) ) + 130;
206 mColor = QColor::fromHsv( h, s, v );
207
208 QgsStringMap properties;
209 properties.insert( QStringLiteral( "color" ), mColor.name() );
210 properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
211 properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
212 properties.insert( QStringLiteral( "color_border" ), QStringLiteral( "black" ) );
213 properties.insert( QStringLiteral( "width_border" ), QStringLiteral( "0.3" ) );
214 properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
215 mShapeStyleSymbol = QgsFillSymbol::createSimple( properties );
216
217}
218
219void TestLayoutItem::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
220{
221 Q_UNUSED( itemStyle )
222
223 QgsEffectStack stack;
224 stack.appendEffect( new QgsDrawSourceEffect() );
225 stack.appendEffect( new QgsInnerGlowEffect() );
226 stack.begin( context );
227
228 QPainter *painter = context.painter();
229
230 painter->save();
231 painter->setRenderHint( QPainter::Antialiasing, false );
232 painter->setPen( Qt::NoPen );
233 painter->setBrush( mColor );
234
235 double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
236
237 QPolygonF shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
238 QList<QPolygonF> rings; //empty list
239
240 mShapeStyleSymbol->startRender( context );
241 mShapeStyleSymbol->renderPolygon( shapePolygon, &rings, nullptr, context );
242 mShapeStyleSymbol->stopRender( context );
243
244// painter->drawRect( r );
245 painter->restore();
246 stack.end( context );
247}
248#endif
A paint effect which draws the source picture with minor or no alterations.
A paint effect which consists of a stack of other chained paint effects.
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
static std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
A paint effect which draws a glow within a picture.
static QgsLayoutFrame * create(QgsLayout *layout)
Creates a new QgsLayoutFrame belonging to the specified layout.
Stores metadata about one layout item class.
QString visibleName() const
Returns a translated, user visible name for the layout item class.
int type() const
Returns the unique item type code for the layout item class.
static QgsLayoutItemAttributeTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemAttributeTable for the specified parent layout.
static QgsLayoutItemChart * create(QgsLayout *layout)
Returns a new chart item for the specified layout.
static QgsLayoutItemElevationProfile * create(QgsLayout *layout)
Returns a new elevation profile item for the specified layout.
static QgsLayoutItemGroup * create(QgsLayout *layout)
Returns a new group item for the specified layout.
static QgsLayoutItemHtml * create(QgsLayout *layout)
Returns a new QgsLayoutItemHtml for the specified parent layout.
static QgsLayoutItemLabel * create(QgsLayout *layout)
Returns a new label item for the specified layout.
static QgsLayoutItemLegend * create(QgsLayout *layout)
Returns a new legend item for the specified layout.
static QgsLayoutItemManualTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemManualTable for the specified parent layout.
static QgsLayoutItemMap * create(QgsLayout *layout)
Returns a new map item for the specified layout.
static QgsLayoutItemMarker * create(QgsLayout *layout)
Returns a new marker item for the specified layout.
Convenience metadata class that uses static functions to create layout items and their configuration ...
static QgsLayoutItemPage * create(QgsLayout *layout)
Returns a new page item for the specified layout.
static QgsLayoutItemPicture * create(QgsLayout *layout)
Returns a new picture item for the specified layout.
static QgsLayoutItemPolygon * create(QgsLayout *layout)
Returns a new polygon item for the specified layout.
static QgsLayoutItemPolyline * create(QgsLayout *layout)
Returns a new polyline item for the specified layout.
QgsLayoutItemRegistry(QObject *parent=nullptr)
Creates a new empty item registry.
void resolvePaths(int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) const
Resolve paths in properties of a particular symbol layer.
bool addLayoutItemType(QgsLayoutItemAbstractMetadata *metadata)
Registers a new layout item type.
bool removeLayoutMultiFrameType(int typeId)
Unregisters a layout multiframe type.
bool removeLayoutItemType(int typeId)
Unregisters a layout item type.
void multiFrameTypeRemoved(int type)
Emitted whenever an multiframe type is removed from the registry with the specified type.
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.
QgsLayoutItemAbstractMetadata * itemMetadata(int type) const
Returns the metadata for the specified item type.
bool addLayoutMultiFrameType(QgsLayoutMultiFrameAbstractMetadata *metadata)
Registers a new layout multiframe type.
@ LayoutManualTable
Manual (fixed) table.
@ LayoutElevationProfile
Elevation profile item.
@ LayoutAttributeTable
Attribute table.
@ LayoutPolyline
Polyline shape item.
@ LayoutItem
Base class for items.
@ LayoutHtml
Html multiframe item.
@ LayoutTextTable
Preset text table.
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
@ LayoutPolygon
Polygon shape item.
QgsLayoutMultiFrameAbstractMetadata * multiFrameMetadata(int type) const
Returns the metadata for the specified multiframe type.
bool populate()
Populates the registry with standard item types.
void multiFrameTypeAdded(int type, const QString &name)
Emitted whenever a new multiframe type is added to the registry, with the specified type and visible ...
QgsLayoutItem * createItem(int type, QgsLayout *layout) const
Creates a new instance of a layout item given the item type, and target layout.
void typeRemoved(int type)
Emitted whenever an item type is removed from the registry with the specified type.
QMap< int, QString > itemTypes() const
Returns a map of available item types to translated name.
QgsLayoutMultiFrame * createMultiFrame(int type, QgsLayout *layout) const
Creates a new instance of a layout multiframe given the multiframe type, and target layout.
static QgsLayoutItemScaleBar * create(QgsLayout *layout)
Returns a new scale bar item for the specified layout.
Layout item for basic filled shapes (e.g.
void setShapeType(QgsLayoutItemShape::Shape type)
Sets the type of shape (e.g.
@ Rectangle
Rectangle shape.
static QgsLayoutItemTextTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemTextTable for the specified parent layout.
Base class for graphical items within a QgsLayout.
Stores metadata about one layout multiframe class.
int type() const
Returns the unique item type code for the layout multiframe class.
QString visibleName() const
Returns a translated, user visible name for the layout multiframe class.
Convenience metadata class that uses static functions to create layout multiframes and their configur...
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:50
virtual void begin(QgsRenderContext &context)
Begins intercepting paint operations to a render context.
virtual void end(QgsRenderContext &context)
Ends interception of paint operations to a render context, and draws the result to the render context...
Resolves relative paths into absolute paths and vice versa.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
QMap< QString, QString > QgsStringMap
Definition qgis.h:7132