QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
780  QColor backgroundColor( int row, int column ) const;
781 
782  friend class TestQgsLayoutTable;
783  friend class TestQgsLayoutManualTable;
785 };
786 
787 #endif // QGSLAYOUTTABLE_H
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
Conditional styling for a rule.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:45
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Stores properties of a column for a QgsLayoutTable.
Styling option for a layout table cell.
QgsLayoutTableStyle()=default
Constructor for QgsLayoutTableStyle.
A class to display a table in the print layout, and allow the table to span over multiple frames.
HeaderMode headerMode() const
Returns the display mode for headers in the table.
QColor backgroundColor() const
Returns the color used for the background of the table.
EmptyTableMode emptyTableBehavior() const
Returns the behavior mode for empty tables.
QgsLayoutTableColumns & columns()
Returns a reference to the list of QgsLayoutTableColumns shown in the table.
QMap< int, double > mMaxColumnWidthMap
Map of maximum width for each column.
bool horizontalGrid() const
Returns whether the grid's horizontal lines are drawn in the table.
double cellMargin() const
Returns the margin distance between cell borders and their contents in mm.
QString emptyTableMessage() const
Returns the message for empty tables with no content rows.
QString mEmptyTableMessage
String to show in empty tables.
bool showEmptyRows() const
Returns whether empty rows are drawn in the table.
bool verticalGrid() const
Returns whether the grid's vertical lines are drawn in the table.
CellStyleGroup
Row or column groups for cell styling.
@ FirstRow
Style first row only.
@ EvenColumns
Style even numbered columns.
@ EvenRows
Style even numbered rows.
@ HeaderRow
Style header row.
@ OddColumns
Style odd numbered columns.
@ FirstColumn
Style first column only.
@ LastColumn
Style last column only.
@ OddRows
Style odd numbered rows.
QgsTextFormat mHeaderTextFormat
QColor gridColor() const
Returns the color used for grid lines in the table.
HeaderMode
Controls where headers are shown in the table.
@ AllFrames
Headers shown on all frames.
QgsLayoutTableColumns mColumns
Columns to show in table.
QgsTextFormat mContentTextFormat
QgsLayoutTableSortColumns & sortColumns()
Returns a reference to the list of QgsLayoutTableSortColumns shown in the table.
WrapBehavior wrapBehavior() const
Returns the wrap behavior for the table, which controls how text within cells is automatically wrappe...
QgsLayoutTableContents mTableContents
Contents to show in table.
double gridStrokeWidth() const
Returns the width of grid lines in the table in mm.
QgsLayoutTableSortColumns mSortColumns
Columns to sort the table.
WrapBehavior
Controls how long strings in the table are handled.
HeaderHAlignment
Controls how headers are horizontally aligned in a table.
@ HeaderLeft
Align headers left.
@ HeaderCenter
Align headers to center.
@ FollowColumn
Header uses the same alignment as the column.
EmptyTableMode
Controls how empty tables are displayed.
@ HideTable
Hides entire table if empty.
QMap< int, double > mMaxRowHeightMap
Map of maximum height for each row.
QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
QVector< QgsLayoutTableColumn > QgsLayoutTableColumns
List of column definitions for a QgsLayoutTable.
QVector< QgsLayoutTableColumn > QgsLayoutTableSortColumns
List of column definitions for sorting a QgsLayoutTable.
QVector< QgsLayoutTableRow > QgsLayoutTableContents
List of QgsLayoutTableRows, representing rows and column cell contents for a QgsLayoutTable.
QVector< QVariant > QgsLayoutTableRow
List of QVariants, representing a the contents of a single row in a QgsLayoutTable.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76