29#include "moc_qgsfieldconditionalformatwidget.cpp"
31using namespace Qt::StringLiterals;
43 connect( fieldRadio, &QAbstractButton::clicked,
this, &QgsFieldConditionalFormatWidget::typeChanged );
44 connect( rowRadio, &QAbstractButton::clicked,
this, &QgsFieldConditionalFormatWidget::typeChanged );
45 connect( mNewButton, &QAbstractButton::clicked,
this, &QgsFieldConditionalFormatWidget::addNewRule );
46 connect( listView, &QAbstractItemView::clicked,
this, &QgsFieldConditionalFormatWidget::ruleClicked );
47 mModel =
new QStandardItemModel( listView );
48 listView->setModel( mModel );
50 connect( fieldRadio, &QRadioButton::toggled, mFieldCombo, &QWidget::setEnabled );
58 mFieldCombo->setLayer( layer );
59 mFieldCombo->setCurrentIndex( 0 );
60 fieldChanged( mFieldCombo->currentField() );
63void QgsFieldConditionalFormatWidget::ruleClicked(
const QModelIndex &index )
65 const QList<QgsConditionalStyle> styles = getStyles();
72 mEditIndex = editIndex;
73 mEditing = editIndex >= 0;
74 mPanelHandled =
false;
82 if ( fieldRadio->isChecked() && style.
rule().isEmpty() )
84 ruleWidget->
setRule( u
"@value "_s );
95 QList<QgsConditionalStyle> styles = getStyles();
98 styles.replace( mEditIndex, ruleWidget->
currentStyle() );
106 if ( fieldRadio->isChecked() )
108 fieldName = mFieldCombo->currentField();
109 mLayer->conditionalStyles()->setFieldStyles( fieldName, styles );
111 else if ( rowRadio->isChecked() )
113 mLayer->conditionalStyles()->setRowStyles( styles );
123 mPanelHandled =
true;
129 mPanelHandled =
true;
138QList<QgsConditionalStyle> QgsFieldConditionalFormatWidget::getStyles()
140 QList<QgsConditionalStyle> styles;
141 if ( fieldRadio->isChecked() )
145 else if ( rowRadio->isChecked() )
147 styles = mLayer->conditionalStyles()->rowStyles();
153void QgsFieldConditionalFormatWidget::addNewRule()
168 QList<QgsConditionalStyle> styles;
171 styles.append( style );
174 styles.append( style );
177 styles.append( style );
180 styles.append( style );
183 styles.append( style );
186 styles.append( style );
190void QgsFieldConditionalFormatWidget::typeChanged()
195void QgsFieldConditionalFormatWidget::reloadStyles()
199 const auto constGetStyles = getStyles();
203 listView->setIconSize( size );
205 for (
const QgsConditionalStyle &style : constGetStyles )
207 QStandardItem *item =
new QStandardItem( style.
displayText() );
209 mModel->appendRow( item );
213void QgsFieldConditionalFormatWidget::fieldChanged(
const QString &fieldName )
215 Q_UNUSED( fieldName )
219void QgsFieldConditionalFormatWidget::deleteCurrentRule()
224 QList<QgsConditionalStyle> styles = getStyles();
225 styles.removeAt( mEditIndex );
227 if ( fieldRadio->isChecked() )
229 fieldName = mFieldCombo->currentField();
230 mLayer->conditionalStyles()->setFieldStyles( fieldName, styles );
232 else if ( rowRadio->isChecked() )
234 mLayer->conditionalStyles()->setRowStyles( styles );
256 btnBackgroundColor->setColor( QColor() );
257 btnTextColor->setColor( QColor() );
258 checkIcon->setChecked(
false );
259 btnChangeIcon->setIcon( QIcon() );
260 btnBackgroundColor->setToNoColor();
261 btnTextColor->setToNoColor();
263 mFontBoldBtn->setChecked(
false );
264 mFontItalicBtn->setChecked(
false );
265 mFontStrikethroughBtn->setChecked(
false );
266 mFontUnderlineBtn->setChecked(
false );
269 mFontUnderlineBtn->setMinimumSize( buttonSize, buttonSize );
270 mFontUnderlineBtn->setMaximumSize( buttonSize, buttonSize );
271 mFontStrikethroughBtn->setMinimumSize( buttonSize, buttonSize );
272 mFontStrikethroughBtn->setMaximumSize( buttonSize, buttonSize );
273 mFontBoldBtn->setMinimumSize( buttonSize, buttonSize );
274 mFontBoldBtn->setMaximumSize( buttonSize, buttonSize );
275 mFontItalicBtn->setMinimumSize( buttonSize, buttonSize );
276 mFontItalicBtn->setMaximumSize( buttonSize, buttonSize );
282 connect( btnBuildExpression, &QAbstractButton::clicked,
this, &QgsEditConditionalFormatRuleWidget::setExpression );
283 connect( mPresetsList,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsEditConditionalFormatRuleWidget::presetSet );
285 btnBackgroundColor->setAllowOpacity(
true );
286 btnBackgroundColor->setShowNoColor(
true );
287 btnTextColor->setAllowOpacity(
true );
288 btnTextColor->setShowNoColor(
true );
289 mPresetsModel =
new QStandardItemModel( mPresetsList );
290 mPresetsList->setModel( mPresetsModel );
294 connect( checkIcon, &QCheckBox::toggled, btnChangeIcon, &QWidget::setEnabled );
304 mRuleEdit->setText( style.
rule() );
305 mNameEdit->setText( style.
name() );
306 setFormattingFromStyle( style );
313 style.
setRule( mRuleEdit->text() );
314 style.
setName( mNameEdit->text() );
316 const QColor backColor = btnBackgroundColor->color();
317 const QColor fontColor = btnTextColor->color();
319 QFont font = mFontFamilyCmbBx->currentFont();
320 font.setBold( mFontBoldBtn->isChecked() );
321 font.setItalic( mFontItalicBtn->isChecked() );
322 font.setStrikeOut( mFontStrikethroughBtn->isChecked() );
323 font.setUnderline( mFontUnderlineBtn->isChecked() );
327 if ( checkIcon->isChecked() )
338void QgsEditConditionalFormatRuleWidget::setExpression()
342 context.setHighlightedVariables( QStringList() << u
"value"_s );
345 dlg.setWindowTitle( tr(
"Conditional Style Rule Expression" ) );
349 const QString expression = dlg.expressionBuilder()->expressionText();
350 mRuleEdit->setText( expression );
354void QgsEditConditionalFormatRuleWidget::presetSet(
int index )
356 if ( index == -1 || mPresets.isEmpty() )
359 const int styleIndex = mPresetsList->currentData( Qt::UserRole + 1 ).toInt();
360 const QgsConditionalStyle style = mPresets.at( styleIndex );
361 setFormattingFromStyle( style );
364void QgsEditConditionalFormatRuleWidget::setFormattingFromStyle(
const QgsConditionalStyle &style )
367 btnTextColor->setColor( style.
textColor() );
368 if (
auto *lSymbol = style.
symbol() )
370 btnChangeIcon->setSymbol( lSymbol->clone() );
371 checkIcon->setChecked(
true );
375 checkIcon->setChecked(
false );
377 const QFont font = style.
font();
378 mFontBoldBtn->setChecked( font.bold() );
379 mFontItalicBtn->setChecked( font.italic() );
380 mFontStrikethroughBtn->setChecked( font.strikeOut() );
381 mFontUnderlineBtn->setChecked( font.underline() );
382 mFontFamilyCmbBx->setCurrentFont( font );
388 mPresetsModel->clear();
389 QStandardItem *item =
new QStandardItem( QString() );
390 mPresetsModel->appendRow( item );
396 QStandardItem *item =
new QStandardItem( u
"abc - 123"_s );
400 item->setForeground( style.
textColor() );
402 item->setIcon( style.
icon() );
403 item->setFont( style.
font() );
404 item->setData( i, Qt::UserRole + 1 );
405 mPresetsModel->appendRow( item );
406 mPresets.append( style );
410 mPresetsList->setCurrentIndex( 0 );
415 mRuleEdit->setText( rule );
418bool QgsEditConditionalFormatRuleWidget::isCustomSet()
420 return ( btnBackgroundColor->color().isValid() || btnTextColor->color().isValid() || mFontButtons->checkedId() != -1 );
static const double UI_SCALE_FACTOR
UI scaling factor.
QList< QgsConditionalStyle > fieldStyles(const QString &fieldName) const
Returns the conditional styles set for the field with matching fieldName.
Conditional styling for a rule.
QString displayText() const
The name of the style.
QString name() const
The name of the style.
void setSymbol(QgsSymbol *value)
Set the icon for the style.
void setName(const QString &value)
Set the name of the style.
QPixmap renderPreview(const QSize &size=QSize()) const
Render a preview icon of the rule, at the specified size.
void setTextColor(const QColor &value)
Set the text color for the style.
void setRule(const QString &value)
Set the rule for the style.
void setBackgroundColor(const QColor &value)
Set the background color for the style.
void setFont(const QFont &value)
Set the font for the style.
QColor backgroundColor() const
The background color for style.
QColor textColor() const
The text color set for style.
QString rule() const
The condition rule set for the style.
QFont font() const
The font for the style.
bool validTextColor() const
Check if the text color is valid for render.
QgsSymbol * symbol() const
The symbol used to generate the icon for the style.
bool isValid() const
isValid Check if this rule is valid.
QPixmap icon() const
The icon set for style generated from the set symbol.
bool validBackgroundColor() const
Check if the background color is valid for render.
A generic dialog for building expression strings.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void fieldChanged(const QString &fieldName)
Emitted when the currently selected field changes.
A marker symbol type, for rendering Point and MultiPoint geometries.
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Represents a vector layer which manages a vector based dataset.
QgsConditionalLayerStyles * conditionalStyles() const
Returns the conditional styles that are set for this layer.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
Single variable definition for use within a QgsExpressionContextScope.