Skip to content

Commit 6a8526f

Browse files
vmoranyalldawson
authored andcommittedApr 27, 2015
Add assistant support to QgsDataDefinedButton
1 parent cf3a712 commit 6a8526f

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed
 

‎python/gui/qgsdatadefinedbutton.sip

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
class QgsDataDefinedButton : QToolButton
1+
/** \ingroup gui
2+
* \class QgsDataDefinedAssistant
3+
* An assistant (wizard) dialog, accessible from a QgsDataDefinedButton.
4+
* Can be used to guide users through creation of an expression for the
5+
* data defined button.
6+
* @note added in 2.10
7+
*/
8+
class QgsDataDefinedAssistant: QDialog
9+
{
10+
%TypeHeaderCode
11+
#include <qgsdatadefinedbutton.h>
12+
%End
13+
public:
14+
virtual QgsDataDefined* dataDefined() const = 0 /Factory/;
15+
};
16+
17+
/** \ingroup gui
18+
* \class QgsDataDefinedButton
19+
* A button for defining data source field mappings or expressions.
20+
*/
21+
class QgsDataDefinedButton : QToolButton
222
{
323
%TypeHeaderCode
424
#include <qgsdatadefinedbutton.h>
@@ -135,6 +155,15 @@ class QgsDataDefinedButton : QToolButton
135155
*/
136156
void clearCheckedWidgets();
137157

158+
/**
159+
* Sets an assistant used to define the data defined object properties.
160+
* Ownership of the assistant is transferred to the widget.
161+
* @param assistant data defined assistant. Set to null to remove the assistant
162+
* option from the button.
163+
* @note added in 2.10
164+
*/
165+
void setAssistant( QgsDataDefinedAssistant * assistant /Transfer/ );
166+
138167
/**
139168
* Common descriptions for expected input values
140169
*/

‎src/gui/qgsdatadefinedbutton.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ QgsDataDefinedButton::QgsDataDefinedButton( QWidget* parent,
8484
mActionPasteExpr = new QAction( tr( "Paste" ), this );
8585
mActionCopyExpr = new QAction( tr( "Copy" ), this );
8686
mActionClearExpr = new QAction( tr( "Clear" ), this );
87+
mActionAssistant = new QAction( tr( "Assistant..." ), this );
8788

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

326+
if ( mAssistant.data() )
327+
{
328+
mDefineMenu->addSeparator();
329+
mDefineMenu->addAction( mActionAssistant );
330+
}
325331
}
326332

327333
void QgsDataDefinedButton::menuActionTriggered( QAction* action )
@@ -371,6 +377,10 @@ void QgsDataDefinedButton::menuActionTriggered( QAction* action )
371377
setExpression( QString( "" ) );
372378
updateGui();
373379
}
380+
else if ( action == mActionAssistant )
381+
{
382+
showAssistant();
383+
}
374384
else if ( mFieldsMenu->actions().contains( action ) ) // a field name clicked
375385
{
376386
if ( action->isEnabled() )
@@ -394,6 +404,25 @@ void QgsDataDefinedButton::showDescriptionDialog()
394404
mv->exec();
395405
}
396406

407+
void QgsDataDefinedButton::showAssistant()
408+
{
409+
if ( !mAssistant.data() )
410+
return;
411+
412+
if ( mAssistant->exec() == QDialog::Accepted )
413+
{
414+
QScopedPointer<QgsDataDefined> dd( mAssistant->dataDefined() );
415+
setUseExpression( dd->useExpression() );
416+
setActive( dd->isActive() );
417+
if ( dd->isActive() && dd->useExpression() )
418+
setExpression( dd->expressionString() );
419+
else if ( dd->isActive() )
420+
setField( dd->field() );
421+
updateGui();
422+
}
423+
activateWindow(); // reset focus to parent window
424+
}
425+
397426
void QgsDataDefinedButton::showExpressionDialog()
398427
{
399428
QgsExpressionBuilderDialog d( const_cast<QgsVectorLayer*>( mVectorLayer ), getExpression() );
@@ -576,6 +605,12 @@ QList<QWidget*> QgsDataDefinedButton::registeredCheckedWidgets()
576605
return wdgtList;
577606
}
578607

608+
void QgsDataDefinedButton::setAssistant( QgsDataDefinedAssistant *assistant )
609+
{
610+
mAssistant.reset( assistant );
611+
mAssistant.data()->setParent( this, Qt::Dialog );
612+
}
613+
579614
void QgsDataDefinedButton::checkCheckedWidgets( bool check )
580615
{
581616
// don't uncheck, only set to checked

‎src/gui/qgsdatadefinedbutton.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,29 @@
1515
#ifndef QGSDATADEFINEDBUTTON_H
1616
#define QGSDATADEFINEDBUTTON_H
1717

18-
#include "qgsfield.h"
18+
#include <QDialog>
1919
#include <QFlags>
2020
#include <QMap>
2121
#include <QPointer>
2222
#include <QToolButton>
23+
#include <QScopedPointer>
2324

2425
class QgsVectorLayer;
2526
class QgsDataDefined;
2627

28+
/** \ingroup gui
29+
* \class QgsDataDefinedAssistant
30+
* An assistant (wizard) dialog, accessible from a QgsDataDefinedButton.
31+
* Can be used to guide users through creation of an expression for the
32+
* data defined button.
33+
* @note added in 2.10
34+
*/
35+
class GUI_EXPORT QgsDataDefinedAssistant: public QDialog
36+
{
37+
public:
38+
virtual QgsDataDefined* dataDefined() const = 0;
39+
};
40+
2741
/** \ingroup gui
2842
* \class QgsDataDefinedButton
2943
* A button for defining data source field mappings or expressions.
@@ -166,6 +180,15 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
166180
*/
167181
void clearCheckedWidgets() { mCheckedWidgets.clear(); }
168182

183+
/**
184+
* Sets an assistant used to define the data defined object properties.
185+
* Ownership of the assistant is transferred to the widget.
186+
* @param assistant data defined assistant. Set to null to remove the assistant
187+
* option from the button.
188+
* @note added in 2.10
189+
*/
190+
void setAssistant( QgsDataDefinedAssistant * assistant );
191+
169192
/**
170193
* Common descriptions for expected input values
171194
*/
@@ -254,10 +277,10 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
254277
private:
255278
void showDescriptionDialog();
256279
void showExpressionDialog();
280+
void showAssistant();
257281
void updateGui();
258282

259283
const QgsVectorLayer* mVectorLayer;
260-
QgsFields mFields;
261284
QStringList mFieldNameList;
262285
QStringList mFieldTypeList;
263286
QMap< QString, QString > mProperty;
@@ -275,6 +298,7 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
275298
QAction* mActionPasteExpr;
276299
QAction* mActionCopyExpr;
277300
QAction* mActionClearExpr;
301+
QAction* mActionAssistant;
278302

279303
DataTypes mDataTypes;
280304
QString mDataTypesString;
@@ -283,6 +307,8 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
283307
QString mUsageInfo;
284308
QString mCurrentDefinition;
285309

310+
QScopedPointer<QgsDataDefinedAssistant> mAssistant;
311+
286312
static QIcon mIconDataDefine;
287313
static QIcon mIconDataDefineOn;
288314
static QIcon mIconDataDefineError;

0 commit comments

Comments
 (0)
Please sign in to comment.