Skip to content
Permalink
Browse files

[feature][layouts] Allow setting per cell text format in manual text …

…tables

Individual cells (or selections of multiple cells) can have an preset
text format set, which overrides the default text format for those cells.

Sponsored by City of Canning
  • Loading branch information
nyalldawson committed Jul 12, 2020
1 parent b584f93 commit 18a36e46227b238d831d3b64f8b4f7f9151c6748
@@ -139,6 +139,10 @@ Replaces the headers in the table with a specified list of :py:class:`QgsLayoutT

virtual bool calculateMaxRowHeights();

virtual QgsTextFormat textFormatForHeader( int column ) const;

virtual QgsTextFormat textFormatForCell( int row, int column ) const;


};

@@ -735,6 +735,24 @@ Checks whether a table contents contains a given row
:param row: row to check for

:return: ``True`` if contents contains rows
%End

virtual QgsTextFormat textFormatForCell( int row, int column ) const;
%Docstring
Returns the text format to use for the cell at the specified ``row`` and ``column``.

.. seealso:: :py:func:`textFormatForHeader`

.. versionadded:: 3.16
%End

virtual QgsTextFormat textFormatForHeader( int column ) const;
%Docstring
Returns the text format to use for the header cell at the specified ``column``.

.. seealso:: :py:func:`textFormatForCell`

.. versionadded:: 3.16
%End

};
@@ -304,6 +304,20 @@ bool QgsLayoutItemManualTable::calculateMaxRowHeights()
return true;
}

QgsTextFormat QgsLayoutItemManualTable::textFormatForHeader( int column ) const
{
// if ( mHeaders.value( column ).)
return QgsLayoutTable::textFormatForHeader( column );
}

QgsTextFormat QgsLayoutItemManualTable::textFormatForCell( int row, int column ) const
{
if ( mContents.value( row ).value( column ).hasTextFormat() )
return mContents.value( row ).value( column ).textFormat();

return QgsLayoutTable::textFormatForCell( row, column );
}

void QgsLayoutItemManualTable::refreshColumns()
{
// refresh columns
@@ -139,6 +139,8 @@ class CORE_EXPORT QgsLayoutItemManualTable: public QgsLayoutTable
bool writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
bool readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context ) override;
bool calculateMaxRowHeights() override;
QgsTextFormat textFormatForHeader( int column ) const override;
QgsTextFormat textFormatForCell( int row, int column ) const override;

private:

Large diffs are not rendered by default.

@@ -725,6 +725,22 @@ class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
*/
bool contentsContainsRow( const QgsLayoutTableContents &contents, const QgsLayoutTableRow &row ) const;

/**
* Returns the text format to use for the cell at the specified \a row and \a column.
*
* \see textFormatForHeader()
* \since QGIS 3.16
*/
virtual QgsTextFormat textFormatForCell( int row, int column ) const;

/**
* Returns the text format to use for the header cell at the specified \a column.
*
* \see textFormatForCell()
* \since QGIS 3.16
*/
virtual QgsTextFormat textFormatForHeader( int column ) const;

private:

QMap< CellStyleGroup, QString > mCellStyleNames;
@@ -49,6 +49,7 @@ class TestQgsLayoutManualTable : public QObject
void rowHeight();
void columnWidth();
void headers();
void cellTextFormat();

private:
QString mReport;
@@ -417,5 +418,60 @@ void TestQgsLayoutManualTable::headers()
QVERIFY( result );
}

void TestQgsLayoutManualTable::cellTextFormat()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemManualTable *table = new QgsLayoutItemManualTable( &l );
QgsLayoutFrame *frame1 = new QgsLayoutFrame( &l, table );
frame1->attemptSetSceneRect( QRectF( 5, 5, 100, 60 ) );
frame1->setFrameEnabled( true );
table->addFrame( frame1 );
table->setBackgroundColor( Qt::yellow );

table->setContentTextFormat( QgsTextFormat::fromQFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ) ) );

frame1->setFrameEnabled( false );
table->setShowGrid( true );
table->setHorizontalGrid( true );
table->setVerticalGrid( true );

QgsTableCell c1( QStringLiteral( "Jet" ) );
QgsTextFormat f1 = table->contentTextFormat();
f1.setSize( 20 );
f1.buffer().setEnabled( true );
f1.buffer().setColor( QColor( 100, 130, 150 ) );
f1.buffer().setSize( 1 );
c1.setTextFormat( f1 );
c1.setHasTextFormat( true );

QgsTableCell c3( QStringLiteral( "Plane" ) );
QgsTextFormat f2 = table->contentTextFormat();
f2.setSize( 16 );
f2.buffer().setEnabled( true );
f2.buffer().setColor( QColor( 150, 110, 90 ) );
f2.buffer().setSize( 1 );
c3.setTextFormat( f2 );
c3.setHasTextFormat( true );

QgsTableCell c5( QStringLiteral( "B" ) );
QgsTextFormat f3 = table->contentTextFormat();
f3.setSize( 36 );
f3.buffer().setEnabled( true );
f3.buffer().setColor( QColor( 200, 110, 90 ) );
f3.buffer().setSize( 1 );
c5.setTextFormat( f3 );
c5.setHasTextFormat( true );

table->setTableContents( QgsTableContents() << ( QgsTableRow() << c1 << QgsTableCell( QStringLiteral( "Helicopter" ) ) << c3 )
<< ( QgsTableRow() << QgsTableCell( QStringLiteral( "A" ) ) << c5 << QgsTableCell( QStringLiteral( "C" ) ) ) );

table->setColumnWidths( QList< double >() << 0 << 0.0 << 30.0 );
QgsLayoutChecker checker( QStringLiteral( "manualtable_textformat" ), &l );
checker.setControlPathPrefix( QStringLiteral( "layout_manual_table" ) );
bool result = checker.testLayout( mReport );
QVERIFY( result );
}

QGSTEST_MAIN( TestQgsLayoutManualTable )
#include "testqgslayoutmanualtable.moc"
Binary file not shown.

0 comments on commit 18a36e4

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