Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add editor widget for editing QgsExpressionContextScope variables
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 05b15dc commit 1c7a5b2
Show file tree
Hide file tree
Showing 14 changed files with 1,451 additions and 51 deletions.
2 changes: 2 additions & 0 deletions images/images.qrc
Expand Up @@ -314,6 +314,7 @@
<file>themes/default/mIconClear.svg</file>
<file>themes/default/mIconClose.png</file>
<file>themes/default/mIconCollapse.png</file>
<file>themes/default/mIconCollapseSmall.svg</file>
<file>themes/default/mIconColorBox.svg</file>
<file>themes/default/mIconColorPicker.svg</file>
<file>themes/default/mIconColorSwatches.svg</file>
Expand All @@ -333,6 +334,7 @@
<file>themes/default/mIconEditable.png</file>
<file>themes/default/mIconEditableEdits.png</file>
<file>themes/default/mIconExpand.png</file>
<file>themes/default/mIconExpandSmall.svg</file>
<file>themes/default/mIconExpression.svg</file>
<file>themes/default/mIconExpressionEditorOpen.svg</file>
<file>themes/default/mIconExpressionFilter.svg</file>
Expand Down
78 changes: 78 additions & 0 deletions images/themes/default/mIconCollapseSmall.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions images/themes/default/mIconExpandSmall.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -118,6 +118,7 @@
%Include qgssvgannotationitem.sip
%Include qgstextannotationitem.sip
%Include qgsuserinputdockwidget.sip
%Include qgsvariableeditorwidget.sip
%Include qgsvectorlayertools.sip
%Include qgsvertexmarker.sip

Expand Down
90 changes: 90 additions & 0 deletions python/gui/qgsvariableeditorwidget.sip
@@ -0,0 +1,90 @@
/** \ingroup gui
* \class QgsVariableEditorWidget
* A tree based widget for editing expression context scope variables. The widget allows editing
* variables from a QgsExpressionContextScope, and can optionally also show inherited
* variables from a QgsExpressionContext.
* \note added in QGIS 2.12
*/

class QgsVariableEditorWidget : QWidget
{
%TypeHeaderCode
#include <qgsvariableeditorwidget.h>
%End

public:

/** Constructor for QgsVariableEditorWidget.
* @param parent parent widget
*/
QgsVariableEditorWidget( QWidget *parent /TransferThis/ = 0 );

~QgsVariableEditorWidget();

/** Overwrites the QgsExpressionContext for the widget. Setting a context
* allows the widget to show all inherited variables for the context,
* and highlight any overriden variables within scopes.
* @param context expression context
* @see context()
*/
void setContext( QgsExpressionContext* context );

/** Returns the current expression context for the widget. QgsVariableEditorWidget widgets
* are created with an empty context by default.
* @see setContext()
*/
QgsExpressionContext* context() const;

/** Reloads all scopes from the editor's current context. This method should be called
* after adding or removing scopes from the attached context.
* @see context()
*/
void reloadContext();

/** Sets the editable scope for the widget. Only variables from the editable scope can
* be modified by users.
* @param scopeIndex index of current editable scope. Set to -1 to disable
* editing and make the widget read-only.
* @see editableScope()
*/
void setEditableScopeIndex( int scopeIndex );

/** Returns the current editable scope for the widget.
* @returns editable scope, or 0 if no editable scope is set
* @see setEditableScopeIndex()
*/
QgsExpressionContextScope* editableScope() const;

/** Sets the setting group for the widget. QgsVariableEditorWidget widgets with
* the same setting group will synchronise their settings, eg the size
* of columns in the tree widget.
* @param group setting group
* @see settingGroup()
*/
void setSettingGroup( const QString &group );

/** Returns the setting group for the widget. QgsVariableEditorWidget widgets with
* the same setting group will synchronise their settings, eg the size
* of columns in the tree widget.
* @returns setting group name
* @see setSettingGroup()
*/
QString settingGroup() const;

/** Returns a map variables set within the editable scope. Read only variables are not
* returned. This method can be used to retrieve the variables edited an added by
* users via the widget.
*/
QgsStringMap variablesInActiveScope() const;

signals:

/** Emitted when the user has modified a scope using the widget.
*/
void scopeChanged();

protected:

void showEvent( QShowEvent *event );

};
8 changes: 8 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -39,6 +39,7 @@
#include "qgscolorschemeregistry.h"
#include "qgssymbollayerv2utils.h"
#include "qgscolordialog.h"
#include "qgsexpressioncontext.h"

#include <QInputDialog>
#include <QFileDialog>
Expand Down Expand Up @@ -838,6 +839,10 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
// load gdal driver list only when gdal tab is first opened
mLoadedGdalDriverList = false;

mVariableEditor->context()->appendScope( QgsExpressionContextUtils::globalScope() );
mVariableEditor->reloadContext();
mVariableEditor->setEditableScopeIndex( 0 );

// restore window and widget geometry/state
restoreOptionsBaseUi();
}
Expand Down Expand Up @@ -1333,6 +1338,9 @@ void QgsOptions::saveOptions()
// TODO[MD] QgisApp::instance()->legend()->updateLegendItemSymbologies();
}

//save variables
QgsExpressionContextUtils::setGlobalVariables( mVariableEditor->variablesInActiveScope() );

// save app stylesheet last (in case reset becomes necessary)
if ( mStyleSheetNewOpts != mStyleSheetOldOpts )
{
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsoptions.h
Expand Up @@ -29,6 +29,8 @@

#include <QList>

class QgsExpressionContext;

/**
* \class QgsOptions
* \brief Set user options and preferences
Expand Down
9 changes: 9 additions & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -48,6 +48,7 @@
#include "qgscolorschemeregistry.h"
#include "qgssymbollayerv2utils.h"
#include "qgscolordialog.h"
#include "qgsexpressioncontext.h"

//qt includes
#include <QInputDialog>
Expand Down Expand Up @@ -524,6 +525,11 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
on_cbxProjectionEnabled_toggled( myProjectionEnabled );
}

mVariableEditor->context()->appendScope( QgsExpressionContextUtils::globalScope() );
mVariableEditor->context()->appendScope( QgsExpressionContextUtils::projectScope() );
mVariableEditor->reloadContext();
mVariableEditor->setEditableScopeIndex( 1 );

projectionSelectorInitialized();
restoreOptionsBaseUi();
restoreState();
Expand Down Expand Up @@ -919,6 +925,9 @@ void QgsProjectProperties::apply()

QgsProject::instance()->relationManager()->setRelations( mRelationManagerDlg->relations() );

//save variables
QgsExpressionContextUtils::setProjectVariables( mVariableEditor->variablesInActiveScope() );

emit refresh();
}

Expand Down
1 change: 1 addition & 0 deletions src/app/qgsprojectproperties.h
Expand Up @@ -26,6 +26,7 @@
class QgsMapCanvas;
class QgsRelationManagerDialog;
class QgsStyleV2;
class QgsExpressionContext;

/** Dialog to set project level properties
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -234,6 +234,7 @@ SET(QGIS_GUI_SRCS
qgssvgannotationitem.cpp
qgstextannotationitem.cpp
qgsuserinputdockwidget.cpp
qgsvariableeditorwidget.cpp
qgsvertexmarker.cpp
qgsunitselectionwidget.cpp
)
Expand Down Expand Up @@ -351,6 +352,7 @@ SET(QGIS_GUI_MOC_HDRS
qgssearchquerybuilder.h
qgsslider.h
qgssublayersdialog.h
qgsvariableeditorwidget.h
qgsunitselectionwidget.h
qgsuserinputdockwidget.h

Expand Down

0 comments on commit 1c7a5b2

Please sign in to comment.