QGIS API Documentation 3.43.0-Master (c67cf405802)
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" ) ), [=]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutMapWidget( qobject_cast<QgsLayoutItemMap *>( item ), mapCanvas ); }, createRubberBand );
116 mapItemMetadata->setItemAddedToLayoutFunction( [=]( 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 {
199 return;
200
203 break;
204
207 break;
208
211 break;
212
215 break;
216
219 break;
220
223 break;
224
227 break;
228
231 break;
232 }
233
234 label->beginCommand( QObject::tr( "Resize to Text" ) );
235 label->adjustSizeToText( reference );
236 label->endCommand();
237 } );
238
239 registry->addLayoutItemGuiMetadata( labelItemMetadata.release() );
240
241
242 // legend item
243
244 auto legendItemMetadata = std::make_unique<QgsLayoutItemGuiMetadata>( QgsLayoutItemRegistry::LayoutLegend, QObject::tr( "Legend" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLegend.svg" ) ), [=]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * { return new QgsLayoutLegendWidget( qobject_cast<QgsLayoutItemLegend *>( item ), mapCanvas ); }, createRubberBand );
245 legendItemMetadata->setItemAddedToLayoutFunction( [=]( QgsLayoutItem *item, const QVariantMap & ) {
246 QgsLayoutItemLegend *legend = qobject_cast<QgsLayoutItemLegend *>( item );
247 Q_ASSERT( legend );
248
249 // try to find a good map to link the legend with by default
251
252 if ( QApplication::isRightToLeft() )
253 {
254 // for right-to-left locales, use an appropriate default layout
255 legend->setSymbolAlignment( Qt::AlignRight );
256 legend->rstyle( Qgis::LegendComponent::Group ).setAlignment( Qt::AlignRight );
257 legend->rstyle( Qgis::LegendComponent::Subgroup ).setAlignment( Qt::AlignRight );
258 legend->rstyle( Qgis::LegendComponent::SymbolLabel ).setAlignment( Qt::AlignRight );
259 legend->setTitleAlignment( Qt::AlignRight );
260 }
261
262 //set default legend font from settings
263 QgsSettings settings;
264 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
265 if ( !defaultFontString.isEmpty() )
266 {
267 QFont font;
268 QgsFontUtils::setFontFamily( font, defaultFontString );
269
271 f.setFont( font );
273
275 f.setFont( font );
277
279 f.setFont( font );
281
283 f.setFont( font );
285 }
286
287 legend->updateLegend();
288 } );
289
290 registry->addLayoutItemGuiMetadata( legendItemMetadata.release() );
291
292 // scalebar item
293
294 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 );
295 scalebarItemMetadata->setItemAddedToLayoutFunction( [=]( QgsLayoutItem *item, const QVariantMap & ) {
296 QgsLayoutItemScaleBar *scalebar = qobject_cast<QgsLayoutItemScaleBar *>( item );
297 Q_ASSERT( scalebar );
298
299 // default to project's scale calculation method
300 scalebar->setMethod( scalebar->layout()->project()->scaleMethod() );
301
302 // try to find a good map to link the scalebar with by default
303 if ( QgsLayoutItemMap *targetMap = findSensibleDefaultLinkedMapItem( scalebar ) )
304 {
305 scalebar->setLinkedMap( targetMap );
306 scalebar->applyDefaultSize( scalebar->guessUnits() );
307 }
308 } );
309
310 registry->addLayoutItemGuiMetadata( scalebarItemMetadata.release() );
311
312
313 // north arrow
314 auto northArrowMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
315 QgsLayoutItemRegistry::LayoutPicture, QObject::tr( "North Arrow" ), QgsApplication::getThemeIcon( QStringLiteral( "/north_arrow.svg" ) ),
316 [=]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
317 return new QgsLayoutPictureWidget( qobject_cast<QgsLayoutItemPicture *>( item ) );
318 },
319 createRubberBand
320 );
321 northArrowMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
322 // count how many existing north arrows are already in layout
323 QList<QgsLayoutItemPicture *> pictureItems;
324 layout->layoutItems( pictureItems );
325 int northArrowCount = 0;
326
327 QgsSettings settings;
328 const QString defaultPath = settings.value( QStringLiteral( "LayoutDesigner/defaultNorthArrow" ), QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ), QgsSettings::Gui ).toString();
329
330 for ( QgsLayoutItemPicture *p : std::as_const( pictureItems ) )
331 {
332 // look for pictures which use the default north arrow svg
333 if ( p->picturePath() == defaultPath )
334 northArrowCount++;
335 }
336
337 auto picture = std::make_unique<QgsLayoutItemPicture>( layout );
338 picture->setNorthMode( QgsLayoutItemPicture::GridNorth );
339 picture->setPicturePath( defaultPath );
340 // set an id by default, so that north arrows are discernible in layout item lists
341 picture->setId( northArrowCount > 0 ? QObject::tr( "North Arrow %1" ).arg( northArrowCount + 1 ) : QObject::tr( "North Arrow" ) );
342 return picture.release();
343 } );
344 northArrowMetadata->setItemAddedToLayoutFunction( [=]( QgsLayoutItem *item, const QVariantMap & ) {
345 QgsLayoutItemPicture *picture = qobject_cast<QgsLayoutItemPicture *>( item );
346 Q_ASSERT( picture );
347
348 QList<QgsLayoutItemMap *> mapItems;
349 picture->layout()->layoutItems( mapItems );
350
351 // try to find a good map to link the north arrow with by default
352 picture->setLinkedMap( findSensibleDefaultLinkedMapItem( picture ) );
353 } );
354 registry->addLayoutItemGuiMetadata( northArrowMetadata.release() );
355
356 // shape items
357
358 auto createShapeWidget =
359 []( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
360 return new QgsLayoutShapeWidget( qobject_cast<QgsLayoutItemShape *>( item ) );
361 };
362
363 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), createShapeWidget, createRubberBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout *layout ) -> QgsLayoutItem * {
364 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
365 shape->setShapeType( QgsLayoutItemShape::Rectangle );
366 return shape.release();
367 } ) );
368 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), createShapeWidget, createEllipseBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout *layout ) -> QgsLayoutItem * {
369 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
370 shape->setShapeType( QgsLayoutItemShape::Ellipse );
371 return shape.release();
372 } ) );
373 registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), createShapeWidget, createTriangleBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout *layout ) -> QgsLayoutItem * {
374 auto shape = std::make_unique<QgsLayoutItemShape>( layout );
375 shape->setShapeType( QgsLayoutItemShape::Triangle );
376 return shape.release();
377 } ) );
378
379 // marker
380 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 ) );
381
382 // arrow
383 auto arrowMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
384 QgsLayoutItemRegistry::LayoutPolyline, QObject::tr( "Arrow" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddArrow.svg" ) ),
385 [=]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
386 return new QgsLayoutPolylineWidget( qobject_cast<QgsLayoutItemPolyline *>( item ) );
387 },
388 createRubberBand, QString(), true
389 );
390 arrowMetadata->setItemCreationFunction( []( QgsLayout *layout ) -> QgsLayoutItem * {
391 auto arrow = std::make_unique<QgsLayoutItemPolyline>( layout );
392 arrow->setEndMarker( QgsLayoutItemPolyline::ArrowHead );
393 return arrow.release();
394 } );
395 arrowMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
396 auto band = std::make_unique<QGraphicsItemGroup>();
397 QGraphicsPathItem *poly = new QGraphicsPathItem( band.get() );
398 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
399
400 QGraphicsPathItem *tempPoly = new QGraphicsPathItem( band.get() );
401 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
402
403 band->setZValue( QgsLayout::ZViewTool );
404 return band.release();
405 } );
406 registry->addLayoutItemGuiMetadata( arrowMetadata.release() );
407
408 // node items
409
410 auto polygonMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
411 QgsLayoutItemRegistry::LayoutPolygon, QObject::tr( "Polygon" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolygon.svg" ) ),
412 [=]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
413 return new QgsLayoutPolygonWidget( qobject_cast<QgsLayoutItemPolygon *>( item ) );
414 },
415 createRubberBand, QStringLiteral( "nodes" ), true
416 );
417 polygonMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
418 auto band = std::make_unique<QGraphicsItemGroup>();
419 QGraphicsPolygonItem *poly = new QGraphicsPolygonItem( band.get() );
420 poly->setBrush( QBrush( QColor( 227, 22, 22, 20 ) ) );
421 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
422
423 QGraphicsPolygonItem *tempPoly = new QGraphicsPolygonItem( band.get() );
424 tempPoly->setBrush( Qt::NoBrush );
425 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
426
427 band->setZValue( QgsLayout::ZViewTool );
428 return band.release();
429 } );
430 registry->addLayoutItemGuiMetadata( polygonMetadata.release() );
431
432 auto polylineMetadata = std::make_unique<QgsLayoutItemGuiMetadata>(
433 QgsLayoutItemRegistry::LayoutPolyline, QObject::tr( "Polyline" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolyline.svg" ) ),
434 [=]( QgsLayoutItem *item ) -> QgsLayoutItemBaseWidget * {
435 return new QgsLayoutPolylineWidget( qobject_cast<QgsLayoutItemPolyline *>( item ) );
436 },
437 createRubberBand, QStringLiteral( "nodes" ), true
438 );
439 polylineMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * ) -> QGraphicsItemGroup * {
440 auto band = std::make_unique<QGraphicsItemGroup>();
441 QGraphicsPathItem *poly = new QGraphicsPathItem( band.get() );
442 poly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
443
444 QGraphicsPathItem *tempPoly = new QGraphicsPathItem( band.get() );
445 tempPoly->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0, Qt::DotLine ) );
446
447 band->setZValue( QgsLayout::ZViewTool );
448 return band.release();
449 } );
450 registry->addLayoutItemGuiMetadata( polylineMetadata.release() );
451
452
453 // html item
454
455 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 );
456 htmlItemMetadata->setItemCreationFunction( [=]( QgsLayout *layout ) -> QgsLayoutItem * {
457 auto htmlMultiFrame = std::make_unique<QgsLayoutItemHtml>( layout );
458 QgsLayoutItemHtml *html = htmlMultiFrame.get();
459 layout->addMultiFrame( htmlMultiFrame.release() );
460 auto frame = std::make_unique<QgsLayoutFrame>( layout, html );
461 QgsLayoutFrame *f = frame.get();
462 html->addFrame( frame.release() );
463 // cppcheck-suppress returnDanglingLifetime
464 return f;
465 } );
466 registry->addLayoutItemGuiMetadata( htmlItemMetadata.release() );
467
468 // attribute table item
469
470 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 );
471 attributeTableItemMetadata->setItemCreationFunction( [=]( QgsLayout *layout ) -> QgsLayoutItem * {
472 auto tableMultiFrame = std::make_unique<QgsLayoutItemAttributeTable>( layout );
473 QgsLayoutItemAttributeTable *table = tableMultiFrame.get();
474
475 //set first vector layer from layer registry as table source
476 QMap<QString, QgsMapLayer *> layerMap = layout->project()->mapLayers();
477 for ( auto it = layerMap.constBegin(); it != layerMap.constEnd(); ++it )
478 {
479 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( it.value() ) )
480 {
481 table->setVectorLayer( vl );
482 break;
483 }
484 }
485
486 //set default table fonts from settings
487 QgsSettings settings;
488 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
489 if ( !defaultFontString.isEmpty() )
490 {
491 QgsTextFormat format;
492 QFont f = format.font();
493 QgsFontUtils::setFontFamily( f, defaultFontString );
494 format.setFont( f );
495 tableMultiFrame->setContentTextFormat( format );
496 f.setBold( true );
497 format.setFont( f );
498 tableMultiFrame->setHeaderTextFormat( format );
499 }
500
501 layout->addMultiFrame( tableMultiFrame.release() );
502 auto frame = std::make_unique<QgsLayoutFrame>( layout, table );
503 QgsLayoutFrame *f = frame.get();
504 table->addFrame( frame.release() );
505 // cppcheck-suppress returnDanglingLifetime
506 return f;
507 } );
508 registry->addLayoutItemGuiMetadata( attributeTableItemMetadata.release() );
509
510 // manual table item
511
512 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 );
513 manualTableItemMetadata->setItemCreationFunction( [=]( QgsLayout *layout ) -> QgsLayoutItem * {
514 auto tableMultiFrame = std::make_unique<QgsLayoutItemManualTable>( layout );
515 QgsLayoutItemManualTable *table = tableMultiFrame.get();
516
517 // initially start with a 2x2 empty table
518 QgsTableContents contents;
519 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
520 contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
521 table->setTableContents( contents );
522
523 //set default table fonts from settings
524 QgsSettings settings;
525 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
526 if ( !defaultFontString.isEmpty() )
527 {
528 QgsTextFormat format;
529 QFont f = format.font();
530 QgsFontUtils::setFontFamily( f, defaultFontString );
531 format.setFont( f );
532 tableMultiFrame->setContentTextFormat( format );
533 f.setBold( true );
534 format.setFont( f );
535 tableMultiFrame->setHeaderTextFormat( format );
536 }
537
538 layout->addMultiFrame( tableMultiFrame.release() );
539
540 auto frame = std::make_unique<QgsLayoutFrame>( layout, table );
541 QgsLayoutFrame *f = frame.get();
542 table->addFrame( frame.release() );
543 // cppcheck-suppress returnDanglingLifetime
544 return f;
545 } );
546 manualTableItemMetadata->setItemDoubleClickedFunction( [=]( QgsLayoutItem *item, Qgis::MouseHandlesAction ) {
547 QgsLayoutManualTableWidget::openTableDesigner( qobject_cast<QgsLayoutFrame *>( item ) );
548 } );
549 registry->addLayoutItemGuiMetadata( manualTableItemMetadata.release() );
550
551
552 // elevation profile item
553
554 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 );
555 elevationProfileItemMetadata->setItemCreationFunction( [=]( QgsLayout *layout ) -> QgsLayoutItem * {
556 auto profileItem = std::make_unique<QgsLayoutItemElevationProfile>( layout );
557
558 //set default fonts from settings
559 QgsSettings settings;
560 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
561 if ( !defaultFontString.isEmpty() )
562 {
563 QgsTextFormat format = profileItem->plot()->xAxis().textFormat();
564 QFont f = format.font();
565 QgsFontUtils::setFontFamily( f, defaultFontString );
566 format.setFont( f );
567 profileItem->plot()->xAxis().setTextFormat( format );
568
569 format = profileItem->plot()->yAxis().textFormat();
570 f = format.font();
571 QgsFontUtils::setFontFamily( f, defaultFontString );
572 format.setFont( f );
573 profileItem->plot()->yAxis().setTextFormat( format );
574 }
575 return profileItem.release();
576 } );
577 registry->addLayoutItemGuiMetadata( elevationProfileItemMetadata.release() );
578}
@ 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:5804
@ 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)
@ ResizeRightUp
Resize right up (Top right handle)
@ ResizeLeft
Resize left (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:66
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.