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