QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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
72class CORE_EXPORT QgsLayoutTableStyle
73{
74 public:
75
77
79 bool enabled = false;
80
82 QColor cellBackgroundColor = QColor( 255, 255, 255, 255 );
83
90 bool writeXml( QDomElement &styleElem, QDomDocument &doc ) const;
91
97 bool readXml( const QDomElement &styleElem );
98
99};
100
106class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
107{
108 Q_OBJECT
109
110 public:
111
122
127 {
128 FirstFrame = 0,
130 NoHeaders
131 };
132
137 {
138 HeadersOnly = 0,
140 ShowMessage
141 };
142
147 {
148 TruncateText = 0,
149 WrapText
150 };
151
167
171 QgsLayoutTable( QgsLayout *layout );
172
173 ~QgsLayoutTable() override;
174
179 void setCellMargin( double margin );
180
185 double cellMargin() const { return mCellMargin; }
186
191 void setEmptyTableBehavior( EmptyTableMode mode );
192
198 EmptyTableMode emptyTableBehavior() const { return mEmptyTableMode; }
199
207 void setEmptyTableMessage( const QString &message );
208
216 QString emptyTableMessage() const { return mEmptyTableMessage; }
217
223 void setShowEmptyRows( bool showEmpty );
224
229 bool showEmptyRows() const { return mShowEmptyRows; }
230
237 Q_DECL_DEPRECATED void setHeaderFont( const QFont &font ) SIP_DEPRECATED;
238
245 Q_DECL_DEPRECATED QFont headerFont() const SIP_DEPRECATED;
246
254 Q_DECL_DEPRECATED void setHeaderFontColor( const QColor &color ) SIP_DEPRECATED;
255
263 Q_DECL_DEPRECATED QColor headerFontColor() const SIP_DEPRECATED;
264
271 void setHeaderTextFormat( const QgsTextFormat &format );
272
279 QgsTextFormat headerTextFormat() const;
280
285 void setHeaderHAlignment( HeaderHAlignment alignment );
286
291 HeaderHAlignment headerHAlignment() const { return mHeaderHAlignment; }
292
298 void setHeaderMode( HeaderMode mode );
299
305 HeaderMode headerMode() const { return mHeaderMode; }
306
313 Q_DECL_DEPRECATED void setContentFont( const QFont &font ) SIP_DEPRECATED;
314
321 Q_DECL_DEPRECATED QFont contentFont() const SIP_DEPRECATED;
322
330 Q_DECL_DEPRECATED void setContentFontColor( const QColor &color ) SIP_DEPRECATED;
331
339 Q_DECL_DEPRECATED QColor contentFontColor() const SIP_DEPRECATED;
340
347 void setContentTextFormat( const QgsTextFormat &format );
348
355 QgsTextFormat contentTextFormat() const;
356
364 void setShowGrid( bool showGrid );
365
372 bool showGrid() const { return mShowGrid; }
373
380 void setGridStrokeWidth( double width );
381
388 double gridStrokeWidth() const { return mGridStrokeWidth; }
389
396 void setGridColor( const QColor &color );
397
404 QColor gridColor() const { return mGridColor; }
405
414 void setHorizontalGrid( bool horizontalGrid );
415
423 bool horizontalGrid() const { return mHorizontalGrid; }
424
433 void setVerticalGrid( bool verticalGrid );
434
442 bool verticalGrid() const { return mVerticalGrid; }
443
449 void setBackgroundColor( const QColor &color );
450
456 QColor backgroundColor() const { return mBackgroundColor; }
457
463 void setWrapBehavior( WrapBehavior behavior );
464
470 WrapBehavior wrapBehavior() const { return mWrapBehavior; }
471
476 QgsLayoutTableColumns &columns() { return mColumns; }
477
483 void setColumns( const QgsLayoutTableColumns &columns );
484
495 QgsLayoutTableSortColumns &sortColumns() { return mSortColumns; }
496
507 void setSortColumns( const QgsLayoutTableSortColumns &sortColumns );
508
513 void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
514
519 const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
520
527 virtual QMap<int, QString> headerLabels() const SIP_SKIP;
528
534 virtual bool getTableContents( QgsLayoutTableContents &contents ) = 0;
535
541 virtual QgsConditionalStyle conditionalCellStyle( int row, int column ) const;
542
548 virtual QgsExpressionContextScope *scopeForCell( int row, int column ) const SIP_FACTORY;
549
556 virtual int rowSpan( int row, int column ) const;
557
564 virtual int columnSpan( int row, int column ) const;
565
569 QgsLayoutTableContents &contents() { return mTableContents; }
570
571 QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
572 QSizeF minFrameSize( int frameIndex = -1 ) const override;
573
574 bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
575 bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
576 QSizeF totalSize() const override;
577 void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
578
579 public slots:
580
581 void refresh() override;
582
588 virtual void refreshAttributes();
589
590 void recalculateFrameSizes() override;
591
592 protected:
594 double mCellMargin = 1.0;
595
597 EmptyTableMode mEmptyTableMode = HeadersOnly;
598
601
603 bool mShowEmptyRows = false;
604
605
607 HeaderHAlignment mHeaderHAlignment = FollowColumn;
608
610 HeaderMode mHeaderMode = FirstFrame;
611
614
616 bool mShowGrid = true;
617
619 double mGridStrokeWidth = 0.5;
620
622 QColor mGridColor = Qt::black;
623
625 bool mHorizontalGrid = true;
626
628 bool mVerticalGrid = true;
629
631 QColor mBackgroundColor = Qt::white;
632
635
638
641
643 QMap<int, double> mMaxColumnWidthMap;
644
646 QMap<int, double> mMaxRowHeightMap;
647
649
650 WrapBehavior mWrapBehavior = TruncateText;
651
652 QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
653
657 virtual bool calculateMaxColumnWidths();
658
662 virtual bool calculateMaxRowHeights();
663
669 //not const, as needs to call calculateMaxColumnWidths()
670 double totalWidth();
671
676 //not const, as needs to call calculateMaxRowHeights()
677 double totalHeight();
678
691 int rowsVisible( QgsRenderContext &context, double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
692
703 int rowsVisible( QgsRenderContext &context, int frameIndex, int firstRow, bool includeEmptyRows ) const;
704
711 QPair<int, int> rowRange( QgsRenderContext &context, int frameIndex ) const;
712
722 void drawHorizontalGridLines( QgsLayoutItemRenderContext &context, int firstRow, int lastRow, bool drawHeaderLines ) const;
723
739 void drawVerticalGridLines( QgsLayoutItemRenderContext &context, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
740
744 void recalculateTableSize();
745
752 bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;
753
760 virtual QgsTextFormat textFormatForCell( int row, int column ) const;
761
768 virtual QgsTextFormat textFormatForHeader( int column ) const;
769
776 virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;
777
784 virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;
785
786 private:
787
788 QMap< CellStyleGroup, QString > mCellStyleNames;
789
791 void initStyles();
792
801 QColor backgroundColor( int row, int column, int rowSpan = 1, int columnSpan = 1 ) const;
802
803 friend class TestQgsLayoutTable;
804 friend class TestQgsLayoutManualTable;
806};
807
808#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.
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
A class to display a table in the print layout, and allow 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
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.
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