QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 : qgis::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 : qgis::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 = qgis::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 )
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->zoomToExtent( mapCanvas->mapSettings().visibleExtent() );
131  }
132  } );
133  registry->addLayoutItemGuiMetadata( mapItemMetadata.release() );
134 
135  // picture item
136 
137  registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutPicture, QObject::tr( "Picture" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddImage.svg" ) ),
138  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
139  {
140  return new QgsLayoutPictureWidget( qobject_cast< QgsLayoutItemPicture * >( item ) );
141  }, createRubberBand ) );
142 
143 
144  // label item
145 
146  auto labelItemMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutLabel, QObject::tr( "Label" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabel.svg" ) ),
147  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
148  {
149  return new QgsLayoutLabelWidget( qobject_cast< QgsLayoutItemLabel * >( item ) );
150  }, createRubberBand );
151  labelItemMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item )
152  {
153  QgsLayoutItemLabel *label = qobject_cast< QgsLayoutItemLabel * >( item );
154  Q_ASSERT( label );
155 
156  label->setText( QObject::tr( "Lorem ipsum" ) );
157  if ( QApplication::isRightToLeft() )
158  {
159  label->setHAlign( Qt::AlignRight );
160  }
161  QSizeF minSize = label->sizeForText();
162  QSizeF currentSize = label->rect().size();
163 
164  //make sure label size is sufficient to fit text
165  double labelWidth = std::max( minSize.width(), currentSize.width() );
166  double labelHeight = std::max( minSize.height(), currentSize.height() );
167  label->attemptSetSceneRect( QRectF( label->pos().x(), label->pos().y(), labelWidth, labelHeight ) );
168  } );
169 
170  registry->addLayoutItemGuiMetadata( labelItemMetadata.release() );
171 
172 
173  // legend item
174 
175  auto legendItemMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutLegend, QObject::tr( "Legend" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLegend.svg" ) ),
176  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
177  {
178  return new QgsLayoutLegendWidget( qobject_cast< QgsLayoutItemLegend * >( item ), mapCanvas );
179  }, createRubberBand );
180  legendItemMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item )
181  {
182  QgsLayoutItemLegend *legend = qobject_cast< QgsLayoutItemLegend * >( item );
183  Q_ASSERT( legend );
184 
185  // try to find a good map to link the legend with by default
186  legend->setLinkedMap( findSensibleDefaultLinkedMapItem( legend ) );
187 
188  if ( QApplication::isRightToLeft() )
189  {
190  // for right-to-left locales, use an appropriate default layout
191  legend->setSymbolAlignment( Qt::AlignRight );
192  legend->rstyle( QgsLegendStyle::Group ).setAlignment( Qt::AlignRight );
193  legend->rstyle( QgsLegendStyle::Subgroup ).setAlignment( Qt::AlignRight );
194  legend->rstyle( QgsLegendStyle::SymbolLabel ).setAlignment( Qt::AlignRight );
195  legend->setTitleAlignment( Qt::AlignRight );
196  }
197 
198  //set default legend font from settings
199  QgsSettings settings;
200  const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
201  if ( !defaultFontString.isEmpty() )
202  {
203  legend->rstyle( QgsLegendStyle::Title ).rfont().setFamily( defaultFontString );
204  legend->rstyle( QgsLegendStyle::Group ).rfont().setFamily( defaultFontString );
205  legend->rstyle( QgsLegendStyle::Subgroup ).rfont().setFamily( defaultFontString );
206  legend->rstyle( QgsLegendStyle::SymbolLabel ).rfont().setFamily( defaultFontString );
207  }
208 
209  legend->updateLegend();
210  } );
211 
212  registry->addLayoutItemGuiMetadata( legendItemMetadata.release() );
213 
214  // scalebar item
215 
216  auto scalebarItemMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutScaleBar, QObject::tr( "Scale Bar" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionScaleBar.svg" ) ),
217  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
218  {
219  return new QgsLayoutScaleBarWidget( qobject_cast< QgsLayoutItemScaleBar * >( item ) );
220  }, createRubberBand );
221  scalebarItemMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item )
222  {
223  QgsLayoutItemScaleBar *scalebar = qobject_cast< QgsLayoutItemScaleBar * >( item );
224  Q_ASSERT( scalebar );
225 
226  // try to find a good map to link the scalebar with by default
227  if ( QgsLayoutItemMap *targetMap = findSensibleDefaultLinkedMapItem( scalebar ) )
228  {
229  scalebar->setLinkedMap( targetMap );
230  scalebar->applyDefaultSize( scalebar->guessUnits() );
231  }
232  } );
233 
234  registry->addLayoutItemGuiMetadata( scalebarItemMetadata.release() );
235 
236 
237  // north arrow
238  std::unique_ptr< QgsLayoutItemGuiMetadata > northArrowMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata>(
239  QgsLayoutItemRegistry::LayoutPicture, QObject::tr( "North Arrow" ), QgsApplication::getThemeIcon( QStringLiteral( "/north_arrow.svg" ) ),
240  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
241  {
242  return new QgsLayoutPictureWidget( qobject_cast< QgsLayoutItemPicture * >( item ) );
243  }, createRubberBand );
244  northArrowMetadata->setItemCreationFunction( []( QgsLayout * layout )->QgsLayoutItem *
245  {
246 
247  // count how many existing north arrows are already in layout
248  QList< QgsLayoutItemPicture * > pictureItems;
249  layout->layoutItems( pictureItems );
250  int northArrowCount = 0;
251 
252  QgsSettings settings;
253  const QString defaultPath = settings.value( QStringLiteral( "LayoutDesigner/defaultNorthArrow" ), QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ), QgsSettings::Gui ).toString();
254 
255  for ( QgsLayoutItemPicture *p : qgis::as_const( pictureItems ) )
256  {
257  // look for pictures which use the default north arrow svg
258  if ( p->picturePath() == defaultPath )
259  northArrowCount++;
260  }
261 
262  std::unique_ptr< QgsLayoutItemPicture > picture = qgis::make_unique< QgsLayoutItemPicture >( layout );
263  picture->setNorthMode( QgsLayoutItemPicture::GridNorth );
264  picture->setPicturePath( defaultPath );
265  // set an id by default, so that north arrows are discernible in layout item lists
266  picture->setId( northArrowCount > 0 ? QObject::tr( "North Arrow %1" ).arg( northArrowCount + 1 ) : QObject::tr( "North Arrow" ) );
267  return picture.release();
268  } );
269  northArrowMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item )
270  {
271  QgsLayoutItemPicture *picture = qobject_cast< QgsLayoutItemPicture * >( item );
272  Q_ASSERT( picture );
273 
274  QList<QgsLayoutItemMap *> mapItems;
275  picture->layout()->layoutItems( mapItems );
276 
277  // try to find a good map to link the north arrow with by default
278  picture->setLinkedMap( findSensibleDefaultLinkedMapItem( picture ) );
279  } );
280  registry->addLayoutItemGuiMetadata( northArrowMetadata.release() );
281 
282  // shape items
283 
284  auto createShapeWidget =
285  []( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
286  {
287  return new QgsLayoutShapeWidget( qobject_cast< QgsLayoutItemShape * >( item ) );
288  };
289 
290  registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), createShapeWidget, createRubberBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout * layout )->QgsLayoutItem*
291  {
292  std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
293  shape->setShapeType( QgsLayoutItemShape::Rectangle );
294  return shape.release();
295  } ) );
296  registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), createShapeWidget, createEllipseBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout * layout )->QgsLayoutItem*
297  {
298  std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
299  shape->setShapeType( QgsLayoutItemShape::Ellipse );
300  return shape.release();
301  } ) );
302  registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), createShapeWidget, createTriangleBand, QStringLiteral( "shapes" ), false, QgsLayoutItemAbstractGuiMetadata::Flags(), []( QgsLayout * layout )->QgsLayoutItem*
303  {
304  std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
305  shape->setShapeType( QgsLayoutItemShape::Triangle );
306  return shape.release();
307  } ) );
308 
309  // marker
310  registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutMarker, QObject::tr( "Marker" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMarker.svg" ) ),
311  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
312  {
313  return new QgsLayoutMarkerWidget( qobject_cast< QgsLayoutItemMarker * >( item ) );
314  }, nullptr ) );
315 
316  // arrow
317  std::unique_ptr< QgsLayoutItemGuiMetadata > arrowMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata>(
318  QgsLayoutItemRegistry::LayoutPolyline, QObject::tr( "Arrow" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddArrow.svg" ) ),
319  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
320  {
321  return new QgsLayoutPolylineWidget( qobject_cast< QgsLayoutItemPolyline * >( item ) );
322  }, createRubberBand, QString(), true );
323  arrowMetadata->setItemCreationFunction( []( QgsLayout * layout )->QgsLayoutItem *
324  {
325  std::unique_ptr< QgsLayoutItemPolyline > arrow = qgis::make_unique< QgsLayoutItemPolyline >( layout );
326  arrow->setEndMarker( QgsLayoutItemPolyline::ArrowHead );
327  return arrow.release();
328  } );
329  arrowMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * )->QGraphicsPathItem*
330  {
331  std::unique_ptr< QGraphicsPathItem > band = qgis::make_unique< QGraphicsPathItem >();
332  band->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
333  band->setZValue( QgsLayout::ZViewTool );
334  return band.release();
335  } );
336  registry->addLayoutItemGuiMetadata( arrowMetadata.release() );
337 
338  // node items
339 
340  std::unique_ptr< QgsLayoutItemGuiMetadata > polygonMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >(
341  QgsLayoutItemRegistry::LayoutPolygon, QObject::tr( "Polygon" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolygon.svg" ) ),
342  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
343  {
344  return new QgsLayoutPolygonWidget( qobject_cast< QgsLayoutItemPolygon * >( item ) );
345  }, createRubberBand, QStringLiteral( "nodes" ), true );
346  polygonMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * )->QGraphicsPolygonItem*
347  {
348  std::unique_ptr< QGraphicsPolygonItem > band = qgis::make_unique< QGraphicsPolygonItem >();
349  band->setBrush( Qt::NoBrush );
350  band->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
351  band->setZValue( QgsLayout::ZViewTool );
352  return band.release();
353  } );
354  registry->addLayoutItemGuiMetadata( polygonMetadata.release() );
355 
356  std::unique_ptr< QgsLayoutItemGuiMetadata > polylineMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata>(
357  QgsLayoutItemRegistry::LayoutPolyline, QObject::tr( "Polyline" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddPolyline.svg" ) ),
358  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
359  {
360  return new QgsLayoutPolylineWidget( qobject_cast< QgsLayoutItemPolyline * >( item ) );
361  }, createRubberBand, QStringLiteral( "nodes" ), true );
362  polylineMetadata->setNodeRubberBandCreationFunction( []( QgsLayoutView * )->QGraphicsPathItem*
363  {
364  std::unique_ptr< QGraphicsPathItem > band = qgis::make_unique< QGraphicsPathItem >();
365  band->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
366  band->setZValue( QgsLayout::ZViewTool );
367  return band.release();
368  } );
369  registry->addLayoutItemGuiMetadata( polylineMetadata.release() );
370 
371 
372  // html item
373 
374  auto htmlItemMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutHtml, QObject::tr( "HTML" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddHtml.svg" ) ),
375  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
376  {
377  return new QgsLayoutHtmlWidget( qobject_cast< QgsLayoutFrame * >( item ) );
378  }, createRubberBand );
379  htmlItemMetadata->setItemCreationFunction( [ = ]( QgsLayout * layout )->QgsLayoutItem *
380  {
381  std::unique_ptr< QgsLayoutItemHtml > htmlMultiFrame = qgis::make_unique< QgsLayoutItemHtml >( layout );
382  QgsLayoutItemHtml *html = htmlMultiFrame.get();
383  layout->addMultiFrame( htmlMultiFrame.release() );
384  std::unique_ptr< QgsLayoutFrame > frame = qgis::make_unique< QgsLayoutFrame >( layout, html );
385  QgsLayoutFrame *f = frame.get();
386  html->addFrame( frame.release() );
387  return f;
388  } );
389  registry->addLayoutItemGuiMetadata( htmlItemMetadata.release() );
390 
391  // attribute table item
392 
393  auto attributeTableItemMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutAttributeTable, QObject::tr( "Attribute Table" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddTable.svg" ) ),
394  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
395  {
396  return new QgsLayoutAttributeTableWidget( qobject_cast< QgsLayoutFrame * >( item ) );
397  }, createRubberBand );
398  attributeTableItemMetadata->setItemCreationFunction( [ = ]( QgsLayout * layout )->QgsLayoutItem *
399  {
400  std::unique_ptr< QgsLayoutItemAttributeTable > tableMultiFrame = qgis::make_unique< QgsLayoutItemAttributeTable >( layout );
401  QgsLayoutItemAttributeTable *table = tableMultiFrame.get();
402 
403  //set first vector layer from layer registry as table source
404  QMap<QString, QgsMapLayer *> layerMap = layout->project()->mapLayers();
405  for ( auto it = layerMap.constBegin() ; it != layerMap.constEnd(); ++it )
406  {
407  if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( it.value() ) )
408  {
409  table->setVectorLayer( vl );
410  break;
411  }
412  }
413 
414  //set default table fonts from settings
415  QgsSettings settings;
416  const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
417  if ( !defaultFontString.isEmpty() )
418  {
419  QgsTextFormat format;
420  QFont f = format.font();
421  f.setFamily( defaultFontString );
422  format.setFont( f );
423  tableMultiFrame->setContentTextFormat( format );
424  f.setBold( true );
425  format.setFont( f );
426  tableMultiFrame->setHeaderTextFormat( format );
427  }
428 
429  layout->addMultiFrame( tableMultiFrame.release() );
430  std::unique_ptr< QgsLayoutFrame > frame = qgis::make_unique< QgsLayoutFrame >( layout, table );
431  QgsLayoutFrame *f = frame.get();
432  table->addFrame( frame.release() );
433  return f;
434  } );
435  registry->addLayoutItemGuiMetadata( attributeTableItemMetadata.release() );
436 
437  // manual table item
438 
439  auto manualTableItemMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutManualTable, QObject::tr( "Fixed Table" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddManualTable.svg" ) ),
440  [ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
441  {
442  return new QgsLayoutManualTableWidget( qobject_cast< QgsLayoutFrame * >( item ) );
443  }, createRubberBand );
444  manualTableItemMetadata->setItemCreationFunction( [ = ]( QgsLayout * layout )->QgsLayoutItem *
445  {
446  std::unique_ptr< QgsLayoutItemManualTable > tableMultiFrame = qgis::make_unique< QgsLayoutItemManualTable >( layout );
447  QgsLayoutItemManualTable *table = tableMultiFrame.get();
448 
449  // initially start with a 2x2 empty table
450  QgsTableContents contents;
451  contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
452  contents << ( QgsTableRow() << QgsTableCell() << QgsTableCell() );
453  table->setTableContents( contents );
454 
455  //set default table fonts from settings
456  QgsSettings settings;
457  const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
458  if ( !defaultFontString.isEmpty() )
459  {
460  QgsTextFormat format;
461  QFont f = format.font();
462  f.setFamily( defaultFontString );
463  format.setFont( f );
464  tableMultiFrame->setContentTextFormat( format );
465  f.setBold( true );
466  format.setFont( f );
467  tableMultiFrame->setHeaderTextFormat( format );
468  }
469 
470  layout->addMultiFrame( tableMultiFrame.release() );
471 
472  std::unique_ptr< QgsLayoutFrame > frame = qgis::make_unique< QgsLayoutFrame >( layout, table );
473  QgsLayoutFrame *f = frame.get();
474  table->addFrame( frame.release() );
475  return f;
476  } );
477  registry->addLayoutItemGuiMetadata( manualTableItemMetadata.release() );
478 }
QgsLayoutItemRegistry::LayoutPolygon
@ LayoutPolygon
Polygon shape item.
Definition: qgslayoutitemregistry.h:322
QgsLayoutObject::layout
const QgsLayout * layout() const
Returns the layout the object is attached to.
Definition: qgslayoutobject.cpp:126
QgsLayoutItemPicture::setLinkedMap
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map object for rotation.
Definition: qgslayoutitempicture.cpp:666
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:177
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsLayoutItemGuiGroup
Stores GUI metadata about a group of layout item classes.
Definition: qgslayoutitemguiregistry.h:288
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:121
QgsProject::mapLayers
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Definition: qgsproject.cpp:3436
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:391
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:259
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
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:510
QgsLayoutItemHtml
A layout multiframe subclass for HTML content.
Definition: qgslayoutitemhtml.h:37
QgsLayoutItemPicture::GridNorth
@ GridNorth
Align to grid north.
Definition: qgslayoutitempicture.h:65
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:468
QgsLayoutViewRectangularRubberBand
QgsLayoutViewRectangularRubberBand is rectangular rubber band for use within QgsLayoutView widgets.
Definition: qgslayoutviewrubberband.h:160
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
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:140
QgsLayoutViewTriangleRubberBand
QgsLayoutViewTriangleRubberBand is triangular rubber band for use within QgsLayoutView widgets.
Definition: qgslayoutviewrubberband.h:226
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:352
QgsLayoutItemLegend::rstyle
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns reference to modifiable legend style.
Definition: qgslayoutitemlegend.cpp:321
QgsLegendStyle::SymbolLabel
@ SymbolLabel
Symbol label (excluding icon)
Definition: qgslegendstyle.h:48
QgsLegendStyle::rfont
QFont & rfont()
Returns a modifiable reference to the component's font.
Definition: qgslegendstyle.h:82
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:41
QgsTableContents
QVector< QgsTableRow > QgsTableContents
A set of table rows.
Definition: qgstablecell.h:220
QgsLayoutHtmlWidget
A widget for configuring layout html items.
Definition: qgslayouthtmlwidget.h:38
qgslayoutframe.h
QgsLayoutItemLegend
A layout item subclass for map legends.
Definition: qgslayoutitemlegend.h:114
QgsLegendStyle::Title
@ Title
Legend title.
Definition: qgslegendstyle.h:44
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:37
qgslayoutpolygonwidget.h
QgsTableCell
Encapsulates the contents and formatting of a single table cell.
Definition: qgstablecell.h:36
QgsLayoutMapWidget
Input widget for the configuration of QgsLayoutItemMap.
Definition: qgslayoutmapwidget.h:46
QgsLayoutFrame
Base class for frame items, which form a layout multiframe item.
Definition: qgslayoutframe.h:32
qgslayoutguiutils.h
QgsLayoutItemLabel::setText
void setText(const QString &text)
Sets the label's preset text.
Definition: qgslayoutitemlabel.cpp:224
qgslayoutattributetablewidget.h
QgsLayoutItemRegistry::LayoutPolyline
@ LayoutPolyline
Polyline shape item.
Definition: qgslayoutitemregistry.h:323
QgsLayoutPolylineWidget
Input widget for QgsLayoutItemPolyline.
Definition: qgslayoutpolylinewidget.h:36
QgsLayoutItem::setBackgroundColor
void setBackgroundColor(const QColor &color)
Sets the background color for this item.
Definition: qgslayoutitem.cpp:892
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:221
qgslayoutscalebarwidget.h
QgsLayoutAttributeTableWidget
A widget for configuring layout attribute table items.
Definition: qgslayoutattributetablewidget.h:39
qgslayoutitemguiregistry.h
QgsLayoutItemAttributeTable
A layout table subclass that displays attributes from a vector layer.
Definition: qgslayoutitemattributetable.h:35
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:71
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
QgsLayoutItemGuiMetadata
Convenience metadata class that uses static functions to handle layout item GUI behavior.
Definition: qgslayoutitemguiregistry.h:169
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:540
qgslayout.h
QgsLayoutItemRegistry::LayoutScaleBar
@ LayoutScaleBar
Scale bar item.
Definition: qgslayoutitemregistry.h:324
QgsLayoutItemLegend::setLinkedMap
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map to associate with the legend.
Definition: qgslayoutitemlegend.cpp:750
QgsLayout::ZViewTool
@ ZViewTool
Z-value for temporary view tool items.
Definition: qgslayout.h:64
QgsLayoutItemShape::Rectangle
@ Rectangle
Rectangle shape.
Definition: qgslayoutitemshape.h:42
QgsLayoutItemBaseWidget
A base class for property widgets for layout items.
Definition: qgslayoutitemwidget.h:123
qgslayoutlegendwidget.h
qgslayoutpolylinewidget.h
QgsLayoutItemPicture
A layout item subclass that displays SVG files or raster format images (jpg, png, ....
Definition: qgslayoutitempicture.h:36
QgsLayoutItemPolyline::ArrowHead
@ ArrowHead
Show arrow marker.
Definition: qgslayoutitempolyline.h:42
qgslayouthtmlwidget.h
qgslayoutitemmanualtable.h
qgslayoutitemlegend.h
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:318
QgsLayoutItemRegistry::LayoutLabel
@ LayoutLabel
Label item.
Definition: qgslayoutitemregistry.h:319
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:498
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:60
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:387
QgsLayoutItemRegistry::LayoutPicture
@ LayoutPicture
Picture item.
Definition: qgslayoutitemregistry.h:318
QgsLayoutItemLegend::setTitleAlignment
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
Definition: qgslayoutitemlegend.cpp:316
QgsLayoutManualTableWidget
A widget for configuring layout manual table items.
Definition: qgslayoutmanualtablewidget.h:40
QgsLayoutItemRegistry::LayoutMarker
@ LayoutMarker
Marker item.
Definition: qgslayoutitemregistry.h:339
QgsLayoutItemRegistry::LayoutMap
@ LayoutMap
Map item.
Definition: qgslayoutitemregistry.h:317
qgslayoutitempicture.h
QgsLayoutPolygonWidget
Input widget for QgsLayoutItemPolygon.
Definition: qgslayoutpolygonwidget.h:36
QgsLayoutView
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
qgslayoutitempolyline.h
QgsTextFormat::font
QFont font() const
Returns the font used for rendering text.
Definition: qgstextformat.cpp:151
qgslayoutitemshape.h
QgsLayoutItemAttributeTable::setVectorLayer
void setVectorLayer(QgsVectorLayer *layer)
Sets the vector layer from which to display feature attributes.
Definition: qgslayoutitemattributetable.cpp:72
QgsLayoutItemRegistry::LayoutHtml
@ LayoutHtml
Html multiframe item.
Definition: qgslayoutitemregistry.h:332
QgsLayoutViewRubberBand
QgsLayoutViewRubberBand is an abstract base class for temporary rubber band items in various shapes,...
Definition: qgslayoutviewrubberband.h:38
QgsLayoutItemLabel
A layout item subclass for text labels.
Definition: qgslayoutitemlabel.h:35
QgsLayoutItemScaleBar
A layout item subclass for scale bars.
Definition: qgslayoutitemscalebar.h:36
QgsLayoutItemShape::Triangle
@ Triangle
Triangle shape.
Definition: qgslayoutitemshape.h:43
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:122
QgsLayoutItemRegistry::LayoutShape
@ LayoutShape
Shape item.
Definition: qgslayoutitemregistry.h:321
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 takes output image size into accou...
Definition: qgsmapsettings.cpp:371
QgsLayoutItemRegistry::LayoutItem
@ LayoutItem
Base class for items.
Definition: qgslayoutitemregistry.h:308
qgslayoutitemregistry.h
QgsLayoutItemRegistry::LayoutAttributeTable
@ LayoutAttributeTable
Attribute table.
Definition: qgslayoutitemregistry.h:333
QgsLayoutItemRegistry::LayoutManualTable
@ LayoutManualTable
Manual (fixed) table.
Definition: qgslayoutitemregistry.h:338
QgsLayoutMarkerWidget
A widget for configuring layout shape items.
Definition: qgslayoutmarkerwidget.h:37
QgsLayoutItemRegistry::LayoutLegend
@ LayoutLegend
Legend item.
Definition: qgslayoutitemregistry.h:320
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:99
QgsLegendStyle::Group
@ Group
Legend group title.
Definition: qgslegendstyle.h:45
QgsLegendStyle::Subgroup
@ Subgroup
Legend subgroup title.
Definition: qgslegendstyle.h:46
QgsLayoutScaleBarWidget
A widget to define the properties of a QgsLayoutItemScaleBar.
Definition: qgslayoutscalebarwidget.h:39
QgsLayoutItemManualTable
A layout table subclass that displays manually entered (and formatted) content.
Definition: qgslayoutitemmanualtable.h:32
QgsLayoutItemLegend::setSymbolAlignment
void setSymbolAlignment(Qt::AlignmentFlag alignment)
Sets the alignment for placement of legend symbols.
Definition: qgslayoutitemlegend.cpp:426
QgsLayoutItemLegend::updateLegend
void updateLegend()
Updates the model and all legend entries.
Definition: qgslayoutitemlegend.cpp:537
qgslayoutitemhtml.h
QgsLayoutViewEllipticalRubberBand
QgsLayoutViewEllipseRubberBand is elliptical rubber band for use within QgsLayoutView widgets.
Definition: qgslayoutviewrubberband.h:193
QgsSettings::Gui
@ Gui
Definition: qgssettings.h:71
QgsLayoutPictureWidget
A widget for configuring layout picture items.
Definition: qgslayoutpicturewidget.h:38
qgslayoutitemmap.h
QgsLayoutItemGuiRegistry
Registry of available layout item GUI behavior.
Definition: qgslayoutitemguiregistry.h:332