Skip to content

Commit

Permalink
QgsFieldCombobBox do not allow expression
Browse files Browse the repository at this point in the history
    editExpression protected, setLayer uses QgsVectorLayer
    make epxression widget editable
  • Loading branch information
3nids committed May 2, 2014
1 parent 760f835 commit 86700d2
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 403 deletions.
39 changes: 12 additions & 27 deletions python/gui/qgsfieldcombobox.sip
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* @brief The QgsFieldComboBox is a combo box which displays the list of fields of a given layer.
* If allowed, expression may also be added at the bottom of the list of fields.
* It can be combined with a QgsExpressioButton to set the expression using the dedicated expression builder dialog.
* It might also be combined with a QgsMapLayerComboBox and the fields will be automatically updated according to the chosen layer.
* @see QgsExpressioButton, QgsMapLayerComboBox
* It might be combined with a QgsMapLayerComboBox to automatically update fields according to a chosen layer.
* If expression must be used, QgsFieldExpressionWidget shall be used instead.
* @see QgsMapLayerComboBox
* @note added in 2.3
*/
class QgsFieldComboBox : QComboBox
Expand All @@ -20,37 +19,23 @@ class QgsFieldComboBox : QComboBox
*/
explicit QgsFieldComboBox( QWidget *parent /TransferThis/ = 0 );

/**
* @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
*/
QString currentField( bool *isExpression = 0 );

//!! setAllowExpression sets if expression can be added the combo box
void setAllowExpression( bool allowExpression );
//! returns if the widget allows expressions to be added or not
bool allowExpression();

//! return the currently selected field
QString currentField();

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

signals:
/**
* @brief setField sets the currently selected field
* if expressions are allowed in the widget,
* then it will either set it as selected
* if it already exists, or it will add it otherwise
*/
//! the signal is emitted when the currently selected field changes
void fieldChanged( QString fieldName );

public slots:
/**
* @brief setLayer sets the layer of which the fields are listed
*/
//! set the layer of which the fields are listed
void setLayer( QgsMapLayer* layer );
/**
* @brief setField sets the currently selected field
*/

//! setField sets the currently selected field
void setField( QString fieldName );

protected slots:
void indexChanged( int i );
};
16 changes: 10 additions & 6 deletions python/gui/qgsfieldexpressionwidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ class QgsFieldExpressionWidget : QWidget
//! set the geometry calculator used in the expression dialog
void setGeomCalculator( const QgsDistanceArea &da );

//! return a pointer to the combo box in the widget
QgsFieldComboBox* fieldComboBox();

//! return a pointer to the tool button used in the widget
QToolButton* toolButton();

/**
* @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
Expand All @@ -32,13 +26,23 @@ class QgsFieldExpressionWidget : QWidget
//! Returns the currently used layer
QgsVectorLayer* layer();

signals:
//! the signal is emitted when the currently selected field changes
void fieldChanged( QString fieldName );

public slots:
//! set the layer used to display the fields and expression
void setLayer( QgsMapLayer* layer );

//! sets the current field or expression in the widget
void setField( 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 indexChanged( int i );
};
32 changes: 16 additions & 16 deletions python/gui/qgsfieldmodel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ class QgsFieldModel : QAbstractItemModel

public:
enum FieldRoles {
FieldNameRole = 33, /* Qt::UserRole + 1, SIP does not accept any arithmetic" */
FieldIndexRole = 34,
ExpressionRole = 35
/* SIP does not accept any arithmetic" */
FieldNameRole = 33, /* return field name if index corresponds to a field */
FieldIndexRole = 34, /* return field index if index corresponds to a field */
ExpressionRole = 35, /* return field name or expression */
IsExpressionRole = 36, /* return if index corresponds to an expression */
ExpressionValidityRole = 37 /* return if expression is valid or not */
};

/**
* @brief QgsFieldModel creates a model to display the fields of a given layer
*/
explicit QgsFieldModel( QObject *parent /TransferThis/ = 0 );

/**
* @brief indexFromName returns the index corresponding to a given fieldName
*/
//! return the index corresponding to a given fieldName
QModelIndex indexFromName( QString fieldName );

/**
* @brief setAllowExpression determines if expressions are allowed to be added to the model
*/
//! returns the currently used layer
void setAllowExpression( bool allowExpression );
bool allowExpression();

Expand All @@ -39,19 +38,20 @@ class QgsFieldModel : QAbstractItemModel
* @return the model index of the newly added expression
*/
QModelIndex setExpression( QString expression );

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

/**
* @brief layer returns the currently used layer
*/
//! returns the currently used layer
QgsMapLayer* layer();

public slots:
/**
* @brief setLayer sets the layer of whch fields are displayed
*/
//! set the layer of whch fields are displayed
void setLayer( QgsMapLayer *layer );


protected slots:
virtual void updateModel();

// QAbstractItemModel interface
public:
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget

public slots:
void changeCategorizedSymbol();
void categoryColumnChanged();
void categoryColumnChanged( QString field );
void categoriesDoubleClicked( const QModelIndex & idx );
void addCategory();
void addCategories();
void deleteCategories();
void deleteAllCategories();
void setExpression();

void rotationFieldChanged( QString fldName );
void sizeScaleFieldChanged( QString fldName );
Expand All @@ -39,9 +38,6 @@ class QgsCategorizedSymbolRendererV2Widget : QgsRendererV2Widget
// Called by virtual refreshSymbolView()
void populateCategories();

//! populate column combo
void populateColumns();

//! return row index for the currently selected category (-1 if on no selection)
int currentCategoryRow();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class QgsGraduatedSymbolRendererV2Widget : QgsRendererV2Widget

public slots:
void changeGraduatedSymbol();
void graduatedColumnChanged();
void setExpression();
void graduatedColumnChanged( QString field );
void classifyGraduated();
void reapplyColorRamp();
void rangesDoubleClicked( const QModelIndex & idx );
Expand Down Expand Up @@ -45,9 +44,6 @@ class QgsGraduatedSymbolRendererV2Widget : QgsRendererV2Widget
QList<int> selectedClasses();
QgsRangeList selectedRanges();

//! populate column combos in categorized and graduated page
void populateColumns();

void changeRangeSymbol( int rangeIdx );
void changeRange( int rangeIdx );

Expand Down
43 changes: 5 additions & 38 deletions src/gui/qgsfieldcombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,15 @@ void QgsFieldComboBox::setField( QString fieldName )
if ( idx.isValid() )
{
setCurrentIndex( idx.row() );
return;
}

if ( mAllowExpression )
else
{
mFieldModel->setExpression( fieldName );
setCurrentIndex( findText( fieldName ) );
return;
setCurrentIndex( -1 );
}
setCurrentIndex( -1 );
}

QString QgsFieldComboBox::currentField( bool *isExpression )
QString QgsFieldComboBox::currentField()
{
if ( isExpression )
{
*isExpression = false;
}

int i = currentIndex();

const QModelIndex index = mFieldModel->index( i, 0 );
Expand All @@ -74,31 +64,8 @@ QString QgsFieldComboBox::currentField( bool *isExpression )
return "";
}

QString fieldName = mFieldModel->data( index, QgsFieldModel::FieldNameRole ).toString();
if ( !fieldName.isEmpty() )
{
return fieldName;
}

if ( mAllowExpression )
{
QString expression = mFieldModel->data( index, QgsFieldModel::ExpressionRole ).toString();
if ( !expression.isEmpty() )
{
if ( isExpression )
{
*isExpression = true;
}
return expression;
}
}

return "";
}

void QgsFieldComboBox::setAllowExpression( bool allowExpression )
{
mFieldModel->setAllowExpression( allowExpression );
QString name = mFieldModel->data( index, QgsFieldModel::FieldNameRole ).toString();
return name;
}

void QgsFieldComboBox::indexChanged( int i )
Expand Down
36 changes: 8 additions & 28 deletions src/gui/qgsfieldcombobox.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ class QgsVectorLayer;

/**
* @brief The QgsFieldComboBox is a combo box which displays the list of fields of a given layer.
* If allowed, expression may also be added at the bottom of the list of fields.
* It can be combined with a QgsExpressioButton to set the expression using the dedicated expression builder dialog.
* It might also be combined with a QgsMapLayerComboBox and the fields will be automatically updated according to the chosen layer.
* @see QgsExpressioButton, QgsMapLayerComboBox
* It might be combined with a QgsMapLayerComboBox to automatically update fields according to a chosen layer.
* If expression must be used, QgsFieldExpressionWidget shall be used instead.
* @see QgsMapLayerComboBox
* @note added in 2.3
*/
class GUI_EXPORT QgsFieldComboBox : public QComboBox
Expand All @@ -40,47 +39,28 @@ class GUI_EXPORT QgsFieldComboBox : public QComboBox
*/
explicit QgsFieldComboBox( QWidget *parent = 0 );

/**
* @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
*/
QString currentField( bool *isExpression = 0 );

//!! setAllowExpression sets if expression can be added the combo box
void setAllowExpression( bool allowExpression );
//! returns if the widget allows expressions to be added or not
bool allowExpression() { return mAllowExpression; }
//! return the currently selected field
QString currentField();

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

signals:
/**
* @brief fieldChanged the signal is emitted when the currently selected field changes
*/
//! the signal is emitted when the currently selected field changes
void fieldChanged( QString fieldName );

public slots:
/**
* @brief setLayer sets the layer of which the fields are listed
*/
//! set the layer of which the fields are listed
void setLayer( QgsMapLayer* layer );

/**
* @brief setField sets the currently selected field
* if expressions are allowed in the widget,
* then it will either set it as selected
* if it already exists, or it will add it otherwise
*/
//! setField sets the currently selected field
void setField( QString fieldName );

protected slots:
void indexChanged( int i );

private:
QgsFieldModel* mFieldModel;
bool mAllowExpression;

};

#endif // QGSFIELDCOMBOBOX_H
Loading

0 comments on commit 86700d2

Please sign in to comment.