QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
496  QgsLayoutTableSortColumns &sortColumns() { return mSortColumns; }
497 
504  void setSortColumns( const QgsLayoutTableSortColumns &sortColumns );
505 
510  void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
511 
516  const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
517 
524  virtual QMap<int, QString> headerLabels() const SIP_SKIP;
525 
531  virtual bool getTableContents( QgsLayoutTableContents &contents ) = 0;
532 
538  virtual QgsConditionalStyle conditionalCellStyle( int row, int column ) const;
539 
545  virtual QgsExpressionContextScope *scopeForCell( int row, int column ) const SIP_FACTORY;
546 
550  QgsLayoutTableContents &contents() { return mTableContents; }
551 
552  QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
553  QSizeF minFrameSize( int frameIndex = -1 ) const override;
554 
555  bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
556  bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
557  QSizeF totalSize() const override;
558  void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
559 
560  public slots:
561 
562  void refresh() override;
563 
569  virtual void refreshAttributes();
570 
571  void recalculateFrameSizes() override;
572 
573  protected:
575  double mCellMargin = 1.0;
576 
578  EmptyTableMode mEmptyTableMode = HeadersOnly;
579 
582 
584  bool mShowEmptyRows = false;
585 
586 
588  HeaderHAlignment mHeaderHAlignment = FollowColumn;
589 
591  HeaderMode mHeaderMode = FirstFrame;
592 
595 
597  bool mShowGrid = true;
598 
600  double mGridStrokeWidth = 0.5;
601 
603  QColor mGridColor = Qt::black;
604 
606  bool mHorizontalGrid = true;
607 
609  bool mVerticalGrid = true;
610 
612  QColor mBackgroundColor = Qt::white;
613 
616 
619 
622 
624  QMap<int, double> mMaxColumnWidthMap;
625 
627  QMap<int, double> mMaxRowHeightMap;
628 
629  QSizeF mTableSize;
630 
631  WrapBehavior mWrapBehavior = TruncateText;
632 
633  QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
634 
638  virtual bool calculateMaxColumnWidths();
639 
643  virtual bool calculateMaxRowHeights();
644 
650  //not const, as needs to call calculateMaxColumnWidths()
651  double totalWidth();
652 
657  //not const, as needs to call calculateMaxRowHeights()
658  double totalHeight();
659 
672  int rowsVisible( QgsRenderContext &context, double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
673 
684  int rowsVisible( QgsRenderContext &context, int frameIndex, int firstRow, bool includeEmptyRows ) const;
685 
692  QPair<int, int> rowRange( QgsRenderContext &context, int frameIndex ) const;
693 
703  void drawHorizontalGridLines( QgsLayoutItemRenderContext &context, int firstRow, int lastRow, bool drawHeaderLines ) const;
704 
720  void drawVerticalGridLines( QgsLayoutItemRenderContext &context, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
721 
725  void recalculateTableSize();
726 
733  bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;
734 
741  virtual QgsTextFormat textFormatForCell( int row, int column ) const;
742 
749  virtual QgsTextFormat textFormatForHeader( int column ) const;
750 
757  virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;
758 
765  virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;
766 
767  private:
768 
769  QMap< CellStyleGroup, QString > mCellStyleNames;
770 
772  void initStyles();
773 
774  bool textRequiresWrapping( QgsRenderContext &context, const QString &text, double columnWidth, const QgsTextFormat &format ) const;
775 
776  QStringList wrappedText( QgsRenderContext &context, const QString &value, double columnWidth, const QgsTextFormat &format ) const;
777 
784  QColor backgroundColor( int row, int column ) const;
785 
786  friend class TestQgsLayoutTable;
787  friend class TestQgsLayoutManualTable;
789 };
790 
791 #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:122
QgsLayoutTable::mTableContents
QgsLayoutTableContents mTableContents
Contents to show in table.
Definition: qgslayouttable.h:621
QgsLayoutTable::mHeaderTextFormat
QgsTextFormat mHeaderTextFormat
Definition: qgslayouttable.h:593
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:35
QgsLayoutTable::mMaxColumnWidthMap
QMap< int, double > mMaxColumnWidthMap
Map of maximum width for each column.
Definition: qgslayouttable.h:624
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:162
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:496
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:45
qgslayoutmultiframe.h
QgsLayoutTable::mEmptyTableMessage
QString mEmptyTableMessage
String to show in empty tables.
Definition: qgslayouttable.h:581
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:58
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:49
QgsConditionalStyle
Conditional styling for a rule.
Definition: qgsconditionalstyle.h:113
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:618
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:55
QgsLayoutTable::OddRows
@ OddRows
Style odd numbered rows.
Definition: qgslayouttable.h:165
qgis_sip.h
QgsLayoutTable::mTableSize
QSizeF mTableSize
Definition: qgslayouttable.h:629
QgsLayoutMultiFrame::writePropertiesToElement
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores multiframe state within an XML DOM element.
Definition: qgslayoutmultiframe.cpp:567
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:133
QgsLayoutTable::WrapBehavior
WrapBehavior
Controls how long strings in the table are handled.
Definition: qgslayouttable.h:153
QgsLayoutTable
A class to display a table in the print layout, and allow the table to span over multiple frames.
Definition: qgslayouttable.h:113
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:143
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext.
Definition: qgsexpressioncontext.h:112
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::QgsLayoutTableStyle
QgsLayoutTableStyle()=default
Constructor for QgsLayoutTableStyle.
QgsLayoutTableStyle
Styling option for a layout table cell.
Definition: qgslayouttable.h:77
QgsLayoutTable::LastColumn
@ LastColumn
Style last column only.
Definition: qgslayouttable.h:168
QgsLayoutTable::mCellStyles
QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles
Definition: qgslayouttable.h:633
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:627
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:572
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:615
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:594
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