QGIS API Documentation 4.1.0-Master (376402f9aeb)
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"
55
56#include <QString>
57
58using namespace Qt::StringLiterals;
59
69{
70 // start by trying to find a selected map
71 QList<QgsLayoutItemMap *> mapItems;
72 referenceItem->layout()->layoutItems( mapItems );
73
74 QgsLayoutItemMap *targetMap = nullptr;
75 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
76 {
77 if ( map->isSelected() )
78 {
79 return map;
80 }
81 }
82
83 // nope, no selection... hm, was the item drawn over a map? If so, use the topmost intersecting one
84 double largestZValue = std::numeric_limits<double>::lowest();
85 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
86 {
87 if ( map->collidesWithItem( referenceItem ) && map->zValue() > largestZValue )
88 {
89 targetMap = map;
90 largestZValue = map->zValue();
91 }
92 }
93 if ( targetMap )
94 return targetMap;
95
96 // ah frick it, just use the reference (or biggest!) map
97 return referenceItem->layout()->referenceMap();
98}
99
101{
103
104 registry->addItemGroup( QgsLayoutItemGuiGroup( u"shapes"_s, QObject::tr( "Shape" ), QgsApplication::getThemeIcon( u"/mActionAddBasicShape.svg"_s ) ) );
105 registry->addItemGroup( QgsLayoutItemGuiGroup( u"nodes"_s, QObject::tr( "Node Item" ), QgsApplication::getThemeIcon( u"/mActionAddNodesItem.svg"_s ) ) );
106
107 auto createRubberBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * { return new QgsLayoutViewRectangularRubberBand( view ); } );
108 auto createEllipseBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * { return new QgsLayoutViewEllipticalRubberBand( view ); } );
109 auto createTriangleBand = ( []( QgsLayoutView *view ) -> QgsLayoutViewRubberBand * { return new QgsLayoutViewTriangleRubberBand( view ); } );
110
111#if 0
112 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, u"test"_s, QgsApplication::getThemeIcon( u"/mActionAddLabel.svg"_s ), nullptr, createRubberBand ) );
113#endif
114
115 // map item
116
117 auto mapItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
119 QObject::tr( "Map" ),
120 QgsApplication::getThemeIcon( u"/mActionAddMap.svg"_s ),
121 [mapCanvas]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutMapWidget( qobject_cast<QgsLayoutItemMap *>( item ), mapCanvas ); },
122 createRubberBand
123 );
124 mapItemMetadata->setItemAddedToLayoutFunction( [mapCanvas]( QgsLayoutItem *item, const QVariantMap & ) {
125 QgsLayoutItemMap *map = qobject_cast<QgsLayoutItemMap *>( item );
126 Q_ASSERT( map );
127
128 //get the color for map canvas background and set map background color accordingly
129 map->setBackgroundColor( QgsProject::instance()->backgroundColor() );
130
131 if ( mapCanvas )
132 {
133 map->setMapRotation( mapCanvas->rotation() );
134 map->zoomToExtent( mapCanvas->mapSettings().visibleExtent() );
135 }
136
137 // auto assign a unique id to map items
138 QList<QgsLayoutItemMap *> mapsList;
139 if ( map->layout() )
140 map->layout()->layoutItems( mapsList );
141
142 int counter = mapsList.size() + 1;
143 bool existing = false;
144 while ( true )
145 {
146 existing = false;
147 for ( QgsLayoutItemMap *otherMap : std::as_const( mapsList ) )
148 {
149 if ( map == otherMap )
150 continue;
151
152 if ( otherMap->id() == QObject::tr( "Map %1" ).arg( counter ) )
153 {
154 existing = true;
155 break;
156 }
157 }
158 if ( existing )
159 counter++;
160 else
161 break;
162 }
163 map->setId( QObject::tr( "Map %1" ).arg( counter ) );
164 } );
165 registry->addLayoutItemGuiMetadata( mapItemMetadata.release() );
166
167 // picture item
168
171 QObject::tr( "Picture" ),
172 QgsApplication::getThemeIcon( u"/mActionAddImage.svg"_s ),
173 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPictureWidget( qobject_cast<QgsLayoutItemPicture *>( item ) ); },
174 createRubberBand
175 ) );
176
177 // label item
178
179 auto labelItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
181 QObject::tr( "Label" ),
182 QgsApplication::getThemeIcon( u"/mActionLabel.svg"_s ),
183 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutLabelWidget( qobject_cast<QgsLayoutItemLabel *>( item ) ); },
184 createRubberBand
185 );
186 labelItemMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap &properties ) {
187 QgsLayoutItemLabel *label = qobject_cast<QgsLayoutItemLabel *>( item );
188 Q_ASSERT( label );
189
190 label->setText( properties.value( u"expression"_s ).toString().isEmpty() ? QObject::tr( "Lorem ipsum" ) : u"[% %1 %]"_s.arg( properties.value( u"expression"_s ).toString() ) );
191 if ( QApplication::isRightToLeft() )
192 {
193 label->setHAlign( Qt::AlignRight );
194 }
195 QSizeF minSize = label->sizeForText();
196 QSizeF currentSize = label->rect().size();
197
198 //make sure label size is sufficient to fit text
199 double labelWidth = std::max( minSize.width(), currentSize.width() );
200 double labelHeight = std::max( minSize.height(), currentSize.height() );
201 label->attemptSetSceneRect( QRectF( label->pos().x(), label->pos().y(), labelWidth, labelHeight ) );
202 } );
203
204 labelItemMetadata->setItemDoubleClickedFunction( []( QgsLayoutItem *item, Qgis::MouseHandlesAction action ) {
205 QgsLayoutItemLabel *label = qobject_cast<QgsLayoutItemLabel *>( item );
206
207 // size to text doesn't have any real meaning for HTML content, skip it
208 if ( label->mode() == QgsLayoutItemLabel::ModeHtml )
209 return;
210
211 Q_ASSERT( label );
213 switch ( action )
214 {
222 return;
223
226 break;
227
230 break;
231
234 break;
235
238 break;
239
242 break;
243
246 break;
247
250 break;
251
254 break;
255 }
256
257 label->beginCommand( QObject::tr( "Resize to Text" ) );
258 label->adjustSizeToText( reference );
259 label->endCommand();
260 } );
261
262 registry->addLayoutItemGuiMetadata( labelItemMetadata.release() );
263
264
265 // legend item
266
267 auto legendItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
269 QObject::tr( "Legend" ),
270 QgsApplication::getThemeIcon( u"/mActionAddLegend.svg"_s ),
271 [mapCanvas]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutLegendWidget( qobject_cast<QgsLayoutItemLegend *>( item ), mapCanvas ); },
272 createRubberBand
273 );
274 legendItemMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap & ) {
275 QgsLayoutItemLegend *legend = qobject_cast<QgsLayoutItemLegend *>( item );
276 Q_ASSERT( legend );
277
278 // set to default sync mode
280
281 // try to find a good map to link the legend with by default
283
284 if ( QApplication::isRightToLeft() )
285 {
286 // for right-to-left locales, use an appropriate default layout
287 legend->setSymbolAlignment( Qt::AlignRight );
288 legend->rstyle( Qgis::LegendComponent::Group ).setAlignment( Qt::AlignRight );
289 legend->rstyle( Qgis::LegendComponent::Subgroup ).setAlignment( Qt::AlignRight );
290 legend->rstyle( Qgis::LegendComponent::SymbolLabel ).setAlignment( Qt::AlignRight );
291 legend->setTitleAlignment( Qt::AlignRight );
292 }
293
294 //set default legend font from settings
295 const QString defaultFontString = QgsLayout::settingsLayoutDefaultFont->value();
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 const QString defaultPath = QgsLayout::settingsLayoutDefaultNorthArrow->value();
365
366 for ( QgsLayoutItemPicture *p : std::as_const( pictureItems ) )
367 {
368 // look for pictures which use the default north arrow svg
369 if ( p->picturePath() == defaultPath )
370 northArrowCount++;
371 }
372
373 auto picture = std::make_unique<QgsLayoutItemPicture>( layout );
374 picture->setNorthMode( QgsLayoutItemPicture::GridNorth );
375 picture->setPicturePath( defaultPath );
376 // set an id by default, so that north arrows are discernible in layout item lists
377 picture->setId( northArrowCount > 0 ? QObject::tr( "North Arrow %1" ).arg( northArrowCount + 1 ) : QObject::tr( "North Arrow" ) );
378 return picture.release();
379 } );
380 northArrowMetadata->setItemAddedToLayoutFunction( []( QgsLayoutItem *item, const QVariantMap & ) {
381 QgsLayoutItemPicture *picture = qobject_cast<QgsLayoutItemPicture *>( item );
382 Q_ASSERT( picture );
383
384 QList<QgsLayoutItemMap *> mapItems;
385 picture->layout()->layoutItems( mapItems );
386
387 // try to find a good map to link the north arrow with by default
388 picture->setLinkedMap( findSensibleDefaultLinkedMapItem( picture ) );
389 } );
390 registry->addLayoutItemGuiMetadata( northArrowMetadata.release() );
391
392 // shape items
393
394 auto createShapeWidget = []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutShapeWidget( qobject_cast<QgsLayoutItemShape *>( item ) ); };
395
398 QObject::tr( "Rectangle" ),
399 QgsApplication::getThemeIcon( u"/mActionAddBasicRectangle.svg"_s ),
400 createShapeWidget,
401 createRubberBand,
402 u"shapes"_s,
403 false,
405 []( QgsLayout *layout ) -> QgsLayoutItem * {
406 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
407 shape->setShapeType( QgsLayoutItemShape::Rectangle );
408 return shape.release();
409 }
410 ) );
413 QObject::tr( "Ellipse" ),
414 QgsApplication::getThemeIcon( u"/mActionAddBasicCircle.svg"_s ),
415 createShapeWidget,
416 createEllipseBand,
417 u"shapes"_s,
418 false,
420 []( QgsLayout *layout ) -> QgsLayoutItem * {
421 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
422 shape->setShapeType( QgsLayoutItemShape::Ellipse );
423 return shape.release();
424 }
425 ) );
428 QObject::tr( "Triangle" ),
429 QgsApplication::getThemeIcon( u"/mActionAddBasicTriangle.svg"_s ),
430 createShapeWidget,
431 createTriangleBand,
432 u"shapes"_s,
433 false,
435 []( QgsLayout *layout ) -> QgsLayoutItem * {
436 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
437 shape->setShapeType( QgsLayoutItemShape::Triangle );
438 return shape.release();
439 }
440 ) );
441
442 // marker
445 QObject::tr( "Marker" ),
446 QgsApplication::getThemeIcon( u"/mActionAddMarker.svg"_s ),
447 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutMarkerWidget( qobject_cast<QgsLayoutItemMarker *>( item ) ); },
448 nullptr
449 ) );
450
451 // arrow
452 auto arrowMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
454 QObject::tr( "Arrow" ),
455 QgsApplication::getThemeIcon( u"/mActionAddArrow.svg"_s ),
456 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPolylineWidget( qobject_cast<QgsLayoutItemPolyline *>( item ) ); },
457 createRubberBand,
458 QString(),
459 true
460 );
461 arrowMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
462 auto arrow = std::make_unique<QgsLayoutItemPolyline>( layout );
463 arrow->setEndMarker( QgsLayoutItemPolyline::ArrowHead );
464 return arrow.release();
465 } );
466 arrowMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
467 auto band = std::make_unique<QGraphicsItemGroup>();
468 QGraphicsPathItem *poly = new QGraphicsPathItem( band.get() );
469 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
470
471 QGraphicsPathItem *tempPoly = new QGraphicsPathItem( band.get() );
472 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
473
474 band->setZValue( QgsLayout::ZViewTool );
475 return band.release();
476 } );
477 registry->addLayoutItemGuiMetadata( arrowMetadata.release() );
478
479 // node items
480
481 auto polygonMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
483 QObject::tr( "Polygon" ),
484 QgsApplication::getThemeIcon( u"/mActionAddPolygon.svg"_s ),
485 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPolygonWidget( qobject_cast<QgsLayoutItemPolygon *>( item ) ); },
486 createRubberBand,
487 u"nodes"_s,
488 true
489 );
490 polygonMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
491 auto band = std::make_unique<QGraphicsItemGroup>();
492 QGraphicsPolygonItem *poly = new QGraphicsPolygonItem( band.get() );
493 poly->setBrush( QBrush( QColor( 227, 22, 22, 20 ) ) );
494 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
495
496 QGraphicsPolygonItem *tempPoly = new QGraphicsPolygonItem( band.get() );
497 tempPoly->setBrush( Qt::NoBrush );
498 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
499
500 band->setZValue( QgsLayout::ZViewTool );
501 return band.release();
502 } );
503 registry->addLayoutItemGuiMetadata( polygonMetadata.release() );
504
505 auto polylineMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
507 QObject::tr( "Polyline" ),
508 QgsApplication::getThemeIcon( u"/mActionAddPolyline.svg"_s ),
509 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutPolylineWidget( qobject_cast<QgsLayoutItemPolyline *>( item ) ); },
510 createRubberBand,
511 u"nodes"_s,
512 true
513 );
514 polylineMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
515 auto band = std::make_unique<QGraphicsItemGroup>();
516 QGraphicsPathItem *poly = new QGraphicsPathItem( band.get() );
517 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
518
519 QGraphicsPathItem *tempPoly = new QGraphicsPathItem( band.get() );
520 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
521
522 band->setZValue( QgsLayout::ZViewTool );
523 return band.release();
524 } );
525 registry->addLayoutItemGuiMetadata( polylineMetadata.release() );
526
527
528 // html item
529
530 auto htmlItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
532 QObject::tr( "HTML" ),
533 QgsApplication::getThemeIcon( u"/mActionAddHtml.svg"_s ),
534 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutHtmlWidget( qobject_cast<QgsLayoutFrame *>( item ) ); },
535 createRubberBand
536 );
537 htmlItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
538 auto htmlMultiFrame = std::make_unique<QgsLayoutItemHtml>( layout );
539 QgsLayoutItemHtml *html = htmlMultiFrame.get();
540 layout->addMultiFrame( htmlMultiFrame.release() );
541 auto frame = std::make_unique<QgsLayoutFrame>( layout, html );
542 QgsLayoutFrame *f = frame.get();
543 html->addFrame( frame.release() );
544 // cppcheck-suppress returnDanglingLifetime
545 return f;
546 } );
547 registry->addLayoutItemGuiMetadata( htmlItemMetadata.release() );
548
549 // attribute table item
550
551 auto attributeTableItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
553 QObject::tr( "Attribute Table" ),
554 QgsApplication::getThemeIcon( u"/mActionAddTable.svg"_s ),
555 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutAttributeTableWidget( qobject_cast<QgsLayoutFrame *>( item ) ); },
556 createRubberBand
557 );
558 attributeTableItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
559 auto tableMultiFrame = std::make_unique<QgsLayoutItemAttributeTable>( layout );
560 QgsLayoutItemAttributeTable *table = tableMultiFrame.get();
561
562 //set first vector layer from layer registry as table source
563 QMap<QString, QgsMapLayer *> layerMap = layout->project()->mapLayers();
564 for ( auto it = layerMap.constBegin(); it != layerMap.constEnd(); ++it )
565 {
566 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( it.value() ) )
567 {
568 table->setVectorLayer( vl );
569 break;
570 }
571 }
572
573 //set default table fonts from settings
574 const QString defaultFontString = QgsLayout::settingsLayoutDefaultFont->value();
575 if ( !defaultFontString.isEmpty() )
576 {
577 QgsTextFormat format;
578 QFont f = format.font();
579 QgsFontUtils::setFontFamily( f, defaultFontString );
580 format.setFont( f );
581 tableMultiFrame->setContentTextFormat( format );
582 f.setBold( true );
583 format.setFont( f );
584 tableMultiFrame->setHeaderTextFormat( format );
585 }
586
587 layout->addMultiFrame( tableMultiFrame.release() );
588 auto frame = std::make_unique<QgsLayoutFrame>( layout, table );
589 QgsLayoutFrame *f = frame.get();
590 table->addFrame( frame.release() );
591 // cppcheck-suppress returnDanglingLifetime
592 return f;
593 } );
594 registry->addLayoutItemGuiMetadata( attributeTableItemMetadata.release() );
595
596 // manual table item
597
598 auto manualTableItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
600 QObject::tr( "Fixed Table" ),
601 QgsApplication::getThemeIcon( u"/mActionAddManualTable.svg"_s ),
602 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutManualTableWidget( qobject_cast<QgsLayoutFrame *>( item ) ); },
603 createRubberBand
604 );
605 manualTableItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
606 auto tableMultiFrame = std::make_unique<QgsLayoutItemManualTable>( layout );
607 QgsLayoutItemManualTable *table = tableMultiFrame.get();
608
609 // initially start with a 2x2 empty table
610 QgsTableContents contents;
611 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
612 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
613 table->setTableContents( contents );
614
615 //set default table fonts from settings
616 const QString defaultFontString = QgsLayout::settingsLayoutDefaultFont->value();
617 if ( !defaultFontString.isEmpty() )
618 {
619 QgsTextFormat format;
620 QFont f = format.font();
621 QgsFontUtils::setFontFamily( f, defaultFontString );
622 format.setFont( f );
623 tableMultiFrame->setContentTextFormat( format );
624 f.setBold( true );
625 format.setFont( f );
626 tableMultiFrame->setHeaderTextFormat( format );
627 }
628
629 layout->addMultiFrame( tableMultiFrame.release() );
630
631 auto frame = std::make_unique<QgsLayoutFrame>( layout, table );
632 QgsLayoutFrame *f = frame.get();
633 table->addFrame( frame.release() );
634 // cppcheck-suppress returnDanglingLifetime
635 return f;
636 } );
637 manualTableItemMetadata->setItemDoubleClickedFunction( []( QgsLayoutItem *item, Qgis::MouseHandlesAction ) {
638 QgsLayoutManualTableWidget::openTableDesigner( qobject_cast<QgsLayoutFrame *>( item ) );
639 } );
640 registry->addLayoutItemGuiMetadata( manualTableItemMetadata.release() );
641
642
643 // elevation profile item
644
645 auto elevationProfileItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
647 QObject::tr( "Elevation Profile" ),
648 QgsApplication::getThemeIcon( u"/mActionElevationProfile.svg"_s ),
649 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutElevationProfileWidget( qobject_cast<QgsLayoutItemElevationProfile *>( item ) ); },
650 createRubberBand
651 );
652 elevationProfileItemMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
653 auto profileItem = std::make_unique<QgsLayoutItemElevationProfile>( layout );
654
655 //set default fonts from settings
656 const QString defaultFontString = QgsLayout::settingsLayoutDefaultFont->value();
657 if ( !defaultFontString.isEmpty() )
658 {
659 QgsTextFormat format = profileItem->plot()->xAxis().textFormat();
660 QFont f = format.font();
661 QgsFontUtils::setFontFamily( f, defaultFontString );
662 format.setFont( f );
663 profileItem->plot()->xAxis().setTextFormat( format );
664
665 format = profileItem->plot()->yAxis().textFormat();
666 f = format.font();
667 QgsFontUtils::setFontFamily( f, defaultFontString );
668 format.setFont( f );
669 profileItem->plot()->yAxis().setTextFormat( format );
670 }
671 return profileItem.release();
672 } );
673 registry->addLayoutItemGuiMetadata( elevationProfileItemMetadata.release() );
674
675 // chart item
676
679 QObject::tr( "Chart" ),
680 QgsApplication::getThemeIcon( u"/mActionAddChart.svg"_s ),
681 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutChartWidget( qobject_cast<QgsLayoutItemChart *>( item ) ); },
682 createRubberBand
683 ) );
684}
@ Group
Legend group title.
Definition qgis.h:4879
@ Subgroup
Legend subgroup title.
Definition qgis.h:4880
@ Title
Legend title.
Definition qgis.h:4878
@ SymbolLabel
Symbol label (excluding icon).
Definition qgis.h:4882
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:6538
@ ResizeRightDown
Resize right down (Bottom right handle).
Definition qgis.h:6547
@ NoAction
No action.
Definition qgis.h:6553
@ SelectItem
Select item.
Definition qgis.h:6552
@ ResizeLeftUp
Resize left up (Top left handle).
Definition qgis.h:6544
@ ResizeLeftDown
Resize left down (Bottom left handle).
Definition qgis.h:6546
@ ResizeRight
Resize right (Right handle).
Definition qgis.h:6543
@ MoveItem
Move item.
Definition qgis.h:6539
@ ResizeDown
Resize down (Bottom handle).
Definition qgis.h:6541
@ RotateTopRight
Rotate from top right handle.
Definition qgis.h:6549
@ RotateTopLeft
Rotate from top left handle.
Definition qgis.h:6548
@ RotateBottomRight
Rotate right bottom right handle.
Definition qgis.h:6551
@ ResizeRightUp
Resize right up (Top right handle).
Definition qgis.h:6545
@ ResizeLeft
Resize left (Left handle).
Definition qgis.h:6542
@ RotateBottomLeft
Rotate from bottom left handle.
Definition qgis.h:6550
@ ResizeUp
Resize up (Top handle).
Definition qgis.h:6540
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:51
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition qgslayout.h:121
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...
static const QgsSettingsEntryString * settingsLayoutDefaultNorthArrow
Settings entry for the default north arrow SVG path.
Definition qgslayout.h:674
@ ZViewTool
Z-value for temporary view tool items.
Definition qgslayout.h:64
static const QgsSettingsEntryString * settingsLayoutDefaultFont
Settings entry for the default font family used for new layout items.
Definition qgslayout.h:668
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:136
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
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.