QGIS API Documentation  3.6.0-Noosa (5873452)
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 <QFont>
25 #include <QColor>
26 #include <QPair>
27 
29 
36 typedef QVector< QVariant > QgsLayoutTableRow;
37 
44 #ifndef SIP_RUN
45 typedef QVector< QgsLayoutTableRow > QgsLayoutTableContents;
46 #else
47 typedef QVector< QVector< QVariant > > QgsLayoutTableContents;
48 #endif
49 
50 
56 typedef QVector< QgsLayoutTableColumn * > QgsLayoutTableColumns;
57 
58 
66 class CORE_EXPORT QgsLayoutTableStyle
67 {
68  public:
69 
71  QgsLayoutTableStyle() = default;
72 
74  bool enabled = false;
75 
77  QColor cellBackgroundColor = QColor( 255, 255, 255, 255 );
78 
85  bool writeXml( QDomElement &styleElem, QDomDocument &doc ) const;
86 
92  bool readXml( const QDomElement &styleElem );
93 
94 };
95 
102 class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
103 {
104  Q_OBJECT
105 
106  public:
107 
112  {
116  HeaderRight
117  };
118 
123  {
124  FirstFrame = 0,
126  NoHeaders
127  };
128 
133  {
134  HeadersOnly = 0,
136  ShowMessage
137  };
138 
143  {
144  TruncateText = 0,
145  WrapText
146  };
147 
152  {
161  LastRow
162  };
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 
194  EmptyTableMode emptyTableBehavior() const { return mEmptyTableMode; }
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 
232  void setHeaderFont( const QFont &font );
233 
239  QFont headerFont() const { return mHeaderFont; }
240 
247  void setHeaderFontColor( const QColor &color );
248 
255  QColor headerFontColor() const { return mHeaderFontColor; }
256 
261  void setHeaderHAlignment( HeaderHAlignment alignment );
262 
267  HeaderHAlignment headerHAlignment() const { return mHeaderHAlignment; }
268 
274  void setHeaderMode( HeaderMode mode );
275 
281  HeaderMode headerMode() const { return mHeaderMode; }
282 
288  void setContentFont( const QFont &font );
289 
295  QFont contentFont() const { return mContentFont; }
296 
303  void setContentFontColor( const QColor &color );
304 
311  QColor contentFontColor() const { return mContentFontColor; }
312 
320  void setShowGrid( bool showGrid );
321 
328  bool showGrid() const { return mShowGrid; }
329 
336  void setGridStrokeWidth( double width );
337 
344  double gridStrokeWidth() const { return mGridStrokeWidth; }
345 
352  void setGridColor( const QColor &color );
353 
360  QColor gridColor() const { return mGridColor; }
361 
370  void setHorizontalGrid( bool horizontalGrid );
371 
379  bool horizontalGrid() const { return mHorizontalGrid; }
380 
389  void setVerticalGrid( bool verticalGrid );
390 
398  bool verticalGrid() const { return mVerticalGrid; }
399 
405  void setBackgroundColor( const QColor &color );
406 
412  QColor backgroundColor() const { return mBackgroundColor; }
413 
419  void setWrapBehavior( WrapBehavior behavior );
420 
426  WrapBehavior wrapBehavior() const { return mWrapBehavior; }
427 
432  QgsLayoutTableColumns &columns() { return mColumns; }
433 
439  void setColumns( const QgsLayoutTableColumns &columns );
440 
445  void setCellStyle( CellStyleGroup group, const QgsLayoutTableStyle &style );
446 
451  const QgsLayoutTableStyle *cellStyle( CellStyleGroup group ) const;
452 
459  virtual QMap<int, QString> headerLabels() const SIP_SKIP;
460 
466  virtual bool getTableContents( QgsLayoutTableContents &contents ) = 0;
467 
471  QgsLayoutTableContents &contents() { return mTableContents; }
472 
473  QSizeF fixedFrameSize( int frameIndex = -1 ) const override;
474  QSizeF minFrameSize( int frameIndex = -1 ) const override;
475 
476  bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
477  bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
478  QSizeF totalSize() const override;
479  void render( QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex ) override;
480 
481  public slots:
482 
483  void refresh() override;
484 
490  virtual void refreshAttributes();
491 
492  void recalculateFrameSizes() override;
493 
494  protected:
496  double mCellMargin = 1.0;
497 
499  EmptyTableMode mEmptyTableMode = HeadersOnly;
500 
503 
505  bool mShowEmptyRows = false;
506 
508  QFont mHeaderFont;
509 
511  QColor mHeaderFontColor = Qt::black;
512 
514  HeaderHAlignment mHeaderHAlignment = FollowColumn;
515 
517  HeaderMode mHeaderMode = FirstFrame;
518 
521 
523  QColor mContentFontColor = Qt::black;
524 
526  bool mShowGrid = true;
527 
529  double mGridStrokeWidth = 0.5;
530 
532  QColor mGridColor = Qt::black;
533 
535  bool mHorizontalGrid = true;
536 
538  bool mVerticalGrid = true;
539 
541  QColor mBackgroundColor = Qt::white;
542 
545 
548 
550  QMap<int, double> mMaxColumnWidthMap;
551 
553  QMap<int, double> mMaxRowHeightMap;
554 
555  QSizeF mTableSize;
556 
557  WrapBehavior mWrapBehavior = TruncateText;
558 
559  QMap< CellStyleGroup, QgsLayoutTableStyle * > mCellStyles;
560 
564  virtual bool calculateMaxColumnWidths();
565 
569  virtual bool calculateMaxRowHeights();
570 
576  //not const, as needs to call calculateMaxColumnWidths()
577  double totalWidth();
578 
583  //not const, as needs to call calculateMaxRowHeights()
584  double totalHeight();
585 
597  int rowsVisible( double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
598 
608  int rowsVisible( int frameIndex, int firstRow, bool includeEmptyRows ) const;
609 
615  QPair<int, int> rowRange( int frameIndex ) const;
616 
626  void drawHorizontalGridLines( QPainter *painter, int firstRow, int lastRow, bool drawHeaderLines ) const;
627 
643  void drawVerticalGridLines( QPainter *painter, const QMap<int, double> &maxWidthMap, int firstRow, int lastRow, bool hasHeader, bool mergeCells = false ) const SIP_SKIP;
644 
648  void recalculateTableSize();
649 
656  bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;
657 
658  private:
659 
660  QMap< CellStyleGroup, QString > mCellStyleNames;
661 
663  void initStyles();
664 
665  bool textRequiresWrapping( const QString &text, double columnWidth, const QFont &font ) const;
666 
667  QString wrappedText( const QString &value, double columnWidth, const QFont &font ) const;
668 
675  QColor backgroundColor( int row, int column ) const;
676 
677  friend class TestQgsLayoutTable;
679 };
680 
681 #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 composer 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.
#define SIP_SKIP
Definition: qgis_sip.h:119
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 QgsComposerTable.