QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
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
18#include "qgsfontutils.h"
19#include "qgsgui.h"
23#include "qgslayoutframe.h"
24#include "qgslayouthtmlwidget.h"
26#include "qgslayoutitemchart.h"
29#include "qgslayoutitemhtml.h"
30#include "qgslayoutitemlabel.h"
31#include "qgslayoutitemlegend.h"
33#include "qgslayoutitemmap.h"
34#include "qgslayoutitemmarker.h"
40#include "qgslayoutitemshape.h"
44#include "qgslayoutmapwidget.h"
52#include "qgsmapcanvas.h"
53#include "qgsplot.h"
54
55#include <QString>
56
57using namespace Qt::StringLiterals;
58
68{
69 // start by trying to find a selected map
70 QList<QgsLayoutItemMap *> mapItems;
71 referenceItem->layout()->layoutItems( mapItems );
72
73 QgsLayoutItemMap *targetMap = nullptr;
74 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
75 {
76 if ( map->isSelected() )
77 {
78 return map;
79 }
80 }
81
82 // nope, no selection... hm, was the item drawn over a map? If so, use the topmost intersecting one
83 double largestZValue = std::numeric_limits<double>::lowest();
84 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
85 {
86 if ( map->collidesWithItem( referenceItem ) && map->zValue() > largestZValue )
87 {
88 targetMap = map;
89 largestZValue = map->zValue();
90 }
91 }
92 if ( targetMap )
93 return targetMap;
94
95 // ah frick it, just use the reference (or biggest!) map
96 return referenceItem->layout()->referenceMap();
97}
98
100{
102
103 registry->addItemGroup( QgsLayoutItemGuiGroup( u"shapes"_s, QObject::tr( "Shape" ), QgsApplication::getThemeIcon( u"/mActionAddBasicShape.svg"_s ) ) );
104 registry->addItemGroup( QgsLayoutItemGuiGroup( u"nodes"_s, QObject::tr( "Node Item" ), QgsApplication::getThemeIcon( u"/mActionAddNodesItem.svg"_s ) ) );
105
106 auto createRubberBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * { return new QgsLayoutViewRectangularRubberBand( view ); } );
107 auto createEllipseBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * { return new QgsLayoutViewEllipticalRubberBand( view ); } );
108 auto createTriangleBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * { return new QgsLayoutViewTriangleRubberBand( view ); } );
109
110#if 0
111 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, u"test"_s, QgsApplication::getThemeIcon( u"/mActionAddLabel.svg"_s ), nullptr, createRubberBand ) );
112#endif
113
114 // map item
115
116 auto mapItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
118 QObject::tr( "Map" ),
119 QgsApplication::getThemeIcon( u"/mActionAddMap.svg"_s ),
120 [mapCanvas]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutMapWidget( qobject_cast<QgsLayoutItemMap *>( item ), mapCanvas ); },
121 createRubberBand
122 );
123 mapItemMetadata->setItemAddedToLayoutFunction( [mapCanvas]( QgsLayoutItem *item, const QVariantMap & ) {
124 QgsLayoutItemMap *map = qobject_cast<QgsLayoutItemMap *>( item );
125 Q_ASSERT( map );
126
127 //get the color for map canvas background and set map background color accordingly
128 map->setBackgroundColor( QgsProject::instance()->backgroundColor() );
129
130 if ( mapCanvas )
131 {
132 map->setMapRotation( mapCanvas->rotation() );
133 map->zoomToExtent( mapCanvas->mapSettings().visibleExtent() );
134 }
135
136 // auto assign a unique id to map items
137 QList<QgsLayoutItemMap *> mapsList;
138 if ( map->layout() )
139 map->layout()->layoutItems( mapsList );
140
141 int counter = mapsList.size() + 1;
142 bool existing = false;
143 while ( true )
144 {
145 existing = false;
146 for ( QgsLayoutItemMap *otherMap : std::as_const( mapsList ) )
147 {
148 if ( map == otherMap )
149 continue;
150
151 if ( otherMap->id() == QObject::tr( "Map %1" ).arg( counter ) )
152 {
153 existing = true;
154 break;
155 }
156 }
157 if ( existing )
158 counter++;
159 else
160 break;
161 }
162 map->setId( QObject::tr( "Map %1" ).arg( counter ) );
163 } );
164 registry->addLayoutItemGuiMetadata( mapItemMetadata.release() );
165
166 // picture item
167
170 QObject::tr( "Picture" ),
171 QgsApplication::getThemeIcon( u"/mActionAddImage.svg"_s ),
172 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPictureWidget( qobject_cast<QgsLayoutItemPicture *>( item ) ); },
173 createRubberBand
174 ) );
175
176 // label item
177
178 auto labelItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
180 QObject::tr( "Label" ),
181 QgsApplication::getThemeIcon( u"/mActionLabel.svg"_s ),
182 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutLabelWidget( qobject_cast<QgsLayoutItemLabel *>( item ) ); },
183 createRubberBand
184 );
185 labelItemMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap &properties ) {
186 QgsLayoutItemLabel *label = qobject_cast<QgsLayoutItemLabel *>( item );
187 Q_ASSERT( label );
188
189 label->setText( properties.value( u"expression"_s ).toString().isEmpty() ? QObject::tr( "Lorem ipsum" ) : u"[% %1 %]"_s.arg( properties.value( u"expression"_s ).toString() ) );
190 if ( QApplication::isRightToLeft() )
191 {
192 label->setHAlign( Qt::AlignRight );
193 }
194 QSizeF minSize = label->sizeForText();
195 QSizeF currentSize = label->rect().size();
196
197 //make sure label size is sufficient to fit text
198 double labelWidth = std::max( minSize.width(), currentSize.width() );
199 double labelHeight = std::max( minSize.height(), currentSize.height() );
200 label->attemptSetSceneRect( QRectF( label->pos().x(), label->pos().y(), labelWidth, labelHeight ) );
201 } );
202
203 labelItemMetadata->setItemDoubleClickedFunction( []( QgsLayoutItem *item, Qgis::MouseHandlesAction action ) {
204 QgsLayoutItemLabel *label = qobject_cast<QgsLayoutItemLabel *>( item );
205
206 // size to text doesn't have any real meaning for HTML content, skip it
207 if ( label->mode() == QgsLayoutItemLabel::ModeHtml )
208 return;
209
210 Q_ASSERT( label );
212 switch ( action )
213 {
221 return;
222
225 break;
226
229 break;
230
233 break;
234
237 break;
238
241 break;
242
245 break;
246
249 break;
250
253 break;
254 }
255
256 label->beginCommand( QObject::tr( "Resize to Text" ) );
257 label->adjustSizeToText( reference );
258 label->endCommand();
259 } );
260
261 registry->addLayoutItemGuiMetadata( labelItemMetadata.release() );
262
263
264 // legend item
265
266 auto legendItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
268 QObject::tr( "Legend" ),
269 QgsApplication::getThemeIcon( u"/mActionAddLegend.svg"_s ),
270 [mapCanvas]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutLegendWidget( qobject_cast<QgsLayoutItemLegend *>( item ), mapCanvas ); },
271 createRubberBand
272 );
273 legendItemMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap & ) {
274 QgsLayoutItemLegend *legend = qobject_cast<QgsLayoutItemLegend *>( item );
275 Q_ASSERT( legend );
276
277 // set to default sync mode
279
280 // try to find a good map to link the legend with by default
282
283 if ( QApplication::isRightToLeft() )
284 {
285 // for right-to-left locales, use an appropriate default layout
286 legend->setSymbolAlignment( Qt::AlignRight );
287 legend->rstyle( Qgis::LegendComponent::Group ).setAlignment( Qt::AlignRight );
288 legend->rstyle( Qgis::LegendComponent::Subgroup ).setAlignment( Qt::AlignRight );
289 legend->rstyle( Qgis::LegendComponent::SymbolLabel ).setAlignment( Qt::AlignRight );
290 legend->setTitleAlignment( Qt::AlignRight );
291 }
292
293 //set default legend font from settings
294 QgsSettings settings;
295 const QString defaultFontString = settings.value( u"LayoutDesigner/defaultFont"_s, QVariant(), QgsSettings::Gui ).toString();
296 if ( !defaultFontString.isEmpty() )
297 {
298 QFont font;
299 QgsFontUtils::setFontFamily( font, defaultFontString );
300
302 f.setFont( font );
304
306 f.setFont( font );
308
310 f.setFont( font );
312
314 f.setFont( font );
316 }
317
318 legend->updateLegend();
319 } );
320
321 registry->addLayoutItemGuiMetadata( legendItemMetadata.release() );
322
323 // scalebar item
324
325 auto scalebarItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
327 QObject::tr( "Scale Bar" ),
328 QgsApplication::getThemeIcon( u"/mActionScaleBar.svg"_s ),
329 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutScaleBarWidget( qobject_cast<QgsLayoutItemScaleBar *>( item ) ); },
330 createRubberBand
331 );
332 scalebarItemMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap & ) {
333 QgsLayoutItemScaleBar *scalebar = qobject_cast<QgsLayoutItemScaleBar *>( item );
334 Q_ASSERT( scalebar );
335
336 // default to project's scale calculation method
337 scalebar->setMethod( scalebar->layout()->project()->scaleMethod() );
338
339 // try to find a good map to link the scalebar with by default
340 if ( QgsLayoutItemMap *targetMap = findSensibleDefaultLinkedMapItem( scalebar ) )
341 {
342 scalebar->setLinkedMap( targetMap );
343 scalebar->applyDefaultSize( scalebar->guessUnits() );
344 }
345 } );
346
347 registry->addLayoutItemGuiMetadata( scalebarItemMetadata.release() );
348
349
350 // north arrow
351 auto northArrowMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
353 QObject::tr( "North Arrow" ),
354 QgsApplication::getThemeIcon( u"/north_arrow.svg"_s ),
355 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPictureWidget( qobject_cast<QgsLayoutItemPicture *>( item ) ); },
356 createRubberBand
357 );
358 northArrowMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
359 // count how many existing north arrows are already in layout
360 QList<QgsLayoutItemPicture *> pictureItems;
361 layout->layoutItems( pictureItems );
362 int northArrowCount = 0;
363
364 QgsSettings settings;
365 const QString defaultPath = settings.value( u"LayoutDesigner/defaultNorthArrow"_s, u":/images/north_arrows/layout_default_north_arrow.svg"_s, QgsSettings::Gui ).toString();
366
367 for ( QgsLayoutItemPicture *p : std::as_const( pictureItems ) )
368 {
369 // look for pictures which use the default north arrow svg
370 if ( p->picturePath() == defaultPath )
371 northArrowCount++;
372 }
373
374 auto picture = std::make_unique<QgsLayoutItemPicture>( layout );
375 picture->setNorthMode( QgsLayoutItemPicture::GridNorth );
376 picture->setPicturePath( defaultPath );
377 // set an id by default, so that north arrows are discernible in layout item lists
378 picture->setId( northArrowCount > 0 ? QObject::tr( "North Arrow %1" ).arg( northArrowCount + 1 ) : QObject::tr( "North Arrow" ) );
379 return picture.release();
380 } );
381 northArrowMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap & ) {
382 QgsLayoutItemPicture *picture = qobject_cast<QgsLayoutItemPicture *>( item );
383 Q_ASSERT( picture );
384
385 QList<QgsLayoutItemMap *> mapItems;
386 picture->layout()->layoutItems( mapItems );
387
388 // try to find a good map to link the north arrow with by default
389 picture->setLinkedMap( findSensibleDefaultLinkedMapItem( picture ) );
390 } );
391 registry->addLayoutItemGuiMetadata( northArrowMetadata.release() );
392
393 // shape items
394
395 auto createShapeWidget = []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutShapeWidget( qobject_cast<QgsLayoutItemShape *>( item ) ); };
396
399 QObject::tr( "Rectangle" ),
400 QgsApplication::getThemeIcon( u"/mActionAddBasicRectangle.svg"_s ),
401 createShapeWidget,
402 createRubberBand,
403 u"shapes"_s,
404 false,
406 []( QgsLayout *layout ) -> QgsLayoutItem * {
407 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
408 shape->setShapeType( QgsLayoutItemShape::Rectangle );
409 return shape.release();
410 }
411 ) );
414 QObject::tr( "Ellipse" ),
415 QgsApplication::getThemeIcon( u"/mActionAddBasicCircle.svg"_s ),
416 createShapeWidget,
417 createEllipseBand,
418 u"shapes"_s,
419 false,
421 []( QgsLayout *layout ) -> QgsLayoutItem * {
422 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
423 shape->setShapeType( QgsLayoutItemShape::Ellipse );
424 return shape.release();
425 }
426 ) );
429 QObject::tr( "Triangle" ),
430 QgsApplication::getThemeIcon( u"/mActionAddBasicTriangle.svg"_s ),
431 createShapeWidget,
432 createTriangleBand,
433 u"shapes"_s,
434 false,
436 []( QgsLayout *layout ) -> QgsLayoutItem * {
437 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
438 shape->setShapeType( QgsLayoutItemShape::Triangle );
439 return shape.release();
440 }
441 ) );
442
443 // marker
446 QObject::tr( "Marker" ),
447 QgsApplication::getThemeIcon( u"/mActionAddMarker.svg"_s ),
448 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutMarkerWidget( qobject_cast<QgsLayoutItemMarker *>( item ) ); },
449 nullptr
450 ) );
451
452 // arrow
453 auto arrowMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
455 QObject::tr( "Arrow" ),
456 QgsApplication::getThemeIcon( u"/mActionAddArrow.svg"_s ),
457 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPolylineWidget( qobject_cast<QgsLayoutItemPolyline *>( item ) ); },
458 createRubberBand,
459 QString(),
460 true
461 );
462 arrowMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
463 auto arrow = std::make_unique<QgsLayoutItemPolyline>( layout );
464 arrow->setEndMarker( QgsLayoutItemPolyline::ArrowHead );
465 return arrow.release();
466 } );
467 arrowMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
468 auto band = std::make_unique<QGraphicsItemGroup>();
469 QGraphicsPathItem *poly = new QGraphicsPathItem( band.get() );
470 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
471
472 QGraphicsPathItem *tempPoly = new QGraphicsPathItem( band.get() );
473 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
474
475 band->setZValue( QgsLayout::ZViewTool );
476 return band.release();
477 } );
478 registry->addLayoutItemGuiMetadata( arrowMetadata.release() );
479
480 // node items
481
482 auto polygonMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
484 QObject::tr( "Polygon" ),
485 QgsApplication::getThemeIcon( u"/mActionAddPolygon.svg"_s ),
486 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPolygonWidget( qobject_cast<QgsLayoutItemPolygon *>( item ) ); },
487 createRubberBand,
488 u"nodes"_s,
489 true
490 );
491 polygonMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
492 auto band = std::make_unique<QGraphicsItemGroup>();
493 QGraphicsPolygonItem *poly = new QGraphicsPolygonItem( band.get() );
494 poly->setBrush( QBrush( QColor( 227, 22, 22, 20 ) ) );
495 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
496
497 QGraphicsPolygonItem *tempPoly = new QGraphicsPolygonItem( band.get() );
498 tempPoly->setBrush( Qt::NoBrush );
499 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
500
501 band->setZValue( QgsLayout::ZViewTool );
502 return band.release();
503 } );
504 registry->addLayoutItemGuiMetadata( polygonMetadata.release() );
505
506 auto polylineMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
508 QObject::tr( "Polyline" ),
509 QgsApplication::getThemeIcon( u"/mActionAddPolyline.svg"_s ),
510 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPolylineWidget( qobject_cast<QgsLayoutItemPolyline *>( item ) ); },
511 createRubberBand,
512 u"nodes"_s,
513 true
514 );
515 polylineMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
516 auto band = std::make_unique<QGraphicsItemGroup>();
517 QGraphicsPathItem *poly = new QGraphicsPathItem( band.get() );
518 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
519
520 QGraphicsPathItem *tempPoly = new QGraphicsPathItem( band.get() );
521 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
522
523 band->setZValue( QgsLayout::ZViewTool );
524 return band.release();
525 } );
526 registry->addLayoutItemGuiMetadata( polylineMetadata.release() );
527
528
529 // html item
530
531 auto htmlItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
533 QObject::tr( "HTML" ),
534 QgsApplication::getThemeIcon( u"/mActionAddHtml.svg"_s ),
535 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutHtmlWidget( qobject_cast<QgsLayoutFrame *>( item ) ); },
536 createRubberBand
537 );
538 htmlItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
539 auto htmlMultiFrame = std::make_unique<QgsLayoutItemHtml>( layout );
540 QgsLayoutItemHtml *html = htmlMultiFrame.get();
541 layout->addMultiFrame( htmlMultiFrame.release() );
542 auto frame = std::make_unique<QgsLayoutFrame>( layout, html );
543 QgsLayoutFrame *f = frame.get();
544 html->addFrame( frame.release() );
545 // cppcheck-suppress returnDanglingLifetime
546 return f;
547 } );
548 registry->addLayoutItemGuiMetadata( htmlItemMetadata.release() );
549
550 // attribute table item
551
552 auto attributeTableItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
554 QObject::tr( "Attribute Table" ),
555 QgsApplication::getThemeIcon( u"/mActionAddTable.svg"_s ),
556 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutAttributeTableWidget( qobject_cast<QgsLayoutFrame *>( item ) ); },
557 createRubberBand
558 );
559 attributeTableItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
560 auto tableMultiFrame = std::make_unique<QgsLayoutItemAttributeTable>( layout );
561 QgsLayoutItemAttributeTable *table = tableMultiFrame.get();
562
563 //set first vector layer from layer registry as table source
564 QMap<QString, QgsMapLayer *> layerMap = layout->project()->mapLayers();
565 for ( auto it = layerMap.constBegin(); it != layerMap.constEnd(); ++it )
566 {
567 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( it.value() ) )
568 {
569 table->setVectorLayer( vl );
570 break;
571 }
572 }
573
574 //set default table fonts from settings
575 QgsSettings settings;
576 const QString defaultFontString = settings.value( u"LayoutDesigner/defaultFont"_s, QVariant(), QgsSettings::Gui ).toString();
577 if ( !defaultFontString.isEmpty() )
578 {
579 QgsTextFormat format;
580 QFont f = format.font();
581 QgsFontUtils::setFontFamily( f, defaultFontString );
582 format.setFont( f );
583 tableMultiFrame->setContentTextFormat( format );
584 f.setBold( true );
585 format.setFont( f );
586 tableMultiFrame->setHeaderTextFormat( format );
587 }
588
589 layout->addMultiFrame( tableMultiFrame.release() );
590 auto frame = std::make_unique<QgsLayoutFrame>( layout, table );
591 QgsLayoutFrame *f = frame.get();
592 table->addFrame( frame.release() );
593 // cppcheck-suppress returnDanglingLifetime
594 return f;
595 } );
596 registry->addLayoutItemGuiMetadata( attributeTableItemMetadata.release() );
597
598 // manual table item
599
600 auto manualTableItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
602 QObject::tr( "Fixed Table" ),
603 QgsApplication::getThemeIcon( u"/mActionAddManualTable.svg"_s ),
604 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutManualTableWidget( qobject_cast<QgsLayoutFrame *>( item ) ); },
605 createRubberBand
606 );
607 manualTableItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
608 auto tableMultiFrame = std::make_unique<QgsLayoutItemManualTable>( layout );
609 QgsLayoutItemManualTable *table = tableMultiFrame.get();
610
611 // initially start with a 2x2 empty table
612 QgsTableContents contents;
613 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
614 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
615 table->setTableContents( contents );
616
617 //set default table fonts from settings
618 QgsSettings settings;
619 const QString defaultFontString = settings.value( u"LayoutDesigner/defaultFont"_s, QVariant(), QgsSettings::Gui ).toString();
620 if ( !defaultFontString.isEmpty() )
621 {
622 QgsTextFormat format;
623 QFont f = format.font();
624 QgsFontUtils::setFontFamily( f, defaultFontString );
625 format.setFont( f );
626 tableMultiFrame->setContentTextFormat( format );
627 f.setBold( true );
628 format.setFont( f );
629 tableMultiFrame->setHeaderTextFormat( format );
630 }
631
632 layout->addMultiFrame( tableMultiFrame.release() );
633
634 auto frame = std::make_unique<QgsLayoutFrame>( layout, table );
635 QgsLayoutFrame *f = frame.get();
636 table->addFrame( frame.release() );
637 // cppcheck-suppress returnDanglingLifetime
638 return f;
639 } );
640 manualTableItemMetadata->setItemDoubleClickedFunction( []( QgsLayoutItem *item, Qgis::MouseHandlesAction ) {
641 QgsLayoutManualTableWidget::openTableDesigner( qobject_cast<QgsLayoutFrame *>( item ) );
642 } );
643 registry->addLayoutItemGuiMetadata( manualTableItemMetadata.release() );
644
645
646 // elevation profile item
647
648 auto elevationProfileItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
650 QObject::tr( "Elevation Profile" ),
651 QgsApplication::getThemeIcon( u"/mActionElevationProfile.svg"_s ),
652 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutElevationProfileWidget( qobject_cast<QgsLayoutItemElevationProfile *>( item ) ); },
653 createRubberBand
654 );
655 elevationProfileItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
656 auto profileItem = std::make_unique<QgsLayoutItemElevationProfile>( layout );
657
658 //set default fonts from settings
659 QgsSettings settings;
660 const QString defaultFontString = settings.value( u"LayoutDesigner/defaultFont"_s, QVariant(), QgsSettings::Gui ).toString();
661 if ( !defaultFontString.isEmpty() )
662 {
663 QgsTextFormat format = profileItem->plot()->xAxis().textFormat();
664 QFont f = format.font();
665 QgsFontUtils::setFontFamily( f, defaultFontString );
666 format.setFont( f );
667 profileItem->plot()->xAxis().setTextFormat( format );
668
669 format = profileItem->plot()->yAxis().textFormat();
670 f = format.font();
671 QgsFontUtils::setFontFamily( f, defaultFontString );
672 format.setFont( f );
673 profileItem->plot()->yAxis().setTextFormat( format );
674 }
675 return profileItem.release();
676 } );
677 registry->addLayoutItemGuiMetadata( elevationProfileItemMetadata.release() );
678
679 // chart item
680
683 QObject::tr( "Chart" ),
684 QgsApplication::getThemeIcon( u"/mActionAddChart.svg"_s ),
685 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutChartWidget( qobject_cast<QgsLayoutItemChart *>( item ) ); },
686 createRubberBand
687 ) );
688}
@ Group
Legend group title.
Definition qgis.h:4723
@ Subgroup
Legend subgroup title.
Definition qgis.h:4724
@ Title
Legend title.
Definition qgis.h:4722
@ SymbolLabel
Symbol label (excluding icon).
Definition qgis.h:4726
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:6382
@ ResizeRightDown
Resize right down (Bottom right handle).
Definition qgis.h:6391
@ NoAction
No action.
Definition qgis.h:6397
@ SelectItem
Select item.
Definition qgis.h:6396
@ ResizeLeftUp
Resize left up (Top left handle).
Definition qgis.h:6388
@ ResizeLeftDown
Resize left down (Bottom left handle).
Definition qgis.h:6390
@ ResizeRight
Resize right (Right handle).
Definition qgis.h:6387
@ MoveItem
Move item.
Definition qgis.h:6383
@ ResizeDown
Resize down (Bottom handle).
Definition qgis.h:6385
@ RotateTopRight
Rotate from top right handle.
Definition qgis.h:6393
@ RotateTopLeft
Rotate from top left handle.
Definition qgis.h:6392
@ RotateBottomRight
Rotate right bottom right handle.
Definition qgis.h:6395
@ ResizeRightUp
Resize right up (Top right handle).
Definition qgis.h:6389
@ ResizeLeft
Resize left (Left handle).
Definition qgis.h:6386
@ RotateBottomLeft
Rotate from bottom left handle.
Definition qgis.h:6394
@ ResizeUp
Resize up (Top handle).
Definition qgis.h:6384
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:154
A widget for configuring layout attribute table items.
A widget for configuring layout chart 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.
static const QgsSettingsEntryEnumFlag< Qgis::LegendSyncMode > * settingDefaultLegendSyncMode
Settings entry - Layout legend synchronization mode.
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 setSyncMode(Qgis::LegendSyncMode mode)
Sets the legend's synchronization mode.
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:50
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:135
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:68
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.