QGIS API Documentation 3.39.0-Master (d85f3c2a281)
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#include "qgsapplication.h"
19#include "qgsnumericformat.h"
20#include "qgsreadwritecontext.h"
21
22QgsTableCell::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 , mRowSpan( other.mRowSpan )
35 , mColumnSpan( other.mColumnSpan )
36{}
37
39
41{
42 mContent = other.mContent;
43 mBackgroundColor = other.mBackgroundColor;
44 mForegroundColor = other.mForegroundColor;
45 mTextFormat = other.mTextFormat;
46 mFormat.reset( other.mFormat ? other.mFormat->clone() : nullptr );
47 mHAlign = other.mHAlign;
48 mVAlign = other.mVAlign;
49 mRowSpan = other.mRowSpan;
50 mColumnSpan = other.mColumnSpan;
51 return *this;
52}
53
55{
56 return mFormat.get();
57}
58
60{
61 mFormat.reset( format );
62}
63
64QVariantMap QgsTableCell::properties( const QgsReadWriteContext &context ) const
65{
66 QVariantMap res;
67 res.insert( QStringLiteral( "content" ), mContent );
68 res.insert( QStringLiteral( "background" ), mBackgroundColor );
69 res.insert( QStringLiteral( "foreground" ), mForegroundColor );
70 if ( mFormat )
71 {
72 res.insert( QStringLiteral( "format_type" ), mFormat->id() );
73 res.insert( QStringLiteral( "format" ), mFormat->configuration( context ) );
74 }
75
76 if ( mTextFormat.isValid() )
77 {
78 QDomDocument textDoc;
79 const QDomElement textElem = mTextFormat.writeXml( textDoc, context );
80 textDoc.appendChild( textElem );
81 res.insert( QStringLiteral( "text_format" ), textDoc.toString() );
82 }
83
84 res.insert( QStringLiteral( "halign" ), static_cast< int >( mHAlign ) );
85 res.insert( QStringLiteral( "valign" ), static_cast< int >( mVAlign ) );
86 if ( mRowSpan > 1 )
87 res.insert( QStringLiteral( "row_span" ), mRowSpan );
88 if ( mColumnSpan > 1 )
89 res.insert( QStringLiteral( "column_span" ), mColumnSpan );
90
91 return res;
92}
93
94void QgsTableCell::setProperties( const QVariantMap &properties, const QgsReadWriteContext &context )
95{
96 mContent = properties.value( QStringLiteral( "content" ) );
97 mBackgroundColor = properties.value( QStringLiteral( "background" ) ).value< QColor >();
98 mForegroundColor = properties.value( QStringLiteral( "foreground" ) ).value< QColor >();
99
100 QDomDocument doc;
101 QDomElement elem;
102 const QString textXml = properties.value( QStringLiteral( "text_format" ) ).toString();
103 if ( !textXml.isEmpty() )
104 {
105 doc.setContent( textXml );
106 elem = doc.documentElement();
107 mTextFormat.readXml( elem, context );
108 }
109 else
110 {
111 mTextFormat = QgsTextFormat();
112 }
113
114 if ( properties.contains( QStringLiteral( "format_type" ) ) )
115 {
116
117 mFormat.reset( QgsApplication::numericFormatRegistry()->create( properties.value( QStringLiteral( "format_type" ) ).toString(),
118 properties.value( QStringLiteral( "format" ) ).toMap(),
119 context ) );
120 }
121 else
122 {
123 mFormat.reset();
124 }
125
126 mHAlign = static_cast< Qt::Alignment >( properties.value( QStringLiteral( "halign" ), Qt::AlignLeft ).toInt() );
127 mVAlign = static_cast< Qt::Alignment >( properties.value( QStringLiteral( "valign" ), Qt::AlignVCenter ).toInt() );
128
129 mRowSpan = properties.value( QStringLiteral( "row_span" ), 1 ).toInt();
130 mColumnSpan = properties.value( QStringLiteral( "column_span" ), 1 ).toInt();
131}
132
134{
135 return mHAlign;
136}
137
138void QgsTableCell::setHorizontalAlignment( Qt::Alignment alignment )
139{
140 mHAlign = alignment;
141}
142
144{
145 return mVAlign;
146}
147
148void QgsTableCell::setVerticalAlignment( Qt::Alignment alignment )
149{
150 mVAlign = alignment;
151}
152
153void QgsTableCell::setSpan( int rowSpan, int columnSpan )
154{
155 mRowSpan = rowSpan;
156 mColumnSpan = columnSpan;
157}
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.
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.
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.
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.