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 );
125 mPanelHandled =
true;
131 mPanelHandled =
true;
141QList<QgsConditionalStyle> QgsFieldConditionalFormatWidget::getStyles()
143 QList<QgsConditionalStyle> styles;
144 if ( fieldRadio->isChecked() )
148 else if ( rowRadio->isChecked() )
150 styles = mLayer->conditionalStyles()->rowStyles();
156void QgsFieldConditionalFormatWidget::addNewRule()
172 QList<QgsConditionalStyle> styles;
175 styles.append( style );
178 styles.append( style );
181 styles.append( style );
184 styles.append( style );
187 styles.append( style );
190 styles.append( style );
194void QgsFieldConditionalFormatWidget::typeChanged()
199void QgsFieldConditionalFormatWidget::reloadStyles()
203 const auto constGetStyles = getStyles();
207 listView->setIconSize( size );
209 for (
const QgsConditionalStyle &style : constGetStyles )
211 QStandardItem *item =
new QStandardItem( style.
displayText() );
213 mModel->appendRow( item );
217void QgsFieldConditionalFormatWidget::fieldChanged(
const QString &fieldName )
219 Q_UNUSED( fieldName )
223void QgsFieldConditionalFormatWidget::deleteCurrentRule()
228 QList<QgsConditionalStyle> styles = getStyles();
229 styles.removeAt( mEditIndex );
231 if ( fieldRadio->isChecked() )
233 fieldName = mFieldCombo->currentField();
234 mLayer->conditionalStyles()->setFieldStyles( fieldName, styles );
236 else if ( rowRadio->isChecked() )
238 mLayer->conditionalStyles()->setRowStyles( styles );
261 btnBackgroundColor->setColor( QColor() );
262 btnTextColor->setColor( QColor() );
263 checkIcon->setChecked(
false );
264 btnChangeIcon->setIcon( QIcon() );
265 btnBackgroundColor->setToNoColor();
266 btnTextColor->setToNoColor();
268 mFontBoldBtn->setChecked(
false );
269 mFontItalicBtn->setChecked(
false );
270 mFontStrikethroughBtn->setChecked(
false );
271 mFontUnderlineBtn->setChecked(
false );
274 mFontUnderlineBtn->setMinimumSize( buttonSize, buttonSize );
275 mFontUnderlineBtn->setMaximumSize( buttonSize, buttonSize );
276 mFontStrikethroughBtn->setMinimumSize( buttonSize, buttonSize );
277 mFontStrikethroughBtn->setMaximumSize( buttonSize, buttonSize );
278 mFontBoldBtn->setMinimumSize( buttonSize, buttonSize );
279 mFontBoldBtn->setMaximumSize( buttonSize, buttonSize );
280 mFontItalicBtn->setMinimumSize( buttonSize, buttonSize );
281 mFontItalicBtn->setMaximumSize( buttonSize, buttonSize );
287 connect( btnBuildExpression, &QAbstractButton::clicked,
this, &QgsEditConditionalFormatRuleWidget::setExpression );
288 connect( mPresetsList,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsEditConditionalFormatRuleWidget::presetSet );
290 btnBackgroundColor->setAllowOpacity(
true );
291 btnBackgroundColor->setShowNoColor(
true );
292 btnTextColor->setAllowOpacity(
true );
293 btnTextColor->setShowNoColor(
true );
294 mPresetsModel =
new QStandardItemModel( mPresetsList );
295 mPresetsList->setModel( mPresetsModel );
299 connect( checkIcon, &QCheckBox::toggled, btnChangeIcon, &QWidget::setEnabled );
309 mRuleEdit->setText( style.
rule() );
310 mNameEdit->setText( style.
name() );
311 setFormattingFromStyle( style );
318 style.
setRule( mRuleEdit->text() );
319 style.
setName( mNameEdit->text() );
321 const QColor backColor = btnBackgroundColor->color();
322 const QColor fontColor = btnTextColor->color();
324 QFont font = mFontFamilyCmbBx->currentFont();
325 font.setBold( mFontBoldBtn->isChecked() );
326 font.setItalic( mFontItalicBtn->isChecked() );
327 font.setStrikeOut( mFontStrikethroughBtn->isChecked() );
328 font.setUnderline( mFontUnderlineBtn->isChecked() );
332 if ( checkIcon->isChecked() )
343void QgsEditConditionalFormatRuleWidget::setExpression()
347 context.setHighlightedVariables( QStringList() << u
"value"_s );
350 dlg.setWindowTitle( tr(
"Conditional Style Rule Expression" ) );
354 const QString expression = dlg.expressionBuilder()->expressionText();
355 mRuleEdit->setText( expression );
359void QgsEditConditionalFormatRuleWidget::presetSet(
int index )
361 if ( index == -1 || mPresets.isEmpty() )
364 const int styleIndex = mPresetsList->currentData( Qt::UserRole + 1 ).toInt();
365 const QgsConditionalStyle style = mPresets.at( styleIndex );
366 setFormattingFromStyle( style );
369void QgsEditConditionalFormatRuleWidget::setFormattingFromStyle(
const QgsConditionalStyle &style )
372 btnTextColor->setColor( style.
textColor() );
373 if (
auto *lSymbol = style.
symbol() )
375 btnChangeIcon->setSymbol( lSymbol->clone() );
376 checkIcon->setChecked(
true );
380 checkIcon->setChecked(
false );
382 const QFont font = style.
font();
383 mFontBoldBtn->setChecked( font.bold() );
384 mFontItalicBtn->setChecked( font.italic() );
385 mFontStrikethroughBtn->setChecked( font.strikeOut() );
386 mFontUnderlineBtn->setChecked( font.underline() );
387 mFontFamilyCmbBx->setCurrentFont( font );
393 mPresetsModel->clear();
394 QStandardItem *item =
new QStandardItem( QString() );
395 mPresetsModel->appendRow( item );
401 QStandardItem *item =
new QStandardItem( u
"abc - 123"_s );
405 item->setForeground( style.
textColor() );
407 item->setIcon( style.
icon() );
408 item->setFont( style.
font() );
409 item->setData( i, Qt::UserRole + 1 );
410 mPresetsModel->appendRow( item );
411 mPresets.append( style );
415 mPresetsList->setCurrentIndex( 0 );
420 mRuleEdit->setText( rule );
423bool QgsEditConditionalFormatRuleWidget::isCustomSet()
425 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.