QGIS API Documentation 3.99.0-Master (752b475928d)
Loading...
Searching...
No Matches
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
18#include "qgsapplication.h"
19#include "qgsnumericformat.h"
21#include "qgsreadwritecontext.h"
22
24 : mContent( content )
25{}
26
28 : mContent( other.mContent )
29 , mBackgroundColor( other.mBackgroundColor )
30 , mForegroundColor( other.mForegroundColor )
31 , mTextFormat( other.mTextFormat )
32 , mFormat( other.mFormat ? other.mFormat->clone() : nullptr )
33 , mHAlign( other.mHAlign )
34 , mVAlign( other.mVAlign )
35 , mRowSpan( other.mRowSpan )
36 , mColumnSpan( other.mColumnSpan )
37{}
38
40
42{
43 if ( &other == this )
44 return *this;
45
46 mContent = other.mContent;
47 mBackgroundColor = other.mBackgroundColor;
48 mForegroundColor = other.mForegroundColor;
49 mTextFormat = other.mTextFormat;
50 mFormat.reset( other.mFormat ? other.mFormat->clone() : nullptr );
51 mHAlign = other.mHAlign;
52 mVAlign = other.mVAlign;
53 mRowSpan = other.mRowSpan;
54 mColumnSpan = other.mColumnSpan;
55 return *this;
56}
57
59{
60 return mFormat.get();
61}
62
64{
65 mFormat.reset( format );
66}
67
68QVariantMap QgsTableCell::properties( const QgsReadWriteContext &context ) const
69{
70 QVariantMap res;
71 res.insert( QStringLiteral( "content" ), mContent );
72 res.insert( QStringLiteral( "background" ), mBackgroundColor );
73 res.insert( QStringLiteral( "foreground" ), mForegroundColor );
74 if ( mFormat )
75 {
76 res.insert( QStringLiteral( "format_type" ), mFormat->id() );
77 res.insert( QStringLiteral( "format" ), mFormat->configuration( context ) );
78 }
79
80 if ( mTextFormat.isValid() )
81 {
82 QDomDocument textDoc;
83 const QDomElement textElem = mTextFormat.writeXml( textDoc, context );
84 textDoc.appendChild( textElem );
85 res.insert( QStringLiteral( "text_format" ), textDoc.toString() );
86 }
87
88 res.insert( QStringLiteral( "halign" ), static_cast< int >( mHAlign ) );
89 res.insert( QStringLiteral( "valign" ), static_cast< int >( mVAlign ) );
90 if ( mRowSpan > 1 )
91 res.insert( QStringLiteral( "row_span" ), mRowSpan );
92 if ( mColumnSpan > 1 )
93 res.insert( QStringLiteral( "column_span" ), mColumnSpan );
94
95 return res;
96}
97
98void QgsTableCell::setProperties( const QVariantMap &properties, const QgsReadWriteContext &context )
99{
100 mContent = properties.value( QStringLiteral( "content" ) );
101 mBackgroundColor = properties.value( QStringLiteral( "background" ) ).value< QColor >();
102 mForegroundColor = properties.value( QStringLiteral( "foreground" ) ).value< QColor >();
103
104 QDomDocument doc;
105 QDomElement elem;
106 const QString textXml = properties.value( QStringLiteral( "text_format" ) ).toString();
107 if ( !textXml.isEmpty() )
108 {
109 doc.setContent( textXml );
110 elem = doc.documentElement();
111 mTextFormat.readXml( elem, context );
112 }
113 else
114 {
115 mTextFormat = QgsTextFormat();
116 }
117
118 if ( properties.contains( QStringLiteral( "format_type" ) ) )
119 {
120
121 mFormat.reset( QgsApplication::numericFormatRegistry()->create( properties.value( QStringLiteral( "format_type" ) ).toString(),
122 properties.value( QStringLiteral( "format" ) ).toMap(),
123 context ) );
124 }
125 else
126 {
127 mFormat.reset();
128 }
129
130 mHAlign = static_cast< Qt::Alignment >( properties.value( QStringLiteral( "halign" ), Qt::AlignLeft ).toInt() );
131 mVAlign = static_cast< Qt::Alignment >( properties.value( QStringLiteral( "valign" ), Qt::AlignVCenter ).toInt() );
132
133 mRowSpan = properties.value( QStringLiteral( "row_span" ), 1 ).toInt();
134 mColumnSpan = properties.value( QStringLiteral( "column_span" ), 1 ).toInt();
135}
136
138{
139 return mHAlign;
140}
141
142void QgsTableCell::setHorizontalAlignment( Qt::Alignment alignment )
143{
144 mHAlign = alignment;
145}
146
148{
149 return mVAlign;
150}
151
152void QgsTableCell::setVerticalAlignment( Qt::Alignment alignment )
153{
154 mVAlign = alignment;
155}
156
158{
159 mRowSpan = rowSpan;
160 mColumnSpan = columnSpan;
161}
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
Abstract base class for numeric formatters, which allow for formatting a numeric value for display.
A container for the context for various read/write operations on objects.
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.
int columnSpan() const
Returns the column span for the cell.
void setSpan(int rowSpan, int columnSpan)
Sets the row and column span for 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.
QVariant content() const
Returns the cell's content.
void setProperties(const QVariantMap &properties, const QgsReadWriteContext &context)
Sets the properties for the cell.
QgsTableCell & operator=(const QgsTableCell &other)
int rowSpan() const
Returns the row span for the cell.
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.