QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 <QFont>
26 #include <QColor>
27 #include <QPair>
28 
30 
37 typedef QVector< QVariant > QgsLayoutTableRow;
38 
45 #ifndef SIP_RUN
46 typedef QVector< QgsLayoutTableRow > QgsLayoutTableContents;
47 #else
48 typedef QVector< QVector< QVariant > > QgsLayoutTableContents;
49 #endif
50 
51 
57 typedef QVector<QgsLayoutTableColumn> QgsLayoutTableColumns;
58 
64 typedef QVector<QgsLayoutTableColumn> QgsLayoutTableSortColumns;
65 
66 
67 
75 class CORE_EXPORT QgsLayoutTableStyle
76 {
77  public:
78 
80  QgsLayoutTableStyle() = default;
81 
83  bool enabled = false;
84 
86  QColor cellBackgroundColor = QColor( 255, 255, 255, 255 );
87 
94  bool writeXml( QDomElement &styleElem, QDomDocument &doc ) const;
95 
101  bool readXml( const QDomElement &styleElem );
102 
103 };
104 
111 class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
112 {
113  Q_OBJECT
114 
115  public:
116 
121  {
125  HeaderRight
126  };
127 
132  {
133  FirstFrame = 0,
135  NoHeaders
136  };
137 
142  {
143  HeadersOnly = 0,
145  ShowMessage
146  };
147 
152  {
153  TruncateText = 0,
154  WrapText
155  };
156 
161  {
170  LastRow
171  };
172 
176  QgsLayoutTable( QgsLayout *layout );
177 
178  ~QgsLayoutTable() override;
179 
184  void setCellMargin( double margin );
185 
190  double cellMargin() const { return mCellMargin; }
191 
196  void setEmptyTableBehavior( EmptyTableMode mode );
197 
203  EmptyTableMode emptyTableBehavior() const { return mEmptyTableMode; }
204 
212  void setEmptyTableMessage( const QString &message );
213 
221  QString emptyTableMessage() const { return mEmptyTableMessage; }
222 
228  void setShowEmptyRows( bool showEmpty );
229 
234  bool showEmptyRows() const { return mShowEmptyRows; }
235 
241  void setHeaderFont( const QFont &font );
242 
248  QFont headerFont() const { return mHeaderFont; }
249 
256  void setHeaderFontColor( const QColor &color );
257 
264  QColor headerFontColor() const { return mHeaderFontColor; }
265 
270  void setHeaderHAlignment( HeaderHAlignment alignment );
271 
276  HeaderHAlignment headerHAlignment() const { return mHeaderHAlignment; }
277 
283  void setHeaderMode( HeaderMode mode );
284 
290  HeaderMode headerMode() const { return mHeaderMode; }
291 
297  void setContentFont( const QFont &font );
298 
304  QFont contentFont() const { return mContentFont; }
305 
312  void setContentFontColor( const QColor &color );
313 
320  QColor contentFontColor() const { return mContentFontColor; }
321 
329  void setShowGrid( bool showGrid );
330 
337  bool showGrid() const { return mShowGrid; }
338 
345  void setGridStrokeWidth( double width );
346 
353  double gridStrokeWidth() const { return mGridStrokeWidth; }
354 
361  void setGridColor( const QColor &color );
362 
369  QColor gridColor() const { return mGridColor; }
370 
379  void setHorizontalGrid( bool horizontalGrid );
380 
388  bool horizontalGrid() const { return mHorizontalGrid; }
389 
398  void setVerticalGrid( bool verticalGrid );
399 
407  bool verticalGrid() const { return mVerticalGrid; }
408 
414  void setBackgroundColor( const QColor &color );
415 
421  QColor backgroundColor() const { return mBackgroundColor; }
422 
428  void setWrapBehavior( WrapBehavior behavior );
429 
435  WrapBehavior wrapBehavior() const { return mWrapBehavior; }
436 
441  QgsLayoutTableColumns &columns() { return mColumns; }
442 
448  void setColumns( const QgsLayoutTableColumns &columns );
449 
455  QgsLayoutTableSortColumns &sortColumns() { return mSortColumns; }
456 
463  void setSortColumns( const QgsLayoutTableSortColumns &sortColumns );
464 
469  void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
470 
475  const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
476 
483  virtual QMap<int, QString> headerLabels() const SIP_SKIP;
484 
490  virtual bool getTableContents( QgsLayoutTableContents &contents ) = 0;
491 
497  virtual QgsConditionalStyle conditionalCellStyle( int row, int column ) const;
498 
502  QgsLayoutTableContents &contents() { return mTableContents; }
503 
504  QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
505  QSizeF minFrameSize( int frameIndex = -1 ) const override;
506 
507  bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
508  bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
509  QSizeF totalSize() const override;
510  void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
511 
512  public slots:
513 
514  void refresh() override;
515 
521  virtual void refreshAttributes();
522 
523  void recalculateFrameSizes() override;
524 
525  protected:
527  double mCellMargin = 1.0;
528 
530  EmptyTableMode mEmptyTableMode = HeadersOnly;
531 
534 
536  bool mShowEmptyRows = false;
537 
539  QFont mHeaderFont;
540 
542  QColor mHeaderFontColor = Qt::black;
543 
545  HeaderHAlignment mHeaderHAlignment = FollowColumn;
546 
548  HeaderMode mHeaderMode = FirstFrame;
549 
552 
554  QColor mContentFontColor = Qt::black;
555 
557  bool mShowGrid = true;
558 
560  double mGridStrokeWidth = 0.5;
561 
563  QColor mGridColor = Qt::black;
564 
566  bool mHorizontalGrid = true;
567 
569  bool mVerticalGrid = true;
570 
572  QColor mBackgroundColor = Qt::white;
573 
576 
579 
582 
584  QMap<int, double> mMaxColumnWidthMap;
585 
587  QMap<int, double> mMaxRowHeightMap;
588 
589  QSizeF mTableSize;
590 
591  WrapBehavior mWrapBehavior = TruncateText;
592 
593  QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
594 
598  virtual bool calculateMaxColumnWidths();
599 
603  virtual bool calculateMaxRowHeights();
604 
610  //not const, as needs to call calculateMaxColumnWidths()
611  double totalWidth();
612 
617  //not const, as needs to call calculateMaxRowHeights()
618  double totalHeight();
619 
631  int rowsVisible( double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
632 
642  int rowsVisible( int frameIndex, int firstRow, bool includeEmptyRows ) const;
643 
649  QPair<int, int> rowRange( int frameIndex ) const;
650 
660  void drawHorizontalGridLines( QPainter *painter, int firstRow, int lastRow, bool drawHeaderLines ) const;
661 
677  void drawVerticalGridLines( QPainter *painter, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
678 
682  void recalculateTableSize();
683 
690  bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;
691 
692  private:
693 
694  QMap< CellStyleGroup, QString > mCellStyleNames;
695 
697  void initStyles();
698 
699  bool textRequiresWrapping( const QString &text, double columnWidth, const QFont &font ) const;
700 
701  QString wrappedText( const QString &value, double columnWidth, const QFont &font ) const;
702 
709  QColor backgroundColor( int row, int column ) const;
710 
711  friend class TestQgsLayoutTable;
712  friend class TestQgsLayoutManualTable;
714 };
715 
716 #endif // QGSLAYOUTTABLE_H
QgsLayoutTable::headerMode
HeaderMode headerMode() const
Returns the display mode for headers in the table.
Definition: qgslayouttable.h:290
qgsconditionalstyle.h
QgsLayoutMultiFrame::totalSize
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe's content, in layout units.
QgsLayoutTable::showGrid
bool showGrid() const
Returns whether grid lines are drawn in the table.
Definition: qgslayouttable.h:337
QgsLayoutTable::backgroundColor
QColor backgroundColor() const
Returns the color used for the background of the table.
Definition: qgslayouttable.h:421
QgsLayoutTableColumns
QVector< QgsLayoutTableColumn > QgsLayoutTableColumns
Definition: qgslayouttable.h:57
QgsLayoutTable::headerFontColor
QColor headerFontColor() const
Returns the color used to draw header text in the table.
Definition: qgslayouttable.h:264
QgsLayoutTable::HeaderHAlignment
HeaderHAlignment
Controls how headers are horizontally aligned in a table.
Definition: qgslayouttable.h:120
QgsLayoutTable::mTableContents
QgsLayoutTableContents mTableContents
Contents to show in table.
Definition: qgslayouttable.h:581
QgsLayoutTableContents
QVector< QgsLayoutTableRow > QgsLayoutTableContents
Definition: qgslayouttable.h:46
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsLayoutTable::mMaxColumnWidthMap
QMap< int, double > mMaxColumnWidthMap
Map of maximum width for each column.
Definition: qgslayouttable.h:584
QgsLayoutTable::emptyTableBehavior
EmptyTableMode emptyTableBehavior() const
Returns the behavior mode for empty tables.
Definition: qgslayouttable.h:203
QgsLayoutTable::CellStyleGroup
CellStyleGroup
Row or column groups for cell styling.
Definition: qgslayouttable.h:160
QgsLayoutTable::showEmptyRows
bool showEmptyRows() const
Returns whether empty rows are drawn in the table.
Definition: qgslayouttable.h:234
QgsLayoutTable::sortColumns
QgsLayoutTableSortColumns & sortColumns()
Returns a reference to the list of QgsLayoutTableSortColumns shown in the table.
Definition: qgslayouttable.h:455
QgsLayoutTable::columns
QgsLayoutTableColumns & columns()
Returns a reference to the list of QgsLayoutTableColumns shown in the table.
Definition: qgslayouttable.h:441
QgsLayoutItemRenderContext
Definition: qgslayoutitem.h:44
qgslayoutmultiframe.h
QgsLayoutTable::mEmptyTableMessage
QString mEmptyTableMessage
String to show in empty tables.
Definition: qgslayouttable.h:533
QgsLayoutTable::gridStrokeWidth
double gridStrokeWidth() const
Returns the width of grid lines in the table in mm.
Definition: qgslayouttable.h:353
QgsLayoutTable::mContentFont
QFont mContentFont
Table contents font.
Definition: qgslayouttable.h:551
QgsLayoutTable::OddColumns
@ OddColumns
Style odd numbered columns.
Definition: qgslayouttable.h:162
QgsLayoutTable::HideTable
@ HideTable
Hides entire table if empty.
Definition: qgslayouttable.h:144
QgsLayoutTable::FirstRow
@ FirstRow
Style first row only.
Definition: qgslayouttable.h:169
QgsLayoutTable::FirstColumn
@ FirstColumn
Style first column only.
Definition: qgslayouttable.h:166
QgsLayoutMultiFrame
Definition: qgslayoutmultiframe.h:48
QgsConditionalStyle
Definition: qgsconditionalstyle.h:112
QgsLayoutTableSortColumns
QVector< QgsLayoutTableColumn > QgsLayoutTableSortColumns
Definition: qgslayouttable.h:64
QgsLayoutTable::headerHAlignment
HeaderHAlignment headerHAlignment() const
Returns the horizontal alignment for table headers.
Definition: qgslayouttable.h:276
QgsLayoutTable::contentFontColor
QColor contentFontColor() const
Returns the color used to draw text in table body cells.
Definition: qgslayouttable.h:320
QgsLayoutTable::mSortColumns
QgsLayoutTableSortColumns mSortColumns
Columns to sort the table.
Definition: qgslayouttable.h:578
QgsLayoutTable::HeaderRow
@ HeaderRow
Style header row.
Definition: qgslayouttable.h:168
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:369
QgsLayoutTable::verticalGrid
bool verticalGrid() const
Returns whether the grid's vertical lines are drawn in the table.
Definition: qgslayouttable.h:407
QgsCompositionConverter
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
Definition: qgscompositionconverter.h:54
QgsLayoutTable::OddRows
@ OddRows
Style odd numbered rows.
Definition: qgslayouttable.h:164
qgis_sip.h
QgsLayoutTable::mTableSize
QSizeF mTableSize
Definition: qgslayouttable.h:589
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:131
QgsLayoutTable::WrapBehavior
WrapBehavior
Controls how long strings in the table are handled.
Definition: qgslayouttable.h:151
QgsLayoutTable
A class to display a table in the print layout, and allow the table to span over multiple frames.
Definition: qgslayouttable.h:111
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:165
QgsLayoutTable::EmptyTableMode
EmptyTableMode
Controls how empty tables are displayed.
Definition: qgslayouttable.h:141
QgsLayoutTable::HeaderCenter
@ HeaderCenter
Align headers to center.
Definition: qgslayouttable.h:124
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:75
QgsLayoutTable::LastColumn
@ LastColumn
Style last column only.
Definition: qgslayouttable.h:167
QgsLayoutTable::mCellStyles
QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles
Definition: qgslayouttable.h:593
QgsLayoutTable::horizontalGrid
bool horizontalGrid() const
Returns whether the grid's horizontal lines are drawn in the table.
Definition: qgslayouttable.h:388
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
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:190
QgsLayoutTable::mMaxRowHeightMap
QMap< int, double > mMaxRowHeightMap
Map of maximum height for each row.
Definition: qgslayouttable.h:587
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
Definition: qgslayouttable.h:29
QgsLayoutTable::mColumns
QgsLayoutTableColumns mColumns
Columns to show in table.
Definition: qgslayouttable.h:575
QgsLayoutTableColumn
Definition: qgslayouttablecolumn.h:36
QgsLayoutTable::FollowColumn
@ FollowColumn
Header uses the same alignment as the column.
Definition: qgslayouttable.h:122
QgsLayoutTable::emptyTableMessage
QString emptyTableMessage() const
Returns the message for empty tables with no content rows.
Definition: qgslayouttable.h:221
QgsLayoutTable::HeaderLeft
@ HeaderLeft
Align headers left.
Definition: qgslayouttable.h:123
QgsLayoutTable::AllFrames
@ AllFrames
Headers shown on all frames.
Definition: qgslayouttable.h:134
QgsLayoutTable::contentFont
QFont contentFont() const
Returns the font used to draw text in table body cells.
Definition: qgslayouttable.h:304
QgsLayoutTable::mHeaderFont
QFont mHeaderFont
Header font.
Definition: qgslayouttable.h:539
QgsLayoutTable::wrapBehavior
WrapBehavior wrapBehavior() const
Returns the wrap behavior for the table, which controls how text within cells is automatically wrappe...
Definition: qgslayouttable.h:435
QgsLayoutTable::EvenColumns
@ EvenColumns
Style even numbered columns.
Definition: qgslayouttable.h:163
QgsLayoutTable::headerFont
QFont headerFont() const
Returns the font used to draw header text in the table.
Definition: qgslayouttable.h:248