QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgslayoutguiutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutapputils.cpp
3 ---------------------
4 Date : October 2017
5 Copyright : (C) 2017 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgslayoutguiutils.h"
17#include "qgsgui.h"
18#include "qgslayout.h"
22#include "qgslayoutitemshape.h"
23#include "qgslayoutmapwidget.h"
26#include "qgslayoutitemmap.h"
29#include "qgslayoutitemmarker.h"
34#include "qgslayoutitemlabel.h"
36#include "qgslayoutitemlegend.h"
39#include "qgslayoutframe.h"
40#include "qgslayoutitemhtml.h"
41#include "qgslayouthtmlwidget.h"
47#include "qgsmapcanvas.h"
48
58{
59 // start by trying to find a selected map
60 QList<QgsLayoutItemMap *> mapItems;
61 referenceItem->layout()->layoutItems( mapItems );
62
63 QgsLayoutItemMap *targetMap = nullptr;
64 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
65 {
66 if ( map->isSelected() )
67 {
68 return map;
69 }
70 }
71
72 // nope, no selection... hm, was the item drawn over a map? If so, use the topmost intersecting one
73 double largestZValue = std::numeric_limits< double >::lowest();
74 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
75 {
76 if ( map->collidesWithItem( referenceItem ) && map->zValue() > largestZValue )
77 {
78 targetMap = map;
79 largestZValue = map->zValue();
80 }
81 }
82 if ( targetMap )
83 return targetMap;
84
85 // ah frick it, just use the reference (or biggest!) map
86 return referenceItem->layout()->referenceMap();
87}
88
90{
92
93 registry->addItemGroup( QgsLayoutItemGuiGroup( QStringLiteral( "shapes" ), QObject::tr( "Shape" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicShape.svg" ) ) ) );
94 registry->addItemGroup( QgsLayoutItemGuiGroup( QStringLiteral( "nodes" ), QObject::tr( "Node Item" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddNodesItem.svg" ) ) ) );
95
96 auto createRubberBand = ( []( QgsLayoutView * view )->QgsLayoutViewRubberBand *
97 {
98 return new QgsLayoutViewRectangularRubberBand( view );
99 } );
100 auto createEllipseBand = ( []( QgsLayoutView * view )->QgsLayoutViewRubberBand *
101 {
102 return new QgsLayoutViewEllipticalRubberBand( view );
103 } );
104 auto createTriangleBand = ( []( QgsLayoutView * view )->QgsLayoutViewRubberBand *
105 {
106 return new QgsLayoutViewTriangleRubberBand( view );
107 } );
108
109#if 0
110 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, QStringLiteral( "test" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), nullptr, createRubberBand ) );
111#endif
112
113 // map item
114
115 auto mapItemMetadata = std::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutMap, QObject::tr( "Map" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMap.svg" ) ),
116 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
117 {
118 return new QgsLayoutMapWidget( qobject_cast< QgsLayoutItemMap * >( item ), mapCanvas );
119 }, createRubberBand );
120 mapItemMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item, const QVariantMap & )
121 {
122 QgsLayoutItemMap *map = qobject_cast< QgsLayoutItemMap * >( item );
123 Q_ASSERT( map );
124
125 //get the color for map canvas background and set map background color accordingly
126 map->setBackgroundColor( QgsProject::instance()->backgroundColor() );
127
128 if ( mapCanvas )
129 {
130 map->setMapRotation( mapCanvas->rotation() );
131 map->zoomToExtent( mapCanvas->mapSettings().visibleExtent() );
132 }
133
134 // auto assign a unique id to map items
135 QList<QgsLayoutItemMap *> mapsList;
136 if ( map->layout() )
137 map->layout()->layoutItems( mapsList );
138
139 int counter = mapsList.size() + 1;
140 bool existing = false;
141 while ( true )
142 {
143 existing = false;
144 for ( QgsLayoutItemMap *otherMap : std::as_const( mapsList ) )
145 {
146 if ( map == otherMap )
147 continue;
148
149 if ( otherMap->id() == QObject::tr( "Map %1" ).arg( counter ) )
150 {
151 existing = true;
152 break;
153 }
154 }
155 if ( existing )
156 counter++;
157 else
158 break;
159 }
160 map->setId( QObject::tr( "Map %1" ).arg( counter ) );
161 } );
162 registry->addLayoutItemGuiMetadata( mapItemMetadata.release() );
163
164 // picture item
165
166 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutPicture, QObject::tr( "Picture" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddImage.svg" ) ),
167 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
168 {
169 return new QgsLayoutPictureWidget( qobject_cast< QgsLayoutItemPicture * >( item ) );
170 }, createRubberBand ) );
171
172
173 // label item
174
175 auto labelItemMetadata = std::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutLabel, QObject::tr( "Label" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabel.svg" ) ),
176 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
177 {
178 return new QgsLayoutLabelWidget( qobject_cast< QgsLayoutItemLabel * >( item ) );
179 }, createRubberBand );
180 labelItemMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item, const QVariantMap & properties )
181 {
182 QgsLayoutItemLabel *label = qobject_cast< QgsLayoutItemLabel * >( item );
183 Q_ASSERT( label );
184
185 label->setText( properties.value( QStringLiteral( "expression" ) ).toString().isEmpty() ? QObject::tr( "Lorem ipsum" ) : QStringLiteral( "[% %1 %]" ).arg( properties.value( QStringLiteral( "expression" ) ).toString() ) );
186 if ( QApplication::isRightToLeft() )
187 {
188 label->setHAlign( Qt::AlignRight );
189 }
190 QSizeF minSize = label->sizeForText();
191 QSizeF currentSize = label->rect().size();
192
193 //make sure label size is sufficient to fit text
194 double labelWidth = std::max( minSize.width(), currentSize.width() );
195 double labelHeight = std::max( minSize.height(), currentSize.height() );
196 label->attemptSetSceneRect( QRectF( label->pos().x(), label->pos().y(), labelWidth, labelHeight ) );
197 } );
198
199 registry->addLayoutItemGuiMetadata( labelItemMetadata.release() );
200
201
202 // legend item
203
204 auto legendItemMetadata = std::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutLegend, QObject::tr( "Legend" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLegend.svg" ) ),
205 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
206 {
207 return new QgsLayoutLegendWidget( qobject_cast< QgsLayoutItemLegend * >( item ), mapCanvas );
208 }, createRubberBand );
209 legendItemMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item, const QVariantMap & )
210 {
211 QgsLayoutItemLegend *legend = qobject_cast< QgsLayoutItemLegend * >( item );
212 Q_ASSERT( legend );
213
214 // try to find a good map to link the legend with by default
216
217 if ( QApplication::isRightToLeft() )
218 {
219 // for right-to-left locales, use an appropriate default layout
220 legend->setSymbolAlignment( Qt::AlignRight );
221 legend->rstyle( QgsLegendStyle::Group ).setAlignment( Qt::AlignRight );
222 legend->rstyle( QgsLegendStyle::Subgroup ).setAlignment( Qt::AlignRight );
223 legend->rstyle( QgsLegendStyle::SymbolLabel ).setAlignment( Qt::AlignRight );
224 legend->setTitleAlignment( Qt::AlignRight );
225 }
226
227 //set default legend font from settings
228 QgsSettings settings;
229 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
230 if ( !defaultFontString.isEmpty() )
231 {
232 legend->rstyle( QgsLegendStyle::Title ).rfont().setFamily( defaultFontString );
233 legend->rstyle( QgsLegendStyle::Group ).rfont().setFamily( defaultFontString );
234 legend->rstyle( QgsLegendStyle::Subgroup ).rfont().setFamily( defaultFontString );
235 legend->rstyle( QgsLegendStyle::SymbolLabel ).rfont().setFamily( defaultFontString );
236 }
237
238 legend->updateLegend();
239 } );
240
241 registry->addLayoutItemGuiMetadata( legendItemMetadata.release() );
242
243 // scalebar item
244
245 auto scalebarItemMetadata = std::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutScaleBar, QObject::tr( "Scale Bar" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionScaleBar.svg" ) ),
246 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
247 {
248 return new QgsLayoutScaleBarWidget( qobject_cast< QgsLayoutItemScaleBar * >( item ) );
249 }, createRubberBand );
250 scalebarItemMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item, const QVariantMap & )
251 {
252 QgsLayoutItemScaleBar *scalebar = qobject_cast< QgsLayoutItemScaleBar * >( item );
253 Q_ASSERT( scalebar );
254
255 // try to find a good map to link the scalebar with by default
256 if ( QgsLayoutItemMap *targetMap = findSensibleDefaultLinkedMapItem( scalebar ) )
257 {
258 scalebar->setLinkedMap( targetMap );
259 scalebar->applyDefaultSize( scalebar->guessUnits() );
260 }
261 } );
262
263 registry->addLayoutItemGuiMetadata( scalebarItemMetadata.release() );
264
265
266 // north arrow
267 std::unique_ptr< QgsLayoutItemGuiMetadata > northArrowMetadata = std::make_unique< QgsLayoutItemGuiMetadata>(
268 QgsLayoutItemRegistry::LayoutPicture, QObject::tr( "North Arrow" ), QgsApplication::getThemeIcon( QStringLiteral( "/north_arrow.svg" ) ),
269 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
270 {
271 return new QgsLayoutPictureWidget( qobject_cast< QgsLayoutItemPicture * >( item ) );
272 }, createRubberBand );
273 northArrowMetadata->setItemCreationFunction( []( QgsLayout * layout )->QgsLayoutItem *
274 {
275
276 // count how many existing north arrows are already in layout
277 QList< QgsLayoutItemPicture * > pictureItems;
278 layout->layoutItems( pictureItems );
279 int northArrowCount = 0;
280
281 QgsSettings settings;
282 const QString defaultPath = settings.value( QStringLiteral( "LayoutDesigner/defaultNorthArrow" ), QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ), QgsSettings::Gui ).toString();
283
284 for ( QgsLayoutItemPicture *p : std::as_const( pictureItems ) )
285 {
286 // look for pictures which use the default north arrow svg
287 if ( p->picturePath() == defaultPath )
288 northArrowCount++;
289 }
290
291 std::unique_ptr< QgsLayoutItemPicture > picture = std::make_unique< QgsLayoutItemPicture >( layout );
292 picture->setNorthMode( QgsLayoutItemPicture::GridNorth );
293 picture->setPicturePath( defaultPath );
294 // set an id by default, so that north arrows are discernible in layout item lists
295 picture->setId( northArrowCount > 0 ? QObject::tr( "North Arrow %1" ).arg( northArrowCount + 1 ) : QObject::tr( "North Arrow" ) );
296 return picture.release();
297 } );
298 northArrowMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item, const QVariantMap & )
299 {
300 QgsLayoutItemPicture *picture = qobject_cast< QgsLayoutItemPicture * >( item );
301 Q_ASSERT( picture );
302
303 QList<QgsLayoutItemMap *> mapItems;
304 picture->layout()->layoutItems( mapItems );
305
306 // try to find a good map to link the north arrow with by default
307 picture->setLinkedMap( findSensibleDefaultLinkedMapItem( picture ) );
308 } );
309 registry->addLayoutItemGuiMetadata( northArrowMetadata.release() );
310
311 // shape items
312
313 auto createShapeWidget =
315 {
316 return new QgsLayoutShapeWidget( qobject_cast< QgsLayoutItemShape * >( item ) );
317 };
318
319 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), createShapeWidget, createRubberBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout * layout )->QgsLayoutItem*
320 {
321 std::unique_ptr< QgsLayoutItemShape > shape = std::make_unique< QgsLayoutItemShape >( layout );
322 shape->setShapeType( QgsLayoutItemShape::Rectangle );
323 return shape.release();
324 } ) );
325 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), createShapeWidget, createEllipseBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout * layout )->QgsLayoutItem*
326 {
327 std::unique_ptr< QgsLayoutItemShape > shape = std::make_unique< QgsLayoutItemShape >( layout );
328 shape->setShapeType( QgsLayoutItemShape::Ellipse );
329 return shape.release();
330 } ) );
331 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), createShapeWidget, createTriangleBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout * layout )->QgsLayoutItem*
332 {
333 std::unique_ptr< QgsLayoutItemShape > shape = std::make_unique< QgsLayoutItemShape >( layout );
334 shape->setShapeType( QgsLayoutItemShape::Triangle );
335 return shape.release();
336 } ) );
337
338 // marker
339 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutMarker, QObject::tr( "Marker" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMarker.svg" ) ),
340 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
341 {
342 return new QgsLayoutMarkerWidget( qobject_cast< QgsLayoutItemMarker * >( item ) );
343 }, nullptr ) );
344
345 // arrow
346 std::unique_ptr< QgsLayoutItemGuiMetadata > arrowMetadata = std::make_unique< QgsLayoutItemGuiMetadata>(
347 QgsLayoutItemRegistry::LayoutPolyline, QObject::tr( "Arrow" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddArrow.svg" ) ),
348 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
349 {
350 return new QgsLayoutPolylineWidget( qobject_cast< QgsLayoutItemPolyline * >( item ) );
351 }, createRubberBand, QString(), true );
352 arrowMetadata->setItemCreationFunction( []( QgsLayout * layout )->QgsLayoutItem *
353 {
354 std::unique_ptr< QgsLayoutItemPolyline > arrow = std::make_unique< QgsLayoutItemPolyline >( layout );
355 arrow->setEndMarker( QgsLayoutItemPolyline::ArrowHead );
356 return arrow.release();
357 } );
358 arrowMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * )->QGraphicsPathItem*
359 {
360 std::unique_ptr< QGraphicsPathItem > band = std::make_unique< QGraphicsPathItem >();
361 band->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
362 band->setZValue( QgsLayout::ZViewTool );
363 return band.release();
364 } );
365 registry->addLayoutItemGuiMetadata( arrowMetadata.release() );
366
367 // node items
368
369 std::unique_ptr< QgsLayoutItemGuiMetadata > polygonMetadata = std::make_unique< QgsLayoutItemGuiMetadata >(
370 QgsLayoutItemRegistry::LayoutPolygon, QObject::tr( "Polygon" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolygon.svg" ) ),
371 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
372 {
373 return new QgsLayoutPolygonWidget( qobject_cast< QgsLayoutItemPolygon * >( item ) );
374 }, createRubberBand, QStringLiteral( "nodes" ), true );
375 polygonMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * )->QGraphicsPolygonItem*
376 {
377 std::unique_ptr< QGraphicsPolygonItem > band = std::make_unique< QGraphicsPolygonItem >();
378 band->setBrush( Qt::NoBrush );
379 band->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
380 band->setZValue( QgsLayout::ZViewTool );
381 return band.release();
382 } );
383 registry->addLayoutItemGuiMetadata( polygonMetadata.release() );
384
385 std::unique_ptr< QgsLayoutItemGuiMetadata > polylineMetadata = std::make_unique< QgsLayoutItemGuiMetadata>(
386 QgsLayoutItemRegistry::LayoutPolyline, QObject::tr( "Polyline" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolyline.svg" ) ),
387 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
388 {
389 return new QgsLayoutPolylineWidget( qobject_cast< QgsLayoutItemPolyline * >( item ) );
390 }, createRubberBand, QStringLiteral( "nodes" ), true );
391 polylineMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * )->QGraphicsPathItem*
392 {
393 std::unique_ptr< QGraphicsPathItem > band = std::make_unique< QGraphicsPathItem >();
394 band->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
395 band->setZValue( QgsLayout::ZViewTool );
396 return band.release();
397 } );
398 registry->addLayoutItemGuiMetadata( polylineMetadata.release() );
399
400
401 // html item
402
403 auto htmlItemMetadata = std::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutHtml, QObject::tr( "HTML" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddHtml.svg" ) ),
404 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
405 {
406 return new QgsLayoutHtmlWidget( qobject_cast< QgsLayoutFrame * >( item ) );
407 }, createRubberBand );
408 htmlItemMetadata->setItemCreationFunction( [ = ]( QgsLayout * layout )->QgsLayoutItem *
409 {
410 std::unique_ptr< QgsLayoutItemHtml > htmlMultiFrame = std::make_unique< QgsLayoutItemHtml >( layout );
411 QgsLayoutItemHtml *html = htmlMultiFrame.get();
412 layout->addMultiFrame( htmlMultiFrame.release() );
413 std::unique_ptr< QgsLayoutFrame > frame = std::make_unique< QgsLayoutFrame >( layout, html );
414 QgsLayoutFrame *f = frame.get();
415 html->addFrame( frame.release() );
416 return f;
417 } );
418 registry->addLayoutItemGuiMetadata( htmlItemMetadata.release() );
419
420 // attribute table item
421
422 auto attributeTableItemMetadata = std::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutAttributeTable, QObject::tr( "Attribute Table" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddTable.svg" ) ),
423 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
424 {
425 return new QgsLayoutAttributeTableWidget( qobject_cast< QgsLayoutFrame * >( item ) );
426 }, createRubberBand );
427 attributeTableItemMetadata->setItemCreationFunction( [ = ]( QgsLayout * layout )->QgsLayoutItem *
428 {
429 std::unique_ptr< QgsLayoutItemAttributeTable > tableMultiFrame = std::make_unique< QgsLayoutItemAttributeTable >( layout );
430 QgsLayoutItemAttributeTable *table = tableMultiFrame.get();
431
432 //set first vector layer from layer registry as table source
433 QMap<QString, QgsMapLayer *> layerMap = layout->project()->mapLayers();
434 for ( auto it = layerMap.constBegin() ; it != layerMap.constEnd(); ++it )
435 {
436 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( it.value() ) )
437 {
438 table->setVectorLayer( vl );
439 break;
440 }
441 }
442
443 //set default table fonts from settings
444 QgsSettings settings;
445 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
446 if ( !defaultFontString.isEmpty() )
447 {
448 QgsTextFormat format;
449 QFont f = format.font();
450 f.setFamily( defaultFontString );
451 format.setFont( f );
452 tableMultiFrame->setContentTextFormat( format );
453 f.setBold( true );
454 format.setFont( f );
455 tableMultiFrame->setHeaderTextFormat( format );
456 }
457
458 layout->addMultiFrame( tableMultiFrame.release() );
459 std::unique_ptr< QgsLayoutFrame > frame = std::make_unique< QgsLayoutFrame >( layout, table );
460 QgsLayoutFrame *f = frame.get();
461 table->addFrame( frame.release() );
462 return f;
463 } );
464 registry->addLayoutItemGuiMetadata( attributeTableItemMetadata.release() );
465
466 // manual table item
467
468 auto manualTableItemMetadata = std::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutManualTable, QObject::tr( "Fixed Table" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddManualTable.svg" ) ),
469 [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
470 {
471 return new QgsLayoutManualTableWidget( qobject_cast< QgsLayoutFrame * >( item ) );
472 }, createRubberBand );
473 manualTableItemMetadata->setItemCreationFunction( [ = ]( QgsLayout * layout )->QgsLayoutItem *
474 {
475 std::unique_ptr< QgsLayoutItemManualTable > tableMultiFrame = std::make_unique< QgsLayoutItemManualTable >( layout );
476 QgsLayoutItemManualTable *table = tableMultiFrame.get();
477
478 // initially start with a 2x2 empty table
479 QgsTableContents contents;
480 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
481 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
482 table->setTableContents( contents );
483
484 //set default table fonts from settings
485 QgsSettings settings;
486 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
487 if ( !defaultFontString.isEmpty() )
488 {
489 QgsTextFormat format;
490 QFont f = format.font();
491 f.setFamily( defaultFontString );
492 format.setFont( f );
493 tableMultiFrame->setContentTextFormat( format );
494 f.setBold( true );
495 format.setFont( f );
496 tableMultiFrame->setHeaderTextFormat( format );
497 }
498
499 layout->addMultiFrame( tableMultiFrame.release() );
500
501 std::unique_ptr< QgsLayoutFrame > frame = std::make_unique< QgsLayoutFrame >( layout, table );
502 QgsLayoutFrame *f = frame.get();
503 table->addFrame( frame.release() );
504 return f;
505 } );
506 registry->addLayoutItemGuiMetadata( manualTableItemMetadata.release() );
507}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Definition: qgsgui.cpp:128
A widget for configuring layout attribute table items.
Base class for frame items, which form a layout multiframe item.
static void registerGuiForKnownItemTypes(QgsMapCanvas *mapCanvas)
Registers the GUI handlers for known layout item types.
A widget for configuring layout html items.
A layout table subclass that displays attributes from a vector layer.
void setVectorLayer(QgsVectorLayer *layer)
Sets the vector layer from which to display feature attributes.
A base class for property widgets for layout items.
Stores GUI metadata about a group of layout item classes.
Convenience metadata class that uses static functions to handle layout item GUI behavior.
Registry of available layout item GUI behavior.
bool addItemGroup(const QgsLayoutItemGuiGroup &group)
Registers a new item group with the registry.
bool addLayoutItemGuiMetadata(QgsLayoutItemAbstractGuiMetadata *metadata)
Registers the gui metadata for a new layout item type.
A layout multiframe subclass for HTML content.
A layout item subclass for text labels.
void setHAlign(Qt::AlignmentFlag alignment)
Sets the horizontal alignment of the label.
QSizeF sizeForText() const
Returns the required item size (in layout units) for the label's text to fill the item.
void setText(const QString &text)
Sets the label's preset text.
A layout item subclass for map legends.
void updateLegend()
Updates the model and all legend entries.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map to associate with the legend.
void setSymbolAlignment(Qt::AlignmentFlag alignment)
Sets the alignment for placement of legend symbols.
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns reference to modifiable legend style.
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
A layout table subclass that displays manually entered (and formatted) content.
void setTableContents(const QgsTableContents &contents)
Sets the contents of the table.
Layout graphical items for displaying a map.
void zoomToExtent(const QgsRectangle &extent)
Zooms the map so that the specified extent is fully visible within the map item.
void setMapRotation(double rotation)
Sets the rotation for the map - this does not affect the layout item shape, only the way the map is d...
A layout item subclass that displays SVG files or raster format images (jpg, png, ....
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map object for rotation.
@ GridNorth
Align to grid north.
@ ArrowHead
Show arrow marker.
@ LayoutManualTable
Manual (fixed) table.
@ LayoutAttributeTable
Attribute table.
@ LayoutPolyline
Polyline shape item.
@ LayoutScaleBar
Scale bar item.
@ LayoutItem
Base class for items.
@ LayoutHtml
Html multiframe item.
@ LayoutPolygon
Polygon shape item.
A layout item subclass for scale bars.
QgsUnitTypes::DistanceUnit guessUnits() const
Attempts to guess the most reasonable unit choice for the scalebar, given the current linked map's sc...
void applyDefaultSize(QgsUnitTypes::DistanceUnit units=QgsUnitTypes::DistanceMeters)
Applies the default size to the scale bar (scale bar 1/5 of map item width)
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map item linked to the scalebar.
@ Ellipse
Ellipse shape.
@ Rectangle
Rectangle shape.
@ Triangle
Triangle shape.
Base class for graphical items within a QgsLayout.
void setBackgroundColor(const QColor &color)
Sets the background color for this item.
virtual void setId(const QString &id)
Set the item's id name.
void attemptSetSceneRect(const QRectF &rect, bool includesFrame=false)
Attempts to update the item's position and size to match the passed rect in layout coordinates.
A widget for layout item settings.
A widget for configuring layout manual table items.
Input widget for the configuration of QgsLayoutItemMap.
A widget for configuring layout shape items.
virtual void addFrame(QgsLayoutFrame *frame, bool recalcFrameSizes=true)
Adds a frame to the multiframe.
const QgsLayout * layout() const
Returns the layout the object is attached to.
A widget for configuring layout picture items.
Input widget for QgsLayoutItemPolygon.
Input widget for QgsLayoutItemPolyline.
A widget to define the properties of a QgsLayoutItemScaleBar.
A widget for configuring layout shape items.
QgsLayoutViewEllipseRubberBand is elliptical rubber band for use within QgsLayoutView widgets.
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets.
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes,...
QgsLayoutViewTriangleRubberBand is triangular rubber band for use within QgsLayoutView widgets.
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:51
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition: qgslayout.h:122
void addMultiFrame(QgsLayoutMultiFrame *multiFrame)
Adds a multiFrame to the layout.
Definition: qgslayout.cpp:572
QgsLayoutItemMap * referenceMap() const
Returns the map item which will be used to generate corresponding world files when the layout is expo...
Definition: qgslayout.cpp:430
@ ZViewTool
Z-value for temporary view tool items.
Definition: qgslayout.h:65
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
void setAlignment(Qt::Alignment alignment)
Sets the alignment for the legend component.
@ Group
Legend group title.
@ Subgroup
Legend subgroup title.
@ Title
Legend title.
@ SymbolLabel
Symbol label (excluding icon)
QFont & rfont()
Returns a modifiable reference to the component's font.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
double rotation() const
Gets the current map canvas rotation in clockwise degrees.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:477
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Encapsulates the contents and formatting of a single table cell.
Definition: qgstablecell.h:36
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
void setFont(const QFont &font)
Sets the font used for rendering text.
QFont font() const
Returns the font used for rendering text.
Represents a vector layer which manages a vector based data sets.
QgsLayoutItemMap * findSensibleDefaultLinkedMapItem(QgsLayoutItem *referenceItem)
Attempts to find the best guess at a map item to link referenceItem to, by:
QVector< QgsTableRow > QgsTableContents
A set of table rows.
Definition: qgstablecell.h:220
QVector< QgsTableCell > QgsTableRow
A row of table cells.
Definition: qgstablecell.h:211