Skip to content
Permalink
Browse files

[fix #10168] expression widget: grey out when disabled

use const when possible
  • Loading branch information
3nids committed May 6, 2014
1 parent c1c9d3f commit b9f5477b7ffa73c1f8ae79fd623a76c9fc5bd2ce
@@ -24,6 +24,8 @@ class QgsFieldComboBox : QComboBox

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

void changeEvent( QEvent* event );

signals:
//! the signal is emitted when the currently selected field changes
@@ -20,11 +20,14 @@ class QgsFieldExpressionWidget : QWidget
/**
* @brief currentField returns the currently selected field or expression if allowed
* @param isExpression determines if the string returned is the name of a field or an expression
* @param isValid determines if the expression (or field) returned is valid
*/
QString currentField( bool *isExpression = 0 );
QString currentField( bool *isExpression = 0, bool *isValid = 0 );

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

void changeEvent( QEvent* event );

signals:
//! the signal is emitted when the currently selected field changes
@@ -44,7 +44,7 @@ QgsFieldExpressionWidget::QgsFieldExpressionWidget( QWidget *parent )
layout->addWidget( mButton );

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

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

QString QgsFieldExpressionWidget::currentField( bool *isExpression )
QString QgsFieldExpressionWidget::currentField( bool *isExpression , bool *isValid )
{
if ( isExpression )
{
*isExpression = false;
}
if ( isValid )
{
*isValid = true;
}

int i = mCombo->currentIndex();

const QModelIndex index = mFieldModel->index( i, 0 );
if ( !index.isValid() )
{
@@ -77,6 +80,10 @@ QString QgsFieldExpressionWidget::currentField( bool *isExpression )
{
*isExpression = mFieldModel->data( index, QgsFieldModel::IsExpressionRole ).toBool();
}
if ( isValid )
{
*isValid = mFieldModel->data( index, QgsFieldModel::ExpressionValidityRole ).toBool();
}
QString expression = mFieldModel->data( index, QgsFieldModel::ExpressionRole ).toString();
return expression;
}
@@ -100,7 +107,7 @@ void QgsFieldExpressionWidget::setLayer( QgsVectorLayer *layer )
mFieldModel->setLayer( layer );
}

void QgsFieldExpressionWidget::setField( QString fieldName )
void QgsFieldExpressionWidget::setField( const QString fieldName )
{
if ( fieldName.isEmpty() )
return;
@@ -112,6 +119,8 @@ void QgsFieldExpressionWidget::setField( QString fieldName )
idx = mFieldModel->setExpression( fieldName );
}
mCombo->setCurrentIndex( idx.row() );

currentFieldChanged();
}

void QgsFieldExpressionWidget::editExpression()
@@ -136,39 +145,57 @@ void QgsFieldExpressionWidget::editExpression()
}
}

void QgsFieldExpressionWidget::expressionEdited( QString expression )
void QgsFieldExpressionWidget::expressionEdited( const QString expression )
{
QModelIndex idx = mFieldModel->setExpression( expression );
mCombo->setCurrentIndex( idx.row() );
currentFieldChanged();
}

void QgsFieldExpressionWidget::changeEvent( QEvent* event )
{
mFieldModel->removeExpression();
setField( expression );
if ( event->type() == QEvent::EnabledChange )
{
updateLineEditStyle();
}
}

void QgsFieldExpressionWidget::indexChanged( int i )
void QgsFieldExpressionWidget::currentFieldChanged( int i /* =0 */ )
{
Q_UNUSED( i );
bool isExpression;
QString fieldName = currentField( &isExpression );
bool isValid = true;

QFont font = mCombo->lineEdit()->font();
font.setItalic( isExpression );
mCombo->lineEdit()->setFont( font );
updateLineEditStyle();

bool isExpression, isValid;
QString fieldName = currentField( &isExpression, &isValid );
emit fieldChanged( fieldName );
emit fieldChanged( fieldName, isValid );
}

void QgsFieldExpressionWidget::updateLineEditStyle()
{
QPalette palette;
palette.setColor( QPalette::Text, Qt::black );
if ( isExpression )
if ( !isEnabled() )
{
QModelIndex idx = mFieldModel->indexFromName( fieldName );
if ( idx.isValid() )
palette.setColor( QPalette::Text, Qt::gray );
}
else
{
bool isExpression, isValid;
currentField( &isExpression, &isValid );

QFont font = mCombo->lineEdit()->font();
font.setItalic( isExpression );
mCombo->lineEdit()->setFont( font );

if ( isExpression && !isValid )
{
palette.setColor( QPalette::Text, Qt::red );
}
else
{
isValid = mFieldModel->data( idx, QgsFieldModel::ExpressionValidityRole ).toBool();
if ( !isValid )
{
palette.setColor( QPalette::Text, Qt::red );
}
palette.setColor( QPalette::Text, Qt::black );
}
}
mCombo->lineEdit()->setPalette( palette );

emit fieldChanged( fieldName );
emit fieldChanged( fieldName, isValid );
}
@@ -47,12 +47,15 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
/**
* @brief currentField returns the currently selected field or expression if allowed
* @param isExpression determines if the string returned is the name of a field or an expression
* @param isValid determines if the expression (or field) returned is valid
*/
QString currentField( bool *isExpression = 0 );
QString currentField( bool *isExpression = 0, bool *isValid = 0 );

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

void changeEvent( QEvent* event );

signals:
//! the signal is emitted when the currently selected field changes
void fieldChanged( QString fieldName );
@@ -68,16 +71,18 @@ class GUI_EXPORT QgsFieldExpressionWidget : public QWidget
void setLayer( QgsMapLayer* layer );

//! sets the current field or expression in the widget
void setField( QString fieldName );
void setField( const QString fieldName );

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

//! when expression is edited by the user in the line edit
void expressionEdited( QString expression );
void expressionEdited( const QString expression );

void currentFieldChanged( int i = 0 );

void indexChanged( int i );
void updateLineEditStyle();

private:
QComboBox* mCombo;
@@ -28,7 +28,7 @@ QgsFieldModel::QgsFieldModel( QObject *parent )
{
}

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

QModelIndex QgsFieldModel::setExpression( QString expression )
QModelIndex QgsFieldModel::setExpression( const QString expression )
{
if ( !mAllowExpression )
return QModelIndex();
@@ -49,7 +49,7 @@ class GUI_EXPORT QgsFieldModel : public QAbstractItemModel
explicit QgsFieldModel( QObject *parent = 0 );

//! return the index corresponding to a given fieldName
QModelIndex indexFromName( QString fieldName );
QModelIndex indexFromName( const QString fieldName );

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

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

0 comments on commit b9f5477

Please sign in to comment.
You can’t perform that action at this time.