Skip to content

Commit

Permalink
Add text format to QgsTableCell
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 12, 2020
1 parent 2c1bcff commit c962eaa
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
48 changes: 48 additions & 0 deletions python/core/auto_generated/qgstablecell.sip.in
Expand Up @@ -78,6 +78,54 @@ Sets the cell's foreground ``color``.
Set an invalid ``color`` if a default color should be used for the foreground.

.. seealso:: :py:func:`foregroundColor`
%End

QgsTextFormat textFormat() const;
%Docstring
Returns the cell's text format.

.. note::

The text format will only be used if :py:func:`~QgsTableCell.hasTextFormat` returns ``True``.

.. seealso:: :py:func:`setTextFormat`

.. versionadded:: 3.16
%End

void setTextFormat( const QgsTextFormat &format );
%Docstring
Sets the cell's text ``format``.

.. note::

The text format will only be used if :py:func:`~QgsTableCell.hasTextFormat` returns ``True``.

.. seealso:: :py:func:`textFormat`

.. versionadded:: 3.16
%End

bool hasTextFormat() const;
%Docstring
Returns ``True`` if the cell has a specific text format which should be applied.

.. seealso:: :py:func:`textFormat`

.. seealso:: :py:func:`setHasTextFormat`

.. versionadded:: 3.16
%End

void setHasTextFormat( bool hasTextFormat );
%Docstring
Sets whether the cell has a specific text format which should be applied.

.. seealso:: :py:func:`setTextFormat`

.. seealso:: :py:func:`hasTextFormat`

.. versionadded:: 3.16
%End

const QgsNumericFormat *numericFormat() const;
Expand Down
30 changes: 27 additions & 3 deletions src/core/qgstablecell.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgsapplication.h"
#include "qgsnumericformatregistry.h"
#include "qgsnumericformat.h"
#include "qgsreadwritecontext.h"

QgsTableCell::QgsTableCell( const QVariant &content )
: mContent( content )
Expand All @@ -26,6 +27,8 @@ QgsTableCell::QgsTableCell( const QgsTableCell &other )
: mContent( other.mContent )
, mBackgroundColor( other.mBackgroundColor )
, mForegroundColor( other.mForegroundColor )
, mHasTextFormat( other.mHasTextFormat )
, mTextFormat( other.mTextFormat )
, mFormat( other.mFormat ? other.mFormat->clone() : nullptr )
{}

Expand All @@ -35,7 +38,9 @@ QgsTableCell &QgsTableCell::operator=( const QgsTableCell &other )
{
mContent = other.mContent;
mBackgroundColor = other.mBackgroundColor;
mForegroundColor = other.mForegroundColor;
mForegroundColor = other.mForegroundColor;
mHasTextFormat = other.mHasTextFormat;
mTextFormat = other.mTextFormat;
mFormat.reset( other.mFormat ? other.mFormat->clone() : nullptr );
return *this;
}
Expand All @@ -54,21 +59,40 @@ QVariantMap QgsTableCell::properties( const QgsReadWriteContext &context ) const
{
QVariantMap res;
res.insert( QStringLiteral( "content" ), mContent );
res.insert( QStringLiteral( "foreground" ), mForegroundColor );
res.insert( QStringLiteral( "background" ), mBackgroundColor );
res.insert( QStringLiteral( "foreground" ), mForegroundColor );
if ( mFormat )
{
res.insert( QStringLiteral( "format_type" ), mFormat->id() );
res.insert( QStringLiteral( "format" ), mFormat->configuration( context ) );
}

res.insert( QStringLiteral( "has_text_format" ), mHasTextFormat );
QDomDocument textDoc;
QDomElement textElem = mTextFormat.writeXml( textDoc, context );
textDoc.appendChild( textElem );
res.insert( QStringLiteral( "text_format" ), textDoc.toString() );

return res;
}

void QgsTableCell::setProperties( const QVariantMap &properties, const QgsReadWriteContext &context )
{
mContent = properties.value( QStringLiteral( "content" ) );
mForegroundColor = properties.value( QStringLiteral( "foreground" ) ).value< QColor >();
mBackgroundColor = properties.value( QStringLiteral( "background" ) ).value< QColor >();
mForegroundColor = properties.value( QStringLiteral( "foreground" ) ).value< QColor >();

QDomDocument doc;
QDomElement elem;
const QString textXml = properties.value( QStringLiteral( "text_format" ) ).toString();
if ( !textXml.isEmpty() )
{
doc.setContent( textXml );
elem = doc.documentElement();
mTextFormat.readXml( elem, context );
}
mHasTextFormat = properties.value( QStringLiteral( "has_text_format" ) ).toBool();

if ( properties.contains( QStringLiteral( "format_type" ) ) )
{

Expand Down
41 changes: 41 additions & 0 deletions src/core/qgstablecell.h
Expand Up @@ -17,6 +17,7 @@

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgstextformat.h"
#include <QVariant>
#include <QColor>
#include <memory>
Expand Down Expand Up @@ -94,6 +95,44 @@ class CORE_EXPORT QgsTableCell
*/
void setForegroundColor( const QColor &color ) { mForegroundColor = color; }

/**
* Returns the cell's text format.
*
* \note The text format will only be used if hasTextFormat() returns TRUE.
*
* \see setTextFormat()
* \since QGIS 3.16
*/
QgsTextFormat textFormat() const { return mTextFormat; }

/**
* Sets the cell's text \a format.
*
* \note The text format will only be used if hasTextFormat() returns TRUE.
*
* \see textFormat()
* \since QGIS 3.16
*/
void setTextFormat( const QgsTextFormat &format ) { mTextFormat = format; }

/**
* Returns TRUE if the cell has a specific text format which should be applied.
*
* \see textFormat()
* \see setHasTextFormat()
* \since QGIS 3.16
*/
bool hasTextFormat() const { return mHasTextFormat; }

/**
* Sets whether the cell has a specific text format which should be applied.
*
* \see setTextFormat()
* \see hasTextFormat()
* \since QGIS 3.16
*/
void setHasTextFormat( bool hasTextFormat ) { mHasTextFormat = hasTextFormat; }

/**
* Returns the numeric format used for numbers in the cell, or NULLPTR if no format is set.
*
Expand Down Expand Up @@ -138,6 +177,8 @@ class CORE_EXPORT QgsTableCell
QVariant mContent;
QColor mBackgroundColor;
QColor mForegroundColor;
bool mHasTextFormat = false;
QgsTextFormat mTextFormat;
std::unique_ptr< QgsNumericFormat > mFormat;
};

Expand Down
18 changes: 17 additions & 1 deletion tests/src/python/test_qgstablecell.py
Expand Up @@ -13,7 +13,8 @@
import qgis # NOQA
from qgis.core import (QgsTableCell,
QgsBearingNumericFormat,
QgsReadWriteContext)
QgsReadWriteContext,
QgsTextFormat)

from qgis.PyQt.QtGui import QColor

Expand All @@ -35,6 +36,7 @@ def testCell(self):
self.assertFalse(c.backgroundColor().isValid())
self.assertFalse(c.foregroundColor().isValid())
self.assertFalse(c.numericFormat())
self.assertFalse(c.hasTextFormat())

c.setBackgroundColor(QColor(255, 0, 0))
c.setForegroundColor(QColor(255, 0, 255))
Expand All @@ -43,6 +45,13 @@ def testCell(self):
self.assertEqual(c.foregroundColor().name(), '#ff00ff')
self.assertIsInstance(c.numericFormat(), QgsBearingNumericFormat)

format = QgsTextFormat()
format.setSize(16.8)
c.setTextFormat(format)
c.setHasTextFormat(True)
self.assertEqual(c.textFormat().size(), 16.8)
self.assertTrue(c.hasTextFormat())

def testProperties(self):
c = QgsTableCell('test')

Expand All @@ -55,12 +64,17 @@ def testProperties(self):
self.assertFalse(c2.backgroundColor().isValid())
self.assertFalse(c2.foregroundColor().isValid())
self.assertFalse(c2.numericFormat())
self.assertFalse(c2.hasTextFormat())

c.setBackgroundColor(QColor(255, 0, 0))
c.setForegroundColor(QColor(255, 0, 255))
format = QgsBearingNumericFormat()
format.setShowPlusSign(True)
c.setNumericFormat(format)
text_format = QgsTextFormat()
text_format.setSize(16.8)
c.setTextFormat(text_format)
c.setHasTextFormat(True)
props = c.properties(QgsReadWriteContext())

c3 = QgsTableCell()
Expand All @@ -71,6 +85,8 @@ def testProperties(self):
self.assertEqual(c3.foregroundColor().name(), '#ff00ff')
self.assertIsInstance(c3.numericFormat(), QgsBearingNumericFormat)
self.assertTrue(c3.numericFormat().showPlusSign())
self.assertEqual(c3.textFormat().size(), 16.8)
self.assertTrue(c3.hasTextFormat())


if __name__ == '__main__':
Expand Down

0 comments on commit c962eaa

Please sign in to comment.