QGIS API Documentation  3.11.0-Master (68611307d7)
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 
59 
67 class CORE_EXPORT QgsLayoutTableStyle
68 {
69  public:
70 
72  QgsLayoutTableStyle() = default;
73 
75  bool enabled = false;
76 
78  QColor cellBackgroundColor = QColor( 255, 255, 255, 255 );
79 
86  bool writeXml( QDomElement &styleElem, QDomDocument &doc ) const;
87 
93  bool readXml( const QDomElement &styleElem );
94 
95 };
96 
103 class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
104 {
105  Q_OBJECT
106 
107  public:
108 
113  {
117  HeaderRight
118  };
119 
124  {
125  FirstFrame = 0,
127  NoHeaders
128  };
129 
134  {
135  HeadersOnly = 0,
137  ShowMessage
138  };
139 
144  {
145  TruncateText = 0,
146  WrapText
147  };
148 
153  {
162  LastRow
163  };
164 
168  QgsLayoutTable( QgsLayout *layout );
169 
170  ~QgsLayoutTable() override;
171 
176  void setCellMargin( double margin );
177 
182  double cellMargin() const { return mCellMargin; }
183 
188  void setEmptyTableBehavior( EmptyTableMode mode );
189 
195  EmptyTableMode emptyTableBehavior() const { return mEmptyTableMode; }
196 
204  void setEmptyTableMessage( const QString &message );
205 
213  QString emptyTableMessage() const { return mEmptyTableMessage; }
214 
220  void setShowEmptyRows( bool showEmpty );
221 
226  bool showEmptyRows() const { return mShowEmptyRows; }
227 
233  void setHeaderFont( const QFont &font );
234 
240  QFont headerFont() const { return mHeaderFont; }
241 
248  void setHeaderFontColor( const QColor &color );
249 
256  QColor headerFontColor() const { return mHeaderFontColor; }
257 
262  void setHeaderHAlignment( HeaderHAlignment alignment );
263 
268  HeaderHAlignment headerHAlignment() const { return mHeaderHAlignment; }
269 
275  void setHeaderMode( HeaderMode mode );
276 
282  HeaderMode headerMode() const { return mHeaderMode; }
283 
289  void setContentFont( const QFont &font );
290 
296  QFont contentFont() const { return mContentFont; }
297 
304  void setContentFontColor( const QColor &color );
305 
312  QColor contentFontColor() const { return mContentFontColor; }
313 
321  void setShowGrid( bool showGrid );
322 
329  bool showGrid() const { return mShowGrid; }
330 
337  void setGridStrokeWidth( double width );
338 
345  double gridStrokeWidth() const { return mGridStrokeWidth; }
346 
353  void setGridColor( const QColor &color );
354 
361  QColor gridColor() const { return mGridColor; }
362 
371  void setHorizontalGrid( bool horizontalGrid );
372 
380  bool horizontalGrid() const { return mHorizontalGrid; }
381 
390  void setVerticalGrid( bool verticalGrid );
391 
399  bool verticalGrid() const { return mVerticalGrid; }
400 
406  void setBackgroundColor( const QColor &color );
407 
413  QColor backgroundColor() const { return mBackgroundColor; }
414 
420  void setWrapBehavior( WrapBehavior behavior );
421 
427  WrapBehavior wrapBehavior() const { return mWrapBehavior; }
428 
433  QgsLayoutTableColumns &columns() { return mColumns; }
434 
440  void setColumns( const QgsLayoutTableColumns &columns );
441 
446  void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
447 
452  const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
453 
460  virtual QMap<int, QString> headerLabels() const SIP_SKIP;
461 
467  virtual bool getTableContents( QgsLayoutTableContents &contents ) = 0;
468 
474  virtual QgsConditionalStyle conditionalCellStyle( int row, int column ) const;
475 
479  QgsLayoutTableContents &contents() { return mTableContents; }
480 
481  QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
482  QSizeF minFrameSize( int frameIndex = -1 ) const override;
483 
484  bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
485  bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
486  QSizeF totalSize() const override;
487  void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
488 
489  public slots:
490 
491  void refresh() override;
492 
498  virtual void refreshAttributes();
499 
500  void recalculateFrameSizes() override;
501 
502  protected:
504  double mCellMargin = 1.0;
505 
507  EmptyTableMode mEmptyTableMode = HeadersOnly;
508 
511 
513  bool mShowEmptyRows = false;
514 
516  QFont mHeaderFont;
517 
519  QColor mHeaderFontColor = Qt::black;
520 
522  HeaderHAlignment mHeaderHAlignment = FollowColumn;
523 
525  HeaderMode mHeaderMode = FirstFrame;
526 
529 
531  QColor mContentFontColor = Qt::black;
532 
534  bool mShowGrid = true;
535 
537  double mGridStrokeWidth = 0.5;
538 
540  QColor mGridColor = Qt::black;
541 
543  bool mHorizontalGrid = true;
544 
546  bool mVerticalGrid = true;
547 
549  QColor mBackgroundColor = Qt::white;
550 
553 
556 
558  QMap<int, double> mMaxColumnWidthMap;
559 
561  QMap<int, double> mMaxRowHeightMap;
562 
563  QSizeF mTableSize;
564 
565  WrapBehavior mWrapBehavior = TruncateText;
566 
567  QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
568 
572  virtual bool calculateMaxColumnWidths();
573 
577  virtual bool calculateMaxRowHeights();
578 
584  //not const, as needs to call calculateMaxColumnWidths()
585  double totalWidth();
586 
591  //not const, as needs to call calculateMaxRowHeights()
592  double totalHeight();
593 
605  int rowsVisible( double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
606 
616  int rowsVisible( int frameIndex, int firstRow, bool includeEmptyRows ) const;
617 
623  QPair<int, int> rowRange( int frameIndex ) const;
624 
634  void drawHorizontalGridLines( QPainter *painter, int firstRow, int lastRow, bool drawHeaderLines ) const;
635 
651  void drawVerticalGridLines( QPainter *painter, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
652 
656  void recalculateTableSize();
657 
664  bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;
665 
666  private:
667 
668  QMap< CellStyleGroup, QString > mCellStyleNames;
669 
671  void initStyles();
672 
673  bool textRequiresWrapping( const QString &text, double columnWidth, const QFont &font ) const;
674 
675  QString wrappedText( const QString &value, double columnWidth, const QFont &font ) const;
676 
683  QColor backgroundColor( int row, int column ) const;
684 
685  friend class TestQgsLayoutTable;
686  friend class TestQgsLayoutManualTable;
688 };
689 
690 #endif // QGSLAYOUTTABLE_H
EmptyTableMode
Controls how empty tables are displayed.
The class is used as a container of context for various read/write operations on other objects...
QMap< int, double > mMaxRowHeightMap
Map of maximum height for each row.
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe&#39;s content, in layout units.
void refresh() override
Refreshes the multiframe, causing a recalculation of any property overrides.
QString emptyTableMessage() const
Returns the message for empty tables with no content rows.
QMap< CellStyleGroup, QgsLayoutTableStyle *> mCellStyles
Style odd numbered columns.
virtual QSizeF minFrameSize(int frameIndex=-1) const
Returns the minimum size for a frames, if desired.
QColor contentFontColor() const
Returns the color used to draw text in table body cells.
Style first column only.
WrapBehavior
Controls how long strings in the table are handled.
bool horizontalGrid() const
Returns whether the grid&#39;s horizontal lines are drawn in the table.
Header uses the same alignment as the column.
QColor gridColor() const
Returns the color used for grid lines in the table.
A class to display a table in the print layout, and allow the table to span over multiple frames...
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores multiframe state within an XML DOM element.
Styling option for a layout table cell.
QVector< QgsLayoutTableColumn *> QgsLayoutTableColumns
List of column definitions for a QgsLayoutTable.
Style odd numbered rows.
Style even numbered columns.
QString mEmptyTableMessage
String to show in empty tables.
double gridStrokeWidth() const
Returns the width of grid lines in the table in mm.
bool showEmptyRows() const
Returns whether empty rows are drawn in the table.
QFont contentFont() const
Returns the font used to draw text in table body cells.
Stores properties of a column for a QgsLayoutTable.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
EmptyTableMode emptyTableBehavior() const
Returns the behavior mode for empty tables.
Style first row only.
Conditional styling for a rule.
#define SIP_SKIP
Definition: qgis_sip.h:126
QFont mHeaderFont
Header font.
WrapBehavior wrapBehavior() const
Returns the wrap behavior for the table, which controls how text within cells is automatically wrappe...
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QColor headerFontColor() const
Returns the color used to draw header text in the table.
virtual void recalculateFrameSizes()
Recalculates the portion of the multiframe item which is shown in each of its component frames...
QFont mContentFont
Table contents font.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
Style header row.
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets multiframe state from a DOM element.
Align headers left.
double cellMargin() const
Returns the margin distance between cell borders and their contents in mm.
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
bool verticalGrid() const
Returns whether the grid&#39;s vertical lines are drawn in the table.
bool showGrid() const
Returns whether grid lines are drawn in the table.
QgsLayoutTableColumns mColumns
Columns to show in table.
virtual void render(QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex)=0
Renders a portion of the multiframe&#39;s content into a render context.
virtual QSizeF fixedFrameSize(int frameIndex=-1) const
Returns the fixed size for a frame, if desired.
QgsLayoutTableContents & contents()
Returns the current contents of the table.
HeaderHAlignment headerHAlignment() const
Returns the horizontal alignment for table headers.
Headers shown on all frames.
QgsLayoutTableContents mTableContents
Contents to show in table.
QFont headerFont() const
Returns the font used to draw header text in the table.
QColor backgroundColor() const
Returns the color used for the background of the table.
HeaderMode headerMode() const
Returns the display mode for headers in the table.
QMap< int, double > mMaxColumnWidthMap
Map of maximum width for each column.
HeaderMode
Controls where headers are shown in the table.
HeaderHAlignment
Controls how headers are horizontally aligned in a table.
Style even numbered rows.
QgsLayoutTableColumns & columns()
Returns a reference to the list of QgsLayoutTableColumns shown in the table.
Align headers to center.
CellStyleGroup
Row or column groups for cell styling.
QVector< QgsLayoutTableRow > QgsLayoutTableContents
List of QgsLayoutTableRows, representing rows and column cell contents for a QgsLayoutTable.
Style last column only.
Hides entire table if empty.
QVector< QVariant > QgsLayoutTableRow
List of QVariants, representing a the contents of a single row in a QgsLayoutTable.