Skip to content
Permalink
Browse files

Add text format to QgsTableCell

  • Loading branch information
nyalldawson committed Jul 12, 2020
1 parent 2c1bcff commit c962eaaa043caa5c1c5c1567425eedcda1893eac
@@ -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;
@@ -17,6 +17,7 @@
#include "qgsapplication.h"
#include "qgsnumericformatregistry.h"
#include "qgsnumericformat.h"
#include "qgsreadwritecontext.h"

QgsTableCell::QgsTableCell( const QVariant &content )
: mContent( content )
@@ -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 )
{}

@@ -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;
}
@@ -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" ) ) )
{

@@ -17,6 +17,7 @@

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgstextformat.h"
#include <QVariant>
#include <QColor>
#include <memory>
@@ -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.
*
@@ -138,6 +177,8 @@ class CORE_EXPORT QgsTableCell
QVariant mContent;
QColor mBackgroundColor;
QColor mForegroundColor;
bool mHasTextFormat = false;
QgsTextFormat mTextFormat;
std::unique_ptr< QgsNumericFormat > mFormat;
};

@@ -13,7 +13,8 @@
import qgis # NOQA
from qgis.core import (QgsTableCell,
QgsBearingNumericFormat,
QgsReadWriteContext)
QgsReadWriteContext,
QgsTextFormat)

from qgis.PyQt.QtGui import QColor

@@ -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))
@@ -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')

@@ -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()
@@ -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__':

0 comments on commit c962eaa

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