QGIS API Documentation 3.99.0-Master (d270888f95f)
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#include <QString>
25
26#include "moc_qgstableeditorformattingwidget.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QgsPanelWidget( parent )
32{
33 setupUi( this );
34 setPanelTitle( tr( "Cell Contents" ) );
35
36 mFormatNumbersCheckBox->setTristate( false );
37
38 mFontButton->setShowNullFormat( true );
39 mFontButton->setNoFormatString( tr( "Clear Formatting" ) );
40
41 mBackgroundColorButton->setAllowOpacity( true );
42 mBackgroundColorButton->setColorDialogTitle( tr( "Background Color" ) );
43 mBackgroundColorButton->setDefaultColor( QColor( 255, 255, 255 ) );
44 mBackgroundColorButton->setShowNull( true );
45
46 mHorizontalAlignComboBox->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight | Qt::AlignJustify );
47 mVerticalAlignComboBox->setAvailableAlignments( Qt::AlignTop | Qt::AlignVCenter | Qt::AlignBottom );
48
49 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
50 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
51
52 connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, [this] {
53 if ( !mBlockSignals )
54 emit backgroundColorChanged( mBackgroundColorButton->color() );
55 } );
56 connect( mBackgroundColorButton, &QgsColorButton::cleared, this, [this] {
57 if ( !mBlockSignals )
58 emit backgroundColorChanged( QColor() );
59 } );
60
61 connect( mFormatNumbersCheckBox, &QCheckBox::stateChanged, this, [this]( int state ) {
62 mCustomizeFormatButton->setEnabled( state == Qt::Checked );
63 if ( state != Qt::PartiallyChecked )
64 mFormatNumbersCheckBox->setTristate( false );
65 if ( !mBlockSignals )
67 } );
68
69 connect( mFontButton, &QgsFontButton::changed, this, [this] {
70 if ( !mBlockSignals )
71 emit textFormatChanged();
72 } );
73
74 mCustomizeFormatButton->setEnabled( false );
75 connect( mCustomizeFormatButton, &QPushButton::clicked, this, [this] {
77 widget->setFormat( mNumericFormat.get() );
78 widget->setPanelTitle( tr( "Number Format" ) );
79 connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [this, widget] {
80 mNumericFormat.reset( widget->format() );
82 } );
83 openPanel( widget );
84 } );
85
86 connect( mRowHeightSpinBox, qOverload<double>( &QDoubleSpinBox::valueChanged ), this, [this]( double height ) {
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, [this]( double width ) {
97 if ( !mBlockSignals )
98 {
99 emit columnWidthChanged( width );
100
101 mBlockSignals++;
102 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
103 mBlockSignals--;
104 }
105 } );
106
107 connect( mHorizontalAlignComboBox, &QgsAlignmentComboBox::changed, this, [this] {
108 if ( !mBlockSignals )
109 {
110 emit horizontalAlignmentChanged( mHorizontalAlignComboBox->currentAlignment() );
111 }
112 } );
113
114 connect( mVerticalAlignComboBox, &QgsAlignmentComboBox::changed, this, [this] {
115 if ( !mBlockSignals )
116 {
117 emit verticalAlignmentChanged( mVerticalAlignComboBox->currentAlignment() );
118 }
119 } );
120
121 connect( mExpressionEdit, qOverload<const QString &>( &QgsFieldExpressionWidget::fieldChanged ), this, [this]( const QString &expression ) {
122 if ( !mBlockSignals )
123 {
124 emit cellPropertyChanged( expression.isEmpty() ? QgsProperty() : QgsProperty::fromExpression( expression ) );
125 }
126 } );
127
128 mExpressionEdit->setAllowEmptyFieldName( true );
129
130 mExpressionEdit->registerExpressionContextGenerator( this );
131 mFontButton->registerExpressionContextGenerator( this );
132}
133
135{
136 if ( !mNumericFormat || mFormatNumbersCheckBox->checkState() != Qt::Checked )
137 return nullptr;
138
139 return mNumericFormat->clone();
140}
141
143{
144 return mFontButton->textFormat();
145}
146
148{
149 mBlockSignals++;
150 mBackgroundColorButton->setColor( color );
151 mBlockSignals--;
152}
153
155{
156 mNumericFormat.reset( format ? format->clone() : nullptr );
157 mBlockSignals++;
158 mFormatNumbersCheckBox->setTristate( isMixedFormat );
159 mFormatNumbersCheckBox->setCheckState( isMixedFormat ? Qt::PartiallyChecked : ( mNumericFormat.get() ? Qt::Checked : Qt::Unchecked ) );
160 mBlockSignals--;
161}
162
164{
165 mBlockSignals++;
166 mFontButton->setTextFormat( format );
167 mBlockSignals--;
168}
169
171{
172 mBlockSignals++;
173 if ( height < 0 )
174 mRowHeightSpinBox->setClearValue( 0, tr( "Mixed" ) );
175 else
176 mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
177 mRowHeightSpinBox->setValue( height < 0 ? 0 : height );
178 mBlockSignals--;
179}
180
182{
183 mBlockSignals++;
184 if ( width < 0 )
185 mColumnWidthSpinBox->setClearValue( 0, tr( "Mixed" ) );
186 else
187 mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );
188 mColumnWidthSpinBox->setValue( width < 0 ? 0 : width );
189 mBlockSignals--;
190}
191
193{
194 mBlockSignals++;
195 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
196 mHorizontalAlignComboBox->setCurrentIndex( -1 );
197 else
198 mHorizontalAlignComboBox->setCurrentAlignment( alignment );
199 mBlockSignals--;
200}
201
203{
204 mBlockSignals++;
205 if ( alignment & Qt::AlignHorizontal_Mask && alignment & Qt::AlignVertical_Mask )
206 mVerticalAlignComboBox->setCurrentIndex( -1 );
207 else
208 mVerticalAlignComboBox->setCurrentAlignment( alignment );
209 mBlockSignals--;
210}
211
213{
214 mBlockSignals++;
215 if ( !property.isActive() )
216 mExpressionEdit->setExpression( QString() );
217 else
218 mExpressionEdit->setExpression( property.asExpression() );
219 mBlockSignals--;
220}
221
223{
224 mExpressionEdit->setLayer( layer );
225}
226
228{
229 mContextGenerator = generator;
230}
231
233{
234 QgsExpressionContext context;
235 if ( mContextGenerator )
236 context = mContextGenerator->createExpressionContext();
237
239 // TODO -- could set real row/column numbers here, in certain circumstances...
240 cellScope->setVariable( u"row_number"_s, 0 );
241 cellScope->setVariable( u"column_number"_s, 0 );
242 context.appendScope( cellScope );
243
244 context.setHighlightedVariables( QStringList() << u"row_number"_s << u"column_number"_s );
245 return context;
246}
247
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:83
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.