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