QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgslayouttable.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayouttable.h
3  ----------------
4  begin : November 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSLAYOUTTABLE_H
19 #define QGSLAYOUTTABLE_H
20 
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include "qgslayoutmultiframe.h"
24 #include "qgsconditionalstyle.h"
25 #include "qgstextformat.h"
26 #include <QFont>
27 #include <QColor>
28 #include <QPair>
29 
31 
38 typedef QVector< QVariant > QgsLayoutTableRow;
39 
46 #ifndef SIP_RUN
47 typedef QVector< QgsLayoutTableRow > QgsLayoutTableContents;
48 #else
49 typedef QVector< QVector< QVariant > > QgsLayoutTableContents;
50 #endif
51 
52 
58 typedef QVector<QgsLayoutTableColumn> QgsLayoutTableColumns;
59 
65 typedef QVector<QgsLayoutTableColumn> QgsLayoutTableSortColumns;
66 
67 
68 
76 class CORE_EXPORT QgsLayoutTableStyle
77 {
78  public:
79 
81  QgsLayoutTableStyle() = default;
82 
84  bool enabled = false;
85 
87  QColor cellBackgroundColor = QColor( 255, 255, 255, 255 );
88 
95  bool writeXml( QDomElement &styleElem, QDomDocument &doc ) const;
96 
102  bool readXml( const QDomElement &styleElem );
103 
104 };
105 
112 class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
113 {
114  Q_OBJECT
115 
116  public:
117 
122  {
126  HeaderRight
127  };
128 
133  {
134  FirstFrame = 0,
136  NoHeaders
137  };
138 
143  {
144  HeadersOnly = 0,
146  ShowMessage
147  };
148 
153  {
154  TruncateText = 0,
155  WrapText
156  };
157 
162  {
171  LastRow
172  };
173 
177  QgsLayoutTable( QgsLayout *layout );
178 
179  ~QgsLayoutTable() override;
180 
185  void setCellMargin( double margin );
186 
191  double cellMargin() const { return mCellMargin; }
192 
197  void setEmptyTableBehavior( EmptyTableMode mode );
198 
204  EmptyTableMode emptyTableBehavior() const { return mEmptyTableMode; }
205 
213  void setEmptyTableMessage( const QString &message );
214 
222  QString emptyTableMessage() const { return mEmptyTableMessage; }
223 
229  void setShowEmptyRows( bool showEmpty );
230 
235  bool showEmptyRows() const { return mShowEmptyRows; }
236 
243  Q_DECL_DEPRECATED void setHeaderFont( const QFont &font ) SIP_DEPRECATED;
244 
251  Q_DECL_DEPRECATED QFont headerFont() const SIP_DEPRECATED;
252 
260  Q_DECL_DEPRECATED void setHeaderFontColor( const QColor &color ) SIP_DEPRECATED;
261 
269  Q_DECL_DEPRECATED QColor headerFontColor() const SIP_DEPRECATED;
270 
277  void setHeaderTextFormat( const QgsTextFormat &format );
278 
285  QgsTextFormat headerTextFormat() const;
286 
291  void setHeaderHAlignment( HeaderHAlignment alignment );
292 
297  HeaderHAlignment headerHAlignment() const { return mHeaderHAlignment; }
298 
304  void setHeaderMode( HeaderMode mode );
305 
311  HeaderMode headerMode() const { return mHeaderMode; }
312 
319  Q_DECL_DEPRECATED void setContentFont( const QFont &font ) SIP_DEPRECATED;
320 
327  Q_DECL_DEPRECATED QFont contentFont() const SIP_DEPRECATED;
328 
336  Q_DECL_DEPRECATED void setContentFontColor( const QColor &color ) SIP_DEPRECATED;
337 
345  Q_DECL_DEPRECATED QColor contentFontColor() const SIP_DEPRECATED;
346 
353  void setContentTextFormat( const QgsTextFormat &format );
354 
361  QgsTextFormat contentTextFormat() const;
362 
370  void setShowGrid( bool showGrid );
371 
378  bool showGrid() const { return mShowGrid; }
379 
386  void setGridStrokeWidth( double width );
387 
394  double gridStrokeWidth() const { return mGridStrokeWidth; }
395 
402  void setGridColor( const QColor &color );
403 
410  QColor gridColor() const { return mGridColor; }
411 
420  void setHorizontalGrid( bool horizontalGrid );
421 
429  bool horizontalGrid() const { return mHorizontalGrid; }
430 
439  void setVerticalGrid( bool verticalGrid );
440 
448  bool verticalGrid() const { return mVerticalGrid; }
449 
455  void setBackgroundColor( const QColor &color );
456 
462  QColor backgroundColor() const { return mBackgroundColor; }
463 
469  void setWrapBehavior( WrapBehavior behavior );
470 
476  WrapBehavior wrapBehavior() const { return mWrapBehavior; }
477 
482  QgsLayoutTableColumns &columns() { return mColumns; }
483 
489  void setColumns( const QgsLayoutTableColumns &columns );
490 
501  QgsLayoutTableSortColumns &sortColumns() { return mSortColumns; }
502 
513  void setSortColumns( const QgsLayoutTableSortColumns &sortColumns );
514 
519  void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
520 
525  const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
526 
533  virtual QMap<int, QString> headerLabels() const SIP_SKIP;
534 
540  virtual bool getTableContents( QgsLayoutTableContents &contents ) = 0;
541 
547  virtual QgsConditionalStyle conditionalCellStyle( int row, int column ) const;
548 
554  virtual QgsExpressionContextScope *scopeForCell( int row, int column ) const SIP_FACTORY;
555 
559  QgsLayoutTableContents &contents() { return mTableContents; }
560 
561  QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
562  QSizeF minFrameSize( int frameIndex = -1 ) const override;
563 
564  bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
565  bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
566  QSizeF totalSize() const override;
567  void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
568 
569  public slots:
570 
571  void refresh() override;
572 
578  virtual void refreshAttributes();
579 
580  void recalculateFrameSizes() override;
581 
582  protected:
584  double mCellMargin = 1.0;
585 
587  EmptyTableMode mEmptyTableMode = HeadersOnly;
588 
591 
593  bool mShowEmptyRows = false;
594 
595 
597  HeaderHAlignment mHeaderHAlignment = FollowColumn;
598 
600  HeaderMode mHeaderMode = FirstFrame;
601 
604 
606  bool mShowGrid = true;
607 
609  double mGridStrokeWidth = 0.5;
610 
612  QColor mGridColor = Qt::black;
613 
615  bool mHorizontalGrid = true;
616 
618  bool mVerticalGrid = true;
619 
621  QColor mBackgroundColor = Qt::white;
622 
625 
628 
631 
633  QMap<int, double> mMaxColumnWidthMap;
634 
636  QMap<int, double> mMaxRowHeightMap;
637 
638  QSizeF mTableSize;
639 
640  WrapBehavior mWrapBehavior = TruncateText;
641 
642  QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
643 
647  virtual bool calculateMaxColumnWidths();
648 
652  virtual bool calculateMaxRowHeights();
653 
659  //not const, as needs to call calculateMaxColumnWidths()
660  double totalWidth();
661 
666  //not const, as needs to call calculateMaxRowHeights()
667  double totalHeight();
668 
681  int rowsVisible( QgsRenderContext &context, double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
682 
693  int rowsVisible( QgsRenderContext &context, int frameIndex, int firstRow, bool includeEmptyRows ) const;
694 
701  QPair<int, int> rowRange( QgsRenderContext &context, int frameIndex ) const;
702 
712  void drawHorizontalGridLines( QgsLayoutItemRenderContext &context, int firstRow, int lastRow, bool drawHeaderLines ) const;
713 
729  void drawVerticalGridLines( QgsLayoutItemRenderContext &context, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
730 
734  void recalculateTableSize();
735 
742  bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;
743 
750  virtual QgsTextFormat textFormatForCell( int row, int column ) const;
751 
758  virtual QgsTextFormat textFormatForHeader( int column ) const;
759 
766  virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;
767 
774  virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;
775 
776  private:
777 
778  QMap< CellStyleGroup, QString > mCellStyleNames;
779 
781  void initStyles();
782 
789  QColor backgroundColor( int row, int column ) const;
790 
791  friend class TestQgsLayoutTable;
792  friend class TestQgsLayoutManualTable;
794 };
795 
796 #endif // QGSLAYOUTTABLE_H
QgsLayoutTable::headerMode
HeaderMode headerMode() const
Returns the display mode for headers in the table.
Definition: qgslayouttable.h:311
qgsconditionalstyle.h
QgsLayoutMultiFrame::totalSize
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe's content, in layout units.
QgsLayoutTable::backgroundColor
QColor backgroundColor() const
Returns the color used for the background of the table.
Definition: qgslayouttable.h:462
QgsLayoutTableColumns
QVector< QgsLayoutTableColumn > QgsLayoutTableColumns
List of column definitions for a QgsLayoutTable.
Definition: qgslayouttable.h:58
QgsLayoutTable::HeaderHAlignment
HeaderHAlignment
Controls how headers are horizontally aligned in a table.
Definition: qgslayouttable.h:121
QgsLayoutTable::mTableContents
QgsLayoutTableContents mTableContents
Contents to show in table.
Definition: qgslayouttable.h:630
QgsLayoutTable::mHeaderTextFormat
QgsTextFormat mHeaderTextFormat
Definition: qgslayouttable.h:602
QgsLayoutTableContents
QVector< QgsLayoutTableRow > QgsLayoutTableContents
List of QgsLayoutTableRows, representing rows and column cell contents for a QgsLayoutTable.
Definition: qgslayouttable.h:47
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsLayoutTable::mMaxColumnWidthMap
QMap< int, double > mMaxColumnWidthMap
Map of maximum width for each column.
Definition: qgslayouttable.h:633
QgsLayoutTable::emptyTableBehavior
EmptyTableMode emptyTableBehavior() const
Returns the behavior mode for empty tables.
Definition: qgslayouttable.h:204
QgsLayoutTable::CellStyleGroup
CellStyleGroup
Row or column groups for cell styling.
Definition: qgslayouttable.h:161
QgsLayoutTable::showEmptyRows
bool showEmptyRows() const
Returns whether empty rows are drawn in the table.
Definition: qgslayouttable.h:235
QgsLayoutTable::sortColumns
QgsLayoutTableSortColumns & sortColumns()
Returns a reference to the list of QgsLayoutTableSortColumns shown in the table.
Definition: qgslayouttable.h:501
QgsLayoutTable::columns
QgsLayoutTableColumns & columns()
Returns a reference to the list of QgsLayoutTableColumns shown in the table.
Definition: qgslayouttable.h:482
QgsLayoutItemRenderContext
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
qgslayoutmultiframe.h
QgsLayoutTable::mEmptyTableMessage
QString mEmptyTableMessage
String to show in empty tables.
Definition: qgslayouttable.h:590
QgsLayoutTable::gridStrokeWidth
double gridStrokeWidth() const
Returns the width of grid lines in the table in mm.
Definition: qgslayouttable.h:394
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsLayoutTable::OddColumns
@ OddColumns
Style odd numbered columns.
Definition: qgslayouttable.h:163
QgsLayoutTable::HideTable
@ HideTable
Hides entire table if empty.
Definition: qgslayouttable.h:145
QgsLayoutTable::FirstRow
@ FirstRow
Style first row only.
Definition: qgslayouttable.h:170
QgsLayoutTable::FirstColumn
@ FirstColumn
Style first column only.
Definition: qgslayouttable.h:167
QgsLayoutMultiFrame
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Definition: qgslayoutmultiframe.h:48
QgsConditionalStyle
Conditional styling for a rule.
Definition: qgsconditionalstyle.h:120
QgsLayoutTableSortColumns
QVector< QgsLayoutTableColumn > QgsLayoutTableSortColumns
List of column definitions for sorting a QgsLayoutTable.
Definition: qgslayouttable.h:65
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayoutTable::mSortColumns
QgsLayoutTableSortColumns mSortColumns
Columns to sort the table.
Definition: qgslayouttable.h:627
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsLayoutTable::HeaderRow
@ HeaderRow
Style header row.
Definition: qgslayouttable.h:169
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayoutTable::gridColor
QColor gridColor() const
Returns the color used for grid lines in the table.
Definition: qgslayouttable.h:410
QgsLayoutTable::verticalGrid
bool verticalGrid() const
Returns whether the grid's vertical lines are drawn in the table.
Definition: qgslayouttable.h:448
QgsCompositionConverter
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
Definition: qgscompositionconverter.h:56
QgsLayoutTable::OddRows
@ OddRows
Style odd numbered rows.
Definition: qgslayouttable.h:165
qgis_sip.h
QgsLayoutTable::mTableSize
QSizeF mTableSize
Definition: qgslayouttable.h:638
QgsLayoutMultiFrame::writePropertiesToElement
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores multiframe state within an XML DOM element.
Definition: qgslayoutmultiframe.cpp:573
QgsLayoutMultiFrame::minFrameSize
virtual QSizeF minFrameSize(int frameIndex=-1) const
Returns the minimum size for a frames, if desired.
Definition: qgslayoutmultiframe.cpp:45
QgsLayoutTable::HeaderMode
HeaderMode
Controls where headers are shown in the table.
Definition: qgslayouttable.h:132
QgsLayoutTable::WrapBehavior
WrapBehavior
Controls how long strings in the table are handled.
Definition: qgslayouttable.h:152
QgsLayoutTable
A class to display a table in the print layout, and allow the table to span over multiple frames.
Definition: qgslayouttable.h:112
QgsLayoutMultiFrame::render
virtual void render(QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex)=0
Renders a portion of the multiframe's content into a render context.
QgsLayoutTable::EvenRows
@ EvenRows
Style even numbered rows.
Definition: qgslayouttable.h:166
QgsLayoutTable::EmptyTableMode
EmptyTableMode
Controls how empty tables are displayed.
Definition: qgslayouttable.h:142
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
QgsLayoutTable::HeaderCenter
@ HeaderCenter
Align headers to center.
Definition: qgslayouttable.h:125
QgsLayoutMultiFrame::refresh
void refresh() override
Refreshes the multiframe, causing a recalculation of any property overrides.
Definition: qgslayoutmultiframe.cpp:338
QgsLayoutTableStyle
Styling option for a layout table cell.
Definition: qgslayouttable.h:76
QgsLayoutTable::LastColumn
@ LastColumn
Style last column only.
Definition: qgslayouttable.h:168
QgsLayoutTable::mCellStyles
QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles
Definition: qgslayouttable.h:642
QgsLayoutTable::horizontalGrid
bool horizontalGrid() const
Returns whether the grid's horizontal lines are drawn in the table.
Definition: qgslayouttable.h:429
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutMultiFrame::fixedFrameSize
virtual QSizeF fixedFrameSize(int frameIndex=-1) const
Returns the fixed size for a frame, if desired.
Definition: qgslayoutmultiframe.cpp:39
QgsLayoutTable::cellMargin
double cellMargin() const
Returns the margin distance between cell borders and their contents in mm.
Definition: qgslayouttable.h:191
QgsLayoutTable::mMaxRowHeightMap
QMap< int, double > mMaxRowHeightMap
Map of maximum height for each row.
Definition: qgslayouttable.h:636
QgsLayoutMultiFrame::recalculateFrameSizes
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of its component frames.
Definition: qgslayoutmultiframe.cpp:96
QgsLayoutMultiFrame::readPropertiesFromElement
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets multiframe state from a DOM element.
Definition: qgslayoutmultiframe.cpp:578
QgsLayoutTableRow
QVector< QVariant > QgsLayoutTableRow
List of QVariants, representing a the contents of a single row in a QgsLayoutTable.
Definition: qgslayouttable.h:30
QgsLayoutTable::mColumns
QgsLayoutTableColumns mColumns
Columns to show in table.
Definition: qgslayouttable.h:624
QgsLayoutTableColumn
Stores properties of a column for a QgsLayoutTable.
Definition: qgslayouttablecolumn.h:37
qgstextformat.h
QgsLayoutTable::FollowColumn
@ FollowColumn
Header uses the same alignment as the column.
Definition: qgslayouttable.h:123
QgsLayoutTable::emptyTableMessage
QString emptyTableMessage() const
Returns the message for empty tables with no content rows.
Definition: qgslayouttable.h:222
QgsLayoutTable::HeaderLeft
@ HeaderLeft
Align headers left.
Definition: qgslayouttable.h:124
QgsLayoutTable::mContentTextFormat
QgsTextFormat mContentTextFormat
Definition: qgslayouttable.h:603
QgsLayoutTable::AllFrames
@ AllFrames
Headers shown on all frames.
Definition: qgslayouttable.h:135
QgsLayoutTable::wrapBehavior
WrapBehavior wrapBehavior() const
Returns the wrap behavior for the table, which controls how text within cells is automatically wrappe...
Definition: qgslayouttable.h:476
QgsLayoutTable::EvenColumns
@ EvenColumns
Style even numbered columns.
Definition: qgslayouttable.h:164