Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add assistant support to QgsDataDefinedButton
  • Loading branch information
vmora authored and nyalldawson committed Apr 27, 2015
1 parent cf3a712 commit 6a8526f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
31 changes: 30 additions & 1 deletion python/gui/qgsdatadefinedbutton.sip
@@ -1,4 +1,24 @@
class QgsDataDefinedButton : QToolButton
/** \ingroup gui
* \class QgsDataDefinedAssistant
* An assistant (wizard) dialog, accessible from a QgsDataDefinedButton.
* Can be used to guide users through creation of an expression for the
* data defined button.
* @note added in 2.10
*/
class QgsDataDefinedAssistant: QDialog
{
%TypeHeaderCode
#include <qgsdatadefinedbutton.h>
%End
public:
virtual QgsDataDefined* dataDefined() const = 0 /Factory/;
};

/** \ingroup gui
* \class QgsDataDefinedButton
* A button for defining data source field mappings or expressions.
*/
class QgsDataDefinedButton : QToolButton
{
%TypeHeaderCode
#include <qgsdatadefinedbutton.h>
Expand Down Expand Up @@ -135,6 +155,15 @@ class QgsDataDefinedButton : QToolButton
*/
void clearCheckedWidgets();

/**
* Sets an assistant used to define the data defined object properties.
* Ownership of the assistant is transferred to the widget.
* @param assistant data defined assistant. Set to null to remove the assistant
* option from the button.
* @note added in 2.10
*/
void setAssistant( QgsDataDefinedAssistant * assistant /Transfer/ );

/**
* Common descriptions for expected input values
*/
Expand Down
35 changes: 35 additions & 0 deletions src/gui/qgsdatadefinedbutton.cpp
Expand Up @@ -84,6 +84,7 @@ QgsDataDefinedButton::QgsDataDefinedButton( QWidget* parent,
mActionPasteExpr = new QAction( tr( "Paste" ), this );
mActionCopyExpr = new QAction( tr( "Copy" ), this );
mActionClearExpr = new QAction( tr( "Clear" ), this );
mActionAssistant = new QAction( tr( "Assistant..." ), this );

// set up sibling widget connections
connect( this, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( disableEnabledWidgets( bool ) ) );
Expand Down Expand Up @@ -322,6 +323,11 @@ void QgsDataDefinedButton::aboutToShowMenu()
mDefineMenu->addAction( mActionPasteExpr );
}

if ( mAssistant.data() )
{
mDefineMenu->addSeparator();
mDefineMenu->addAction( mActionAssistant );
}
}

void QgsDataDefinedButton::menuActionTriggered( QAction* action )
Expand Down Expand Up @@ -371,6 +377,10 @@ void QgsDataDefinedButton::menuActionTriggered( QAction* action )
setExpression( QString( "" ) );
updateGui();
}
else if ( action == mActionAssistant )
{
showAssistant();
}
else if ( mFieldsMenu->actions().contains( action ) ) // a field name clicked
{
if ( action->isEnabled() )
Expand All @@ -394,6 +404,25 @@ void QgsDataDefinedButton::showDescriptionDialog()
mv->exec();
}

void QgsDataDefinedButton::showAssistant()
{
if ( !mAssistant.data() )
return;

if ( mAssistant->exec() == QDialog::Accepted )
{
QScopedPointer<QgsDataDefined> dd( mAssistant->dataDefined() );
setUseExpression( dd->useExpression() );
setActive( dd->isActive() );
if ( dd->isActive() && dd->useExpression() )
setExpression( dd->expressionString() );
else if ( dd->isActive() )
setField( dd->field() );
updateGui();
}
activateWindow(); // reset focus to parent window
}

void QgsDataDefinedButton::showExpressionDialog()
{
QgsExpressionBuilderDialog d( const_cast<QgsVectorLayer*>( mVectorLayer ), getExpression() );
Expand Down Expand Up @@ -576,6 +605,12 @@ QList<QWidget*> QgsDataDefinedButton::registeredCheckedWidgets()
return wdgtList;
}

void QgsDataDefinedButton::setAssistant( QgsDataDefinedAssistant *assistant )
{
mAssistant.reset( assistant );
mAssistant.data()->setParent( this, Qt::Dialog );
}

void QgsDataDefinedButton::checkCheckedWidgets( bool check )
{
// don't uncheck, only set to checked
Expand Down
30 changes: 28 additions & 2 deletions src/gui/qgsdatadefinedbutton.h
Expand Up @@ -15,15 +15,29 @@
#ifndef QGSDATADEFINEDBUTTON_H
#define QGSDATADEFINEDBUTTON_H

#include "qgsfield.h"
#include <QDialog>
#include <QFlags>
#include <QMap>
#include <QPointer>
#include <QToolButton>
#include <QScopedPointer>

class QgsVectorLayer;
class QgsDataDefined;

/** \ingroup gui
* \class QgsDataDefinedAssistant
* An assistant (wizard) dialog, accessible from a QgsDataDefinedButton.
* Can be used to guide users through creation of an expression for the
* data defined button.
* @note added in 2.10
*/
class GUI_EXPORT QgsDataDefinedAssistant: public QDialog
{
public:
virtual QgsDataDefined* dataDefined() const = 0;
};

/** \ingroup gui
* \class QgsDataDefinedButton
* A button for defining data source field mappings or expressions.
Expand Down Expand Up @@ -166,6 +180,15 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
*/
void clearCheckedWidgets() { mCheckedWidgets.clear(); }

/**
* Sets an assistant used to define the data defined object properties.
* Ownership of the assistant is transferred to the widget.
* @param assistant data defined assistant. Set to null to remove the assistant
* option from the button.
* @note added in 2.10
*/
void setAssistant( QgsDataDefinedAssistant * assistant );

/**
* Common descriptions for expected input values
*/
Expand Down Expand Up @@ -254,10 +277,10 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
private:
void showDescriptionDialog();
void showExpressionDialog();
void showAssistant();
void updateGui();

const QgsVectorLayer* mVectorLayer;
QgsFields mFields;
QStringList mFieldNameList;
QStringList mFieldTypeList;
QMap< QString, QString > mProperty;
Expand All @@ -275,6 +298,7 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
QAction* mActionPasteExpr;
QAction* mActionCopyExpr;
QAction* mActionClearExpr;
QAction* mActionAssistant;

DataTypes mDataTypes;
QString mDataTypesString;
Expand All @@ -283,6 +307,8 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
QString mUsageInfo;
QString mCurrentDefinition;

QScopedPointer<QgsDataDefinedAssistant> mAssistant;

static QIcon mIconDataDefine;
static QIcon mIconDataDefineOn;
static QIcon mIconDataDefineError;
Expand Down

0 comments on commit 6a8526f

Please sign in to comment.