29#include "moc_qgslayoutitemmanualtable.cpp"
31using namespace Qt::StringLiterals;
65 return tr(
"<Table frame>" );
77 for (
const QgsTableRow &row : std::as_const( mContents ) )
81 for (
int columnNumber = 0; columnNumber <
mColumns.count(); ++columnNumber )
83 if ( columnNumber < row.count() )
85 QVariant cellContent = row.at( columnNumber ).content();
87 if ( cellContent.userType() == qMetaTypeId<QgsProperty>() )
91 cellContent = cellContent.value<
QgsProperty >().value( context );
94 if ( row.at( columnNumber ).numericFormat() )
95 currentRow << row.at( columnNumber ).numericFormat()->formatDouble( cellContent.toDouble(), numericContext );
97 currentRow << cellContent.toString();
101 currentRow << QString();
114 if ( row < 0 || row >= mContents.size() )
118 if ( column < 0 || column > rowContents.size() )
123 if (
c.foregroundColor().isValid() )
125 if (
c.backgroundColor().isValid() )
146 mRowHeights = heights;
153 mColumnWidths = widths;
161 return mIncludeHeader;
166 mIncludeHeader = included;
168 if ( !mIncludeHeader )
195 tableElem.setAttribute( u
"includeHeader"_s, mIncludeHeader ? u
"1"_s : u
"0"_s );
198 QDomElement headersElem = doc.createElement( u
"headers"_s );
201 QDomElement headerElem = doc.createElement( u
"header"_s );
202 header.writeXml( headerElem, doc );
203 headersElem.appendChild( headerElem );
205 tableElem.appendChild( headersElem );
207 QDomElement contentsElement = doc.createElement( u
"contents"_s );
210 QDomElement rowElement = doc.createElement( u
"row"_s );
211 for (
int i = 0; i <
mColumns.count(); ++i )
213 if ( i < row.count() )
218 contentsElement.appendChild( rowElement );
220 tableElem.appendChild( contentsElement );
222 QDomElement rowHeightsElement = doc.createElement( u
"rowHeights"_s );
223 for (
double height : mRowHeights )
225 QDomElement rowElement = doc.createElement( u
"row"_s );
226 rowElement.setAttribute( u
"height"_s, height );
227 rowHeightsElement.appendChild( rowElement );
229 tableElem.appendChild( rowHeightsElement );
231 QDomElement columnWidthsElement = doc.createElement( u
"columnWidths"_s );
232 for (
double width : mColumnWidths )
234 QDomElement columnElement = doc.createElement( u
"column"_s );
235 columnElement.setAttribute( u
"width"_s, width );
236 columnWidthsElement.appendChild( columnElement );
238 tableElem.appendChild( columnWidthsElement );
248 mIncludeHeader = itemElem.attribute( u
"includeHeader"_s ).toInt();
251 QDomNodeList headersList = itemElem.elementsByTagName( u
"headers"_s );
252 if ( !headersList.isEmpty() )
254 QDomElement headersElem = headersList.at( 0 ).toElement();
255 QDomNodeList headerEntryList = headersElem.elementsByTagName( u
"header"_s );
256 for (
int i = 0; i < headerEntryList.size(); ++i )
258 QDomElement headerElem = headerEntryList.at( i ).toElement();
261 mHeaders.append( header );
266 const QDomNodeList rowHeightNodeList = itemElem.firstChildElement( u
"rowHeights"_s ).childNodes();
267 mRowHeights.reserve( rowHeightNodeList.size() );
268 for (
int r = 0; r < rowHeightNodeList.size(); ++r )
270 const QDomElement rowHeightElement = rowHeightNodeList.at( r ).toElement();
271 mRowHeights.append( rowHeightElement.attribute( u
"height"_s ).toDouble() );
274 mColumnWidths.clear();
275 const QDomNodeList columnWidthNodeList = itemElem.firstChildElement( u
"columnWidths"_s ).childNodes();
276 mColumnWidths.reserve( columnWidthNodeList.size() );
277 for (
int r = 0; r < columnWidthNodeList.size(); ++r )
279 const QDomElement columnWidthElement = columnWidthNodeList.at( r ).toElement();
280 mColumnWidths.append( columnWidthElement.attribute( u
"width"_s ).toDouble() );
284 const QDomElement contentsElement = itemElem.firstChildElement( u
"contents"_s );
285 const QDomNodeList rowNodeList = contentsElement.childNodes();
286 newContents.reserve( rowNodeList.size() );
287 for (
int r = 0; r < rowNodeList.size(); ++r )
290 const QDomElement rowElement = rowNodeList.at( r ).toElement();
291 const QDomNodeList cellNodeList = rowElement.childNodes();
292 row.reserve( cellNodeList.size() );
293 for (
int c = 0;
c < cellNodeList.size(); ++
c )
295 const QDomElement cellElement = cellNodeList.at(
c ).toElement();
313 QMap<int, double> newHeights;
317 const int row = it.key() - 1;
318 const double presetHeight = mRowHeights.value( row );
319 double thisRowHeight = it.value();
320 if ( presetHeight > 0 )
321 thisRowHeight = presetHeight;
322 newHeights.insert( row + 1, thisRowHeight );
336 if ( mContents.value( row ).value( column ).textFormat().isValid() )
337 return mContents.value( row ).value( column ).textFormat();
344 if ( row < mContents.size() && column < mContents.at( row ).size() )
345 return mContents.value( row ).value( column ).horizontalAlignment();
352 if ( row < mContents.size() && column < mContents.at( row ).size() )
353 return mContents.value( row ).value( column ).verticalAlignment();
360 if ( row < mContents.size() && column < mContents.at( row ).size() )
361 return mContents.value( row ).value( column ).rowSpan();
367 if ( row < mContents.size() && column < mContents.at( row ).size() )
368 return mContents.value( row ).value( column ).columnSpan();
372void QgsLayoutItemManualTable::refreshColumns()
376 if ( !mContents.empty() )
380 columns.reserve( firstRow.size() );
385 newCol.setWidth( mColumnWidths.value( colIndex ) );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Conditional styling for a rule.
void setTextColor(const QColor &value)
Set the text color for the style.
void setBackgroundColor(const QColor &value)
Set the background color for the style.
RAII class to pop scope from an expression context on destruction.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool calculateMaxRowHeights() override
Calculates the maximum height of text shown in rows.
QIcon icon() const override
Returns the item's icon.
void setColumnWidths(const QList< double > &widths)
Sets the list of column widths (in millimeters) to use when rendering the table.
Qt::Alignment horizontalAlignmentForCell(int row, int column) const override
Returns the horizontal alignment to use for the cell at the specified row and column.
QString displayName() const override
Returns the multiframe display name.
int rowSpan(int row, int column) const override
Returns the row span for the cell a row, column.
~QgsLayoutItemManualTable() override
QgsConditionalStyle conditionalCellStyle(int row, int column) const override
Returns the conditional style to use for the cell at row, column.
Qt::Alignment verticalAlignmentForCell(int row, int column) const override
Returns the vertical alignment to use for the cell at the specified row and column.
static QgsLayoutItemManualTable * create(QgsLayout *layout)
Returns a new QgsLayoutItemManualTable for the specified parent layout.
void setIncludeTableHeader(bool included)
Sets whether the table includes a header row.
void setHeaders(const QgsLayoutTableColumns &headers)
Replaces the headers in the table with a specified list of QgsLayoutTableColumns.
QgsTextFormat textFormatForCell(int row, int column) const override
Returns the text format to use for the cell at the specified row and column.
void setTableContents(const QgsTableContents &contents)
Sets the contents of the table.
int columnSpan(int row, int column) const override
Returns the column span for the cell a row, column.
QgsTextFormat textFormatForHeader(int column) const override
Returns the text format to use for the header cell at the specified column.
int type() const override
Returns unique multiframe type id.
bool readPropertiesFromElement(const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context) override
Sets multiframe state from a DOM element.
void setRowHeights(const QList< double > &heights)
Sets the list of row heights (in millimeters) to use when rendering the table.
bool includeTableHeader() const
Returns true if the table includes a header row.
QgsLayoutTableColumns & headers()
Returns a reference to the list of headers shown in the table.
QgsLayoutItemManualTable(QgsLayout *layout)
Constructor for QgsLayoutItemManualTable, attached to the specified layout.
QgsTableContents tableContents() const
Returns the contents of the table.
bool writePropertiesToElement(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Stores multiframe state within an XML DOM element.
bool getTableContents(QgsLayoutTableContents &contents) override
Fetches the contents used for the cells in the table.
@ LayoutManualTable
Manual (fixed) table.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
Stores properties of a column for a QgsLayoutTable.
bool readXml(const QDomElement &columnElem)
Reads the column's properties from xml.
QgsLayoutTableColumns & columns()
Returns a reference to the list of QgsLayoutTableColumns shown in the table.
void setColumns(const QgsLayoutTableColumns &columns)
Replaces the columns in the table with a specified list of QgsLayoutTableColumns.
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.
virtual void refreshAttributes()
Refreshes the contents shown in the table by querying for new data.
virtual QgsTextFormat textFormatForHeader(int column) const
Returns the text format to use for the header cell at the specified column.
void setHeaderMode(HeaderMode mode)
Sets the display mode for headers in the table.
void recalculateTableSize()
Recalculates and updates the size of the table and all table frames.
virtual QgsExpressionContextScope * scopeForCell(int row, int column) const
Creates a new QgsExpressionContextScope for the cell at row, column.
@ AllFrames
Headers shown on all frames.
@ NoHeaders
No headers shown for table.
QgsLayoutTableColumns mColumns
Columns to show in table.
virtual Qt::Alignment verticalAlignmentForCell(int row, int column) const
Returns the vertical alignment to use for the cell at the specified row and column.
bool writePropertiesToElement(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Stores multiframe state within an XML DOM element.
virtual QgsTextFormat textFormatForCell(int row, int column) const
Returns the text format to use for the cell at the specified row and column.
bool readPropertiesFromElement(const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context) override
Sets multiframe state from a DOM element.
QgsLayoutTableContents & contents()
Returns the current contents of the table.
virtual bool calculateMaxRowHeights()
Calculates the maximum height of text shown in rows.
QMap< int, double > mMaxRowHeightMap
Map of maximum height for each row.
A context for numeric formats.
A store for object properties.
A container for the context for various read/write operations on objects.
Encapsulates the contents and formatting of a single table cell.
void setProperties(const QVariantMap &properties, const QgsReadWriteContext &context)
Sets the properties for the cell.
Container for all settings relating to text rendering.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
QVector< QgsLayoutTableColumn > QgsLayoutTableColumns
List of column definitions for 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.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QVector< QgsTableRow > QgsTableContents
A set of table rows.
QVector< QgsTableCell > QgsTableRow
A row of table cells.