Skip to content

Commit db36440

Browse files
committed
Start on classes for snapping guide items
1 parent 3aa231e commit db36440

File tree

9 files changed

+426
-1
lines changed

9 files changed

+426
-1
lines changed

python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@
384384
%Include gps/qgsnmeaconnection.sip
385385
%Include gps/qgsgpsdconnection.sip
386386
%Include layout/qgslayout.sip
387+
%Include layout/qgslayoutguidecollection.sip
387388
%Include layout/qgslayoutitem.sip
388389
%Include layout/qgslayoutitemmap.sip
389390
%Include layout/qgslayoutitempage.sip

python/core/layout/qgslayout.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator
2525
ZPage,
2626
ZItem,
2727
ZGrid,
28+
ZGuide,
2829
ZMapTool,
2930
ZSnapIndicator,
3031
};
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/layout/qgslayoutguidecollection.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsLayoutGuide
12+
{
13+
%Docstring
14+
Contains the configuration for a single snap guide used by a layout.
15+
.. versionadded:: 3.0
16+
%End
17+
18+
%TypeHeaderCode
19+
#include "qgslayoutguidecollection.h"
20+
%End
21+
public:
22+
23+
enum Orientation
24+
{
25+
Horizontal,
26+
Vertical,
27+
};
28+
29+
QgsLayoutGuide( QgsLayout *layout, Orientation orientation, const QgsLayoutMeasurement &position );
30+
31+
Orientation orientation() const;
32+
%Docstring
33+
Returns the guide's orientation.
34+
:rtype: Orientation
35+
%End
36+
37+
QgsLayoutMeasurement position() const;
38+
%Docstring
39+
Returns the guide's position within the page.
40+
41+
The position indicates either the horizontal or vertical position
42+
of the guide, depending on the guide's orientation().
43+
44+
.. seealso:: setPosition()
45+
:rtype: QgsLayoutMeasurement
46+
%End
47+
48+
void setPosition( const QgsLayoutMeasurement &position );
49+
%Docstring
50+
Sets the guide's ``position`` within the page.
51+
52+
The ``position`` argument indicates either the horizontal or vertical position
53+
of the guide, depending on the guide's orientation().
54+
55+
.. seealso:: position()
56+
%End
57+
58+
int page() const;
59+
%Docstring
60+
Returns the page number of the guide.
61+
62+
Page numbering begins at 0.
63+
64+
.. seealso:: setPage()
65+
:rtype: int
66+
%End
67+
68+
void setPage( int page );
69+
%Docstring
70+
Sets the ``page`` number of the guide.
71+
72+
Page numbering begins at 0.
73+
74+
.. seealso:: page()
75+
%End
76+
77+
void update();
78+
%Docstring
79+
Updates the position of the guide's line item.
80+
%End
81+
82+
QGraphicsLineItem *item();
83+
%Docstring
84+
Returns the guide's line item.
85+
:rtype: QGraphicsLineItem
86+
%End
87+
88+
};
89+
90+
91+
92+
/************************************************************************
93+
* This file has been generated automatically from *
94+
* *
95+
* src/core/layout/qgslayoutguidecollection.h *
96+
* *
97+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
98+
************************************************************************/

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ SET(QGIS_CORE_SRCS
354354
layout/qgslayout.cpp
355355
layout/qgslayoutcontext.cpp
356356
layout/qgslayoutgridsettings.cpp
357+
layout/qgslayoutguidecollection.cpp
357358
layout/qgslayoutitem.cpp
358359
layout/qgslayoutitemmap.cpp
359360
layout/qgslayoutitempage.cpp
@@ -684,6 +685,7 @@ SET(QGIS_CORE_MOC_HDRS
684685
gps/qgsgpsdconnection.h
685686

686687
layout/qgslayout.h
688+
layout/qgslayoutguidecollection.h
687689
layout/qgslayoutitem.h
688690
layout/qgslayoutitemmap.h
689691
layout/qgslayoutitempage.h

src/core/layout/qgslayout.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
4343
{
4444
ZPage = 0, //!< Z-value for page (paper) items
4545
ZItem = 1, //!< Minimum z value for items
46-
ZGrid = 9999, //!< Z-value for page grids
46+
ZGrid = 9998, //!< Z-value for page grids
47+
ZGuide = 9999, //!< Z-value for page guides
4748
ZMapTool = 10000, //!< Z-value for temporary map tool items
4849
ZSnapIndicator = 10001, //!< Z-value for snapping indicator
4950
};
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/***************************************************************************
2+
qgslayoutguidecollection.cpp
3+
----------------------------
4+
begin : July 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#include "qgslayoutguidecollection.h"
18+
#include "qgslayout.h"
19+
#include <QGraphicsLineItem>
20+
21+
22+
//
23+
// QgsLayoutGuide
24+
//
25+
26+
QgsLayoutGuide::QgsLayoutGuide( QgsLayout *layout, Orientation orientation, const QgsLayoutMeasurement &position )
27+
: mOrientation( orientation )
28+
, mPosition( position )
29+
, mLayout( layout )
30+
, mLineItem( new QGraphicsLineItem() )
31+
{
32+
mLineItem->hide();
33+
mLineItem->setZValue( QgsLayout::ZGuide );
34+
mLayout->addItem( mLineItem.get() );
35+
36+
QPen linePen( Qt::SolidLine );
37+
linePen.setColor( Qt::red );
38+
// use a pen width of 0, since this activates a cosmetic pen
39+
// which doesn't scale with the composer and keeps a constant size
40+
linePen.setWidthF( 0 );
41+
mLineItem->setPen( linePen );
42+
}
43+
44+
QgsLayoutMeasurement QgsLayoutGuide::position() const
45+
{
46+
return mPosition;
47+
}
48+
49+
void QgsLayoutGuide::setPosition( const QgsLayoutMeasurement &position )
50+
{
51+
mPosition = position;
52+
update();
53+
}
54+
55+
int QgsLayoutGuide::page() const
56+
{
57+
return mPage;
58+
}
59+
60+
void QgsLayoutGuide::setPage( int page )
61+
{
62+
mPage = page;
63+
update();
64+
}
65+
66+
void QgsLayoutGuide::update()
67+
{
68+
// first find matching page
69+
if ( mPage >= mLayout->pageCollection()->pageCount() )
70+
{
71+
mLineItem->hide();
72+
return;
73+
}
74+
75+
QgsLayoutItemPage *page = mLayout->pageCollection()->page( mPage );
76+
mLineItem->setParentItem( page );
77+
double layoutPos = mLayout->convertToLayoutUnits( mPosition );
78+
switch ( mOrientation )
79+
{
80+
case Horizontal:
81+
if ( layoutPos > page->rect().width() )
82+
{
83+
mLineItem->hide();
84+
}
85+
else
86+
{
87+
mLineItem->setLine( 0, layoutPos, page->rect().width(), layoutPos );
88+
mLineItem->show();
89+
}
90+
91+
break;
92+
93+
case Vertical:
94+
if ( layoutPos > page->rect().height() )
95+
{
96+
mLineItem->hide();
97+
}
98+
else
99+
{
100+
mLineItem->setLine( layoutPos, 0, layoutPos, page->rect().height() );
101+
mLineItem->show();
102+
}
103+
104+
break;
105+
}
106+
}
107+
108+
QGraphicsLineItem *QgsLayoutGuide::item()
109+
{
110+
return mLineItem.get();
111+
}
112+
113+
QgsLayoutGuide::Orientation QgsLayoutGuide::orientation() const
114+
{
115+
return mOrientation;
116+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/***************************************************************************
2+
qgslayoutguidecollection.h
3+
--------------------------
4+
begin : July 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSLAYOUTGUIDECOLLECTION_H
17+
#define QGSLAYOUTGUIDECOLLECTION_H
18+
19+
#include "qgis_core.h"
20+
#include "qgslayoutmeasurement.h"
21+
#include "qgslayoutpoint.h"
22+
#include <QPen>
23+
#include <memory>
24+
25+
class QgsLayout;
26+
class QGraphicsLineItem;
27+
28+
/**
29+
* \ingroup core
30+
* \class QgsLayoutGuide
31+
* \brief Contains the configuration for a single snap guide used by a layout.
32+
* \since QGIS 3.0
33+
*/
34+
class CORE_EXPORT QgsLayoutGuide
35+
{
36+
37+
public:
38+
39+
//! Guide orientation
40+
enum Orientation
41+
{
42+
Horizontal, //!< Horizontal guide
43+
Vertical, //!< Vertical guide
44+
};
45+
46+
QgsLayoutGuide( QgsLayout *layout, Orientation orientation, const QgsLayoutMeasurement &position );
47+
48+
/**
49+
* Returns the guide's orientation.
50+
*/
51+
Orientation orientation() const;
52+
53+
/**
54+
* Returns the guide's position within the page.
55+
*
56+
* The position indicates either the horizontal or vertical position
57+
* of the guide, depending on the guide's orientation().
58+
*
59+
* \see setPosition()
60+
*/
61+
QgsLayoutMeasurement position() const;
62+
63+
/**
64+
* Sets the guide's \a position within the page.
65+
*
66+
* The \a position argument indicates either the horizontal or vertical position
67+
* of the guide, depending on the guide's orientation().
68+
*
69+
* \see position()
70+
*/
71+
void setPosition( const QgsLayoutMeasurement &position );
72+
73+
/**
74+
* Returns the page number of the guide.
75+
*
76+
* Page numbering begins at 0.
77+
*
78+
* \see setPage()
79+
*/
80+
int page() const;
81+
82+
/**
83+
* Sets the \a page number of the guide.
84+
*
85+
* Page numbering begins at 0.
86+
*
87+
* \see page()
88+
*/
89+
void setPage( int page );
90+
91+
/**
92+
* Updates the position of the guide's line item.
93+
*/
94+
void update();
95+
96+
/**
97+
* Returns the guide's line item.
98+
*/
99+
QGraphicsLineItem *item();
100+
101+
private:
102+
103+
Orientation mOrientation = Vertical;
104+
105+
//! Horizontal/vertical position of guide on page
106+
QgsLayoutMeasurement mPosition;
107+
108+
//! Page number, 0 index based
109+
int mPage = 0;
110+
111+
QgsLayout *mLayout = nullptr;
112+
113+
//! Line item used in scene for guide
114+
std::shared_ptr< QGraphicsLineItem > mLineItem;
115+
};
116+
117+
118+
119+
#endif //QGSLAYOUTGUIDECOLLECTION_H

tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ ADD_PYTHON_TEST(PyQgsLayoutManager test_qgslayoutmanager.py)
7979
ADD_PYTHON_TEST(PyQgsLayoutPageCollection test_qgslayoutpagecollection.py)
8080
ADD_PYTHON_TEST(PyQgsLayoutView test_qgslayoutview.py)
8181
ADD_PYTHON_TEST(PyQgsLayoutGridSettings test_qgslayoutgridsettings.py)
82+
ADD_PYTHON_TEST(PyQgsLayoutGuide test_qgslayoutguides.py)
8283
ADD_PYTHON_TEST(PyQgsLayoutItemPropertiesDialog test_qgslayoutitempropertiesdialog.py)
8384
ADD_PYTHON_TEST(PyQgsLayoutSnapper test_qgslayoutsnapper.py)
8485
ADD_PYTHON_TEST(PyQgsLayoutUnitsComboBox test_qgslayoutunitscombobox.py)

0 commit comments

Comments
 (0)