Skip to content

Commit b9f5477

Browse files
committed
[fix #10168] expression widget: grey out when disabled
use const when possible
1 parent c1c9d3f commit b9f5477

6 files changed

+72
-35
lines changed

python/gui/qgsfieldcombobox.sip

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class QgsFieldComboBox : QComboBox
2424

2525
//! Returns the currently used layer
2626
QgsVectorLayer* layer();
27+
28+
void changeEvent( QEvent* event );
2729

2830
signals:
2931
//! the signal is emitted when the currently selected field changes

python/gui/qgsfieldexpressionwidget.sip

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ class QgsFieldExpressionWidget : QWidget
2020
/**
2121
* @brief currentField returns the currently selected field or expression if allowed
2222
* @param isExpression determines if the string returned is the name of a field or an expression
23+
* @param isValid determines if the expression (or field) returned is valid
2324
*/
24-
QString currentField( bool *isExpression = 0 );
25+
QString currentField( bool *isExpression = 0, bool *isValid = 0 );
2526

2627
//! Returns the currently used layer
2728
QgsVectorLayer* layer();
29+
30+
void changeEvent( QEvent* event );
2831

2932
signals:
3033
//! the signal is emitted when the currently selected field changes

src/gui/qgsfieldexpressionwidget.cpp

+53-26
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ QgsFieldExpressionWidget::QgsFieldExpressionWidget( QWidget *parent )
4444
layout->addWidget( mButton );
4545

4646
connect( mCombo->lineEdit(), SIGNAL( textEdited( QString ) ), this, SLOT( expressionEdited( QString ) ) );
47-
connect( mCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( indexChanged( int ) ) );
47+
connect( mCombo, SIGNAL( activated( int ) ), this, SLOT( currentFieldChanged( int ) ) );
4848
connect( mButton, SIGNAL( clicked() ), this, SLOT( editExpression() ) );
4949
}
5050

@@ -58,15 +58,18 @@ void QgsFieldExpressionWidget::setGeomCalculator( const QgsDistanceArea &da )
5858
mDa = QSharedPointer<const QgsDistanceArea>( new QgsDistanceArea( da ) );
5959
}
6060

61-
QString QgsFieldExpressionWidget::currentField( bool *isExpression )
61+
QString QgsFieldExpressionWidget::currentField( bool *isExpression , bool *isValid )
6262
{
6363
if ( isExpression )
6464
{
6565
*isExpression = false;
6666
}
67+
if ( isValid )
68+
{
69+
*isValid = true;
70+
}
6771

6872
int i = mCombo->currentIndex();
69-
7073
const QModelIndex index = mFieldModel->index( i, 0 );
7174
if ( !index.isValid() )
7275
{
@@ -77,6 +80,10 @@ QString QgsFieldExpressionWidget::currentField( bool *isExpression )
7780
{
7881
*isExpression = mFieldModel->data( index, QgsFieldModel::IsExpressionRole ).toBool();
7982
}
83+
if ( isValid )
84+
{
85+
*isValid = mFieldModel->data( index, QgsFieldModel::ExpressionValidityRole ).toBool();
86+
}
8087
QString expression = mFieldModel->data( index, QgsFieldModel::ExpressionRole ).toString();
8188
return expression;
8289
}
@@ -100,7 +107,7 @@ void QgsFieldExpressionWidget::setLayer( QgsVectorLayer *layer )
100107
mFieldModel->setLayer( layer );
101108
}
102109

103-
void QgsFieldExpressionWidget::setField( QString fieldName )
110+
void QgsFieldExpressionWidget::setField( const QString fieldName )
104111
{
105112
if ( fieldName.isEmpty() )
106113
return;
@@ -112,6 +119,8 @@ void QgsFieldExpressionWidget::setField( QString fieldName )
112119
idx = mFieldModel->setExpression( fieldName );
113120
}
114121
mCombo->setCurrentIndex( idx.row() );
122+
123+
currentFieldChanged();
115124
}
116125

117126
void QgsFieldExpressionWidget::editExpression()
@@ -136,39 +145,57 @@ void QgsFieldExpressionWidget::editExpression()
136145
}
137146
}
138147

139-
void QgsFieldExpressionWidget::expressionEdited( QString expression )
148+
void QgsFieldExpressionWidget::expressionEdited( const QString expression )
149+
{
150+
QModelIndex idx = mFieldModel->setExpression( expression );
151+
mCombo->setCurrentIndex( idx.row() );
152+
currentFieldChanged();
153+
}
154+
155+
void QgsFieldExpressionWidget::changeEvent( QEvent* event )
140156
{
141-
mFieldModel->removeExpression();
142-
setField( expression );
157+
if ( event->type() == QEvent::EnabledChange )
158+
{
159+
updateLineEditStyle();
160+
}
143161
}
144162

145-
void QgsFieldExpressionWidget::indexChanged( int i )
163+
void QgsFieldExpressionWidget::currentFieldChanged( int i /* =0 */ )
146164
{
147165
Q_UNUSED( i );
148-
bool isExpression;
149-
QString fieldName = currentField( &isExpression );
150-
bool isValid = true;
151166

152-
QFont font = mCombo->lineEdit()->font();
153-
font.setItalic( isExpression );
154-
mCombo->lineEdit()->setFont( font );
167+
updateLineEditStyle();
155168

169+
bool isExpression, isValid;
170+
QString fieldName = currentField( &isExpression, &isValid );
171+
emit fieldChanged( fieldName );
172+
emit fieldChanged( fieldName, isValid );
173+
}
174+
175+
void QgsFieldExpressionWidget::updateLineEditStyle()
176+
{
156177
QPalette palette;
157-
palette.setColor( QPalette::Text, Qt::black );
158-
if ( isExpression )
178+
if ( !isEnabled() )
159179
{
160-
QModelIndex idx = mFieldModel->indexFromName( fieldName );
161-
if ( idx.isValid() )
180+
palette.setColor( QPalette::Text, Qt::gray );
181+
}
182+
else
183+
{
184+
bool isExpression, isValid;
185+
currentField( &isExpression, &isValid );
186+
187+
QFont font = mCombo->lineEdit()->font();
188+
font.setItalic( isExpression );
189+
mCombo->lineEdit()->setFont( font );
190+
191+
if ( isExpression && !isValid )
192+
{
193+
palette.setColor( QPalette::Text, Qt::red );
194+
}
195+
else
162196
{
163-
isValid = mFieldModel->data( idx, QgsFieldModel::ExpressionValidityRole ).toBool();
164-
if ( !isValid )
165-
{
166-
palette.setColor( QPalette::Text, Qt::red );
167-
}
197+
palette.setColor( QPalette::Text, Qt::black );
168198
}
169199
}
170200
mCombo->lineEdit()->setPalette( palette );
171-
172-
emit fieldChanged( fieldName );
173-
emit fieldChanged( fieldName, isValid );
174201
}

src/gui/qgsfieldexpressionwidget.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
4747
/**
4848
* @brief currentField returns the currently selected field or expression if allowed
4949
* @param isExpression determines if the string returned is the name of a field or an expression
50+
* @param isValid determines if the expression (or field) returned is valid
5051
*/
51-
QString currentField( bool *isExpression = 0 );
52+
QString currentField( bool *isExpression = 0, bool *isValid = 0 );
5253

5354
//! Returns the currently used layer
5455
QgsVectorLayer* layer();
5556

57+
void changeEvent( QEvent* event );
58+
5659
signals:
5760
//! the signal is emitted when the currently selected field changes
5861
void fieldChanged( QString fieldName );
@@ -68,16 +71,18 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
6871
void setLayer( QgsMapLayer* layer );
6972

7073
//! sets the current field or expression in the widget
71-
void setField( QString fieldName );
74+
void setField( const QString fieldName );
7275

7376
protected slots:
7477
//! open the expression dialog to edit the current or add a new expression
7578
void editExpression();
7679

7780
//! when expression is edited by the user in the line edit
78-
void expressionEdited( QString expression );
81+
void expressionEdited( const QString expression );
82+
83+
void currentFieldChanged( int i = 0 );
7984

80-
void indexChanged( int i );
85+
void updateLineEditStyle();
8186

8287
private:
8388
QComboBox* mCombo;

src/gui/qgsfieldmodel.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ QgsFieldModel::QgsFieldModel( QObject *parent )
2828
{
2929
}
3030

31-
QModelIndex QgsFieldModel::indexFromName( QString fieldName )
31+
QModelIndex QgsFieldModel::indexFromName( const QString fieldName )
3232
{
3333
int r = mFields.indexFromName( fieldName );
3434
QModelIndex idx = index( r, 0 );
@@ -104,7 +104,7 @@ void QgsFieldModel::setAllowExpression( bool allowExpression )
104104
}
105105
}
106106

107-
QModelIndex QgsFieldModel::setExpression( QString expression )
107+
QModelIndex QgsFieldModel::setExpression( const QString expression )
108108
{
109109
if ( !mAllowExpression )
110110
return QModelIndex();

src/gui/qgsfieldmodel.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
4949
explicit QgsFieldModel( QObject *parent = 0 );
5050

5151
//! return the index corresponding to a given fieldName
52-
QModelIndex indexFromName( QString fieldName );
52+
QModelIndex indexFromName( const QString fieldName );
5353

5454
//! returns the currently used layer
5555
void setAllowExpression( bool allowExpression );
@@ -59,7 +59,7 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
5959
* @brief setExpression sets a single expression to be added after the fields at the end of the model
6060
* @return the model index of the newly added expression
6161
*/
62-
QModelIndex setExpression( QString expression );
62+
QModelIndex setExpression( const QString expression );
6363

6464
//! remove expressions from the model
6565
void removeExpression();

0 commit comments

Comments
 (0)