QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
17#include "moc_qgstableeditorformattingwidget.cpp"
19#include "qgsnumericformat.h"
20#include "qgis.h"
21#include "qgsproperty.h"
22#include <QPointer>
23
25 : QgsPanelWidget( parent )
26{
27 setupUi( this );
28 setPanelTitle( tr( "Cell Contents" ) );
29
30 mFormatNumbersCheckBox->setTristate( false );
31
32 mFontButton->setShowNullFormat( true );
33 mFontButton->setNoFormatString( tr( "Clear Formatting" ) );
34
35 mBackgroundColorButton->setAllowOpacity( true );
36 mBackgroundColorButton->setColorDialogTitle( tr( "Background Color" ) );
37 mBackgroundColorButton->setDefaultColor( QColor( 255, 255, 255 ) );
38 mBackgroundColorButton->setShowNull( true );
39
40 mHorizontalAlignComboBox->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight | Qt::AlignJustify );
41 mVerticalAlignComboBox->setAvailableAlignments( Qt::AlignTop | Qt::AlignVCenter | Qt::AlignBottom );
42
43 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
44 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
45
46 connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, [=] {
47 if ( !mBlockSignals )
48 emit backgroundColorChanged( mBackgroundColorButton->color() );
49 } );
50 connect( mBackgroundColorButton, &QgsColorButton::cleared, this, [=] {
51 if ( !mBlockSignals )
52 emit backgroundColorChanged( QColor() );
53 } );
54
55 connect( mFormatNumbersCheckBox, &QCheckBox::stateChanged, this, [=]( int state ) {
56 mCustomizeFormatButton->setEnabled( state == Qt::Checked );
57 if ( state != Qt::PartiallyChecked )
58 mFormatNumbersCheckBox->setTristate( false );
59 if ( !mBlockSignals )
61 } );
62
63 connect( mFontButton, &QgsFontButton::changed, this, [=] {
64 if ( !mBlockSignals )
65 emit textFormatChanged();
66 } );
67
68 mCustomizeFormatButton->setEnabled( false );
69 connect( mCustomizeFormatButton, &QPushButton::clicked, this, [=] {
71 widget->setFormat( mNumericFormat.get() );
72 widget->setPanelTitle( tr( "Number Format" ) );
73 connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [=] {
74 mNumericFormat.reset( widget->format() );
76 } );
77 openPanel( widget );
78 } );
79
80 connect( mRowHeightSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [=]( double height ) {
81 if ( !mBlockSignals )
82 {
83 emit rowHeightChanged( height );
84
85 mBlockSignals++;
86 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
87 mBlockSignals--;
88 }
89 } );
90 connect( mColumnWidthSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [=]( double width ) {
91 if ( !mBlockSignals )
92 {
93 emit columnWidthChanged( width );
94
95 mBlockSignals++;
96 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
97 mBlockSignals--;
98 }
99 } );
100
101 connect( mHorizontalAlignComboBox, &QgsAlignmentComboBox::changed, this, [=] {
102 if ( !mBlockSignals )
103 {
104 emit horizontalAlignmentChanged( mHorizontalAlignComboBox->currentAlignment() );
105 }
106 } );
107
108 connect( mVerticalAlignComboBox, &QgsAlignmentComboBox::changed, this, [=] {
109 if ( !mBlockSignals )
110 {
111 emit verticalAlignmentChanged( mVerticalAlignComboBox->currentAlignment() );
112 }
113 } );
114
115 connect( mExpressionEdit, qOverload<const QString &>( &QgsFieldExpressionWidget::fieldChanged ), this, [=]( const QString &expression ) {
116 if ( !mBlockSignals )
117 {
118 emit cellPropertyChanged( expression.isEmpty() ? QgsProperty() : QgsProperty::fromExpression( expression ) );
119 }
120 } );
121
122 mExpressionEdit->setAllowEmptyFieldName( true );
123
124 mExpressionEdit->registerExpressionContextGenerator( this );
125 mFontButton->registerExpressionContextGenerator( this );
126}
127
129{
130 if ( !mNumericFormat || mFormatNumbersCheckBox->checkState() != Qt::Checked )
131 return nullptr;
132
133 return mNumericFormat->clone();
134}
135
137{
138 return mFontButton->textFormat();
139}
140
142{
143 mBlockSignals++;
144 mBackgroundColorButton->setColor( color );
145 mBlockSignals--;
146}
147
149{
150 mNumericFormat.reset( format ? format->clone() : nullptr );
151 mBlockSignals++;
152 mFormatNumbersCheckBox->setTristate( isMixedFormat );
153 mFormatNumbersCheckBox->setCheckState( isMixedFormat ? Qt::PartiallyChecked : ( mNumericFormat.get() ? Qt::Checked : Qt::Unchecked ) );
154 mBlockSignals--;
155}
156
158{
159 mBlockSignals++;
160 mFontButton->setTextFormat( format );
161 mBlockSignals--;
162}
163
165{
166 mBlockSignals++;
167 if ( height < 0 )
168 mRowHeightSpinBox->setClearValue( 0, tr( "Mixed" ) );
169 else
170 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
171 mRowHeightSpinBox->setValue( height < 0 ? 0 : height );
172 mBlockSignals--;
173}
174
176{
177 mBlockSignals++;
178 if ( width < 0 )
179 mColumnWidthSpinBox->setClearValue( 0, tr( "Mixed" ) );
180 else
181 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
182 mColumnWidthSpinBox->setValue( width < 0 ? 0 : width );
183 mBlockSignals--;
184}
185
187{
188 mBlockSignals++;
189 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
190 mHorizontalAlignComboBox->setCurrentIndex( -1 );
191 else
192 mHorizontalAlignComboBox->setCurrentAlignment( alignment );
193 mBlockSignals--;
194}
195
197{
198 mBlockSignals++;
199 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
200 mVerticalAlignComboBox->setCurrentIndex( -1 );
201 else
202 mVerticalAlignComboBox->setCurrentAlignment( alignment );
203 mBlockSignals--;
204}
205
207{
208 mBlockSignals++;
209 if ( !property.isActive() )
210 mExpressionEdit->setExpression( QString() );
211 else
212 mExpressionEdit->setExpression( property.asExpression() );
213 mBlockSignals--;
214}
215
217{
218 mExpressionEdit->setLayer( layer );
219}
220
222{
223 mContextGenerator = generator;
224}
225
227{
228 QgsExpressionContext context;
229 if ( mContextGenerator )
230 context = mContextGenerator->createExpressionContext();
231
233 // TODO -- could set real row/column numbers here, in certain circumstances...
234 cellScope->setVariable( QStringLiteral( "row_number" ), 0 );
235 cellScope->setVariable( QStringLiteral( "column_number" ), 0 );
236 context.appendScope( cellScope );
237
238 context.setHighlightedVariables( QStringList() << QStringLiteral( "row_number" ) << QStringLiteral( "column_number" ) );
239 return context;
240}
241
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.
Base class for all map layer types.
Definition qgsmaplayer.h:76
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.
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 setLayer(QgsMapLayer *layer)
Set the layer to be used for in the expression editor context.
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.