Skip to content
Permalink
Browse files

[feature][layouts] Port attribute table to QgsTextRenderer

Allows for full range of text formatting in layout attribute tables,
including buffers & shadows, word spacing, etc
  • Loading branch information
nyalldawson committed Jul 10, 2020
1 parent f56654f commit 11297b84b3e2a66495f53a9d7d25b73ad8873418
@@ -194,25 +194,31 @@ Returns whether empty rows are drawn in the table.
.. seealso:: :py:func:`setShowEmptyRows`
%End

void setHeaderFont( const QFont &font );
void setHeaderFont( const QFont &font ) /Deprecated/;
%Docstring
Sets the ``font`` used to draw header text in the table.

.. seealso:: :py:func:`headerFont`

.. seealso:: :py:func:`setContentFont`

.. deprecated::
use setHeaderTextFormat() instead
%End

QFont headerFont() const;
QFont headerFont() const /Deprecated/;
%Docstring
Returns the font used to draw header text in the table.

.. seealso:: :py:func:`setHeaderFont`

.. seealso:: :py:func:`contentFont`

.. deprecated::
use headerTextFormat() instead
%End

void setHeaderFontColor( const QColor &color );
void setHeaderFontColor( const QColor &color ) /Deprecated/;
%Docstring
Sets the ``color`` used to draw header text in the table.

@@ -221,9 +227,12 @@ Sets the ``color`` used to draw header text in the table.
.. seealso:: :py:func:`setHeaderFont`

.. seealso:: :py:func:`setContentFontColor`

.. deprecated::
use setHeaderTextFormat() instead
%End

QColor headerFontColor() const;
QColor headerFontColor() const /Deprecated/;
%Docstring
Returns the color used to draw header text in the table.

@@ -232,6 +241,31 @@ Returns the color used to draw header text in the table.
.. seealso:: :py:func:`headerFont`

.. seealso:: :py:func:`contentFontColor`

.. deprecated::
use headerTextFormat() instead
%End

void setHeaderTextFormat( const QgsTextFormat &format );
%Docstring
Sets the ``format`` used to draw header text in the table.

.. seealso:: :py:func:`headerTextFormat`

.. seealso:: :py:func:`setContentTextFormat`

.. versionadded:: 3.16
%End

QgsTextFormat headerTextFormat() const;
%Docstring
Returns the format used to draw header text in the table.

.. seealso:: :py:func:`setHeaderTextFormat`

.. seealso:: :py:func:`contentTextFormat`

.. versionadded:: 3.16
%End

void setHeaderHAlignment( HeaderHAlignment alignment );
@@ -264,25 +298,31 @@ if and where headers are shown in the table.
.. seealso:: :py:func:`setHeaderMode`
%End

void setContentFont( const QFont &font );
void setContentFont( const QFont &font ) /Deprecated/;
%Docstring
Sets the ``font`` used to draw text in table body cells.

.. seealso:: :py:func:`contentFont`

.. seealso:: :py:func:`setHeaderFont`

.. deprecated::
use setContentTextFormat() instead
%End

QFont contentFont() const;
QFont contentFont() const /Deprecated/;
%Docstring
Returns the font used to draw text in table body cells.

.. seealso:: :py:func:`setContentFont`

.. seealso:: :py:func:`headerFont`

.. deprecated::
use contextTextFormat() instead
%End

void setContentFontColor( const QColor &color );
void setContentFontColor( const QColor &color ) /Deprecated/;
%Docstring
Sets the ``color`` used to draw text in table body cells.

@@ -291,9 +331,12 @@ Sets the ``color`` used to draw text in table body cells.
.. seealso:: :py:func:`setContentFont`

.. seealso:: :py:func:`setHeaderFontColor`

.. deprecated::
use setContentTextFormat() instead
%End

QColor contentFontColor() const;
QColor contentFontColor() const /Deprecated/;
%Docstring
Returns the color used to draw text in table body cells.

@@ -302,6 +345,31 @@ Returns the color used to draw text in table body cells.
.. seealso:: :py:func:`contentFont`

.. seealso:: :py:func:`headerFontColor`

.. deprecated::
use contextTextFormat() instead
%End

void setContentTextFormat( const QgsTextFormat &format );
%Docstring
Sets the ``format`` used to draw content text in the table.

.. seealso:: :py:func:`contentTextFormat`

.. seealso:: :py:func:`setHeaderTextFormat`

.. versionadded:: 3.16
%End

QgsTextFormat contentTextFormat() const;
%Docstring
Returns the format used to draw content text in the table.

.. seealso:: :py:func:`setContentTextFormat`

.. seealso:: :py:func:`headerTextFormat`

.. versionadded:: 3.16
%End

void setShowGrid( bool showGrid );
@@ -583,8 +651,6 @@ new data.








@@ -602,11 +668,12 @@ Calculates the maximum height of text shown in rows.

double totalHeight();

int rowsVisible( double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
int rowsVisible( QgsRenderContext &context, double frameHeight, int firstRow, bool includeHeader, bool includeEmptyRows ) const;
%Docstring
Calculates how many content rows would be visible within a frame of the specified
height.

:param context: render context
:param frameHeight: height of frame
:param firstRow: index of first row visible in frame (where 0 = first row in table)
:param includeHeader: set to ``True`` if frame would include a header row
@@ -617,10 +684,11 @@ height.
:return: number of visible content rows (excluding header row)
%End

int rowsVisible( int frameIndex, int firstRow, bool includeEmptyRows ) const;
int rowsVisible( QgsRenderContext &context, int frameIndex, int firstRow, bool includeEmptyRows ) const;
%Docstring
Calculates how many content rows are visible within a given frame.

:param context: render context
:param frameIndex: index number for frame
:param firstRow: index of first row visible in frame (where 0 = first row in table)
:param includeEmptyRows: set to ``True`` to also include rows which would be empty in the returned count. For instance,
@@ -630,20 +698,21 @@ Calculates how many content rows are visible within a given frame.
:return: number of visible content rows (excludes header rows)
%End

QPair<int, int> rowRange( int frameIndex ) const;
QPair<int, int> rowRange( QgsRenderContext &context, int frameIndex ) const;
%Docstring
Calculates a range of rows which should be visible in a given frame.

:param context: render context
:param frameIndex: index number for frame

:return: row range
%End

void drawHorizontalGridLines( QPainter *painter, int firstRow, int lastRow, bool drawHeaderLines ) const;
void drawHorizontalGridLines( QgsLayoutItemRenderContext &context, int firstRow, int lastRow, bool drawHeaderLines ) const;
%Docstring
Draws the horizontal grid lines for the table.

:param painter: destination painter for grid lines
:param context: destination render context
:param firstRow: index corresponding to first row shown in frame
:param lastRow: index corresponding to last row shown in frame. If greater than the number of content rows in the
table, then the default row height will be used for the remaining rows.
@@ -1554,8 +1554,10 @@ bool QgsGeoreferencerMainWindow::writePDFReportFile( const QString &fileName, co
QFont tableHeaderFont;
tableHeaderFont.setPointSize( 9 );
tableHeaderFont.setBold( true );
QgsTextFormat tableHeaderFormat = QgsTextFormat::fromQFont( tableHeaderFont );
QFont tableContentFont;
tableContentFont.setPointSize( 9 );
QgsTextFormat tableContentFormat = QgsTextFormat::fromQFont( tableContentFont );

QgsSettings s;
double leftMargin = s.value( QStringLiteral( "/Plugin-GeoReferencer/Config/LeftMarginPDF" ), "2.0" ).toDouble();
@@ -1636,8 +1638,8 @@ bool QgsGeoreferencerMainWindow::writePDFReportFile( const QString &fileName, co
calculateMeanError( meanError );

parameterTable = new QgsLayoutItemTextTable( &layout );
parameterTable->setHeaderFont( tableHeaderFont );
parameterTable->setContentFont( tableContentFont );
parameterTable->setHeaderTextFormat( tableHeaderFormat );
parameterTable->setContentTextFormat( tableContentFormat );

QgsLayoutTableColumns columns;
columns << QgsLayoutTableColumn( tr( "Translation x" ) )
@@ -1679,8 +1681,8 @@ bool QgsGeoreferencerMainWindow::writePDFReportFile( const QString &fileName, co
resPlotItem->setConvertScaleToMapUnits( residualUnits == tr( "map units" ) );

QgsLayoutItemTextTable *gcpTable = new QgsLayoutItemTextTable( &layout );
gcpTable->setHeaderFont( tableHeaderFont );
gcpTable->setContentFont( tableContentFont );
gcpTable->setHeaderTextFormat( tableHeaderFormat );
gcpTable->setContentTextFormat( tableContentFormat );
gcpTable->setHeaderMode( QgsLayoutTable::AllFrames );
QgsLayoutTableColumns columns;
columns << QgsLayoutTableColumn( tr( "ID" ) )
@@ -1421,18 +1421,48 @@ bool QgsCompositionConverter::readTableXml( QgsLayoutItemAttributeTable *layoutI
layoutItem->setEmptyTableBehavior( static_cast<QgsLayoutTable::EmptyTableMode>( itemElem.attribute( QStringLiteral( "emptyTableMode" ), QStringLiteral( "0" ) ).toInt() ) );
layoutItem->setEmptyTableMessage( itemElem.attribute( QStringLiteral( "emptyTableMessage" ), QObject::tr( "No matching records" ) ) );
layoutItem->setShowEmptyRows( itemElem.attribute( QStringLiteral( "showEmptyRows" ), QStringLiteral( "0" ) ).toInt() );
if ( !QgsFontUtils::setFromXmlChildNode( layoutItem->mHeaderFont, itemElem, QStringLiteral( "headerFontProperties" ) ) )
QFont headerFont;
if ( !QgsFontUtils::setFromXmlChildNode( headerFont, itemElem, QStringLiteral( "headerFontProperties" ) ) )
{
layoutItem->mHeaderFont.fromString( itemElem.attribute( QStringLiteral( "headerFont" ), QString() ) );
headerFont.fromString( itemElem.attribute( QStringLiteral( "headerFont" ), QString() ) );
}
layoutItem->setHeaderFontColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "headerFontColor" ), QStringLiteral( "0,0,0,255" ) ) ) );
QgsTextFormat headerFormat = layoutItem->headerTextFormat();
headerFormat.setFont( headerFont );
if ( headerFont.pointSizeF() > 0 )
{
headerFormat.setSize( headerFont.pointSizeF() );
headerFormat.setSizeUnit( QgsUnitTypes::RenderPoints );
}
else if ( headerFont.pixelSize() > 0 )
{
headerFormat.setSize( headerFont.pixelSize() );
headerFormat.setSizeUnit( QgsUnitTypes::RenderPixels );
}
headerFormat.setColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "headerFontColor" ), QStringLiteral( "0,0,0,255" ) ) ) );
layoutItem->setHeaderTextFormat( headerFormat );
layoutItem->setHeaderHAlignment( static_cast<QgsLayoutTable::HeaderHAlignment>( itemElem.attribute( QStringLiteral( "headerHAlignment" ), QStringLiteral( "0" ) ).toInt() ) ) ;
layoutItem->setHeaderMode( static_cast<QgsLayoutTable::HeaderMode>( itemElem.attribute( QStringLiteral( "headerMode" ), QStringLiteral( "0" ) ).toInt() ) );
if ( !QgsFontUtils::setFromXmlChildNode( layoutItem->mContentFont, itemElem, QStringLiteral( "contentFontProperties" ) ) )

QFont contentFont;
if ( !QgsFontUtils::setFromXmlChildNode( contentFont, itemElem, QStringLiteral( "contentFontProperties" ) ) )
{
layoutItem->mContentFont.fromString( itemElem.attribute( QStringLiteral( "contentFont" ), QString() ) );
contentFont.fromString( itemElem.attribute( QStringLiteral( "contentFont" ), QString() ) );
}
layoutItem->setContentFontColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "contentFontColor" ), QStringLiteral( "0,0,0,255" ) ) ) );
QgsTextFormat contentFormat = layoutItem->contentTextFormat();
contentFormat.setFont( contentFont );
if ( contentFont.pointSizeF() > 0 )
{
contentFormat.setSize( contentFont.pointSizeF() );
contentFormat.setSizeUnit( QgsUnitTypes::RenderPoints );
}
else if ( contentFont.pixelSize() > 0 )
{
contentFormat.setSize( contentFont.pixelSize() );
contentFormat.setSizeUnit( QgsUnitTypes::RenderPixels );
}
contentFormat.setColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "contentFontColor" ), QStringLiteral( "0,0,0,255" ) ) ) );
layoutItem->setContentTextFormat( contentFormat );

layoutItem->setCellMargin( itemElem.attribute( QStringLiteral( "cellMargin" ), QStringLiteral( "1.0" ) ).toDouble() );
layoutItem->setGridStrokeWidth( itemElem.attribute( QStringLiteral( "gridStrokeWidth" ), QStringLiteral( "0.5" ) ).toDouble() );
layoutItem->setHorizontalGrid( itemElem.attribute( QStringLiteral( "horizontalGrid" ), QStringLiteral( "1" ) ).toInt() );

0 comments on commit 11297b8

Please sign in to comment.
You can’t perform that action at this time.