Skip to content
Permalink
Browse files

New class QgsLayoutGuideCollection for storing sets of layout snap gu…

…ides

The class is a QAbstractListModel subclass, making it easy
for creation of different views showing the guides in a layout.

Additionally, a QSortFilterProxyModel (QgsLayoutGuideProxyModel)
has been created to filter QgsLayoutGuideCollection to guides
with matching page numbers and orientations.
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent fa5eb4e commit f5126b0abd865ae9091e750fa10239277d1711f4
@@ -159,6 +159,13 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator
%End


QgsLayoutGuideCollection &guides();
%Docstring
Returns a reference to the layout's guide collection, which manages page snap guides.
:rtype: QgsLayoutGuideCollection
%End


virtual QgsExpressionContext createExpressionContext() const;

%Docstring
@@ -26,7 +26,34 @@ class QgsLayoutGuide : QObject
Vertical,
};

QgsLayoutGuide( QgsLayout *layout, Orientation orientation, const QgsLayoutMeasurement &position );
QgsLayoutGuide( Orientation orientation, const QgsLayoutMeasurement &position );
%Docstring
Constructor for a new guide with the specified ``orientation`` and
initial ``position``.

A layout must be set by calling setLayout() before the guide can be used.
Adding the guide to a QgsLayoutGuideCollection will automatically set
the corresponding layout for you.
%End

QgsLayout *layout() const;
%Docstring
Returns the layout the guide belongs to.
.. seealso:: setLayout()
:rtype: QgsLayout
%End

void setLayout( QgsLayout *layout );
%Docstring
Sets the ``layout`` the guide belongs to.

.. note::

Adding the guide to a QgsLayoutGuideCollection will automatically set
the corresponding layout for you.

.. seealso:: layout()
%End

Orientation orientation() const;
%Docstring
@@ -85,9 +112,103 @@ class QgsLayoutGuide : QObject
:rtype: QGraphicsLineItem
%End

double layoutPosition() const;
%Docstring
Returns the guide's position in absolute layout units.
:rtype: float
%End

signals:

void positionChanged();
%Docstring
Emitted when the guide's position is changed.
%End

};

class QgsLayoutGuideCollection : QAbstractListModel
{
%Docstring
Stores and manages the snap guides used by a layout.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutguidecollection.h"
%End
public:

enum Roles
{
OrientationRole,
PositionRole,
UnitsRole,
PageRole,
};

QgsLayoutGuideCollection( QgsLayout *layout );
%Docstring
Constructor for QgsLayoutGuideCollection belonging to the specified layout.
%End
~QgsLayoutGuideCollection();

virtual int rowCount( const QModelIndex & ) const;

virtual QVariant data( const QModelIndex &index, int role ) const;

virtual bool setData( const QModelIndex &index, const QVariant &value, int role );

virtual Qt::ItemFlags flags( const QModelIndex &index ) const;


void addGuide( QgsLayoutGuide *guide /Transfer/ );
%Docstring
Adds a ``guide`` to the collection. Ownership of the guide is transferred to the
collection, and the guide will automatically have the correct layout
set.
%End

void update();
%Docstring
Updates the position (and visibility) of all guide line items.
%End

QList< QgsLayoutGuide * > guides( QgsLayoutGuide::Orientation orientation );
%Docstring
Returns the list of guides contained in the collection with the specified
``orientation``.
:rtype: list of QgsLayoutGuide
%End

};


class QgsLayoutGuideProxyModel : QSortFilterProxyModel
{
%Docstring
Filters QgsLayoutGuideCollection models to guides of a single orientation (horizontal or vertical).
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutguidecollection.h"
%End
public:

explicit QgsLayoutGuideProxyModel( QObject *parent /TransferThis/, QgsLayoutGuide::Orientation orientation, int page );
%Docstring
Constructor for QgsLayoutGuideProxyModel, filtered to guides of the specified ``orientation`` and ``page`` only.

Page numbers begin at 0.
%End

virtual bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;

virtual bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;


};

/************************************************************************
* This file has been generated automatically from *
@@ -16,11 +16,13 @@

#include "qgslayout.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutguidecollection.h"

QgsLayout::QgsLayout( QgsProject *project )
: QGraphicsScene()
, mProject( project )
, mSnapper( QgsLayoutSnapper( this ) )
, mGuideCollection( new QgsLayoutGuideCollection( this ) )
, mPageCollection( new QgsLayoutPageCollection( this ) )
{
// just to make sure - this should be the default, but maybe it'll change in some future Qt version...
@@ -70,6 +72,16 @@ QgsLayoutPoint QgsLayout::convertFromLayoutUnits( const QPointF &point, const Qg
return mContext.measurementConverter().convert( QgsLayoutPoint( point.x(), point.y(), mUnits ), unit );
}

QgsLayoutGuideCollection &QgsLayout::guides()
{
return *mGuideCollection;
}

const QgsLayoutGuideCollection &QgsLayout::guides() const
{
return *mGuideCollection;
}

QgsExpressionContext QgsLayout::createExpressionContext() const
{
QgsExpressionContext context = QgsExpressionContext();
@@ -23,6 +23,7 @@
#include "qgsexpressioncontextgenerator.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutgridsettings.h"
#include "qgslayoutguidecollection.h"

class QgsLayoutItemMap;

@@ -182,6 +183,16 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
*/
SIP_SKIP const QgsLayoutGridSettings &gridSettings() const { return mGridSettings; }

/**
* Returns a reference to the layout's guide collection, which manages page snap guides.
*/
QgsLayoutGuideCollection &guides();

/**
* Returns a reference to the layout's guide collection, which manages page snap guides.
*/
SIP_SKIP const QgsLayoutGuideCollection &guides() const;

/**
* Creates an expression context relating to the layout's current state. The context includes
* scopes for global, project, layout and layout context properties.
@@ -292,6 +303,7 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
QgsLayoutContext mContext;
QgsLayoutSnapper mSnapper;
QgsLayoutGridSettings mGridSettings;
std::unique_ptr< QgsLayoutGuideCollection > mGuideCollection;

std::unique_ptr< QgsLayoutPageCollection > mPageCollection;

0 comments on commit f5126b0

Please sign in to comment.
You can’t perform that action at this time.