QGIS API Documentation  3.25.0-Master (10b47c2603)
qgstablecell.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstablecell.h
3  --------------
4  begin : January 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgstablecell.h"
17 #include "qgsapplication.h"
19 #include "qgsnumericformat.h"
20 #include "qgsreadwritecontext.h"
21 
22 QgsTableCell::QgsTableCell( const QVariant &content )
23  : mContent( content )
24 {}
25 
27  : mContent( other.mContent )
28  , mBackgroundColor( other.mBackgroundColor )
29  , mForegroundColor( other.mForegroundColor )
30  , mTextFormat( other.mTextFormat )
31  , mFormat( other.mFormat ? other.mFormat->clone() : nullptr )
32  , mHAlign( other.mHAlign )
33  , mVAlign( other.mVAlign )
34 {}
35 
36 QgsTableCell::~QgsTableCell() = default;
37 
39 {
40  mContent = other.mContent;
41  mBackgroundColor = other.mBackgroundColor;
42  mForegroundColor = other.mForegroundColor;
43  mTextFormat = other.mTextFormat;
44  mFormat.reset( other.mFormat ? other.mFormat->clone() : nullptr );
45  mHAlign = other.mHAlign;
46  mVAlign = other.mVAlign;
47  return *this;
48 }
49 
51 {
52  return mFormat.get();
53 }
54 
56 {
57  mFormat.reset( format );
58 }
59 
60 QVariantMap QgsTableCell::properties( const QgsReadWriteContext &context ) const
61 {
62  QVariantMap res;
63  res.insert( QStringLiteral( "content" ), mContent );
64  res.insert( QStringLiteral( "background" ), mBackgroundColor );
65  res.insert( QStringLiteral( "foreground" ), mForegroundColor );
66  if ( mFormat )
67  {
68  res.insert( QStringLiteral( "format_type" ), mFormat->id() );
69  res.insert( QStringLiteral( "format" ), mFormat->configuration( context ) );
70  }
71 
72  if ( mTextFormat.isValid() )
73  {
74  QDomDocument textDoc;
75  const QDomElement textElem = mTextFormat.writeXml( textDoc, context );
76  textDoc.appendChild( textElem );
77  res.insert( QStringLiteral( "text_format" ), textDoc.toString() );
78  }
79 
80  res.insert( QStringLiteral( "halign" ), static_cast< int >( mHAlign ) );
81  res.insert( QStringLiteral( "valign" ), static_cast< int >( mVAlign ) );
82 
83  return res;
84 }
85 
86 void QgsTableCell::setProperties( const QVariantMap &properties, const QgsReadWriteContext &context )
87 {
88  mContent = properties.value( QStringLiteral( "content" ) );
89  mBackgroundColor = properties.value( QStringLiteral( "background" ) ).value< QColor >();
90  mForegroundColor = properties.value( QStringLiteral( "foreground" ) ).value< QColor >();
91 
92  QDomDocument doc;
93  QDomElement elem;
94  const QString textXml = properties.value( QStringLiteral( "text_format" ) ).toString();
95  if ( !textXml.isEmpty() )
96  {
97  doc.setContent( textXml );
98  elem = doc.documentElement();
99  mTextFormat.readXml( elem, context );
100  }
101  else
102  {
103  mTextFormat = QgsTextFormat();
104  }
105 
106  if ( properties.contains( QStringLiteral( "format_type" ) ) )
107  {
108 
109  mFormat.reset( QgsApplication::numericFormatRegistry()->create( properties.value( QStringLiteral( "format_type" ) ).toString(),
110  properties.value( QStringLiteral( "format" ) ).toMap(),
111  context ) );
112  }
113  else
114  {
115  mFormat.reset();
116  }
117 
118  mHAlign = static_cast< Qt::Alignment >( properties.value( QStringLiteral( "halign" ), Qt::AlignLeft ).toInt() );
119  mVAlign = static_cast< Qt::Alignment >( properties.value( QStringLiteral( "valign" ), Qt::AlignVCenter ).toInt() );
120 }
121 
123 {
124  return mHAlign;
125 }
126 
127 void QgsTableCell::setHorizontalAlignment( Qt::Alignment alignment )
128 {
129  mHAlign = alignment;
130 }
131 
132 Qt::Alignment QgsTableCell::verticalAlignment() const
133 {
134  return mVAlign;
135 }
136 
137 void QgsTableCell::setVerticalAlignment( Qt::Alignment alignment )
138 {
139  mVAlign = alignment;
140 }
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
The class is used as a container of context for various read/write operations on other objects.
Encapsulates the contents and formatting of a single table cell.
Definition: qgstablecell.h:36
void setHorizontalAlignment(Qt::Alignment alignment)
Sets the horizontal alignment for text in the cell.
Qt::Alignment horizontalAlignment() const
Returns the horizontal alignment for text in the cell.
QgsTableCell(const QVariant &content=QVariant())
Constructor for QgsTableCell, with the specified content.
void setVerticalAlignment(Qt::Alignment alignment)
Sets the vertical alignment for text in the cell.
Qt::Alignment verticalAlignment() const
Returns the vertical alignment for text in the cell.
void setProperties(const QVariantMap &properties, const QgsReadWriteContext &context)
Sets the properties for the cell.
QgsTableCell & operator=(const QgsTableCell &other)
QVariantMap properties(const QgsReadWriteContext &context) const
Returns the properties of the cell.
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the cell, or nullptr if no format is set.
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the cell, or nullptr if no specific format is set.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
bool isValid() const
Returns true if the format is valid.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.