QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  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 }
QgsTableCell::verticalAlignment
Qt::Alignment verticalAlignment() const
Returns the vertical alignment for text in the cell.
Definition: qgstablecell.cpp:132
qgsnumericformatregistry.h
QgsTableCell::setHorizontalAlignment
void setHorizontalAlignment(Qt::Alignment alignment)
Sets the horizontal alignment for text in the cell.
Definition: qgstablecell.cpp:127
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsTableCell::QgsTableCell
QgsTableCell(const QVariant &content=QVariant())
Constructor for QgsTableCell, with the specified content.
Definition: qgstablecell.cpp:22
QgsTableCell::~QgsTableCell
~QgsTableCell()
qgsreadwritecontext.h
QgsTableCell::setVerticalAlignment
void setVerticalAlignment(Qt::Alignment alignment)
Sets the vertical alignment for text in the cell.
Definition: qgstablecell.cpp:137
QgsTableCell::operator=
QgsTableCell & operator=(const QgsTableCell &other)
Definition: qgstablecell.cpp:38
QgsNumericFormat
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
Definition: qgsnumericformat.h:218
qgsapplication.h
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsTableCell::numericFormat
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the cell, or nullptr if no format is set.
Definition: qgstablecell.cpp:50
QgsTableCell
Encapsulates the contents and formatting of a single table cell.
Definition: qgstablecell.h:36
QgsTableCell::setNumericFormat
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the cell, or nullptr if no specific format is set.
Definition: qgstablecell.cpp:55
qgstablecell.h
QgsTextFormat::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
Definition: qgstextformat.cpp:406
QgsTableCell::properties
QVariantMap properties(const QgsReadWriteContext &context) const
Returns the properties of the cell.
Definition: qgstablecell.cpp:60
QgsTextFormat::isValid
bool isValid() const
Returns true if the format is valid.
Definition: qgstextformat.cpp:93
qgsnumericformat.h
QgsTableCell::setProperties
void setProperties(const QVariantMap &properties, const QgsReadWriteContext &context)
Sets the properties for the cell.
Definition: qgstablecell.cpp:86
QgsTableCell::horizontalAlignment
Qt::Alignment horizontalAlignment() const
Returns the horizontal alignment for text in the cell.
Definition: qgstablecell.cpp:122
QgsApplication::numericFormatRegistry
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
Definition: qgsapplication.cpp:2273
QgsTextFormat::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
Definition: qgstextformat.cpp:555