QGIS API Documentation  2.14.0-Essen
qgsfieldconditionalformatwidget.cpp
Go to the documentation of this file.
2 
4 #include "qgssymbolv2.h"
7 #include "qgsstylev2.h"
8 
10  : QWidget( parent )
11  , mLayer( nullptr )
12  , mEditIndex( 0 )
13  , mEditing( false )
14  , mSymbol( nullptr )
15 {
16  setupUi( this );
17  mDeleteButton->hide();
18  connect( mFieldCombo, SIGNAL( fieldChanged( QString ) ), SLOT( fieldChanged( QString ) ) );
19  connect( fieldRadio, SIGNAL( clicked() ), SLOT( reloadStyles() ) );
20  connect( rowRadio, SIGNAL( clicked() ), SLOT( reloadStyles() ) );
21  connect( mNewButton, SIGNAL( clicked() ), SLOT( addNewRule() ) );
22  connect( mSaveRule, SIGNAL( clicked() ), SLOT( saveRule() ) );
23  connect( mCancelButton, SIGNAL( clicked() ), SLOT( cancelRule() ) );
24  connect( mDeleteButton, SIGNAL( clicked() ), SLOT( deleteRule() ) );
25  connect( listView, SIGNAL( clicked( QModelIndex ) ), SLOT( ruleClicked( QModelIndex ) ) );
26  connect( btnChangeIcon , SIGNAL( clicked() ), SLOT( updateIcon() ) );
27  connect( btnBuildExpression , SIGNAL( clicked() ), SLOT( setExpression() ) );
28  connect( mPresetsList , SIGNAL( currentIndexChanged( int ) ), SLOT( presetSet( int ) ) );
29  btnBackgroundColor->setAllowAlpha( true );
30  btnBackgroundColor->setShowNoColor( true );
31  btnTextColor->setAllowAlpha( true );
32  btnTextColor->setShowNoColor( true );
33  mPresetsModel = new QStandardItemModel( listView );
34  mModel = new QStandardItemModel( listView );
35  listView->setModel( mModel );
36  mPresetsList->setModel( mPresetsModel );
37 
39 }
40 
42 {
43  delete mSymbol;
44 }
45 
46 void QgsFieldConditionalFormatWidget::updateIcon()
47 {
49 
50  QgsSymbolV2SelectorDialog dlg( mSymbol, QgsStyleV2::defaultStyle(), nullptr, this );
51  if ( !dlg.exec() )
52  {
53  return;
54  }
55 
56  QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mSymbol, btnChangeIcon->iconSize() );
57  btnChangeIcon->setIcon( icon );
58 }
59 
60 void QgsFieldConditionalFormatWidget::setExpression()
61 {
62  QgsExpressionContext context;
66  context.lastScope()->setVariable( "value", 0 );
67  context.setHighlightedVariables( QStringList() << "value" );
68 
69  QgsExpressionBuilderDialog dlg( mLayer, mRuleEdit->text(), this, "generic", context );
70  dlg.setWindowTitle( tr( "Conditional style rule expression" ) );
71 
72  if ( dlg.exec() )
73  {
74  QString expression = dlg.expressionBuilder()->expressionText();
75  mRuleEdit->setText( expression );
76  }
77 }
78 
79 void QgsFieldConditionalFormatWidget::presetSet( int index )
80 {
81  if ( index == -1 || mPresets.isEmpty() )
82  return;
83 
84  QgsConditionalStyle style = mPresets.at( index );
85  setFormattingFromStyle( style );
86 }
87 
89 {
90  mLayer = theLayer;
91  mFieldCombo->setLayer( theLayer );
92  mFieldCombo->setCurrentIndex( 0 );
93 }
94 
95 void QgsFieldConditionalFormatWidget::ruleClicked( const QModelIndex& index )
96 {
97  QList<QgsConditionalStyle> styles = getStyles();
98  QgsConditionalStyle style = styles.at( index.row() );
99  editStyle( index.row(), style );
100 }
101 
103 {
104  pages->setCurrentIndex( 1 );
105  mEditIndex = editIndex;
106  mEditing = true;
107  mDeleteButton->show();
108  loadStyle( style );
109 }
110 
112 {
113  mRuleEdit->setText( style.rule() );
114  mNameEdit->setText( style.name() );
115  setFormattingFromStyle( style );
116 }
117 void QgsFieldConditionalFormatWidget::setFormattingFromStyle( const QgsConditionalStyle& style )
118 {
119  btnBackgroundColor->setColor( style.backgroundColor() );
120  btnTextColor->setColor( style.textColor() );
121  if ( !style.icon().isNull() )
122  {
123  checkIcon->setChecked( true );
124  QIcon icon( style.icon() );
125  btnChangeIcon->setIcon( icon );
126  }
127  else
128  {
129  checkIcon->setChecked( false );
130  btnChangeIcon->setIcon( QIcon() );
131  }
132  if ( style.symbol() )
133  {
134  mSymbol = style.symbol()->clone();
135  }
136  else
137  {
138  mSymbol = nullptr;
139  }
140  QFont font = style.font();
141  mFontBoldBtn->setChecked( font.bold() );
142  mFontItalicBtn->setChecked( font.italic() );
143  mFontStrikethroughBtn->setChecked( font.strikeOut() );
144  mFontUnderlineBtn->setChecked( font.underline() );
145  mFontFamilyCmbBx->setFont( font );
146 }
147 
148 QList<QgsConditionalStyle> QgsFieldConditionalFormatWidget::getStyles()
149 {
151  if ( fieldRadio->isChecked() )
152  {
153  styles = mLayer->conditionalStyles()->fieldStyles( mFieldCombo->currentField() );
154  }
155  if ( rowRadio->isChecked() )
156  {
157  styles = mLayer->conditionalStyles()->rowStyles();
158  }
159  return styles;
160 }
161 
162 void QgsFieldConditionalFormatWidget::deleteRule()
163 {
164  QList<QgsConditionalStyle> styles = getStyles();
165  styles.removeAt( mEditIndex );
166  QString fieldName;
167  if ( fieldRadio->isChecked() )
168  {
169  fieldName = mFieldCombo->currentField();
170  mLayer->conditionalStyles()->setFieldStyles( fieldName, styles );
171  }
172  if ( rowRadio->isChecked() )
173  {
174  mLayer->conditionalStyles()->setRowStyles( styles );
175  }
176 
177  pages->setCurrentIndex( 0 );
178  reloadStyles();
179  emit rulesUpdated( fieldName );
180 }
181 
182 void QgsFieldConditionalFormatWidget::cancelRule()
183 {
184  pages->setCurrentIndex( 0 );
185  reloadStyles();
186  reset();
187 }
188 
189 void QgsFieldConditionalFormatWidget::addNewRule()
190 {
191  pages->setCurrentIndex( 1 );
192  reset();
193 }
194 
196 {
197  mSymbol = nullptr;
198  mNameEdit->clear();
199  mRuleEdit->clear();
200  if ( fieldRadio->isChecked() )
201  {
202  mRuleEdit->setText( "@value " );
203  }
204  btnBackgroundColor->setColor( QColor() );
205  btnTextColor->setColor( QColor() );
206  mPresetsList->setCurrentIndex( 0 );
207  mDeleteButton->hide();
208  mEditing = false;
209  checkIcon->setChecked( false );
210  btnChangeIcon->setIcon( QIcon() );
211  btnBackgroundColor->setToNoColor();
212  btnTextColor->setToNoColor();
213 
214  mFontBoldBtn->setChecked( false );
215  mFontItalicBtn->setChecked( false );
216  mFontStrikethroughBtn->setChecked( false );
217  mFontUnderlineBtn->setChecked( false );
218 }
219 
220 
222 {
223  mPresets.clear();
224  mPresetsModel->clear();
225  Q_FOREACH ( const QgsConditionalStyle& style, styles )
226  {
227  if ( style.isValid() )
228  {
229  QStandardItem* item = new QStandardItem( "abc - 123" );
230  if ( style.backgroundColor().isValid() )
231  item->setBackground( style.backgroundColor() );
232  if ( style.textColor().isValid() )
233  item->setForeground( style.textColor() );
234  if ( style.symbol() )
235  item->setIcon( style.icon() );
236  item->setFont( style.font() );
237  mPresetsModel->appendRow( item );
238  mPresets.append( style );
239  }
240  }
241  mPresetsList->setCurrentIndex( 0 );
242 }
243 
245 {
248  style.setBackgroundColor( QColor( 154, 216, 113 ) );
249  styles.append( style );
250  style = QgsConditionalStyle();
251  style.setBackgroundColor( QColor( 251, 193, 78 ) );
252  styles.append( style );
253  style = QgsConditionalStyle();
254  style.setBackgroundColor( QColor( 251, 154, 153 ) );
255  styles.append( style );
256  style = QgsConditionalStyle();
257  style.setTextColor( QColor( 154, 216, 113 ) );
258  styles.append( style );
259  style = QgsConditionalStyle();
260  style.setTextColor( QColor( 251, 193, 78 ) );
261  styles.append( style );
262  style = QgsConditionalStyle();
263  style.setTextColor( QColor( 251, 154, 153 ) );
264  styles.append( style );
265  return styles;
266 }
267 
268 void QgsFieldConditionalFormatWidget::saveRule()
269 {
270  QList<QgsConditionalStyle> styles = getStyles();
271 
273 
274  style.setRule( mRuleEdit->text() );
275  style.setName( mNameEdit->text() );
276 
277  QColor backColor = btnBackgroundColor->color();
278  QColor fontColor = btnTextColor->color();
279 
280  QFont font = mFontFamilyCmbBx->currentFont();
281  font.setBold( mFontBoldBtn->isChecked() );
282  font.setItalic( mFontItalicBtn->isChecked() );
283  font.setStrikeOut( mFontStrikethroughBtn->isChecked() );
284  font.setUnderline( mFontUnderlineBtn->isChecked() );
285  style.setFont( font );
286  style.setBackgroundColor( backColor );
287  style.setTextColor( fontColor );
288  if ( mSymbol && checkIcon->isChecked() )
289  {
290  style.setSymbol( mSymbol );
291  }
292  else
293  {
294  style.setSymbol( nullptr );
295  }
296  if ( mEditing )
297  {
298  styles.replace( mEditIndex, style );
299  }
300  else
301  {
302  styles.append( style );
303  }
304 
305  QString fieldName;
306  if ( fieldRadio->isChecked() )
307  {
308  fieldName = mFieldCombo->currentField();
309  mLayer->conditionalStyles()->setFieldStyles( fieldName, styles );
310  }
311  if ( rowRadio->isChecked() )
312  {
313  mLayer->conditionalStyles()->setRowStyles( styles );
314  }
315  pages->setCurrentIndex( 0 );
316  reloadStyles();
317  emit rulesUpdated( fieldName );
318  reset();
319 }
320 
321 void QgsFieldConditionalFormatWidget::reloadStyles()
322 {
323  mModel->clear();
324 
325  Q_FOREACH ( const QgsConditionalStyle& style, getStyles() )
326  {
327  QStandardItem* item = new QStandardItem( style.displayText() );
328  item->setIcon( QIcon( style.renderPreview() ) );
329  mModel->appendRow( item );
330  }
331 }
332 
333 void QgsFieldConditionalFormatWidget::fieldChanged( const QString& fieldName )
334 {
335  Q_UNUSED( fieldName );
336  reloadStyles();
337 }
338 
340 {
341  pages->setCurrentIndex( 0 );
342 }
343 
344 bool QgsFieldConditionalFormatWidget::isCustomSet()
345 {
346  return ( btnBackgroundColor->color().isValid()
347  || btnTextColor->color().isValid()
348  || mFontButtons->checkedId() != -1 );
349 }
void clear()
void setName(const QString &value)
Set the name of the style.
static unsigned index
void setPresets(const QList< QgsConditionalStyle > &styles)
Set the presets that can be used for quick pick.
void setupUi(QWidget *widget)
void setIcon(const QIcon &icon)
QColor textColor() const
The text color set for style.
void setRule(const QString &value)
Set the rule for the style.
void setBackground(const QBrush &brush)
QFont font() const
The font for the style.
virtual QgsSymbolV2 * clone() const =0
QStyle * style() const
QList< QgsConditionalStyle > fieldStyles(const QString &fieldName)
Returns the conditional styles set for the field UI properties.
const T & at(int i) const
void removeAt(int i)
void setUnderline(bool enable)
QgsConditionalLayerStyles * conditionalStyles() const
Return the conditional styles that are set for this layer.
int exec()
const QPixmap * icon() const
QList< QgsConditionalStyle > defaultPresets() const
The default presets for the widget.
QString tr(const char *sourceText, const char *disambiguation, int n)
void setVariable(const QString &name, const QVariant &value)
Convenience method for setting a variable in the context scope by name and value. ...
bool bold() const
bool italic() const
void setBold(bool enable)
void setColor(const QColor &color)
QList< QgsConditionalStyle > rowStyles()
void viewRules()
Switches the widget to the rules page.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
void reset()
Resets the formatting options to their default state.
void append(const T &value)
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
QPixmap icon() const
The icon set for style generated from the set symbol.
Conditional styling for a rule.
bool isEmpty() const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static QgsStyleV2 * defaultStyle()
return default application-wide style
Definition: qgsstylev2.cpp:51
int row() const
QgsFieldConditionalFormatWidget(QWidget *parent=nullptr)
Constructor for QgsFieldConditionalFormatWidget.
bool underline() const
QColor backgroundColor() const
The background color for style.
void setLayer(QgsVectorLayer *theLayer)
Sets the vector layer associated with the widget.
QString name() const
The name of the style.
void setFont(const QFont &font)
bool isNull() const
void setBackgroundColor(const QColor &value)
Set the background color for the style.
QPixmap renderPreview() const
Render a preview icon of the rule.
const QFont & font() const
void setItalic(bool enable)
void setFont(const QFont &value)
Set the font for the the style.
void setRowStyles(const QList< QgsConditionalStyle > &styles)
Set the conditional styles that apply to full rows of data in the attribute table.
void setForeground(const QBrush &brush)
void setSymbol(QgsSymbolV2 *value)
Set the icon for the style.
static QgsSymbolV2 * defaultSymbol(QGis::GeometryType geomType)
return new default symbol for specified geometry type
void setWindowTitle(const QString &)
void editStyle(int index, const QgsConditionalStyle &style)
Switches the widget to the edit style mode for the specified style.
void setStrikeOut(bool enable)
QString rule() const
The condition rule set for the style.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user...
bool isValid() const
isValid Check if this rule is valid.
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
void setFieldStyles(const QString &fieldName, const QList< QgsConditionalStyle > &styles)
Set the conditional styles for the field UI properties.
bool strikeOut() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
void appendRow(const QList< QStandardItem * > &items)
QString displayText() const
The name of the style.
A generic dialog for building expression strings.
void loadStyle(const QgsConditionalStyle &style)
QgsSymbolV2 * symbol() const
The symbol used to generate the icon for the style.
bool isValid() const
void replace(int i, const T &value)
void rulesUpdated(const QString &fieldName)
Emitted when the conditional styling rules are updated.
void setTextColor(const QColor &value)
Set the text color for the style.