QGIS API Documentation  3.27.0-Master (e113457133)
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
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