QGIS API Documentation 4.1.0-Master (60fea48833c)
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 "qgsconditionalstyle.h"
24#include "qgslayoutmultiframe.h"
25#include "qgstextformat.h"
26
27#include <QColor>
28#include <QFont>
29#include <QPair>
30
32
38typedef QVector< QVariant > QgsLayoutTableRow;
39
45#ifndef SIP_RUN
46typedef QVector< QgsLayoutTableRow > QgsLayoutTableContents;
47#else
48typedef QVector< QVector< QVariant > > QgsLayoutTableContents;
49#endif
50
51
56typedef QVector<QgsLayoutTableColumn> QgsLayoutTableColumns;
57
63typedef QVector<QgsLayoutTableColumn> QgsLayoutTableSortColumns;
64
65
71class CORE_EXPORT QgsLayoutTableStyle
72{
73 public:
75
77 bool enabled = false;
78
80 QColor cellBackgroundColor = QColor( 255, 255, 255, 255 );
81
88 bool writeXml( QDomElement &styleElem, QDomDocument &doc ) const;
89
95 bool readXml( const QDomElement &styleElem );
96};
97
103class CORE_EXPORT QgsLayoutTable : public QgsLayoutMultiFrame
104{
105 Q_OBJECT
106
107 public:
118
128
138
147
163
167 QgsLayoutTable( QgsLayout *layout );
168
169 ~QgsLayoutTable() override;
170
175 void setCellMargin( double margin );
176
181 double cellMargin() const { return mCellMargin; }
182
187 void setEmptyTableBehavior( EmptyTableMode mode );
188
195
203 void setEmptyTableMessage( const QString &message );
204
212 QString emptyTableMessage() const { return mEmptyTableMessage; }
213
219 void setShowEmptyRows( bool showEmpty );
220
225 bool showEmptyRows() const { return mShowEmptyRows; }
226
233 Q_DECL_DEPRECATED void setHeaderFont( const QFont &font ) SIP_DEPRECATED;
234
241 Q_DECL_DEPRECATED QFont headerFont() const SIP_DEPRECATED;
242
250 Q_DECL_DEPRECATED void setHeaderFontColor( const QColor &color ) SIP_DEPRECATED;
251
259 Q_DECL_DEPRECATED QColor headerFontColor() const SIP_DEPRECATED;
260
267 void setHeaderTextFormat( const QgsTextFormat &format );
268
275 QgsTextFormat headerTextFormat() const;
276
281 void setHeaderHAlignment( HeaderHAlignment alignment );
282
288
294 void setHeaderMode( HeaderMode mode );
295
302
309 Q_DECL_DEPRECATED void setContentFont( const QFont &font ) SIP_DEPRECATED;
310
317 Q_DECL_DEPRECATED QFont contentFont() const SIP_DEPRECATED;
318
326 Q_DECL_DEPRECATED void setContentFontColor( const QColor &color ) SIP_DEPRECATED;
327
335 Q_DECL_DEPRECATED QColor contentFontColor() const SIP_DEPRECATED;
336
343 void setContentTextFormat( const QgsTextFormat &format );
344
351 QgsTextFormat contentTextFormat() const;
352
360 void setShowGrid( bool showGrid );
361
368 bool showGrid() const { return mShowGrid; }
369
376 void setGridStrokeWidth( double width );
377
384 double gridStrokeWidth() const { return mGridStrokeWidth; }
385
392 void setGridColor( const QColor &color );
393
400 QColor gridColor() const { return mGridColor; }
401
410 void setHorizontalGrid( bool horizontalGrid );
411
419 bool horizontalGrid() const { return mHorizontalGrid; }
420
429 void setVerticalGrid( bool verticalGrid );
430
438 bool verticalGrid() const { return mVerticalGrid; }
439
445 void setBackgroundColor( const QColor &color );
446
452 QColor backgroundColor() const { return mBackgroundColor; }
453
459 void setWrapBehavior( WrapBehavior behavior );
460
467
473
479 void setColumns( const QgsLayoutTableColumns &columns );
480
492
503 void setSortColumns( const QgsLayoutTableSortColumns &sortColumns );
504
509 void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
510
515 const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
516
523 virtual QMap<int, QString> headerLabels() const SIP_SKIP;
524
531
537 virtual QgsConditionalStyle conditionalCellStyle( int row, int column ) const;
538
544 virtual QgsExpressionContextScope *scopeForCell( int row, int column ) const SIP_FACTORY;
545
552 virtual int rowSpan( int row, int column ) const;
553
560 virtual int columnSpan( int row, int column ) const;
561
566
567 QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
568 QSizeF minFrameSize( int frameIndex = -1 ) const override;
569
570 bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
571 bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
572 QSizeF totalSize() const override;
573 void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
574
575 public slots:
576
577 void refresh() override;
578
584 virtual void refreshAttributes();
585
586 void recalculateFrameSizes() override;
587
588 protected:
590 double mCellMargin = 1.0;
591
594
597
599 bool mShowEmptyRows = false;
600
601
604
607
610
612 bool mShowGrid = true;
613
615 double mGridStrokeWidth = 0.5;
616
618 QColor mGridColor = Qt::black;
619
621 bool mHorizontalGrid = true;
622
624 bool mVerticalGrid = true;
625
627 QColor mBackgroundColor = Qt::white;
628
631
634
637
639 QMap<int, double> mMaxColumnWidthMap;
640
642 QMap<int, double> mMaxRowHeightMap;
643
645
647
648 QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
649
653 virtual bool calculateMaxColumnWidths();
654
658 virtual bool calculateMaxRowHeights();
659
665 //not const, as needs to call calculateMaxColumnWidths()
666 double totalWidth();
667
672 //not const, as needs to call calculateMaxRowHeights()
673 double totalHeight();
674
687 int rowsVisible( QgsRenderContext &context, double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
688
699 int rowsVisible( QgsRenderContext &context, int frameIndex, int firstRow, bool includeEmptyRows ) const;
700
707 QPair<int, int> rowRange( QgsRenderContext &context, int frameIndex ) const;
708
718 void drawHorizontalGridLines( QgsLayoutItemRenderContext &context, int firstRow, int lastRow, bool drawHeaderLines ) const;
719
735 void drawVerticalGridLines( QgsLayoutItemRenderContext &context, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
736
741
749
756 virtual QgsTextFormat textFormatForCell( int row, int column ) const;
757
764 virtual QgsTextFormat textFormatForHeader( int column ) const;
765
772 virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;
773
780 virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;
781
782 private:
783 QMap< CellStyleGroup, QString > mCellStyleNames;
784
786 void initStyles();
787
796 QColor backgroundColor( int row, int column, int rowSpan = 1, int columnSpan = 1 ) const;
797
798 friend class TestQgsLayoutTable;
801};
802
803#endif // QGSLAYOUTTABLE_H
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.
QgsLayoutMultiFrame(QgsLayout *layout)
Construct a new multiframe item, attached to the specified layout.
int frameIndex(QgsLayoutFrame *frame) const
Returns the index of a frame within the multiframe.
Stores properties of a column for a QgsLayoutTable.
Styling option for a layout table cell.
bool readXml(const QDomElement &styleElem)
Reads the style's properties from XML.
QColor cellBackgroundColor
Cell background color.
QgsLayoutTableStyle()=default
bool enabled
Whether the styling option is enabled.
bool writeXml(QDomElement &styleElem, QDomDocument &doc) const
Writes the style's properties to XML for storage.
Displays a table in the print layout, and allows the table to span over multiple frames.
bool mHorizontalGrid
True if grid should be shown.
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.
QgsLayoutTable(QgsLayout *layout)
Constructor for QgsLayoutTable, belonging to the specified layout.
virtual Qt::Alignment horizontalAlignmentForCell(int row, int column) const
Returns the horizontal alignment to use for the cell at the specified row and column.
QColor backgroundColor() const
Returns the color used for the background of the table.
virtual QgsConditionalStyle conditionalCellStyle(int row, int column) const
Returns the conditional style to use for the cell at row, column.
EmptyTableMode emptyTableBehavior() const
Returns the behavior mode for empty tables.
friend class TestQgsLayoutTable
QMap< int, double > mMaxColumnWidthMap
Map of maximum width for each column.
double totalWidth()
Returns total width of table contents.
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.
void drawVerticalGridLines(QgsLayoutItemRenderContext &context, const QMap< int, double > &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells=false) const
Draws the vertical grid lines for the table.
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.
HeaderHAlignment headerHAlignment() const
Returns the horizontal alignment for table headers.
QString mEmptyTableMessage
String to show in empty tables.
virtual int rowSpan(int row, int column) const
Returns the row span for the cell a row, column.
QPair< int, int > rowRange(QgsRenderContext &context, int frameIndex) const
Calculates a range of rows which should be visible in a given frame.
bool showEmptyRows() const
Returns whether empty rows are drawn in the table.
double mGridStrokeWidth
Width of grid lines.
EmptyTableMode mEmptyTableMode
Behavior for empty tables.
virtual QgsTextFormat textFormatForHeader(int column) const
Returns the text format to use for the header cell at the specified column.
WrapBehavior mWrapBehavior
bool verticalGrid() const
Returns whether the grid's vertical lines are drawn in the table.
friend class TestQgsLayoutManualTable
QColor mBackgroundColor
Color for table background.
void recalculateTableSize()
Recalculates and updates the size of the table and all table frames.
void drawHorizontalGridLines(QgsLayoutItemRenderContext &context, int firstRow, int lastRow, bool drawHeaderLines) const
Draws the horizontal grid lines for the table.
virtual QgsExpressionContextScope * scopeForCell(int row, int column) const
Creates a new QgsExpressionContextScope for the cell at row, column.
bool contentsContainsRow(const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row) const
Checks whether a table contents contains a given row.
bool mShowGrid
True if grid should be shown.
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.
@ LastRow
Style last row 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.
@ FirstFrame
Header shown on first frame only.
@ AllFrames
Headers shown on all frames.
@ NoHeaders
No headers shown for table.
QgsLayoutTableColumns mColumns
Columns to show in table.
QgsTextFormat mContentTextFormat
virtual bool getTableContents(QgsLayoutTableContents &contents)=0
Fetches the contents used for the cells in the table.
virtual bool calculateMaxColumnWidths()
Calculates the maximum width of text shown in columns.
HeaderMode mHeaderMode
Header display mode.
HeaderHAlignment mHeaderHAlignment
Alignment for table headers.
WrapBehavior wrapBehavior() const
Returns the wrap behavior for the table, which controls how text within cells is automatically wrappe...
int rowsVisible(QgsRenderContext &context, double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows) const
Calculates how many content rows would be visible within a frame of the specified height.
bool mShowEmptyRows
True if empty rows should be shown in the table.
friend class QgsCompositionConverter
QColor mGridColor
Color for grid lines.
double mCellMargin
Margin between cell borders and cell text.
virtual Qt::Alignment verticalAlignmentForCell(int row, int column) const
Returns the vertical alignment to use for the cell at the specified row and column.
double totalHeight()
Returns total height of table contents.
QgsLayoutTableContents mTableContents
Contents to show in table.
virtual int columnSpan(int row, int column) const
Returns the column span for the cell a row, column.
double gridStrokeWidth() const
Returns the width of grid lines in the table in mm.
QgsLayoutTableSortColumns mSortColumns
Columns to sort the table.
bool showGrid() const
Returns whether grid lines are drawn in the table.
virtual QgsTextFormat textFormatForCell(int row, int column) const
Returns the text format to use for the cell at the specified row and column.
QgsLayoutTableContents & contents()
Returns the current contents of the table.
bool mVerticalGrid
True if grid should be shown.
virtual bool calculateMaxRowHeights()
Calculates the maximum height of text shown in rows.
WrapBehavior
Controls how long strings in the table are handled.
@ WrapText
Text which doesn't fit inside the cell is wrapped. Note that this only applies to text in columns wit...
@ TruncateText
Text which doesn't fit inside the cell is truncated.
HeaderHAlignment
Controls how headers are horizontally aligned in a table.
@ HeaderRight
Align headers right.
@ 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.
@ ShowMessage
Shows preset message instead of table contents.
@ HeadersOnly
Show header rows only.
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:50
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:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_FACTORY
Definition qgis_sip.h:83