QGIS API Documentation 3.43.0-Master (c67cf405802)
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
37typedef QVector< QVariant > QgsLayoutTableRow;
38
44#ifndef SIP_RUN
45typedef QVector< QgsLayoutTableRow > QgsLayoutTableContents;
46#else
47typedef QVector< QVector< QVariant > > QgsLayoutTableContents;
48#endif
49
50
55typedef QVector<QgsLayoutTableColumn> QgsLayoutTableColumns;
56
62typedef QVector<QgsLayoutTableColumn> QgsLayoutTableSortColumns;
63
64
65
71class CORE_EXPORT QgsLayoutTableStyle
72{
73 public:
74
76
78 bool enabled = false;
79
81 QColor cellBackgroundColor = QColor( 255, 255, 255, 255 );
82
89 bool writeXml( QDomElement &styleElem, QDomDocument &doc ) const;
90
96 bool readXml( const QDomElement &styleElem );
97
98};
99
105class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
106{
107 Q_OBJECT
108
109 public:
110
121
126 {
127 FirstFrame = 0,
129 NoHeaders
130 };
131
136 {
137 HeadersOnly = 0,
139 ShowMessage
140 };
141
146 {
147 TruncateText = 0,
148 WrapText
149 };
150
166
170 QgsLayoutTable( QgsLayout *layout );
171
172 ~QgsLayoutTable() override;
173
178 void setCellMargin( double margin );
179
184 double cellMargin() const { return mCellMargin; }
185
190 void setEmptyTableBehavior( EmptyTableMode mode );
191
197 EmptyTableMode emptyTableBehavior() const { return mEmptyTableMode; }
198
206 void setEmptyTableMessage( const QString &message );
207
215 QString emptyTableMessage() const { return mEmptyTableMessage; }
216
222 void setShowEmptyRows( bool showEmpty );
223
228 bool showEmptyRows() const { return mShowEmptyRows; }
229
236 Q_DECL_DEPRECATED void setHeaderFont( const QFont &font ) SIP_DEPRECATED;
237
244 Q_DECL_DEPRECATED QFont headerFont() const SIP_DEPRECATED;
245
253 Q_DECL_DEPRECATED void setHeaderFontColor( const QColor &color ) SIP_DEPRECATED;
254
262 Q_DECL_DEPRECATED QColor headerFontColor() const SIP_DEPRECATED;
263
270 void setHeaderTextFormat( const QgsTextFormat &format );
271
278 QgsTextFormat headerTextFormat() const;
279
284 void setHeaderHAlignment( HeaderHAlignment alignment );
285
290 HeaderHAlignment headerHAlignment() const { return mHeaderHAlignment; }
291
297 void setHeaderMode( HeaderMode mode );
298
304 HeaderMode headerMode() const { return mHeaderMode; }
305
312 Q_DECL_DEPRECATED void setContentFont( const QFont &font ) SIP_DEPRECATED;
313
320 Q_DECL_DEPRECATED QFont contentFont() const SIP_DEPRECATED;
321
329 Q_DECL_DEPRECATED void setContentFontColor( const QColor &color ) SIP_DEPRECATED;
330
338 Q_DECL_DEPRECATED QColor contentFontColor() const SIP_DEPRECATED;
339
346 void setContentTextFormat( const QgsTextFormat &format );
347
354 QgsTextFormat contentTextFormat() const;
355
363 void setShowGrid( bool showGrid );
364
371 bool showGrid() const { return mShowGrid; }
372
379 void setGridStrokeWidth( double width );
380
387 double gridStrokeWidth() const { return mGridStrokeWidth; }
388
395 void setGridColor( const QColor &color );
396
403 QColor gridColor() const { return mGridColor; }
404
413 void setHorizontalGrid( bool horizontalGrid );
414
422 bool horizontalGrid() const { return mHorizontalGrid; }
423
432 void setVerticalGrid( bool verticalGrid );
433
441 bool verticalGrid() const { return mVerticalGrid; }
442
448 void setBackgroundColor( const QColor &color );
449
455 QColor backgroundColor() const { return mBackgroundColor; }
456
462 void setWrapBehavior( WrapBehavior behavior );
463
469 WrapBehavior wrapBehavior() const { return mWrapBehavior; }
470
475 QgsLayoutTableColumns &columns() { return mColumns; }
476
482 void setColumns( const QgsLayoutTableColumns &columns );
483
494 QgsLayoutTableSortColumns &sortColumns() { return mSortColumns; }
495
506 void setSortColumns( const QgsLayoutTableSortColumns &sortColumns );
507
512 void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
513
518 const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
519
526 virtual QMap<int, QString> headerLabels() const SIP_SKIP;
527
533 virtual bool getTableContents( QgsLayoutTableContents &contents ) = 0;
534
540 virtual QgsConditionalStyle conditionalCellStyle( int row, int column ) const;
541
547 virtual QgsExpressionContextScope *scopeForCell( int row, int column ) const SIP_FACTORY;
548
555 virtual int rowSpan( int row, int column ) const;
556
563 virtual int columnSpan( int row, int column ) const;
564
568 QgsLayoutTableContents &contents() { return mTableContents; }
569
570 QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
571 QSizeF minFrameSize( int frameIndex = -1 ) const override;
572
573 bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
574 bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
575 QSizeF totalSize() const override;
576 void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
577
578 public slots:
579
580 void refresh() override;
581
587 virtual void refreshAttributes();
588
589 void recalculateFrameSizes() override;
590
591 protected:
593 double mCellMargin = 1.0;
594
596 EmptyTableMode mEmptyTableMode = HeadersOnly;
597
600
602 bool mShowEmptyRows = false;
603
604
606 HeaderHAlignment mHeaderHAlignment = FollowColumn;
607
609 HeaderMode mHeaderMode = FirstFrame;
610
613
615 bool mShowGrid = true;
616
618 double mGridStrokeWidth = 0.5;
619
621 QColor mGridColor = Qt::black;
622
624 bool mHorizontalGrid = true;
625
627 bool mVerticalGrid = true;
628
630 QColor mBackgroundColor = Qt::white;
631
634
637
640
642 QMap<int, double> mMaxColumnWidthMap;
643
645 QMap<int, double> mMaxRowHeightMap;
646
648
649 WrapBehavior mWrapBehavior = TruncateText;
650
651 QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
652
656 virtual bool calculateMaxColumnWidths();
657
661 virtual bool calculateMaxRowHeights();
662
668 //not const, as needs to call calculateMaxColumnWidths()
669 double totalWidth();
670
675 //not const, as needs to call calculateMaxRowHeights()
676 double totalHeight();
677
690 int rowsVisible( QgsRenderContext &context, double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
691
702 int rowsVisible( QgsRenderContext &context, int frameIndex, int firstRow, bool includeEmptyRows ) const;
703
710 QPair<int, int> rowRange( QgsRenderContext &context, int frameIndex ) const;
711
721 void drawHorizontalGridLines( QgsLayoutItemRenderContext &context, int firstRow, int lastRow, bool drawHeaderLines ) const;
722
738 void drawVerticalGridLines( QgsLayoutItemRenderContext &context, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
739
743 void recalculateTableSize();
744
751 bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;
752
759 virtual QgsTextFormat textFormatForCell( int row, int column ) const;
760
767 virtual QgsTextFormat textFormatForHeader( int column ) const;
768
775 virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;
776
783 virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;
784
785 private:
786
787 QMap< CellStyleGroup, QString > mCellStyleNames;
788
790 void initStyles();
791
800 QColor backgroundColor( int row, int column, int rowSpan = 1, int columnSpan = 1 ) const;
801
802 friend class TestQgsLayoutTable;
803 friend class TestQgsLayoutManualTable;
805};
806
807#endif // QGSLAYOUTTABLE_H
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.
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
Displays a table in the print layout, and allows the table to span over multiple frames.
QgsLayoutTableColumns & columns()
Returns a reference to the list of QgsLayoutTableColumns shown in the table.
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.
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.
QgsLayoutTableSortColumns & sortColumns()
Returns a reference to the list of QgsLayoutTableSortColumns shown in the table.
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
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:49
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
Container for all settings relating to text rendering.
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