QGIS API Documentation 3.29.0-Master (19d7edcfed)
qgstableeditorformattingwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstableeditorformattingwidget.cpp
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
18#include "qgsnumericformat.h"
19#include "qgis.h"
20#include "qgsproperty.h"
21#include <QPointer>
22
24 : QgsPanelWidget( parent )
25{
26 setupUi( this );
27 setPanelTitle( tr( "Cell Contents" ) );
28
29 mFormatNumbersCheckBox->setTristate( false );
30
31 mFontButton->setShowNullFormat( true );
32 mFontButton->setNoFormatString( tr( "Clear Formatting" ) );
33
34 mBackgroundColorButton->setAllowOpacity( true );
35 mBackgroundColorButton->setColorDialogTitle( tr( "Background Color" ) );
36 mBackgroundColorButton->setDefaultColor( QColor( 255, 255, 255 ) );
37 mBackgroundColorButton->setShowNull( true );
38
39 mHorizontalAlignComboBox->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight | Qt::AlignJustify );
40 mVerticalAlignComboBox->setAvailableAlignments( Qt::AlignTop | Qt::AlignVCenter | Qt::AlignBottom );
41
42 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
43 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
44
45 connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, [ = ]
46 {
47 if ( !mBlockSignals )
48 emit backgroundColorChanged( mBackgroundColorButton->color() );
49 } );
50 connect( mBackgroundColorButton, &QgsColorButton::cleared, this, [ = ]
51 {
52 if ( !mBlockSignals )
53 emit backgroundColorChanged( QColor() );
54 } );
55
56 connect( mFormatNumbersCheckBox, &QCheckBox::stateChanged, this, [ = ]( int state )
57 {
58 mCustomizeFormatButton->setEnabled( state == Qt::Checked );
59 if ( state != Qt::PartiallyChecked )
60 mFormatNumbersCheckBox->setTristate( false );
61 if ( !mBlockSignals )
63 } );
64
65 connect( mFontButton, &QgsFontButton::changed, this, [ = ]
66 {
67 if ( !mBlockSignals )
68 emit textFormatChanged();
69 } );
70
71 mCustomizeFormatButton->setEnabled( false );
72 connect( mCustomizeFormatButton, &QPushButton::clicked, this, [ = ]
73 {
75 widget->setFormat( mNumericFormat.get() );
76 widget->setPanelTitle( tr( "Number Format" ) );
77 connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [ = ]
78 {
79 mNumericFormat.reset( widget->format() );
81 } );
82 openPanel( widget );
83 } );
84
85 connect( mRowHeightSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [ = ]( double height )
86 {
87 if ( !mBlockSignals )
88 {
89 emit rowHeightChanged( height );
90
91 mBlockSignals++;
92 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
93 mBlockSignals--;
94 }
95 } );
96 connect( mColumnWidthSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [ = ]( double width )
97 {
98 if ( !mBlockSignals )
99 {
100 emit columnWidthChanged( width );
101
102 mBlockSignals++;
103 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
104 mBlockSignals--;
105 }
106 } );
107
108 connect( mHorizontalAlignComboBox, &QgsAlignmentComboBox::changed, this, [ = ]
109 {
110 if ( !mBlockSignals )
111 {
112 emit horizontalAlignmentChanged( mHorizontalAlignComboBox->currentAlignment() );
113 }
114 } );
115
116 connect( mVerticalAlignComboBox, &QgsAlignmentComboBox::changed, this, [ = ]
117 {
118 if ( !mBlockSignals )
119 {
120 emit verticalAlignmentChanged( mVerticalAlignComboBox->currentAlignment() );
121 }
122 } );
123
124 connect( mExpressionEdit, qOverload<const QString &>( &QgsFieldExpressionWidget::fieldChanged ), this, [ = ]( const QString & expression )
125 {
126 if ( !mBlockSignals )
127 {
128 emit cellPropertyChanged( expression.isEmpty() ? QgsProperty() : QgsProperty::fromExpression( expression ) );
129 }
130 } );
131
132 mExpressionEdit->setAllowEmptyFieldName( true );
133
134 mExpressionEdit->registerExpressionContextGenerator( this );
135 mFontButton->registerExpressionContextGenerator( this );
136}
137
139{
140 if ( !mNumericFormat || mFormatNumbersCheckBox->checkState() != Qt::Checked )
141 return nullptr;
142
143 return mNumericFormat->clone();
144}
145
147{
148 return mFontButton->textFormat();
149}
150
152{
153 mBlockSignals++;
154 mBackgroundColorButton->setColor( color );
155 mBlockSignals--;
156}
157
159{
160 mNumericFormat.reset( format ? format->clone() : nullptr );
161 mBlockSignals++;
162 mFormatNumbersCheckBox->setTristate( isMixedFormat );
163 mFormatNumbersCheckBox->setCheckState( isMixedFormat ? Qt::PartiallyChecked : ( mNumericFormat.get() ? Qt::Checked : Qt::Unchecked ) );
164 mBlockSignals--;
165}
166
168{
169 mBlockSignals++;
170 mFontButton->setTextFormat( format );
171 mBlockSignals--;
172}
173
175{
176 mBlockSignals++;
177 if ( height < 0 )
178 mRowHeightSpinBox->setClearValue( 0, tr( "Mixed" ) );
179 else
180 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
181 mRowHeightSpinBox->setValue( height < 0 ? 0 : height );
182 mBlockSignals--;
183}
184
186{
187 mBlockSignals++;
188 if ( width < 0 )
189 mColumnWidthSpinBox->setClearValue( 0, tr( "Mixed" ) );
190 else
191 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
192 mColumnWidthSpinBox->setValue( width < 0 ? 0 : width );
193 mBlockSignals--;
194}
195
197{
198 mBlockSignals++;
199 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
200 mHorizontalAlignComboBox->setCurrentIndex( -1 );
201 else
202 mHorizontalAlignComboBox->setCurrentAlignment( alignment );
203 mBlockSignals--;
204}
205
207{
208 mBlockSignals++;
209 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
210 mVerticalAlignComboBox->setCurrentIndex( -1 );
211 else
212 mVerticalAlignComboBox->setCurrentAlignment( alignment );
213 mBlockSignals--;
214}
215
217{
218 mBlockSignals++;
219 if ( !property.isActive() )
220 mExpressionEdit->setExpression( QString() );
221 else
222 mExpressionEdit->setExpression( property.asExpression() );
223 mBlockSignals--;
224}
225
227{
228 mContextGenerator = generator;
229}
230
232{
233 QgsExpressionContext context;
234 if ( mContextGenerator )
235 context = mContextGenerator->createExpressionContext();
236
238 // TODO -- could set real row/column numbers here, in certain circumstances...
239 cellScope->setVariable( QStringLiteral( "row_number" ), 0 );
240 cellScope->setVariable( QStringLiteral( "column_number" ), 0 );
241 context.appendScope( cellScope );
242
243 context.setHighlightedVariables( QStringList() << QStringLiteral( "row_number" ) << QStringLiteral( "column_number" ) );
244 return context;
245}
246
void changed()
Emitted when the alignment is changed.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
void cleared()
Emitted when the color is cleared (set to null).
Abstract interface for generating an expression context.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
void changed()
Emitted when the widget's text format settings are changed.
A widget which allows choice of numeric formats and the properties of them.
QgsNumericFormat * format() const
Returns a new format object representing the settings currently configured in the widget.
void changed()
Emitted whenever the format configured55 in the widget is changed.
void setFormat(const QgsNumericFormat *format)
Sets the format to show in the widget.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
Base class for any widget that can be shown as a inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
A store for object properties.
Definition: qgsproperty.h:230
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
bool isActive() const
Returns whether the property is currently active.
void cellPropertyChanged(const QgsProperty &property)
Emitted when the cell contents property shown in the widget is changed.
void textFormatChanged()
Emitted whenever the text format shown in the widget is changed.
void verticalAlignmentChanged(Qt::Alignment alignment)
Emitted when the vertical alignment shown in the widget is changed.
void rowHeightChanged(double height)
Emitted whenever the row height shown in the widget is changed.
QgsTextFormat textFormat() const
Returns the current text format shown in the widget.
void setVerticalAlignment(Qt::Alignment alignment)
Sets the vertical alignment to show in the widget.
void setTextFormat(const QgsTextFormat &format)
Sets the text format to show in the widget.
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
QgsTableEditorFormattingWidget(QWidget *parent=nullptr)
Constructor for QgsTableEditorFormattingWidget with the specified parent widget.
void setBackgroundColor(const QColor &color)
Sets the cell background color to show in the widget.
~QgsTableEditorFormattingWidget() override
void backgroundColorChanged(const QColor &color)
Emitted whenever the cell background color is changed in the widget.
QgsNumericFormat * numericFormat()
Returns the current numeric format shown in the widget, or a nullptr if no numeric format is set.
void setCellProperty(const QgsProperty &property)
Sets the cell content's property to show in the widget.
void numberFormatChanged()
Emitted whenever the numeric format shown in the widget is changed.
void setColumnWidth(double width)
Sets the column width to show in the widget, or 0 for automatic width.
void setHorizontalAlignment(Qt::Alignment alignment)
Sets the horizontal alignment to show in the widget.
void setNumericFormat(QgsNumericFormat *format, bool isMixedFormat)
Sets the numeric format to show in the widget, or nullptr if no numeric format is set.
void horizontalAlignmentChanged(Qt::Alignment alignment)
Emitted when the horizontal alignment shown in the widget is changed.
void columnWidthChanged(double width)
Emitted whenever the column width shown in the widget is changed.
void setRowHeight(double height)
Sets the row height to show in the widget, or 0 for automatic height.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41