QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgis.h"
19#include "qgsnumericformat.h"
21#include "qgsproperty.h"
22
23#include <QPointer>
24
25#include "moc_qgstableeditorformattingwidget.cpp"
26
28 : QgsPanelWidget( parent )
29{
30 setupUi( this );
31 setPanelTitle( tr( "Cell Contents" ) );
32
33 mFormatNumbersCheckBox->setTristate( false );
34
35 mFontButton->setShowNullFormat( true );
36 mFontButton->setNoFormatString( tr( "Clear Formatting" ) );
37
38 mBackgroundColorButton->setAllowOpacity( true );
39 mBackgroundColorButton->setColorDialogTitle( tr( "Background Color" ) );
40 mBackgroundColorButton->setDefaultColor( QColor( 255, 255, 255 ) );
41 mBackgroundColorButton->setShowNull( true );
42
43 mHorizontalAlignComboBox->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight | Qt::AlignJustify );
44 mVerticalAlignComboBox->setAvailableAlignments( Qt::AlignTop | Qt::AlignVCenter | Qt::AlignBottom );
45
46 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
47 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
48
49 connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, [this] {
50 if ( !mBlockSignals )
51 emit backgroundColorChanged( mBackgroundColorButton->color() );
52 } );
53 connect( mBackgroundColorButton, &QgsColorButton::cleared, this, [this] {
54 if ( !mBlockSignals )
55 emit backgroundColorChanged( QColor() );
56 } );
57
58 connect( mFormatNumbersCheckBox, &QCheckBox::stateChanged, this, [this]( int state ) {
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, [this] {
67 if ( !mBlockSignals )
68 emit textFormatChanged();
69 } );
70
71 mCustomizeFormatButton->setEnabled( false );
72 connect( mCustomizeFormatButton, &QPushButton::clicked, this, [this] {
74 widget->setFormat( mNumericFormat.get() );
75 widget->setPanelTitle( tr( "Number Format" ) );
76 connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [this, widget] {
77 mNumericFormat.reset( widget->format() );
79 } );
80 openPanel( widget );
81 } );
82
83 connect( mRowHeightSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [this]( double height ) {
84 if ( !mBlockSignals )
85 {
86 emit rowHeightChanged( height );
87
88 mBlockSignals++;
89 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
90 mBlockSignals--;
91 }
92 } );
93 connect( mColumnWidthSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [this]( double width ) {
94 if ( !mBlockSignals )
95 {
96 emit columnWidthChanged( width );
97
98 mBlockSignals++;
99 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
100 mBlockSignals--;
101 }
102 } );
103
104 connect( mHorizontalAlignComboBox, &QgsAlignmentComboBox::changed, this, [this] {
105 if ( !mBlockSignals )
106 {
107 emit horizontalAlignmentChanged( mHorizontalAlignComboBox->currentAlignment() );
108 }
109 } );
110
111 connect( mVerticalAlignComboBox, &QgsAlignmentComboBox::changed, this, [this] {
112 if ( !mBlockSignals )
113 {
114 emit verticalAlignmentChanged( mVerticalAlignComboBox->currentAlignment() );
115 }
116 } );
117
118 connect( mExpressionEdit, qOverload<const QString &>( &QgsFieldExpressionWidget::fieldChanged ), this, [this]( const QString &expression ) {
119 if ( !mBlockSignals )
120 {
121 emit cellPropertyChanged( expression.isEmpty() ? QgsProperty() : QgsProperty::fromExpression( expression ) );
122 }
123 } );
124
125 mExpressionEdit->setAllowEmptyFieldName( true );
126
127 mExpressionEdit->registerExpressionContextGenerator( this );
128 mFontButton->registerExpressionContextGenerator( this );
129}
130
132{
133 if ( !mNumericFormat || mFormatNumbersCheckBox->checkState() != Qt::Checked )
134 return nullptr;
135
136 return mNumericFormat->clone();
137}
138
140{
141 return mFontButton->textFormat();
142}
143
145{
146 mBlockSignals++;
147 mBackgroundColorButton->setColor( color );
148 mBlockSignals--;
149}
150
152{
153 mNumericFormat.reset( format ? format->clone() : nullptr );
154 mBlockSignals++;
155 mFormatNumbersCheckBox->setTristate( isMixedFormat );
156 mFormatNumbersCheckBox->setCheckState( isMixedFormat ? Qt::PartiallyChecked : ( mNumericFormat.get() ? Qt::Checked : Qt::Unchecked ) );
157 mBlockSignals--;
158}
159
161{
162 mBlockSignals++;
163 mFontButton->setTextFormat( format );
164 mBlockSignals--;
165}
166
168{
169 mBlockSignals++;
170 if ( height < 0 )
171 mRowHeightSpinBox->setClearValue( 0, tr( "Mixed" ) );
172 else
173 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
174 mRowHeightSpinBox->setValue( height < 0 ? 0 : height );
175 mBlockSignals--;
176}
177
179{
180 mBlockSignals++;
181 if ( width < 0 )
182 mColumnWidthSpinBox->setClearValue( 0, tr( "Mixed" ) );
183 else
184 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
185 mColumnWidthSpinBox->setValue( width < 0 ? 0 : width );
186 mBlockSignals--;
187}
188
190{
191 mBlockSignals++;
192 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
193 mHorizontalAlignComboBox->setCurrentIndex( -1 );
194 else
195 mHorizontalAlignComboBox->setCurrentAlignment( alignment );
196 mBlockSignals--;
197}
198
200{
201 mBlockSignals++;
202 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
203 mVerticalAlignComboBox->setCurrentIndex( -1 );
204 else
205 mVerticalAlignComboBox->setCurrentAlignment( alignment );
206 mBlockSignals--;
207}
208
210{
211 mBlockSignals++;
212 if ( !property.isActive() )
213 mExpressionEdit->setExpression( QString() );
214 else
215 mExpressionEdit->setExpression( property.asExpression() );
216 mBlockSignals--;
217}
218
220{
221 mExpressionEdit->setLayer( layer );
222}
223
225{
226 mContextGenerator = generator;
227}
228
230{
231 QgsExpressionContext context;
232 if ( mContextGenerator )
233 context = mContextGenerator->createExpressionContext();
234
236 // TODO -- could set real row/column numbers here, in certain circumstances...
237 cellScope->setVariable( QStringLiteral( "row_number" ), 0 );
238 cellScope->setVariable( QStringLiteral( "column_number" ), 0 );
239 context.appendScope( cellScope );
240
241 context.setHighlightedVariables( QStringList() << QStringLiteral( "row_number" ) << QStringLiteral( "column_number" ) );
242 return context;
243}
244
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.
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:80
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.
Abstract base class for numeric formatters, which allow for formatting a numeric value for display.
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
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.
void setColor(const QColor &color)
Sets the color that text will be rendered in.