mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Code refactor - split grid appearance settings into their own class
This commit is contained in:
parent
55c7cc185e
commit
0d0a3c2cdc
@ -156,6 +156,7 @@
|
||||
%Include composer/qgscomposertexttable.sip
|
||||
%Include composer/qgspaperitem.sip
|
||||
%Include layout/qgslayoutcontext.sip
|
||||
%Include layout/qgslayoutgridsettings.sip
|
||||
%Include layout/qgslayoutmeasurement.sip
|
||||
%Include layout/qgslayoutmeasurementconverter.sip
|
||||
%Include layout/qgspagesizeregistry.sip
|
||||
|
@ -149,6 +149,14 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator
|
||||
%End
|
||||
|
||||
|
||||
QgsLayoutGridSettings &gridSettings();
|
||||
%Docstring
|
||||
Returns a reference to the layout's grid settings, which stores settings relating
|
||||
to grid appearance, spacing and offsets.
|
||||
:rtype: QgsLayoutGridSettings
|
||||
%End
|
||||
|
||||
|
||||
virtual QgsExpressionContext createExpressionContext() const;
|
||||
|
||||
%Docstring
|
||||
|
103
python/core/layout/qgslayoutgridsettings.sip
Normal file
103
python/core/layout/qgslayoutgridsettings.sip
Normal file
@ -0,0 +1,103 @@
|
||||
/************************************************************************
|
||||
* This file has been generated automatically from *
|
||||
* *
|
||||
* src/core/layout/qgslayoutgridsettings.h *
|
||||
* *
|
||||
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
|
||||
************************************************************************/
|
||||
|
||||
|
||||
|
||||
class QgsLayoutGridSettings
|
||||
{
|
||||
%Docstring
|
||||
Contains settings relating to the appearance, spacing and offset for layout grids.
|
||||
.. versionadded:: 3.0
|
||||
%End
|
||||
|
||||
%TypeHeaderCode
|
||||
#include "qgslayoutgridsettings.h"
|
||||
%End
|
||||
public:
|
||||
|
||||
enum Style
|
||||
{
|
||||
StyleLines,
|
||||
StyleDots,
|
||||
StyleCrosses
|
||||
};
|
||||
|
||||
QgsLayoutGridSettings();
|
||||
%Docstring
|
||||
Constructor for QgsLayoutGridSettings.
|
||||
%End
|
||||
|
||||
void setResolution( const QgsLayoutMeasurement &resolution );
|
||||
%Docstring
|
||||
Sets the page/snap grid ``resolution``.
|
||||
.. seealso:: resolution()
|
||||
.. seealso:: setOffset()
|
||||
%End
|
||||
|
||||
QgsLayoutMeasurement resolution() const;
|
||||
%Docstring
|
||||
Returns the page/snap grid resolution.
|
||||
.. seealso:: setResolution()
|
||||
.. seealso:: offset()
|
||||
:rtype: QgsLayoutMeasurement
|
||||
%End
|
||||
|
||||
void setOffset( const QgsLayoutPoint offset );
|
||||
%Docstring
|
||||
Sets the ``offset`` of the page/snap grid.
|
||||
.. seealso:: offset()
|
||||
.. seealso:: setResolution()
|
||||
%End
|
||||
|
||||
QgsLayoutPoint offset() const;
|
||||
%Docstring
|
||||
Returns the offset of the page/snap grid.
|
||||
.. seealso:: setOffset()
|
||||
.. seealso:: resolution()
|
||||
:rtype: QgsLayoutPoint
|
||||
%End
|
||||
|
||||
void setPen( const QPen &pen );
|
||||
%Docstring
|
||||
Sets the ``pen`` used for drawing page/snap grids.
|
||||
.. seealso:: pen()
|
||||
.. seealso:: setStyle()
|
||||
%End
|
||||
|
||||
QPen pen() const;
|
||||
%Docstring
|
||||
Returns the pen used for drawing page/snap grids.
|
||||
.. seealso:: setPen()
|
||||
.. seealso:: style()
|
||||
:rtype: QPen
|
||||
%End
|
||||
|
||||
void setStyle( const Style style );
|
||||
%Docstring
|
||||
Sets the ``style`` used for drawing the page/snap grids.
|
||||
.. seealso:: style()
|
||||
.. seealso:: setPen()
|
||||
%End
|
||||
|
||||
Style style() const;
|
||||
%Docstring
|
||||
Returns the style used for drawing the page/snap grids.
|
||||
.. seealso:: setStyle()
|
||||
.. seealso:: pen()
|
||||
:rtype: Style
|
||||
%End
|
||||
|
||||
};
|
||||
|
||||
/************************************************************************
|
||||
* This file has been generated automatically from *
|
||||
* *
|
||||
* src/core/layout/qgslayoutgridsettings.h *
|
||||
* *
|
||||
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
|
||||
************************************************************************/
|
@ -21,13 +21,6 @@ class QgsLayoutSnapper
|
||||
%End
|
||||
public:
|
||||
|
||||
enum GridStyle
|
||||
{
|
||||
GridLines,
|
||||
GridDots,
|
||||
GridCrosses
|
||||
};
|
||||
|
||||
QgsLayoutSnapper( QgsLayout *layout );
|
||||
%Docstring
|
||||
Constructor for QgsLayoutSnapper, attached to the specified ``layout``.
|
||||
@ -46,66 +39,6 @@ class QgsLayoutSnapper
|
||||
:rtype: int
|
||||
%End
|
||||
|
||||
void setGridResolution( const QgsLayoutMeasurement &resolution );
|
||||
%Docstring
|
||||
Sets the page/snap grid ``resolution``.
|
||||
.. seealso:: gridResolution()
|
||||
.. seealso:: setGridOffset()
|
||||
%End
|
||||
|
||||
QgsLayoutMeasurement gridResolution() const;
|
||||
%Docstring
|
||||
Returns the page/snap grid resolution.
|
||||
.. seealso:: setGridResolution()
|
||||
.. seealso:: gridOffset()
|
||||
:rtype: QgsLayoutMeasurement
|
||||
%End
|
||||
|
||||
void setGridOffset( const QgsLayoutPoint offset );
|
||||
%Docstring
|
||||
Sets the ``offset`` of the page/snap grid.
|
||||
.. seealso:: gridOffset()
|
||||
.. seealso:: setGridResolution()
|
||||
%End
|
||||
|
||||
QgsLayoutPoint gridOffset() const;
|
||||
%Docstring
|
||||
Returns the offset of the page/snap grid.
|
||||
.. seealso:: setGridOffset()
|
||||
.. seealso:: gridResolution()
|
||||
:rtype: QgsLayoutPoint
|
||||
%End
|
||||
|
||||
void setGridPen( const QPen &pen );
|
||||
%Docstring
|
||||
Sets the ``pen`` used for drawing page/snap grids.
|
||||
.. seealso:: gridPen()
|
||||
.. seealso:: setGridStyle()
|
||||
%End
|
||||
|
||||
QPen gridPen() const;
|
||||
%Docstring
|
||||
Returns the pen used for drawing page/snap grids.
|
||||
.. seealso:: setGridPen()
|
||||
.. seealso:: gridStyle()
|
||||
:rtype: QPen
|
||||
%End
|
||||
|
||||
void setGridStyle( const GridStyle style );
|
||||
%Docstring
|
||||
Sets the ``style`` used for drawing the page/snap grids.
|
||||
.. seealso:: gridStyle()
|
||||
.. seealso:: setGridPen()
|
||||
%End
|
||||
|
||||
GridStyle gridStyle() const;
|
||||
%Docstring
|
||||
Returns the style used for drawing the page/snap grids.
|
||||
.. seealso:: setGridStyle()
|
||||
.. seealso:: gridPen()
|
||||
:rtype: GridStyle
|
||||
%End
|
||||
|
||||
bool snapToGrid() const;
|
||||
%Docstring
|
||||
Returns true if snapping to grid is enabled.
|
||||
|
@ -47,51 +47,51 @@ void QgsLayoutPropertiesWidget::updateSnappingElements()
|
||||
{
|
||||
mSnapToleranceSpinBox->setValue( mLayout->snapper().snapTolerance() );
|
||||
|
||||
mGridSpacingUnitsCombo->setUnit( mLayout->snapper().gridResolution().units() );
|
||||
mGridResolutionSpinBox->setValue( mLayout->snapper().gridResolution().length() );
|
||||
mGridSpacingUnitsCombo->setUnit( mLayout->gridSettings().resolution().units() );
|
||||
mGridResolutionSpinBox->setValue( mLayout->gridSettings().resolution().length() );
|
||||
|
||||
mGridOffsetUnitsComboBox->setUnit( mLayout->snapper().gridOffset().units() );
|
||||
mOffsetXSpinBox->setValue( mLayout->snapper().gridOffset().x() );
|
||||
mOffsetYSpinBox->setValue( mLayout->snapper().gridOffset().y() );
|
||||
mGridOffsetUnitsComboBox->setUnit( mLayout->gridSettings().offset().units() );
|
||||
mOffsetXSpinBox->setValue( mLayout->gridSettings().offset().x() );
|
||||
mOffsetYSpinBox->setValue( mLayout->gridSettings().offset().y() );
|
||||
}
|
||||
|
||||
void QgsLayoutPropertiesWidget::gridResolutionChanged( double d )
|
||||
{
|
||||
QgsLayoutMeasurement m = mLayout->snapper().gridResolution();
|
||||
QgsLayoutMeasurement m = mLayout->gridSettings().resolution();
|
||||
m.setLength( d );
|
||||
mLayout->snapper().setGridResolution( m );
|
||||
mLayout->gridSettings().setResolution( m );
|
||||
mLayout->pageCollection()->redraw();
|
||||
}
|
||||
|
||||
void QgsLayoutPropertiesWidget::gridResolutionUnitsChanged( QgsUnitTypes::LayoutUnit unit )
|
||||
{
|
||||
QgsLayoutMeasurement m = mLayout->snapper().gridResolution();
|
||||
QgsLayoutMeasurement m = mLayout->gridSettings().resolution();
|
||||
m.setUnits( unit );
|
||||
mLayout->snapper().setGridResolution( m );
|
||||
mLayout->gridSettings().setResolution( m );
|
||||
mLayout->pageCollection()->redraw();
|
||||
}
|
||||
|
||||
void QgsLayoutPropertiesWidget::gridOffsetXChanged( double d )
|
||||
{
|
||||
QgsLayoutPoint o = mLayout->snapper().gridOffset();
|
||||
QgsLayoutPoint o = mLayout->gridSettings().offset();
|
||||
o.setX( d );
|
||||
mLayout->snapper().setGridOffset( o );
|
||||
mLayout->gridSettings().setOffset( o );
|
||||
mLayout->pageCollection()->redraw();
|
||||
}
|
||||
|
||||
void QgsLayoutPropertiesWidget::gridOffsetYChanged( double d )
|
||||
{
|
||||
QgsLayoutPoint o = mLayout->snapper().gridOffset();
|
||||
QgsLayoutPoint o = mLayout->gridSettings().offset();
|
||||
o.setY( d );
|
||||
mLayout->snapper().setGridOffset( o );
|
||||
mLayout->gridSettings().setOffset( o );
|
||||
mLayout->pageCollection()->redraw();
|
||||
}
|
||||
|
||||
void QgsLayoutPropertiesWidget::gridOffsetUnitsChanged( QgsUnitTypes::LayoutUnit unit )
|
||||
{
|
||||
QgsLayoutPoint o = mLayout->snapper().gridOffset();
|
||||
QgsLayoutPoint o = mLayout->gridSettings().offset();
|
||||
o.setUnits( unit );
|
||||
mLayout->snapper().setGridOffset( o );
|
||||
mLayout->gridSettings().setOffset( o );
|
||||
mLayout->pageCollection()->redraw();
|
||||
}
|
||||
|
||||
|
@ -353,6 +353,7 @@ SET(QGIS_CORE_SRCS
|
||||
|
||||
layout/qgslayout.cpp
|
||||
layout/qgslayoutcontext.cpp
|
||||
layout/qgslayoutgridsettings.cpp
|
||||
layout/qgslayoutitem.cpp
|
||||
layout/qgslayoutitemmap.cpp
|
||||
layout/qgslayoutitempage.cpp
|
||||
@ -936,6 +937,7 @@ SET(QGIS_CORE_HDRS
|
||||
composer/qgspaperitem.h
|
||||
|
||||
layout/qgslayoutcontext.h
|
||||
layout/qgslayoutgridsettings.h
|
||||
layout/qgslayoutmeasurement.h
|
||||
layout/qgslayoutmeasurementconverter.h
|
||||
layout/qgspagesizeregistry.h
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "qgslayoutsnapper.h"
|
||||
#include "qgsexpressioncontextgenerator.h"
|
||||
#include "qgslayoutpagecollection.h"
|
||||
#include "qgslayoutgridsettings.h"
|
||||
|
||||
class QgsLayoutItemMap;
|
||||
|
||||
@ -167,6 +168,18 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
|
||||
*/
|
||||
SIP_SKIP const QgsLayoutSnapper &snapper() const { return mSnapper; }
|
||||
|
||||
/**
|
||||
* Returns a reference to the layout's grid settings, which stores settings relating
|
||||
* to grid appearance, spacing and offsets.
|
||||
*/
|
||||
QgsLayoutGridSettings &gridSettings() { return mGridSettings; }
|
||||
|
||||
/**
|
||||
* Returns a reference to the layout's grid settings, which stores settings relating
|
||||
* to grid appearance, spacing and offsets.
|
||||
*/
|
||||
SIP_SKIP const QgsLayoutGridSettings &gridSettings() const { return mGridSettings; }
|
||||
|
||||
/**
|
||||
* Creates an expression context relating to the layout's current state. The context includes
|
||||
* scopes for global, project, layout and layout context properties.
|
||||
@ -276,6 +289,7 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
|
||||
QgsUnitTypes::LayoutUnit mUnits = QgsUnitTypes::LayoutMillimeters;
|
||||
QgsLayoutContext mContext;
|
||||
QgsLayoutSnapper mSnapper;
|
||||
QgsLayoutGridSettings mGridSettings;
|
||||
|
||||
std::unique_ptr< QgsLayoutPageCollection > mPageCollection;
|
||||
|
||||
|
25
src/core/layout/qgslayoutgridsettings.cpp
Normal file
25
src/core/layout/qgslayoutgridsettings.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
/***************************************************************************
|
||||
qgslayoutgridsettings.cpp
|
||||
-------------------------
|
||||
begin : July 2017
|
||||
copyright : (C) 2017 by Nyall Dawson
|
||||
email : nyall dot dawson at gmail dot com
|
||||
***************************************************************************/
|
||||
/***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#include "qgslayoutgridsettings.h"
|
||||
|
||||
QgsLayoutGridSettings::QgsLayoutGridSettings()
|
||||
: mGridResolution( QgsLayoutMeasurement( 10 ) )
|
||||
{
|
||||
mGridPen = QPen( QColor( 190, 190, 190, 100 ), 0 );
|
||||
mGridPen.setCosmetic( true );
|
||||
}
|
||||
|
114
src/core/layout/qgslayoutgridsettings.h
Normal file
114
src/core/layout/qgslayoutgridsettings.h
Normal file
@ -0,0 +1,114 @@
|
||||
/***************************************************************************
|
||||
qgslayoutgridsettings.h
|
||||
-----------------------
|
||||
begin : July 2017
|
||||
copyright : (C) 2017 by Nyall Dawson
|
||||
email : nyall dot dawson at gmail dot com
|
||||
***************************************************************************/
|
||||
/***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
#ifndef QGSLAYOUTGRIDSETTINGS_H
|
||||
#define QGSLAYOUTGRIDSETTINGS_H
|
||||
|
||||
#include "qgis_core.h"
|
||||
#include "qgslayoutmeasurement.h"
|
||||
#include "qgslayoutpoint.h"
|
||||
#include <QPen>
|
||||
|
||||
class QgsLayout;
|
||||
|
||||
/**
|
||||
* \ingroup core
|
||||
* \class QgsLayoutGridSettings
|
||||
* \brief Contains settings relating to the appearance, spacing and offset for layout grids.
|
||||
* \since QGIS 3.0
|
||||
*/
|
||||
class CORE_EXPORT QgsLayoutGridSettings
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
//! Style for drawing the page/snapping grid
|
||||
enum Style
|
||||
{
|
||||
StyleLines, //! Solid lines
|
||||
StyleDots, //! Dots
|
||||
StyleCrosses //! Crosses
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor for QgsLayoutGridSettings.
|
||||
*/
|
||||
QgsLayoutGridSettings();
|
||||
|
||||
/**
|
||||
* Sets the page/snap grid \a resolution.
|
||||
* \see resolution()
|
||||
* \see setOffset()
|
||||
*/
|
||||
void setResolution( const QgsLayoutMeasurement &resolution ) { mGridResolution = resolution; }
|
||||
|
||||
/**
|
||||
* Returns the page/snap grid resolution.
|
||||
* \see setResolution()
|
||||
* \see offset()
|
||||
*/
|
||||
QgsLayoutMeasurement resolution() const { return mGridResolution;}
|
||||
|
||||
/**
|
||||
* Sets the \a offset of the page/snap grid.
|
||||
* \see offset()
|
||||
* \see setResolution()
|
||||
*/
|
||||
void setOffset( const QgsLayoutPoint offset ) { mGridOffset = offset; }
|
||||
|
||||
/**
|
||||
* Returns the offset of the page/snap grid.
|
||||
* \see setOffset()
|
||||
* \see resolution()
|
||||
*/
|
||||
QgsLayoutPoint offset() const { return mGridOffset; }
|
||||
|
||||
/**
|
||||
* Sets the \a pen used for drawing page/snap grids.
|
||||
* \see pen()
|
||||
* \see setStyle()
|
||||
*/
|
||||
void setPen( const QPen &pen ) { mGridPen = pen; }
|
||||
|
||||
/**
|
||||
* Returns the pen used for drawing page/snap grids.
|
||||
* \see setPen()
|
||||
* \see style()
|
||||
*/
|
||||
QPen pen() const { return mGridPen; }
|
||||
|
||||
/**
|
||||
* Sets the \a style used for drawing the page/snap grids.
|
||||
* \see style()
|
||||
* \see setPen()
|
||||
*/
|
||||
void setStyle( const Style style ) { mGridStyle = style; }
|
||||
|
||||
/**
|
||||
* Returns the style used for drawing the page/snap grids.
|
||||
* \see setStyle()
|
||||
* \see pen()
|
||||
*/
|
||||
Style style() const { return mGridStyle; }
|
||||
|
||||
private:
|
||||
|
||||
QgsLayoutMeasurement mGridResolution;
|
||||
QgsLayoutPoint mGridOffset;
|
||||
QPen mGridPen;
|
||||
Style mGridStyle = StyleLines;
|
||||
};
|
||||
|
||||
#endif //QGSLAYOUTGRIDSETTINGS_H
|
@ -212,13 +212,13 @@ void QgsLayoutItemPageGrid::paint( QPainter *painter, const QStyleOptionGraphics
|
||||
return;
|
||||
|
||||
const QgsLayoutContext &context = mLayout->context();
|
||||
const QgsLayoutSnapper &snapper = mLayout->snapper();
|
||||
const QgsLayoutGridSettings &grid = mLayout->gridSettings();
|
||||
|
||||
if ( !context.gridVisible() || snapper.gridResolution().length() <= 0 )
|
||||
if ( !context.gridVisible() || grid.resolution().length() <= 0 )
|
||||
return;
|
||||
|
||||
QPointF gridOffset = mLayout->convertToLayoutUnits( snapper.gridOffset() );
|
||||
double gridResolution = mLayout->convertToLayoutUnits( snapper.gridResolution() );
|
||||
QPointF gridOffset = mLayout->convertToLayoutUnits( grid.offset() );
|
||||
double gridResolution = mLayout->convertToLayoutUnits( grid.resolution() );
|
||||
int gridMultiplyX = static_cast< int >( gridOffset.x() / gridResolution );
|
||||
int gridMultiplyY = static_cast< int >( gridOffset.y() / gridResolution );
|
||||
double currentXCoord = gridOffset.x() - gridMultiplyX * gridResolution;
|
||||
@ -229,11 +229,11 @@ void QgsLayoutItemPageGrid::paint( QPainter *painter, const QStyleOptionGraphics
|
||||
//turn of antialiasing so grid is nice and sharp
|
||||
painter->setRenderHint( QPainter::Antialiasing, false );
|
||||
|
||||
switch ( snapper.gridStyle() )
|
||||
switch ( grid.style() )
|
||||
{
|
||||
case QgsLayoutSnapper::GridLines:
|
||||
case QgsLayoutGridSettings::StyleLines:
|
||||
{
|
||||
painter->setPen( snapper.gridPen() );
|
||||
painter->setPen( grid.pen() );
|
||||
|
||||
//draw vertical lines
|
||||
for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
|
||||
@ -250,14 +250,14 @@ void QgsLayoutItemPageGrid::paint( QPainter *painter, const QStyleOptionGraphics
|
||||
break;
|
||||
}
|
||||
|
||||
case QgsLayoutSnapper::GridDots:
|
||||
case QgsLayoutSnapper::GridCrosses:
|
||||
case QgsLayoutGridSettings::StyleDots:
|
||||
case QgsLayoutGridSettings::StyleCrosses:
|
||||
{
|
||||
QPen gridPen = snapper.gridPen();
|
||||
QPen gridPen = grid.pen();
|
||||
painter->setPen( gridPen );
|
||||
painter->setBrush( QBrush( gridPen.color() ) );
|
||||
double halfCrossLength = 1;
|
||||
if ( snapper.gridStyle() == QgsLayoutSnapper::GridDots )
|
||||
if ( grid.style() == QgsLayoutGridSettings::StyleDots )
|
||||
{
|
||||
//dots are actually drawn as tiny crosses a few pixels across
|
||||
//set halfCrossLength to equivalent of 1 pixel
|
||||
|
@ -19,10 +19,7 @@
|
||||
|
||||
QgsLayoutSnapper::QgsLayoutSnapper( QgsLayout *layout )
|
||||
: mLayout( layout )
|
||||
, mGridResolution( QgsLayoutMeasurement( 10 ) )
|
||||
{
|
||||
mGridPen = QPen( QColor( 190, 190, 190, 100 ), 0 );
|
||||
mGridPen.setCosmetic( true );
|
||||
}
|
||||
|
||||
QPointF QgsLayoutSnapper::snapPoint( QPointF point, double scaleFactor, bool &snapped ) const
|
||||
@ -44,10 +41,13 @@ QPointF QgsLayoutSnapper::snapPoint( QPointF point, double scaleFactor, bool &sn
|
||||
QPointF QgsLayoutSnapper::snapPointToGrid( QPointF point, double scaleFactor, bool &snapped ) const
|
||||
{
|
||||
snapped = false;
|
||||
if ( !mLayout || !mSnapToGrid || mGridResolution.length() <= 0 )
|
||||
if ( !mLayout || !mSnapToGrid )
|
||||
{
|
||||
return point;
|
||||
}
|
||||
const QgsLayoutGridSettings &grid = mLayout->gridSettings();
|
||||
if ( grid.resolution().length() <= 0 )
|
||||
return point;
|
||||
|
||||
//calculate y offset to current page
|
||||
QPointF pagePoint = mLayout->pageCollection()->positionOnPage( point );
|
||||
@ -56,8 +56,8 @@ QPointF QgsLayoutSnapper::snapPointToGrid( QPointF point, double scaleFactor, bo
|
||||
double yAtTopOfPage = mLayout->pageCollection()->page( mLayout->pageCollection()->pageNumberForPoint( point ) )->pos().y();
|
||||
|
||||
//snap x coordinate
|
||||
double gridRes = mLayout->convertToLayoutUnits( mGridResolution );
|
||||
QPointF gridOffset = mLayout->convertToLayoutUnits( mGridOffset );
|
||||
double gridRes = mLayout->convertToLayoutUnits( grid.resolution() );
|
||||
QPointF gridOffset = mLayout->convertToLayoutUnits( grid.offset() );
|
||||
int xRatio = static_cast< int >( ( point.x() - gridOffset.x() ) / gridRes + 0.5 ); //NOLINT
|
||||
int yRatio = static_cast< int >( ( yPage - gridOffset.y() ) / gridRes + 0.5 ); //NOLINT
|
||||
|
||||
|
@ -35,14 +35,6 @@ class CORE_EXPORT QgsLayoutSnapper
|
||||
|
||||
public:
|
||||
|
||||
//! Style for drawing the page/snapping grid
|
||||
enum GridStyle
|
||||
{
|
||||
GridLines, //! Solid lines
|
||||
GridDots, //! Dots
|
||||
GridCrosses //! Crosses
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor for QgsLayoutSnapper, attached to the specified \a layout.
|
||||
*/
|
||||
@ -60,62 +52,6 @@ class CORE_EXPORT QgsLayoutSnapper
|
||||
*/
|
||||
int snapTolerance() const { return mTolerance; }
|
||||
|
||||
/**
|
||||
* Sets the page/snap grid \a resolution.
|
||||
* \see gridResolution()
|
||||
* \see setGridOffset()
|
||||
*/
|
||||
void setGridResolution( const QgsLayoutMeasurement &resolution ) { mGridResolution = resolution; }
|
||||
|
||||
/**
|
||||
* Returns the page/snap grid resolution.
|
||||
* \see setGridResolution()
|
||||
* \see gridOffset()
|
||||
*/
|
||||
QgsLayoutMeasurement gridResolution() const { return mGridResolution;}
|
||||
|
||||
/**
|
||||
* Sets the \a offset of the page/snap grid.
|
||||
* \see gridOffset()
|
||||
* \see setGridResolution()
|
||||
*/
|
||||
void setGridOffset( const QgsLayoutPoint offset ) { mGridOffset = offset; }
|
||||
|
||||
/**
|
||||
* Returns the offset of the page/snap grid.
|
||||
* \see setGridOffset()
|
||||
* \see gridResolution()
|
||||
*/
|
||||
QgsLayoutPoint gridOffset() const { return mGridOffset; }
|
||||
|
||||
/**
|
||||
* Sets the \a pen used for drawing page/snap grids.
|
||||
* \see gridPen()
|
||||
* \see setGridStyle()
|
||||
*/
|
||||
void setGridPen( const QPen &pen ) { mGridPen = pen; }
|
||||
|
||||
/**
|
||||
* Returns the pen used for drawing page/snap grids.
|
||||
* \see setGridPen()
|
||||
* \see gridStyle()
|
||||
*/
|
||||
QPen gridPen() const { return mGridPen; }
|
||||
|
||||
/**
|
||||
* Sets the \a style used for drawing the page/snap grids.
|
||||
* \see gridStyle()
|
||||
* \see setGridPen()
|
||||
*/
|
||||
void setGridStyle( const GridStyle style ) { mGridStyle = style; }
|
||||
|
||||
/**
|
||||
* Returns the style used for drawing the page/snap grids.
|
||||
* \see setGridStyle()
|
||||
* \see gridPen()
|
||||
*/
|
||||
GridStyle gridStyle() const { return mGridStyle; }
|
||||
|
||||
/**
|
||||
* Returns true if snapping to grid is enabled.
|
||||
* \see setSnapToGrid()
|
||||
@ -157,12 +93,6 @@ class CORE_EXPORT QgsLayoutSnapper
|
||||
QgsLayout *mLayout = nullptr;
|
||||
|
||||
int mTolerance = 5;
|
||||
|
||||
QgsLayoutMeasurement mGridResolution;
|
||||
QgsLayoutPoint mGridOffset;
|
||||
QPen mGridPen;
|
||||
GridStyle mGridStyle = GridLines;
|
||||
|
||||
bool mSnapToGrid = false;
|
||||
|
||||
};
|
||||
|
@ -78,6 +78,7 @@ ADD_PYTHON_TEST(PyQgsLayerTree test_qgslayertree.py)
|
||||
ADD_PYTHON_TEST(PyQgsLayoutManager test_qgslayoutmanager.py)
|
||||
ADD_PYTHON_TEST(PyQgsLayoutPageCollection test_qgslayoutpagecollection.py)
|
||||
ADD_PYTHON_TEST(PyQgsLayoutView test_qgslayoutview.py)
|
||||
ADD_PYTHON_TEST(PyQgsLayoutGridSettings test_qgslayoutgridsettings.py)
|
||||
ADD_PYTHON_TEST(PyQgsLayoutItemPropertiesDialog test_qgslayoutitempropertiesdialog.py)
|
||||
ADD_PYTHON_TEST(PyQgsLayoutSnapper test_qgslayoutsnapper.py)
|
||||
ADD_PYTHON_TEST(PyQgsLayoutUnitsComboBox test_qgslayoutunitscombobox.py)
|
||||
|
55
tests/src/python/test_qgslayoutgridsettings.py
Normal file
55
tests/src/python/test_qgslayoutgridsettings.py
Normal file
@ -0,0 +1,55 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""QGIS Unit tests for QgsLayoutGridSettings.
|
||||
|
||||
.. note:: This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
"""
|
||||
__author__ = 'Nyall Dawson'
|
||||
__date__ = '05/07/2017'
|
||||
__copyright__ = 'Copyright 2017, The QGIS Project'
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import qgis # NOQA
|
||||
|
||||
from qgis.core import (QgsProject,
|
||||
QgsLayout,
|
||||
QgsLayoutGridSettings,
|
||||
QgsLayoutMeasurement,
|
||||
QgsUnitTypes,
|
||||
QgsLayoutPoint,
|
||||
QgsLayoutItemPage)
|
||||
from qgis.PyQt.QtGui import (QPen,
|
||||
QColor)
|
||||
|
||||
from qgis.testing import start_app, unittest
|
||||
|
||||
start_app()
|
||||
|
||||
|
||||
class TestQgsLayoutGridSettings(unittest.TestCase):
|
||||
|
||||
def testGettersSetters(self):
|
||||
p = QgsProject()
|
||||
l = QgsLayout(p)
|
||||
s = QgsLayoutGridSettings()
|
||||
s.setResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutPoints))
|
||||
self.assertEqual(s.resolution().length(), 5.0)
|
||||
self.assertEqual(s.resolution().units(), QgsUnitTypes.LayoutPoints)
|
||||
|
||||
s.setOffset(QgsLayoutPoint(6, 7, QgsUnitTypes.LayoutPixels))
|
||||
self.assertEqual(s.offset().x(), 6.0)
|
||||
self.assertEqual(s.offset().y(), 7.0)
|
||||
self.assertEqual(s.offset().units(), QgsUnitTypes.LayoutPixels)
|
||||
|
||||
s.setPen(QPen(QColor(255, 0, 255)))
|
||||
self.assertEqual(s.pen().color().name(), QColor(255, 0, 255).name())
|
||||
|
||||
s.setStyle(QgsLayoutGridSettings.StyleDots)
|
||||
self.assertEqual(s.style(), QgsLayoutGridSettings.StyleDots)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -17,13 +17,13 @@ import qgis # NOQA
|
||||
from qgis.core import (QgsProject,
|
||||
QgsLayout,
|
||||
QgsLayoutSnapper,
|
||||
QgsLayoutGridSettings,
|
||||
QgsLayoutMeasurement,
|
||||
QgsUnitTypes,
|
||||
QgsLayoutPoint,
|
||||
QgsLayoutItemPage)
|
||||
from qgis.PyQt.QtCore import QRectF, QPointF
|
||||
from qgis.PyQt.QtGui import (QTransform,
|
||||
QPen,
|
||||
from qgis.PyQt.QtCore import QPointF
|
||||
from qgis.PyQt.QtGui import (QPen,
|
||||
QColor)
|
||||
|
||||
from qgis.testing import start_app, unittest
|
||||
@ -37,20 +37,6 @@ class TestQgsLayoutSnapper(unittest.TestCase):
|
||||
p = QgsProject()
|
||||
l = QgsLayout(p)
|
||||
s = QgsLayoutSnapper(l)
|
||||
s.setGridResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutPoints))
|
||||
self.assertEqual(s.gridResolution().length(), 5.0)
|
||||
self.assertEqual(s.gridResolution().units(), QgsUnitTypes.LayoutPoints)
|
||||
|
||||
s.setGridOffset(QgsLayoutPoint(6, 7, QgsUnitTypes.LayoutPixels))
|
||||
self.assertEqual(s.gridOffset().x(), 6.0)
|
||||
self.assertEqual(s.gridOffset().y(), 7.0)
|
||||
self.assertEqual(s.gridOffset().units(), QgsUnitTypes.LayoutPixels)
|
||||
|
||||
s.setGridPen(QPen(QColor(255, 0, 255)))
|
||||
self.assertEqual(s.gridPen().color().name(), QColor(255, 0, 255).name())
|
||||
|
||||
s.setGridStyle(QgsLayoutSnapper.GridDots)
|
||||
self.assertEqual(s.gridStyle(), QgsLayoutSnapper.GridDots)
|
||||
|
||||
s.setSnapToGrid(False)
|
||||
self.assertFalse(s.snapToGrid())
|
||||
@ -69,7 +55,8 @@ class TestQgsLayoutSnapper(unittest.TestCase):
|
||||
l.pageCollection().addPage(page)
|
||||
s = QgsLayoutSnapper(l)
|
||||
|
||||
s.setGridResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutMillimeters))
|
||||
l.gridSettings().setResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutMillimeters))
|
||||
|
||||
s.setSnapToGrid(True)
|
||||
s.setSnapTolerance(1)
|
||||
|
||||
@ -113,12 +100,11 @@ class TestQgsLayoutSnapper(unittest.TestCase):
|
||||
self.assertEqual(point, QPointF(0.5, 0.5))
|
||||
|
||||
# with offset grid
|
||||
s.setGridOffset(QgsLayoutPoint(2, 0))
|
||||
l.gridSettings().setOffset(QgsLayoutPoint(2, 0))
|
||||
point, snapped = s.snapPointToGrid(QPointF(13, 23), 1)
|
||||
self.assertTrue(snapped)
|
||||
self.assertEqual(point, QPointF(12, 23))
|
||||
|
||||
|
||||
def testSnapPoint(self):
|
||||
p = QgsProject()
|
||||
l = QgsLayout(p)
|
||||
@ -128,7 +114,7 @@ class TestQgsLayoutSnapper(unittest.TestCase):
|
||||
s = QgsLayoutSnapper(l)
|
||||
|
||||
# first test snapping to grid
|
||||
s.setGridResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutMillimeters))
|
||||
l.gridSettings().setResolution(QgsLayoutMeasurement(5, QgsUnitTypes.LayoutMillimeters))
|
||||
s.setSnapToGrid(True)
|
||||
s.setSnapTolerance(1)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user