Skip to content

Commit

Permalink
expression button and expression capability added to the field model
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 2, 2014
1 parent 1f20630 commit 760f835
Show file tree
Hide file tree
Showing 14 changed files with 525 additions and 153 deletions.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
%Include qgsexpressionbuilderwidget.sip
%Include qgsexpressionhighlighter.sip
%Include qgsfieldcombobox.sip
%Include qgsfieldexpressionwidget.sip
%Include qgsfieldmodel.sip
%Include qgsfieldvalidator.sip
%Include qgsfiledropedit.sip
Expand Down
22 changes: 19 additions & 3 deletions python/gui/qgsfieldcombobox.sip
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/**
* @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
* @note added in 2.3
*/
class QgsFieldComboBox : QComboBox
Expand All @@ -17,13 +21,25 @@ class QgsFieldComboBox : QComboBox
explicit QgsFieldComboBox( QWidget *parent /TransferThis/ = 0 );

/**
* @brief currentField returns the currently selected field
* @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();
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();

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

signals:
/**
* @brief fieldChanged the signal is emitted when the currently selected field changes
* @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
*/
void fieldChanged( QString fieldName );

Expand Down
44 changes: 44 additions & 0 deletions python/gui/qgsfieldexpressionwidget.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

class QgsFieldExpressionWidget : QWidget
{
%TypeHeaderCode
#include "qgsfieldexpressionwidget.h"
%End

public:
/**
* @brief QgsFieldExpressionWidget creates a widget with a combo box to display the fields and expression and a button to open the expression dialog
*/
explicit QgsFieldExpressionWidget( QWidget *parent /TransferThis/ = 0 );

//! define the title used in the expression dialog
void setExpressionDialogTitle( QString title );

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

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

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 );

//! open the expression dialog to edit the current or add a new expression
void editExpression();
};
27 changes: 24 additions & 3 deletions python/gui/qgsfieldmodel.sip
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@

/**
* @brief The QgsFieldModel class is a model to display the list of fields of a layer in widgets.
* If allowed, expressions might be added to the end of the model.
* It can be associated with a QgsMapLayerModel to dynamically display a layer and its fields.
* @note added in 2.3
*/

class QgsFieldModel : QAbstractItemModel
{
%TypeHeaderCode
#include "qgsfieldmodel.h"
%End

public:
static const int FieldNameRole;
static const int FieldIndexRole;
enum FieldRoles {
FieldNameRole = 33, /* Qt::UserRole + 1, SIP does not accept any arithmetic" */
FieldIndexRole = 34,
ExpressionRole = 35
};

/**
* @brief QgsFieldModel creates a model to display the fields of a given layer
Expand All @@ -24,12 +27,30 @@ class QgsFieldModel : QAbstractItemModel
* @brief indexFromName returns the index corresponding to a given fieldName
*/
QModelIndex indexFromName( QString fieldName );

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

/**
* @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 );

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

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


// QAbstractItemModel interface
public:
Expand Down
63 changes: 15 additions & 48 deletions src/app/qgslabelinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas,
mPreviewSize = 24;

// main layer label-enabling connections
connect( chkEnableLabeling, SIGNAL( toggled( bool ) ), cboFieldName, SLOT( setEnabled( bool ) ) );
connect( chkEnableLabeling, SIGNAL( toggled( bool ) ), btnExpression, SLOT( setEnabled( bool ) ) );
connect( chkEnableLabeling, SIGNAL( toggled( bool ) ), mFieldExpressionWidget, SLOT( setEnabled( bool ) ) );
connect( chkEnableLabeling, SIGNAL( toggled( bool ) ), mLabelingFrame, SLOT( setEnabled( bool ) ) );

// connections for groupboxes with separate activation checkboxes (that need to honor data defined setting)
Expand Down Expand Up @@ -96,7 +95,6 @@ QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas,
connect( mLimitLabelChkBox, SIGNAL( toggled( bool ) ), mLimitLabelSpinBox, SLOT( setEnabled( bool ) ) );

connect( btnEngineSettings, SIGNAL( clicked() ), this, SLOT( showEngineConfigDialog() ) );
connect( btnExpression, SIGNAL( clicked() ), this, SLOT( showExpressionDialog() ) );

// set placement methods page based on geometry type
switch ( layer->geometryType() )
Expand All @@ -121,7 +119,14 @@ QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas,
mDirectSymbolsFrame->setVisible( layer->geometryType() == QGis::Line );
mMinSizeFrame->setVisible( layer->geometryType() != QGis::Point );

populateFieldNames(); // this is just for label text combo box
// field combo and expression button
mFieldExpressionWidget->setLayer( mLayer );
QgsDistanceArea myDa;
myDa.setSourceCrs( mLayer->crs().srsid() );
myDa.setEllipsoidalMode( QgisApp::instance()->mapCanvas()->mapSettings().hasCrsTransformEnabled() );
myDa.setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );
mFieldExpressionWidget->setGeomCalculator( myDa );

populateFontCapitalsComboBox();

// set up quadrant offset button group
Expand Down Expand Up @@ -234,15 +239,11 @@ void QgsLabelingGui::init()

// enable/disable main options based upon whether layer is being labeled
chkEnableLabeling->setChecked( lyr.enabled );
cboFieldName->setEnabled( chkEnableLabeling->isChecked() );
btnExpression->setEnabled( chkEnableLabeling->isChecked() );
mFieldExpressionWidget->setEnabled( chkEnableLabeling->isChecked() );
mLabelingFrame->setEnabled( chkEnableLabeling->isChecked() );

// add the current expression to the bottom of the list
if ( lyr.isExpression && !lyr.fieldName.isEmpty() )
cboFieldName->addItem( lyr.fieldName );

cboFieldName->setCurrentIndex( cboFieldName->findText( lyr.fieldName ) );
// set the current field or add the current expression to the bottom of the list
mFieldExpressionWidget->setField( lyr.fieldName );

// populate placement options
int distUnitIndex = lyr.distInMapUnits ? 1 : 0;
Expand Down Expand Up @@ -509,10 +510,9 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()

lyr.enabled = chkEnableLabeling->isChecked();

lyr.fieldName = cboFieldName->currentText();
// Check if we are an expression. Also treats expressions with just a column name as non expressions,
// this saves time later so we don't have to parse the expression tree.
lyr.isExpression = mLayer->fieldNameIndex( lyr.fieldName ) == -1 && !lyr.fieldName.isEmpty();
bool isExpression;
lyr.fieldName = mFieldExpressionWidget->currentField( &isExpression );
lyr.isExpression = isExpression;

lyr.dist = 0;
lyr.placementFlags = 0;
Expand Down Expand Up @@ -775,15 +775,6 @@ void QgsLabelingGui::setDataDefinedProperty( const QgsDataDefinedButton* ddBtn,
lyr.setDataDefinedProperty( p, map.value( "active" ).toInt(), map.value( "useexpr" ).toInt(), map.value( "expression" ), map.value( "field" ) );
}

void QgsLabelingGui::populateFieldNames()
{
const QgsFields& fields = mLayer->pendingFields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
cboFieldName->addItem( fields[idx].name() );
}
}

void QgsLabelingGui::populateDataDefinedButtons( QgsPalLayerSettings& s )
{
// don't register enable/disable siblings, since visual feedback from data defined buttons should be enough,
Expand Down Expand Up @@ -1175,30 +1166,6 @@ void QgsLabelingGui::showEngineConfigDialog()
dlg.exec();
}

void QgsLabelingGui::showExpressionDialog()
{
QgsExpressionBuilderDialog dlg( mLayer, cboFieldName->currentText() , this );
dlg.setWindowTitle( tr( "Expression based label" ) );

QgsDistanceArea myDa;
myDa.setSourceCrs( mLayer->crs().srsid() );
myDa.setEllipsoidalMode( QgisApp::instance()->mapCanvas()->mapSettings().hasCrsTransformEnabled() );
myDa.setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );
dlg.setGeomCalculator( myDa );

if ( dlg.exec() == QDialog::Accepted )
{
QString expression = dlg.expressionText();
//Only add the expression if the user has entered some text.
if ( !expression.isEmpty() )
{
cboFieldName->addItem( expression );
cboFieldName->setCurrentIndex( cboFieldName->count() - 1 );
}
}
activateWindow(); // set focus back parent
}

void QgsLabelingGui::syncDefinedCheckboxFrame( QgsDataDefinedButton* ddBtn, QCheckBox* chkBx, QFrame* f )
{
if ( ddBtn->isActive() && !chkBx->isChecked() )
Expand Down
1 change: 0 additions & 1 deletion src/app/qgslabelinggui.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
void apply();
void changeTextColor( const QColor &color );
void showEngineConfigDialog();
void showExpressionDialog();
void changeBufferColor( const QColor &color );

void updateUi();
Expand Down
3 changes: 3 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ qgsexpressionselectiondialog.cpp
qgsextentgroupbox.cpp
qgsfeatureselectiondlg.cpp
qgsfieldcombobox.cpp
qgsfieldexpressionwidget.cpp
qgsfieldmodel.cpp
qgsfieldvalidator.cpp
qgsfiledropedit.cpp
Expand Down Expand Up @@ -233,6 +234,7 @@ qgsexpressionselectiondialog.h
qgsextentgroupbox.h
qgsfeatureselectiondlg.h
qgsfieldcombobox.h
qgsfieldexpressionwidget.h
qgsfieldmodel.h
qgsfieldvalidator.h
qgsfilterlineedit.h
Expand Down Expand Up @@ -303,6 +305,7 @@ qgsexpressionhighlighter.h
qgsexpressionselectiondialog.h
qgsfeatureselectiondlg.h
qgsfieldcombobox.h
qgsfieldexpressionwidget.h
qgsfieldmodel.h
qgsfieldvalidator.h
qgsfiledropedit.h
Expand Down
53 changes: 48 additions & 5 deletions src/gui/qgsfieldcombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,41 @@ void QgsFieldComboBox::setLayer( QgsMapLayer *layer )
mFieldModel->setLayer( layer );
}

QgsVectorLayer *QgsFieldComboBox::layer()
{
QgsMapLayer* layer = mFieldModel->layer();
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer );
if ( vl )
return vl;
else
return 0;
}

void QgsFieldComboBox::setField( QString fieldName )
{
QModelIndex idx = mFieldModel->indexFromName( fieldName );
if ( idx.isValid() )
{
setCurrentIndex( idx.row() );
return;
}
else

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

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

int i = currentIndex();

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

QString name = mFieldModel->data( index, QgsFieldModel::FieldNameRole ).toString();
return name;
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 );
}

void QgsFieldComboBox::indexChanged( int i )
Expand Down
Loading

0 comments on commit 760f835

Please sign in to comment.