QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 {
48 if ( !mBlockSignals )
49 emit backgroundColorChanged( mBackgroundColorButton->color() );
50 } );
51 connect( mBackgroundColorButton, &QgsColorButton::cleared, this, [ = ]
52 {
53 if ( !mBlockSignals )
54 emit backgroundColorChanged( QColor() );
55 } );
56
57 connect( mFormatNumbersCheckBox, &QCheckBox::stateChanged, this, [ = ]( int state )
58 {
59 mCustomizeFormatButton->setEnabled( state == Qt::Checked );
60 if ( state != Qt::PartiallyChecked )
61 mFormatNumbersCheckBox->setTristate( false );
62 if ( !mBlockSignals )
64 } );
65
66 connect( mFontButton, &QgsFontButton::changed, this, [ = ]
67 {
68 if ( !mBlockSignals )
69 emit textFormatChanged();
70 } );
71
72 mCustomizeFormatButton->setEnabled( false );
73 connect( mCustomizeFormatButton, &QPushButton::clicked, this, [ = ]
74 {
76 widget->setFormat( mNumericFormat.get() );
77 widget->setPanelTitle( tr( "Number Format" ) );
78 connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [ = ]
79 {
80 mNumericFormat.reset( widget->format() );
82 } );
83 openPanel( widget );
84 } );
85
86 connect( mRowHeightSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [ = ]( double height )
87 {
88 if ( !mBlockSignals )
89 {
90 emit rowHeightChanged( height );
91
92 mBlockSignals++;
93 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
94 mBlockSignals--;
95 }
96 } );
97 connect( mColumnWidthSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [ = ]( double width )
98 {
99 if ( !mBlockSignals )
100 {
101 emit columnWidthChanged( width );
102
103 mBlockSignals++;
104 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
105 mBlockSignals--;
106 }
107 } );
108
109 connect( mHorizontalAlignComboBox, &QgsAlignmentComboBox::changed, this, [ = ]
110 {
111 if ( !mBlockSignals )
112 {
113 emit horizontalAlignmentChanged( mHorizontalAlignComboBox->currentAlignment() );
114 }
115 } );
116
117 connect( mVerticalAlignComboBox, &QgsAlignmentComboBox::changed, this, [ = ]
118 {
119 if ( !mBlockSignals )
120 {
121 emit verticalAlignmentChanged( mVerticalAlignComboBox->currentAlignment() );
122 }
123 } );
124
125 connect( mExpressionEdit, qOverload<const QString &>( &QgsFieldExpressionWidget::fieldChanged ), this, [ = ]( const QString & expression )
126 {
127 if ( !mBlockSignals )
128 {
129 emit cellPropertyChanged( expression.isEmpty() ? QgsProperty() : QgsProperty::fromExpression( expression ) );
130 }
131 } );
132
133 mExpressionEdit->setAllowEmptyFieldName( true );
134
135 mExpressionEdit->registerExpressionContextGenerator( this );
136 mFontButton->registerExpressionContextGenerator( this );
137}
138
140{
141 if ( !mNumericFormat || mFormatNumbersCheckBox->checkState() != Qt::Checked )
142 return nullptr;
143
144 return mNumericFormat->clone();
145}
146
148{
149 return mFontButton->textFormat();
150}
151
153{
154 mBlockSignals++;
155 mBackgroundColorButton->setColor( color );
156 mBlockSignals--;
157}
158
160{
161 mNumericFormat.reset( format ? format->clone() : nullptr );
162 mBlockSignals++;
163 mFormatNumbersCheckBox->setTristate( isMixedFormat );
164 mFormatNumbersCheckBox->setCheckState( isMixedFormat ? Qt::PartiallyChecked : ( mNumericFormat.get() ? Qt::Checked : Qt::Unchecked ) );
165 mBlockSignals--;
166}
167
169{
170 mBlockSignals++;
171 mFontButton->setTextFormat( format );
172 mBlockSignals--;
173}
174
176{
177 mBlockSignals++;
178 if ( height < 0 )
179 mRowHeightSpinBox->setClearValue( 0, tr( "Mixed" ) );
180 else
181 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
182 mRowHeightSpinBox->setValue( height < 0 ? 0 : height );
183 mBlockSignals--;
184}
185
187{
188 mBlockSignals++;
189 if ( width < 0 )
190 mColumnWidthSpinBox->setClearValue( 0, tr( "Mixed" ) );
191 else
192 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
193 mColumnWidthSpinBox->setValue( width < 0 ? 0 : width );
194 mBlockSignals--;
195}
196
198{
199 mBlockSignals++;
200 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
201 mHorizontalAlignComboBox->setCurrentIndex( -1 );
202 else
203 mHorizontalAlignComboBox->setCurrentAlignment( alignment );
204 mBlockSignals--;
205}
206
208{
209 mBlockSignals++;
210 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
211 mVerticalAlignComboBox->setCurrentIndex( -1 );
212 else
213 mVerticalAlignComboBox->setCurrentAlignment( alignment );
214 mBlockSignals--;
215}
216
218{
219 mBlockSignals++;
220 if ( !property.isActive() )
221 mExpressionEdit->setExpression( QString() );
222 else
223 mExpressionEdit->setExpression( property.asExpression() );
224 mBlockSignals--;
225}
226
228{
229 mExpressionEdit->setLayer( layer );
230}
231
233{
234 mContextGenerator = generator;
235}
236
238{
239 QgsExpressionContext context;
240 if ( mContextGenerator )
241 context = mContextGenerator->createExpressionContext();
242
244 // TODO -- could set real row/column numbers here, in certain circumstances...
245 cellScope->setVariable( QStringLiteral( "row_number" ), 0 );
246 cellScope->setVariable( QStringLiteral( "column_number" ), 0 );
247 context.appendScope( cellScope );
248
249 context.setHighlightedVariables( QStringList() << QStringLiteral( "row_number" ) << QStringLiteral( "column_number" ) );
250 return context;
251}
252
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.