Skip to content

Commit

Permalink
Use actual map canvas variables & values for symbology widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 17, 2015
1 parent 87ee7b3 commit a77935f
Show file tree
Hide file tree
Showing 21 changed files with 443 additions and 54 deletions.
24 changes: 24 additions & 0 deletions python/gui/qgsdatadefinedbutton.sip
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,24 @@ class QgsDataDefinedAssistant: QDialog
#include <qgsdatadefinedbutton.h>
%End
public:
QgsDataDefinedAssistant();

virtual QgsDataDefined dataDefined() const = 0;

/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
* @see mapCanvas()
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

/** Returns the map canvas associated with the widget.
* @see setMapCanvas
* @note added in QGIS 2.12
*/
const QgsMapCanvas* mapCanvas() const;

};

/** \ingroup gui
Expand Down Expand Up @@ -187,9 +204,16 @@ class QgsDataDefinedButton : QToolButton
* @param assistant data defined assistant. Set to null to remove the assistant
* option from the button.
* @note added in 2.10
* @see assistant()
*/
void setAssistant( const QString& title, QgsDataDefinedAssistant * assistant /Transfer/ );

/** Returns the assistant used to defined the data defined object properties, if set.
* @see setAssistant()
* @note added in QGIS 2.12
*/
QgsDataDefinedAssistant* assistant();

/**
* Common descriptions for expected input values
*/
Expand Down
6 changes: 6 additions & 0 deletions python/gui/qgsmapcanvas.sip
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ class QgsMapCanvas : QGraphicsView
*/
QgsExpressionContextScope& expressionContextScope();

/** Returns a const reference to the expression context scope for the map canvas.
* @note added in QGIS 2.12
* @see setExpressionContextScope()
*/
// const QgsExpressionContextScope& expressionContextScope() const;

public slots:

/** Repaints the canvas map*/
Expand Down
114 changes: 114 additions & 0 deletions python/gui/symbology-ng/qgsrendererv2widget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ class QgsRendererV2Widget : QWidget
/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
* @see mapCanvas()
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

/** Returns the map canvas associated with the widget.
* @see setMapCanvas
* @note added in QGIS 2.12
*/
const QgsMapCanvas* mapCanvas() const;

/** Returns the vector layer associated with the widget.
* @note added in QGIS 2.12
*/
const QgsVectorLayer* vectorLayer() const;

protected:
/** Subclasses may provide the capability of changing multiple symbols at once by implementing the following two methods
and by connecting the slot contextMenuViewCategories(const QPoint&)*/
Expand All @@ -45,3 +57,105 @@ class QgsRendererV2Widget : QWidget
virtual void paste();

};


/**
Utility classes for "en masse" size definition
*/
class QgsDataDefinedValueDialog : QDialog
{
%TypeHeaderCode
#include <qgsrendererv2widget.h>
%End

public:
/** Constructor
* @param symbolList must not be empty
* @param layer must not be null
* @param label value label
*/
QgsDataDefinedValueDialog( const QList<QgsSymbolV2*>& symbolList, QgsVectorLayer * layer, const QString & label );
virtual ~QgsDataDefinedValueDialog();

/** Sets the map canvas associated with the dialog. This allows the dialog to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
* @see mapCanvas()
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

/** Returns the map canvas associated with the widget.
* @see setMapCanvas
* @note added in QGIS 2.12
*/
const QgsMapCanvas* mapCanvas() const;

/** Returns the vector layer associated with the widget.
* @note added in QGIS 2.12
*/
const QgsVectorLayer* vectorLayer() const;

public slots:
void dataDefinedChanged();

protected:
QgsDataDefined symbolDataDefined() const;
void init( const QString & description ); // needed in children ctor to call virtual

virtual QgsDataDefined symbolDataDefined( const QgsSymbolV2 * ) const = 0;
virtual double value( const QgsSymbolV2 * ) const = 0;
virtual void setDataDefined( QgsSymbolV2* symbol, const QgsDataDefined& dd ) = 0;
};

class QgsDataDefinedSizeDialog : QgsDataDefinedValueDialog
{
%TypeHeaderCode
#include <qgsrendererv2widget.h>
%End

public:
QgsDataDefinedSizeDialog( const QList<QgsSymbolV2*>& symbolList, QgsVectorLayer * layer );

protected:
QgsDataDefined symbolDataDefined( const QgsSymbolV2 * symbol ) const;

double value( const QgsSymbolV2 * symbol ) const;

void setDataDefined( QgsSymbolV2* symbol, const QgsDataDefined& dd );
};

class QgsDataDefinedRotationDialog : QgsDataDefinedValueDialog
{
%TypeHeaderCode
#include <qgsrendererv2widget.h>
%End

public:
QgsDataDefinedRotationDialog( const QList<QgsSymbolV2*>& symbolList, QgsVectorLayer * layer );

protected:
QgsDataDefined symbolDataDefined( const QgsSymbolV2 * symbol ) const;

double value( const QgsSymbolV2 * symbol ) const;

void setDataDefined( QgsSymbolV2* symbol, const QgsDataDefined& dd );
};


class QgsDataDefinedWidthDialog : QgsDataDefinedValueDialog
{
%TypeHeaderCode
#include <qgsrendererv2widget.h>
%End

public:
QgsDataDefinedWidthDialog( const QList<QgsSymbolV2*>& symbolList, QgsVectorLayer * layer );

protected:
QgsDataDefined symbolDataDefined( const QgsSymbolV2 * symbol ) const;

double value( const QgsSymbolV2 * symbol ) const;

void setDataDefined( QgsSymbolV2* symbol, const QgsDataDefined& dd );
};
5 changes: 5 additions & 0 deletions python/gui/symbology-ng/qgssizescalewidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class QgsSizeScaleWidget : QgsDataDefinedAssistant

QgsDataDefined dataDefined() const;

/** Returns the vector layer associated with the widget.
* @note added in QGIS 2.12
*/
const QgsVectorLayer* layer() const;

protected:

virtual void showEvent( QShowEvent * );
Expand Down
7 changes: 7 additions & 0 deletions python/gui/symbology-ng/qgssymbollayerv2widget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ class QgsSymbolLayerV2Widget : QWidget
/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
* @see mapCanvas()
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

/** Returns the map canvas associated with the widget.
* @see setMapCanvas
* @note added in QGIS 2.12
*/
const QgsMapCanvas* mapCanvas() const;

/** Returns the vector layer associated with the widget.
* @note added in QGIS 2.12
*/
Expand Down
7 changes: 7 additions & 0 deletions python/gui/symbology-ng/qgssymbolslistwidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ class QgsSymbolsListWidget : QWidget
/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
* @see mapCanvas()
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas );

/** Returns the map canvas associated with the widget.
* @see setMapCanvas
* @note added in QGIS 2.12
*/
const QgsMapCanvas* mapCanvas() const;

/** Returns the vector layer associated with the widget.
* @note added in QGIS 2.12
*/
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsdatadefinedbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ void QgsDataDefinedButton::setAssistant( const QString& title, QgsDataDefinedAss
mAssistant.data()->setParent( this, Qt::Dialog );
}

QgsDataDefinedAssistant *QgsDataDefinedButton::assistant()
{
return mAssistant.data();
}

void QgsDataDefinedButton::checkCheckedWidgets( bool check )
{
// don't uncheck, only set to checked
Expand Down
28 changes: 28 additions & 0 deletions src/gui/qgsdatadefinedbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

class QgsVectorLayer;
class QgsDataDefined;
class QgsMapCanvas;

/** \ingroup gui
* \class QgsDataDefinedAssistant
Expand All @@ -36,7 +37,27 @@ class QgsDataDefined;
class GUI_EXPORT QgsDataDefinedAssistant: public QDialog
{
public:
QgsDataDefinedAssistant() : mMapCanvas( 0 ) {}

virtual QgsDataDefined dataDefined() const = 0;

/** Sets the map canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* @param canvas map canvas
* @see mapCanvas()
* @note added in QGIS 2.12
*/
virtual void setMapCanvas( QgsMapCanvas* canvas ) { mMapCanvas = canvas; }

/** Returns the map canvas associated with the widget.
* @see setMapCanvas
* @note added in QGIS 2.12
*/
const QgsMapCanvas* mapCanvas() const { return mMapCanvas; }

protected:

QgsMapCanvas* mMapCanvas;
};

/** \ingroup gui
Expand Down Expand Up @@ -214,9 +235,16 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
* @param assistant data defined assistant. Set to null to remove the assistant
* option from the button.
* @note added in 2.10
* @see assistant()
*/
void setAssistant( const QString& title, QgsDataDefinedAssistant * assistant );

/** Returns the assistant used to defined the data defined object properties, if set.
* @see setAssistant()
* @note added in QGIS 2.12
*/
QgsDataDefinedAssistant* assistant();

/**
* Common descriptions for expected input values
*/
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsmapcanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }

/** Returns a const reference to the expression context scope for the map canvas.
* @note added in QGIS 2.12
* @see setExpressionContextScope()
*/
const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }

public slots:

/** Repaints the canvas map*/
Expand Down
24 changes: 17 additions & 7 deletions src/gui/symbology-ng/qgscategorizedsymbolrendererv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "qgsproject.h"
#include "qgsexpression.h"
#include "qgsmapcanvas.h"

#include <QKeyEvent>
#include <QMenu>
Expand Down Expand Up @@ -372,16 +373,25 @@ QgsRendererV2Widget* QgsCategorizedSymbolRendererV2Widget::create( QgsVectorLaye

static QgsExpressionContext _getExpressionContext( const void* context )
{
const QgsCategorizedSymbolRendererV2Widget* widget = ( const QgsCategorizedSymbolRendererV2Widget* ) context;

QgsExpressionContext expContext;
expContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::atlasScope( 0 )
//TODO - use actual map canvas settings
<< QgsExpressionContextUtils::mapSettingsScope( QgsMapSettings() );
<< QgsExpressionContextUtils::atlasScope( 0 );

if ( widget->mapCanvas() )
{
expContext << QgsExpressionContextUtils::mapSettingsScope( widget->mapCanvas()->mapSettings() )
<< new QgsExpressionContextScope( widget->mapCanvas()->expressionContextScope() );
}
else
{
expContext << QgsExpressionContextUtils::mapSettingsScope( QgsMapSettings() );
}

const QgsVectorLayer* layer = ( const QgsVectorLayer* ) context;
if ( layer )
expContext << QgsExpressionContextUtils::layerScope( layer );
if ( widget->vectorLayer() )
expContext << QgsExpressionContextUtils::layerScope( widget->vectorLayer() );

return expContext;
}
Expand Down Expand Up @@ -468,7 +478,7 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV

btnAdvanced->setMenu( advMenu );

mExpressionWidget->registerGetExpressionContextCallback( &_getExpressionContext, layer );
mExpressionWidget->registerGetExpressionContextCallback( &_getExpressionContext, this );
}

QgsCategorizedSymbolRendererV2Widget::~QgsCategorizedSymbolRendererV2Widget()
Expand Down
Loading

0 comments on commit a77935f

Please sign in to comment.