QGIS API Documentation 3.99.0-Master (a26b91b364d)
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"
21#include "qgslayoutitemshape.h"
22#include "qgslayoutmapwidget.h"
25#include "qgslayoutitemmap.h"
28#include "qgslayoutitemmarker.h"
33#include "qgslayoutitemlabel.h"
36#include "qgslayoutitemlegend.h"
39#include "qgslayoutframe.h"
40#include "qgslayoutitemhtml.h"
41#include "qgslayouthtmlwidget.h"
48#include "qgsmapcanvas.h"
49#include "qgsplot.h"
50#include "qgsfontutils.h"
51
61{
62 // start by trying to find a selected map
63 QList<QgsLayoutItemMap *> mapItems;
64 referenceItem->layout()->layoutItems( mapItems );
65
66 QgsLayoutItemMap *targetMap = nullptr;
67 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
68 {
69 if ( map->isSelected() )
70 {
71 return map;
72 }
73 }
74
75 // nope, no selection... hm, was the item drawn over a map? If so, use the topmost intersecting one
76 double largestZValue = std::numeric_limits<double>::lowest();
77 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
78 {
79 if ( map->collidesWithItem( referenceItem ) && map->zValue() > largestZValue )
80 {
81 targetMap = map;
82 largestZValue = map->zValue();
83 }
84 }
85 if ( targetMap )
86 return targetMap;
87
88 // ah frick it, just use the reference (or biggest!) map
89 return referenceItem->layout()->referenceMap();
90}
91
93{
95
96 registry->addItemGroup( QgsLayoutItemGuiGroup( QStringLiteral( "shapes" ), QObject::tr( "Shape" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicShape.svg" ) ) ) );
97 registry->addItemGroup( QgsLayoutItemGuiGroup( QStringLiteral( "nodes" ), QObject::tr( "Node Item" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddNodesItem.svg" ) ) ) );
98
99 auto createRubberBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * {
100 return new QgsLayoutViewRectangularRubberBand( view );
101 } );
102 auto createEllipseBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * {
103 return new QgsLayoutViewEllipticalRubberBand( view );
104 } );
105 auto createTriangleBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * {
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" ) ), [mapCanvas]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutMapWidget( qobject_cast<QgsLayoutItemMap *>( item ), mapCanvas ); }, createRubberBand );
116 mapItemMetadata->setItemAddedToLayoutFunction( [mapCanvas]( QgsLayoutItem *item, const QVariantMap & ) {
117 QgsLayoutItemMap *map = qobject_cast<QgsLayoutItemMap *>( item );
118 Q_ASSERT( map );
119
120 //get the color for map canvas background and set map background color accordingly
121 map->setBackgroundColor( QgsProject::instance()->backgroundColor() );
122
123 if ( mapCanvas )
124 {
125 map->setMapRotation( mapCanvas->rotation() );
126 map->zoomToExtent( mapCanvas->mapSettings().visibleExtent() );
127 }
128
129 // auto assign a unique id to map items
130 QList<QgsLayoutItemMap *> mapsList;
131 if ( map->layout() )
132 map->layout()->layoutItems( mapsList );
133
134 int counter = mapsList.size() + 1;
135 bool existing = false;
136 while ( true )
137 {
138 existing = false;
139 for ( QgsLayoutItemMap *otherMap : std::as_const( mapsList ) )
140 {
141 if ( map == otherMap )
142 continue;
143
144 if ( otherMap->id() == QObject::tr( "Map %1" ).arg( counter ) )
145 {
146 existing = true;
147 break;
148 }
149 }
150 if ( existing )
151 counter++;
152 else
153 break;
154 }
155 map->setId( QObject::tr( "Map %1" ).arg( counter ) );
156 } );
157 registry->addLayoutItemGuiMetadata( mapItemMetadata.release() );
158
159 // picture item
160
161 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutPicture, QObject::tr( "Picture" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddImage.svg" ) ), []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPictureWidget( qobject_cast<QgsLayoutItemPicture *>( item ) ); }, createRubberBand ) );
162
163
164 // label item
165
166 auto labelItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>( QgsLayoutItemRegistry::LayoutLabel, QObject::tr( "Label" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabel.svg" ) ), []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutLabelWidget( qobject_cast<QgsLayoutItemLabel *>( item ) ); }, createRubberBand );
167 labelItemMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap &properties ) {
168 QgsLayoutItemLabel *label = qobject_cast<QgsLayoutItemLabel *>( item );
169 Q_ASSERT( label );
170
171 label->setText( properties.value( QStringLiteral( "expression" ) ).toString().isEmpty() ? QObject::tr( "Lorem ipsum" ) : QStringLiteral( "[% %1 %]" ).arg( properties.value( QStringLiteral( "expression" ) ).toString() ) );
172 if ( QApplication::isRightToLeft() )
173 {
174 label->setHAlign( Qt::AlignRight );
175 }
176 QSizeF minSize = label->sizeForText();
177 QSizeF currentSize = label->rect().size();
178
179 //make sure label size is sufficient to fit text
180 double labelWidth = std::max( minSize.width(), currentSize.width() );
181 double labelHeight = std::max( minSize.height(), currentSize.height() );
182 label->attemptSetSceneRect( QRectF( label->pos().x(), label->pos().y(), labelWidth, labelHeight ) );
183 } );
184
185 labelItemMetadata->setItemDoubleClickedFunction( []( QgsLayoutItem *item, Qgis::MouseHandlesAction action ) {
186 QgsLayoutItemLabel *label = qobject_cast<QgsLayoutItemLabel *>( item );
187
188 // size to text doesn't have any real meaning for HTML content, skip it
189 if ( label->mode() == QgsLayoutItemLabel::ModeHtml )
190 return;
191
192 Q_ASSERT( label );
194 switch ( action )
195 {
203 return;
204
207 break;
208
211 break;
212
215 break;
216
219 break;
220
223 break;
224
227 break;
228
231 break;
232
235 break;
236 }
237
238 label->beginCommand( QObject::tr( "Resize to Text" ) );
239 label->adjustSizeToText( reference );
240 label->endCommand();
241 } );
242
243 registry->addLayoutItemGuiMetadata( labelItemMetadata.release() );
244
245
246 // legend item
247
248 auto legendItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>( QgsLayoutItemRegistry::LayoutLegend, QObject::tr( "Legend" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLegend.svg" ) ), [mapCanvas]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutLegendWidget( qobject_cast<QgsLayoutItemLegend *>( item ), mapCanvas ); }, createRubberBand );
249 legendItemMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap & ) {
250 QgsLayoutItemLegend *legend = qobject_cast<QgsLayoutItemLegend *>( item );
251 Q_ASSERT( legend );
252
253 // try to find a good map to link the legend with by default
255
256 if ( QApplication::isRightToLeft() )
257 {
258 // for right-to-left locales, use an appropriate default layout
259 legend->setSymbolAlignment( Qt::AlignRight );
260 legend->rstyle( Qgis::LegendComponent::Group ).setAlignment( Qt::AlignRight );
261 legend->rstyle( Qgis::LegendComponent::Subgroup ).setAlignment( Qt::AlignRight );
262 legend->rstyle( Qgis::LegendComponent::SymbolLabel ).setAlignment( Qt::AlignRight );
263 legend->setTitleAlignment( Qt::AlignRight );
264 }
265
266 //set default legend font from settings
267 QgsSettings settings;
268 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
269 if ( !defaultFontString.isEmpty() )
270 {
271 QFont font;
272 QgsFontUtils::setFontFamily( font, defaultFontString );
273
275 f.setFont( font );
277
279 f.setFont( font );
281
283 f.setFont( font );
285
287 f.setFont( font );
289 }
290
291 legend->updateLegend();
292 } );
293
294 registry->addLayoutItemGuiMetadata( legendItemMetadata.release() );
295
296 // scalebar item
297
298 auto scalebarItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>( QgsLayoutItemRegistry::LayoutScaleBar, QObject::tr( "Scale Bar" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionScaleBar.svg" ) ), []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutScaleBarWidget( qobject_cast<QgsLayoutItemScaleBar *>( item ) ); }, createRubberBand );
299 scalebarItemMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap & ) {
300 QgsLayoutItemScaleBar *scalebar = qobject_cast<QgsLayoutItemScaleBar *>( item );
301 Q_ASSERT( scalebar );
302
303 // default to project's scale calculation method
304 scalebar->setMethod( scalebar->layout()->project()->scaleMethod() );
305
306 // try to find a good map to link the scalebar with by default
307 if ( QgsLayoutItemMap *targetMap = findSensibleDefaultLinkedMapItem( scalebar ) )
308 {
309 scalebar->setLinkedMap( targetMap );
310 scalebar->applyDefaultSize( scalebar->guessUnits() );
311 }
312 } );
313
314 registry->addLayoutItemGuiMetadata( scalebarItemMetadata.release() );
315
316
317 // north arrow
318 auto northArrowMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
319 QgsLayoutItemRegistry::LayoutPicture, QObject::tr( "North Arrow" ), QgsApplication::getThemeIcon( QStringLiteral( "/north_arrow.svg" ) ),
320 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
321 return new QgsLayoutPictureWidget( qobject_cast<QgsLayoutItemPicture *>( item ) );
322 },
323 createRubberBand
324 );
325 northArrowMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
326 // count how many existing north arrows are already in layout
327 QList<QgsLayoutItemPicture *> pictureItems;
328 layout->layoutItems( pictureItems );
329 int northArrowCount = 0;
330
331 QgsSettings settings;
332 const QString defaultPath = settings.value( QStringLiteral( "LayoutDesigner/defaultNorthArrow" ), QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ), QgsSettings::Gui ).toString();
333
334 for ( QgsLayoutItemPicture *p : std::as_const( pictureItems ) )
335 {
336 // look for pictures which use the default north arrow svg
337 if ( p->picturePath() == defaultPath )
338 northArrowCount++;
339 }
340
341 auto picture = std::make_unique<QgsLayoutItemPicture>( layout );
342 picture->setNorthMode( QgsLayoutItemPicture::GridNorth );
343 picture->setPicturePath( defaultPath );
344 // set an id by default, so that north arrows are discernible in layout item lists
345 picture->setId( northArrowCount > 0 ? QObject::tr( "North Arrow %1" ).arg( northArrowCount + 1 ) : QObject::tr( "North Arrow" ) );
346 return picture.release();
347 } );
348 northArrowMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap & ) {
349 QgsLayoutItemPicture *picture = qobject_cast<QgsLayoutItemPicture *>( item );
350 Q_ASSERT( picture );
351
352 QList<QgsLayoutItemMap *> mapItems;
353 picture->layout()->layoutItems( mapItems );
354
355 // try to find a good map to link the north arrow with by default
356 picture->setLinkedMap( findSensibleDefaultLinkedMapItem( picture ) );
357 } );
358 registry->addLayoutItemGuiMetadata( northArrowMetadata.release() );
359
360 // shape items
361
362 auto createShapeWidget =
363 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
364 return new QgsLayoutShapeWidget( qobject_cast<QgsLayoutItemShape *>( item ) );
365 };
366
367 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), createShapeWidget, createRubberBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout *layout ) -> QgsLayoutItem * {
368 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
369 shape->setShapeType( QgsLayoutItemShape::Rectangle );
370 return shape.release();
371 } ) );
372 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), createShapeWidget, createEllipseBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout *layout ) -> QgsLayoutItem * {
373 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
374 shape->setShapeType( QgsLayoutItemShape::Ellipse );
375 return shape.release();
376 } ) );
377 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), createShapeWidget, createTriangleBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout *layout ) -> QgsLayoutItem * {
378 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
379 shape->setShapeType( QgsLayoutItemShape::Triangle );
380 return shape.release();
381 } ) );
382
383 // marker
384 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutMarker, QObject::tr( "Marker" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMarker.svg" ) ), []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutMarkerWidget( qobject_cast<QgsLayoutItemMarker *>( item ) ); }, nullptr ) );
385
386 // arrow
387 auto arrowMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
388 QgsLayoutItemRegistry::LayoutPolyline, QObject::tr( "Arrow" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddArrow.svg" ) ),
389 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
390 return new QgsLayoutPolylineWidget( qobject_cast<QgsLayoutItemPolyline *>( item ) );
391 },
392 createRubberBand, QString(), true
393 );
394 arrowMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
395 auto arrow = std::make_unique<QgsLayoutItemPolyline>( layout );
396 arrow->setEndMarker( QgsLayoutItemPolyline::ArrowHead );
397 return arrow.release();
398 } );
399 arrowMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
400 auto band = std::make_unique<QGraphicsItemGroup>();
401 QGraphicsPathItem *poly = new QGraphicsPathItem( band.get() );
402 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
403
404 QGraphicsPathItem *tempPoly = new QGraphicsPathItem( band.get() );
405 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
406
407 band->setZValue( QgsLayout::ZViewTool );
408 return band.release();
409 } );
410 registry->addLayoutItemGuiMetadata( arrowMetadata.release() );
411
412 // node items
413
414 auto polygonMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
415 QgsLayoutItemRegistry::LayoutPolygon, QObject::tr( "Polygon" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolygon.svg" ) ),
416 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
417 return new QgsLayoutPolygonWidget( qobject_cast<QgsLayoutItemPolygon *>( item ) );
418 },
419 createRubberBand, QStringLiteral( "nodes" ), true
420 );
421 polygonMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
422 auto band = std::make_unique<QGraphicsItemGroup>();
423 QGraphicsPolygonItem *poly = new QGraphicsPolygonItem( band.get() );
424 poly->setBrush( QBrush( QColor( 227, 22, 22, 20 ) ) );
425 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
426
427 QGraphicsPolygonItem *tempPoly = new QGraphicsPolygonItem( band.get() );
428 tempPoly->setBrush( Qt::NoBrush );
429 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
430
431 band->setZValue( QgsLayout::ZViewTool );
432 return band.release();
433 } );
434 registry->addLayoutItemGuiMetadata( polygonMetadata.release() );
435
436 auto polylineMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
437 QgsLayoutItemRegistry::LayoutPolyline, QObject::tr( "Polyline" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolyline.svg" ) ),
438 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
439 return new QgsLayoutPolylineWidget( qobject_cast<QgsLayoutItemPolyline *>( item ) );
440 },
441 createRubberBand, QStringLiteral( "nodes" ), true
442 );
443 polylineMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
444 auto band = std::make_unique<QGraphicsItemGroup>();
445 QGraphicsPathItem *poly = new QGraphicsPathItem( band.get() );
446 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
447
448 QGraphicsPathItem *tempPoly = new QGraphicsPathItem( band.get() );
449 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
450
451 band->setZValue( QgsLayout::ZViewTool );
452 return band.release();
453 } );
454 registry->addLayoutItemGuiMetadata( polylineMetadata.release() );
455
456
457 // html item
458
459 auto htmlItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>( QgsLayoutItemRegistry::LayoutHtml, QObject::tr( "HTML" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddHtml.svg" ) ), []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutHtmlWidget( qobject_cast<QgsLayoutFrame *>( item ) ); }, createRubberBand );
460 htmlItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
461 auto htmlMultiFrame = std::make_unique<QgsLayoutItemHtml>( layout );
462 QgsLayoutItemHtml *html = htmlMultiFrame.get();
463 layout->addMultiFrame( htmlMultiFrame.release() );
464 auto frame = std::make_unique<QgsLayoutFrame>( layout, html );
465 QgsLayoutFrame *f = frame.get();
466 html->addFrame( frame.release() );
467 // cppcheck-suppress returnDanglingLifetime
468 return f;
469 } );
470 registry->addLayoutItemGuiMetadata( htmlItemMetadata.release() );
471
472 // attribute table item
473
474 auto attributeTableItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>( QgsLayoutItemRegistry::LayoutAttributeTable, QObject::tr( "Attribute Table" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddTable.svg" ) ), []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutAttributeTableWidget( qobject_cast<QgsLayoutFrame *>( item ) ); }, createRubberBand );
475 attributeTableItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
476 auto tableMultiFrame = std::make_unique<QgsLayoutItemAttributeTable>( layout );
477 QgsLayoutItemAttributeTable *table = tableMultiFrame.get();
478
479 //set first vector layer from layer registry as table source
480 QMap<QString, QgsMapLayer *> layerMap = layout->project()->mapLayers();
481 for ( auto it = layerMap.constBegin(); it != layerMap.constEnd(); ++it )
482 {
483 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( it.value() ) )
484 {
485 table->setVectorLayer( vl );
486 break;
487 }
488 }
489
490 //set default table fonts from settings
491 QgsSettings settings;
492 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
493 if ( !defaultFontString.isEmpty() )
494 {
495 QgsTextFormat format;
496 QFont f = format.font();
497 QgsFontUtils::setFontFamily( f, defaultFontString );
498 format.setFont( f );
499 tableMultiFrame->setContentTextFormat( format );
500 f.setBold( true );
501 format.setFont( f );
502 tableMultiFrame->setHeaderTextFormat( format );
503 }
504
505 layout->addMultiFrame( tableMultiFrame.release() );
506 auto frame = std::make_unique<QgsLayoutFrame>( layout, table );
507 QgsLayoutFrame *f = frame.get();
508 table->addFrame( frame.release() );
509 // cppcheck-suppress returnDanglingLifetime
510 return f;
511 } );
512 registry->addLayoutItemGuiMetadata( attributeTableItemMetadata.release() );
513
514 // manual table item
515
516 auto manualTableItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>( QgsLayoutItemRegistry::LayoutManualTable, QObject::tr( "Fixed Table" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddManualTable.svg" ) ), []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutManualTableWidget( qobject_cast<QgsLayoutFrame *>( item ) ); }, createRubberBand );
517 manualTableItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
518 auto tableMultiFrame = std::make_unique<QgsLayoutItemManualTable>( layout );
519 QgsLayoutItemManualTable *table = tableMultiFrame.get();
520
521 // initially start with a 2x2 empty table
522 QgsTableContents contents;
523 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
524 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
525 table->setTableContents( contents );
526
527 //set default table fonts from settings
528 QgsSettings settings;
529 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
530 if ( !defaultFontString.isEmpty() )
531 {
532 QgsTextFormat format;
533 QFont f = format.font();
534 QgsFontUtils::setFontFamily( f, defaultFontString );
535 format.setFont( f );
536 tableMultiFrame->setContentTextFormat( format );
537 f.setBold( true );
538 format.setFont( f );
539 tableMultiFrame->setHeaderTextFormat( format );
540 }
541
542 layout->addMultiFrame( tableMultiFrame.release() );
543
544 auto frame = std::make_unique<QgsLayoutFrame>( layout, table );
545 QgsLayoutFrame *f = frame.get();
546 table->addFrame( frame.release() );
547 // cppcheck-suppress returnDanglingLifetime
548 return f;
549 } );
550 manualTableItemMetadata->setItemDoubleClickedFunction( []( QgsLayoutItem *item, Qgis::MouseHandlesAction ) {
551 QgsLayoutManualTableWidget::openTableDesigner( qobject_cast<QgsLayoutFrame *>( item ) );
552 } );
553 registry->addLayoutItemGuiMetadata( manualTableItemMetadata.release() );
554
555
556 // elevation profile item
557
558 auto elevationProfileItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>( QgsLayoutItemRegistry::LayoutElevationProfile, QObject::tr( "Elevation Profile" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionElevationProfile.svg" ) ), []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutElevationProfileWidget( qobject_cast<QgsLayoutItemElevationProfile *>( item ) ); }, createRubberBand );
559 elevationProfileItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
560 auto profileItem = std::make_unique<QgsLayoutItemElevationProfile>( layout );
561
562 //set default fonts from settings
563 QgsSettings settings;
564 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
565 if ( !defaultFontString.isEmpty() )
566 {
567 QgsTextFormat format = profileItem->plot()->xAxis().textFormat();
568 QFont f = format.font();
569 QgsFontUtils::setFontFamily( f, defaultFontString );
570 format.setFont( f );
571 profileItem->plot()->xAxis().setTextFormat( format );
572
573 format = profileItem->plot()->yAxis().textFormat();
574 f = format.font();
575 QgsFontUtils::setFontFamily( f, defaultFontString );
576 format.setFont( f );
577 profileItem->plot()->yAxis().setTextFormat( format );
578 }
579 return profileItem.release();
580 } );
581 registry->addLayoutItemGuiMetadata( elevationProfileItemMetadata.release() );
582}
@ Group
Legend group title.
@ Subgroup
Legend subgroup title.
@ Title
Legend title.
@ SymbolLabel
Symbol label (excluding icon)
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:5890
@ ResizeRightDown
Resize right down (Bottom right handle)
@ SelectItem
Select item.
@ ResizeLeftUp
Resize left up (Top left handle)
@ ResizeLeftDown
Resize left down (Bottom left handle)
@ ResizeRight
Resize right (Right handle)
@ ResizeDown
Resize down (Bottom handle)
@ RotateTopRight
Rotate from top right handle.
@ RotateTopLeft
Rotate from top left handle.
@ RotateBottomRight
Rotate right bottom right handle.
@ ResizeRightUp
Resize right up (Top right handle)
@ ResizeLeft
Resize left (Left handle)
@ RotateBottomLeft
Rotate from bottom left handle.
@ ResizeUp
Resize up (Top handle)
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static void setFontFamily(QFont &font, const QString &family)
Sets the family for a font object.
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Definition qgsgui.cpp:140
A widget for configuring layout attribute table items.
A widget for layout elevation profile item settings.
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.
Mode mode() const
Returns the label's current mode.
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.
void adjustSizeToText()
Resizes the item so that the label's text fits to the item.
@ ModeHtml
Label displays rendered HTML content.
A layout item subclass for map legends.
QgsLegendStyle & rstyle(Qgis::LegendComponent s)
Returns reference to modifiable legend style.
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.
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.
@ LayoutElevationProfile
Elevation profile item.
@ LayoutAttributeTable
Attribute table.
@ LayoutPolyline
Polyline shape item.
@ LayoutItem
Base class for items.
@ LayoutHtml
Html multiframe item.
@ LayoutPolygon
Polygon shape item.
A layout item subclass for scale bars.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map item linked to the scalebar.
void setMethod(Qgis::ScaleCalculationMethod method)
Sets the scale calculation method, which determines how the bar's scale will be calculated.
Qgis::DistanceUnit guessUnits() const
Attempts to guess the most reasonable unit choice for the scalebar, given the current linked map's sc...
void applyDefaultSize(Qgis::DistanceUnit units=Qgis::DistanceUnit::Meters)
Applies the default size to the scale bar (scale bar 1/5 of map item width)
@ 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.
void beginCommand(const QString &commandText, UndoCommand command=UndoNone)
Starts new undo command for this item.
ReferencePoint
Fixed position reference point.
@ LowerMiddle
Lower center of item.
@ MiddleLeft
Middle left of item.
@ UpperRight
Upper right corner of item.
@ LowerLeft
Lower left corner of item.
@ UpperLeft
Upper left corner of item.
@ UpperMiddle
Upper center of item.
@ MiddleRight
Middle right of item.
@ LowerRight
Lower right corner of item.
void endCommand()
Completes the current item command and push it onto the layout's undo stack.
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.
static void openTableDesigner(QgsLayoutFrame *frame, QWidget *parent=nullptr)
Creates and open the table editor dialog.
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.
An elliptical rubber band for use within QgsLayoutView widgets.
A rectangular rubber band for use within QgsLayoutView widgets.
An abstract base class for temporary rubber band items in various shapes, for use within QgsLayoutVie...
A triangular rubber band for use within QgsLayoutView widgets.
A graphical widget to display and interact with QgsLayouts.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition qgslayout.h:120
void addMultiFrame(QgsLayoutMultiFrame *multiFrame)
Adds a multiFrame to the layout.
QgsLayoutItemMap * referenceMap() const
Returns the map item which will be used to generate corresponding world files when the layout is expo...
@ ZViewTool
Z-value for temporary view tool items.
Definition qgslayout.h:63
QgsProject * project() const
The project associated with the layout.
void setAlignment(Qt::Alignment alignment)
Sets the alignment for the legend component.
QgsTextFormat & textFormat()
Returns the text format used for rendering this legend component.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for rendering this legend component.
Map canvas is a class for displaying all GIS data types on a canvas.
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.
Qgis::ScaleCalculationMethod scaleMethod
Definition qgsproject.h:128
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Stores settings for use within QGIS.
Definition qgssettings.h:65
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.
Container for all settings relating to text rendering.
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 dataset.
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.
QVector< QgsTableCell > QgsTableRow
A row of table cells.