QGIS API Documentation 3.37.0-Master (c2d15952569)
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
78
80 bool enabled = false;
81
83 QColor cellBackgroundColor = QColor( 255, 255, 255, 255 );
84
91 bool writeXml( QDomElement &styleElem, QDomDocument &doc ) const;
92
98 bool readXml( const QDomElement &styleElem );
99
100};
101
107class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
108{
109 Q_OBJECT
110
111 public:
112
123
128 {
129 FirstFrame = 0,
131 NoHeaders
132 };
133
138 {
139 HeadersOnly = 0,
141 ShowMessage
142 };
143
148 {
149 TruncateText = 0,
150 WrapText
151 };
152
168
172 QgsLayoutTable( QgsLayout *layout );
173
174 ~QgsLayoutTable() override;
175
180 void setCellMargin( double margin );
181
186 double cellMargin() const { return mCellMargin; }
187
192 void setEmptyTableBehavior( EmptyTableMode mode );
193
199 EmptyTableMode emptyTableBehavior() const { return mEmptyTableMode; }
200
208 void setEmptyTableMessage( const QString &message );
209
217 QString emptyTableMessage() const { return mEmptyTableMessage; }
218
224 void setShowEmptyRows( bool showEmpty );
225
230 bool showEmptyRows() const { return mShowEmptyRows; }
231
238 Q_DECL_DEPRECATED void setHeaderFont( const QFont &font ) SIP_DEPRECATED;
239
246 Q_DECL_DEPRECATED QFont headerFont() const SIP_DEPRECATED;
247
255 Q_DECL_DEPRECATED void setHeaderFontColor( const QColor &color ) SIP_DEPRECATED;
256
264 Q_DECL_DEPRECATED QColor headerFontColor() const SIP_DEPRECATED;
265
272 void setHeaderTextFormat( const QgsTextFormat &format );
273
280 QgsTextFormat headerTextFormat() const;
281
286 void setHeaderHAlignment( HeaderHAlignment alignment );
287
292 HeaderHAlignment headerHAlignment() const { return mHeaderHAlignment; }
293
299 void setHeaderMode( HeaderMode mode );
300
306 HeaderMode headerMode() const { return mHeaderMode; }
307
314 Q_DECL_DEPRECATED void setContentFont( const QFont &font ) SIP_DEPRECATED;
315
322 Q_DECL_DEPRECATED QFont contentFont() const SIP_DEPRECATED;
323
331 Q_DECL_DEPRECATED void setContentFontColor( const QColor &color ) SIP_DEPRECATED;
332
340 Q_DECL_DEPRECATED QColor contentFontColor() const SIP_DEPRECATED;
341
348 void setContentTextFormat( const QgsTextFormat &format );
349
356 QgsTextFormat contentTextFormat() const;
357
365 void setShowGrid( bool showGrid );
366
373 bool showGrid() const { return mShowGrid; }
374
381 void setGridStrokeWidth( double width );
382
389 double gridStrokeWidth() const { return mGridStrokeWidth; }
390
397 void setGridColor( const QColor &color );
398
405 QColor gridColor() const { return mGridColor; }
406
415 void setHorizontalGrid( bool horizontalGrid );
416
424 bool horizontalGrid() const { return mHorizontalGrid; }
425
434 void setVerticalGrid( bool verticalGrid );
435
443 bool verticalGrid() const { return mVerticalGrid; }
444
450 void setBackgroundColor( const QColor &color );
451
457 QColor backgroundColor() const { return mBackgroundColor; }
458
464 void setWrapBehavior( WrapBehavior behavior );
465
471 WrapBehavior wrapBehavior() const { return mWrapBehavior; }
472
477 QgsLayoutTableColumns &columns() { return mColumns; }
478
484 void setColumns( const QgsLayoutTableColumns &columns );
485
496 QgsLayoutTableSortColumns &sortColumns() { return mSortColumns; }
497
508 void setSortColumns( const QgsLayoutTableSortColumns &sortColumns );
509
514 void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
515
520 const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
521
528 virtual QMap<int, QString> headerLabels() const SIP_SKIP;
529
535 virtual bool getTableContents( QgsLayoutTableContents &contents ) = 0;
536
542 virtual QgsConditionalStyle conditionalCellStyle( int row, int column ) const;
543
549 virtual QgsExpressionContextScope *scopeForCell( int row, int column ) const SIP_FACTORY;
550
554 QgsLayoutTableContents &contents() { return mTableContents; }
555
556 QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
557 QSizeF minFrameSize( int frameIndex = -1 ) const override;
558
559 bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
560 bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
561 QSizeF totalSize() const override;
562 void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
563
564 public slots:
565
566 void refresh() override;
567
573 virtual void refreshAttributes();
574
575 void recalculateFrameSizes() override;
576
577 protected:
579 double mCellMargin = 1.0;
580
582 EmptyTableMode mEmptyTableMode = HeadersOnly;
583
586
588 bool mShowEmptyRows = false;
589
590
592 HeaderHAlignment mHeaderHAlignment = FollowColumn;
593
595 HeaderMode mHeaderMode = FirstFrame;
596
599
601 bool mShowGrid = true;
602
604 double mGridStrokeWidth = 0.5;
605
607 QColor mGridColor = Qt::black;
608
610 bool mHorizontalGrid = true;
611
613 bool mVerticalGrid = true;
614
616 QColor mBackgroundColor = Qt::white;
617
620
623
626
628 QMap<int, double> mMaxColumnWidthMap;
629
631 QMap<int, double> mMaxRowHeightMap;
632
634
635 WrapBehavior mWrapBehavior = TruncateText;
636
637 QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
638
642 virtual bool calculateMaxColumnWidths();
643
647 virtual bool calculateMaxRowHeights();
648
654 //not const, as needs to call calculateMaxColumnWidths()
655 double totalWidth();
656
661 //not const, as needs to call calculateMaxRowHeights()
662 double totalHeight();
663
676 int rowsVisible( QgsRenderContext &context, double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
677
688 int rowsVisible( QgsRenderContext &context, int frameIndex, int firstRow, bool includeEmptyRows ) const;
689
696 QPair<int, int> rowRange( QgsRenderContext &context, int frameIndex ) const;
697
707 void drawHorizontalGridLines( QgsLayoutItemRenderContext &context, int firstRow, int lastRow, bool drawHeaderLines ) const;
708
724 void drawVerticalGridLines( QgsLayoutItemRenderContext &context, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
725
729 void recalculateTableSize();
730
737 bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;
738
745 virtual QgsTextFormat textFormatForCell( int row, int column ) const;
746
753 virtual QgsTextFormat textFormatForHeader( int column ) const;
754
761 virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;
762
769 virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;
770
771 private:
772
773 QMap< CellStyleGroup, QString > mCellStyleNames;
774
776 void initStyles();
777
784 QColor backgroundColor( int row, int column ) const;
785
786 friend class TestQgsLayoutTable;
787 friend class TestQgsLayoutManualTable;
789};
790
791#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
Constructor for QgsLayoutTableStyle.
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