QGIS API Documentation  3.2.0-Bonn (bc43194)
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"
30 #include "qgslayoutitemtexttable.h"
31 #include "qgslayoutframe.h"
32 #include "qgsgloweffect.h"
33 #include "qgseffectstack.h"
34 #include <QPainter>
35 
37  : QObject( parent )
38 {
39 }
40 
42 {
43  qDeleteAll( mMetadata );
44  qDeleteAll( mMultiFrameMetadata );
45 }
46 
48 {
49  if ( !mMetadata.isEmpty() )
50  return false;
51 
52 #if 0
53  // add temporary item to register
54  auto createTemporaryItem = []( QgsLayout * layout )->QgsLayoutItem *
55  {
56  return new TestLayoutItem( layout );
57  };
58 
59  addLayoutItemType( new QgsLayoutItemMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, QStringLiteral( "temp type" ), createTemporaryItem ) );
60 #endif
61 
70  addLayoutItemType( new QgsLayoutItemMetadata( LayoutShape, QObject::tr( "Shape" ), []( QgsLayout * layout )
71  {
72  QgsLayoutItemShape *shape = new QgsLayoutItemShape( layout );
74  return shape;
75  } ) );
78 
82 
83  return true;
84 }
85 
87 {
88  return mMetadata.value( type );
89 }
90 
92 {
93  return mMultiFrameMetadata.value( type );
94 }
95 
97 {
98  if ( !metadata || mMetadata.contains( metadata->type() ) )
99  return false;
100 
101  mMetadata[metadata->type()] = metadata;
102  emit typeAdded( metadata->type(), metadata->visibleName() );
103  return true;
104 }
105 
107 {
108  if ( !metadata || mMultiFrameMetadata.contains( metadata->type() ) )
109  return false;
110 
111  mMultiFrameMetadata[metadata->type()] = metadata;
112  emit multiFrameTypeAdded( metadata->type(), metadata->visibleName() );
113  return true;
114 }
115 
117 {
118  if ( !mMetadata.contains( type ) )
119  return nullptr;
120 
121  return mMetadata[type]->createItem( layout );
122 }
123 
125 {
126  if ( !mMultiFrameMetadata.contains( type ) )
127  return nullptr;
128 
129  return mMultiFrameMetadata[type]->createMultiFrame( layout );
130 }
131 
132 void QgsLayoutItemRegistry::resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const
133 {
134  if ( mMetadata.contains( type ) )
135  {
136  mMetadata[type]->resolvePaths( properties, pathResolver, saving );
137  }
138  else if ( mMultiFrameMetadata.contains( type ) )
139  {
140  mMultiFrameMetadata[type]->resolvePaths( properties, pathResolver, saving );
141  }
142 }
143 
144 QMap<int, QString> QgsLayoutItemRegistry::itemTypes() const
145 {
146  QMap<int, QString> types;
147  for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
148  {
149  types.insert( it.key(), it.value()->visibleName() );
150  }
151  for ( auto it = mMultiFrameMetadata.constBegin(); it != mMultiFrameMetadata.constEnd(); ++it )
152  {
153  types.insert( it.key(), it.value()->visibleName() );
154  }
155 
156  return types;
157 }
158 
160 #if 0
161 TestLayoutItem::TestLayoutItem( QgsLayout *layout )
162  : QgsLayoutItem( layout )
163 {
164  int h = static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
165  int s = ( qrand() % ( 200 - 100 + 1 ) ) + 100;
166  int v = ( qrand() % ( 130 - 255 + 1 ) ) + 130;
167  mColor = QColor::fromHsv( h, s, v );
168 
169  QgsStringMap properties;
170  properties.insert( QStringLiteral( "color" ), mColor.name() );
171  properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
172  properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
173  properties.insert( QStringLiteral( "color_border" ), QStringLiteral( "black" ) );
174  properties.insert( QStringLiteral( "width_border" ), QStringLiteral( "0.3" ) );
175  properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
176  mShapeStyleSymbol = QgsFillSymbol::createSimple( properties );
177 
178 }
179 
180 void TestLayoutItem::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle )
181 {
182  Q_UNUSED( itemStyle );
183 
184  QgsEffectStack stack;
185  stack.appendEffect( new QgsDrawSourceEffect() );
186  stack.appendEffect( new QgsInnerGlowEffect() );
187  stack.begin( context );
188 
189  QPainter *painter = context.painter();
190 
191  painter->save();
192  painter->setRenderHint( QPainter::Antialiasing, false );
193  painter->setPen( Qt::NoPen );
194  painter->setBrush( mColor );
195 
196  double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
197 
198  QPolygonF shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
199  QList<QPolygonF> rings; //empty list
200 
201  mShapeStyleSymbol->startRender( context );
202  mShapeStyleSymbol->renderPolygon( shapePolygon, &rings, nullptr, context );
203  mShapeStyleSymbol->stopRender( context );
204 
205 // painter->drawRect( r );
206  painter->restore();
207  stack.end( context );
208 }
209 #endif
210 
void setShapeType(QgsLayoutItemShape::Shape type)
Sets the type of shape (e.g.
void resolvePaths(int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving) const
Resolve paths in properties of a particular symbol layer.
static QgsLayoutItemHtml * create(QgsLayout *layout)
Returns a new QgsLayoutItemHtml for the specified parent layout.
QgsLayoutItem * createItem(int type, QgsLayout *layout) const
Creates a new instance of a layout item given the item type, and target layout.
Base class for graphical items within a QgsLayout.
static QgsLayoutItemTextTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemTextTable for the specified parent layout.
int type() const
Returns the unique item type code for the layout item class.
static QgsLayoutItemGroup * create(QgsLayout *layout)
Returns a new group item for the specified layout.
bool addLayoutMultiFrameType(QgsLayoutMultiFrameAbstractMetadata *metadata)
Registers a new layout multiframe type.
static QgsLayoutItemPolygon * create(QgsLayout *layout)
Returns a new polygon item for the specified layout.
Convenience metadata class that uses static functions to create layout items and their configuration ...
static QgsFillSymbol * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
Definition: qgssymbol.cpp:1109
static QgsLayoutItemMap * create(QgsLayout *layout)
Returns a new map item for the specified layout.
QgsLayoutItemAbstractMetadata * itemMetadata(int type) const
Returns the metadata for the specified item type.
static QgsLayoutItemLabel * create(QgsLayout *layout)
Returns a new label item for the specified layout.
void multiFrameTypeAdded(int type, const QString &name)
Emitted whenever a new multiframe type is added to the registry, with the specified type and visible ...
static QgsLayoutItemPicture * create(QgsLayout *layout)
Returns a new picture item for the specified layout.
Stores metadata about one layout item class.
bool populate()
Populates the registry with standard item types.
QString visibleName() const
Returns a translated, user visible name for the layout multiframe class.
Stores metadata about one layout multiframe class.
Convenience metadata class that uses static functions to create layout multiframes and their configur...
QgsLayoutItemRegistry(QObject *parent=nullptr)
Creates a new empty item registry.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:501
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
QgsLayoutMultiFrameAbstractMetadata * multiFrameMetadata(int type) const
Returns the metadata for the specified multiframe type.
Layout item for basic filled shapes (e.g.
A paint effect which consists of a stack of other chained paint effects.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
Frame item, part of a QgsLayoutMultiFrame object.
static QgsLayoutItemAttributeTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemAttributeTable for the specified parent layout.
static QgsLayoutItemPolyline * create(QgsLayout *layout)
Returns a new polyline item for the specified layout.
static QgsLayoutItemLegend * create(QgsLayout *layout)
Returns a new legend item for the specified layout.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsLayoutMultiFrame * createMultiFrame(int type, QgsLayout *layout) const
Creates a new instance of a layout multiframe given the multiframe type, and target layout...
static QgsLayoutItemPage * create(QgsLayout *layout)
Returns a new page item for the specified layout.
bool addLayoutItemType(QgsLayoutItemAbstractMetadata *metadata)
Registers a new layout item type.
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 multiframe class.
QMap< int, QString > itemTypes() const
Returns a map of available item types to translated name.
A paint effect which draws a glow within a picture.
A paint effect which draws the source picture with minor or no alterations.
Resolves relative paths into absolute paths and vice versa.
virtual void end(QgsRenderContext &context)
Ends interception of paint operations to a render context, and draws the result to the render context...
virtual void begin(QgsRenderContext &context)
Begins intercepting paint operations to a render context.
static QgsLayoutFrame * create(QgsLayout *layout)
Creates a new QgsLayoutFrame belonging to the specified layout.
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...
static QgsLayoutItemScaleBar * create(QgsLayout *layout)
Returns a new scale bar item for the specified layout.