Skip to content

Commit b4f5025

Browse files
committedJul 18, 2017
Port shape items to layouts
1 parent 38cbbe2 commit b4f5025

12 files changed

+651
-1
lines changed
 

‎python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383
%Include layout/qgslayoutitem.sip
384384
%Include layout/qgslayoutitemmap.sip
385385
%Include layout/qgslayoutitemregistry.sip
386+
%Include layout/qgslayoutitemshape.sip
386387
%Include layout/qgslayoutobject.sip
387388
%Include symbology-ng/qgscptcityarchive.sip
388389
%Include symbology-ng/qgssvgcache.sip

‎python/core/layout/qgslayoutitemregistry.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ class QgsLayoutItemRegistry : QObject
101101

102102
// known
103103
LayoutPage,
104+
LayoutRectangle,
105+
LayoutEllipse,
106+
LayoutTriangle,
104107

105108
// item
106109
PluginItem,
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/layout/qgslayoutitemshape.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsLayoutItemShape : QgsLayoutItem
12+
{
13+
%Docstring
14+
Base class for layout items which are basic shapes (e.g. rectangles, ellipses).
15+
.. versionadded:: 3.0
16+
%End
17+
18+
%TypeHeaderCode
19+
#include "qgslayoutitemshape.h"
20+
%End
21+
public:
22+
23+
void setSymbol( QgsFillSymbol *symbol );
24+
%Docstring
25+
Sets the fill ``symbol`` used to draw the shape. Ownership is not transferred
26+
and a clone of the symbol is made.
27+
.. seealso:: symbol()
28+
%End
29+
30+
QgsFillSymbol *symbol();
31+
%Docstring
32+
Returns the fill symbol used to draw the shape.
33+
.. seealso:: setSymbol()
34+
:rtype: QgsFillSymbol
35+
%End
36+
37+
protected:
38+
39+
explicit QgsLayoutItemShape( QgsLayout *layout );
40+
%Docstring
41+
Constructor for QgsLayoutItemShape, with the specified parent ``layout``.
42+
%End
43+
44+
};
45+
46+
47+
class QgsLayoutItemRectangularShape : QgsLayoutItemShape
48+
{
49+
%Docstring
50+
A rectangular shape item for layouts.
51+
.. versionadded:: 3.0
52+
%End
53+
54+
%TypeHeaderCode
55+
#include "qgslayoutitemshape.h"
56+
%End
57+
public:
58+
59+
explicit QgsLayoutItemRectangularShape( QgsLayout *layout );
60+
%Docstring
61+
Constructor for QgsLayoutItemRectangularShape, with the specified parent ``layout``.
62+
%End
63+
virtual int type() const;
64+
65+
static QgsLayoutItemRectangularShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
66+
%Docstring
67+
:rtype: QgsLayoutItemRectangularShape
68+
%End
69+
70+
void setCornerRadius( QgsLayoutMeasurement radius );
71+
%Docstring
72+
Sets the corner ``radius`` for rounded rectangle corners.
73+
.. seealso:: cornerRadius()
74+
%End
75+
76+
QgsLayoutMeasurement cornerRadius() const;
77+
%Docstring
78+
Returns the corner radius for rounded rectangle corners.
79+
.. seealso:: setCornerRadius()
80+
:rtype: QgsLayoutMeasurement
81+
%End
82+
83+
protected:
84+
85+
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
86+
87+
88+
};
89+
90+
class QgsLayoutItemEllipseShape : QgsLayoutItemShape
91+
{
92+
%Docstring
93+
A ellipse shape item for layouts.
94+
.. versionadded:: 3.0
95+
%End
96+
97+
%TypeHeaderCode
98+
#include "qgslayoutitemshape.h"
99+
%End
100+
public:
101+
102+
explicit QgsLayoutItemEllipseShape( QgsLayout *layout );
103+
%Docstring
104+
Constructor for QgsLayoutItemEllipseShape, with the specified parent ``layout``.
105+
%End
106+
virtual int type() const;
107+
108+
static QgsLayoutItemEllipseShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
109+
%Docstring
110+
:rtype: QgsLayoutItemEllipseShape
111+
%End
112+
113+
protected:
114+
115+
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
116+
117+
118+
};
119+
120+
class QgsLayoutItemTriangleShape : QgsLayoutItemShape
121+
{
122+
%Docstring
123+
A triangle shape item for layouts.
124+
.. versionadded:: 3.0
125+
%End
126+
127+
%TypeHeaderCode
128+
#include "qgslayoutitemshape.h"
129+
%End
130+
public:
131+
132+
explicit QgsLayoutItemTriangleShape( QgsLayout *layout );
133+
%Docstring
134+
Constructor for QgsLayoutItemTriangleShape, with the specified parent ``layout``.
135+
%End
136+
virtual int type() const;
137+
138+
static QgsLayoutItemTriangleShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
139+
%Docstring
140+
:rtype: QgsLayoutItemTriangleShape
141+
%End
142+
143+
protected:
144+
145+
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
146+
147+
148+
};
149+
150+
/************************************************************************
151+
* This file has been generated automatically from *
152+
* *
153+
* src/core/layout/qgslayoutitemshape.h *
154+
* *
155+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
156+
************************************************************************/

‎python/gui/layout/qgslayoutviewrubberband.sip

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,36 @@ class QgsLayoutViewEllipticalRubberBand : QgsLayoutViewRubberBand
179179
virtual QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 );
180180

181181

182+
};
183+
184+
class QgsLayoutViewTriangleRubberBand : QgsLayoutViewRubberBand
185+
{
186+
%Docstring
187+
QgsLayoutViewTriangleRubberBand is triangular rubber band for use within QgsLayoutView widgets.
188+
.. versionadded:: 3.0
189+
%End
190+
191+
%TypeHeaderCode
192+
#include "qgslayoutviewrubberband.h"
193+
%End
194+
public:
195+
196+
QgsLayoutViewTriangleRubberBand( QgsLayoutView *view = 0 );
197+
%Docstring
198+
Constructor for QgsLayoutViewTriangleRubberBand.
199+
%End
200+
virtual QgsLayoutViewTriangleRubberBand *create( QgsLayoutView *view ) const /Factory/;
201+
202+
203+
~QgsLayoutViewTriangleRubberBand();
204+
205+
virtual void start( QPointF position, Qt::KeyboardModifiers modifiers );
206+
207+
virtual void update( QPointF position, Qt::KeyboardModifiers modifiers );
208+
209+
virtual QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 );
210+
211+
182212
};
183213
/************************************************************************
184214
* This file has been generated automatically from *

‎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/qgslayoutitem.cpp
355355
layout/qgslayoutitemmap.cpp
356356
layout/qgslayoutitemregistry.cpp
357+
layout/qgslayoutitemshape.cpp
357358
layout/qgslayoutmeasurement.cpp
358359
layout/qgslayoutmeasurementconverter.cpp
359360
layout/qgslayoutobject.cpp
@@ -677,6 +678,7 @@ SET(QGIS_CORE_MOC_HDRS
677678
layout/qgslayoutitem.h
678679
layout/qgslayoutitemmap.h
679680
layout/qgslayoutitemregistry.h
681+
layout/qgslayoutitemshape.h
680682
layout/qgslayoutobject.h
681683

682684
symbology-ng/qgscptcityarchive.h

‎src/core/layout/qgslayoutitemregistry.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
***************************************************************************/
1616

1717
#include "qgslayoutitemregistry.h"
18+
#include "qgslayoutitemshape.h"
1819
#include "qgsgloweffect.h"
1920
#include "qgseffectstack.h"
2021
#include <QPainter>
@@ -41,6 +42,10 @@ bool QgsLayoutItemRegistry::populate()
4142
};
4243

4344
addLayoutItemType( new QgsLayoutItemMetadata( 101, QStringLiteral( "temp type" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), createTemporaryItem ) );
45+
addLayoutItemType( new QgsLayoutItemMetadata( LayoutRectangle, QStringLiteral( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), QgsLayoutItemRectangularShape::create ) );
46+
addLayoutItemType( new QgsLayoutItemMetadata( LayoutEllipse, QStringLiteral( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), QgsLayoutItemEllipseShape::create ) );
47+
addLayoutItemType( new QgsLayoutItemMetadata( LayoutTriangle, QStringLiteral( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), QgsLayoutItemTriangleShape::create ) );
48+
4449
return true;
4550
}
4651

‎src/core/layout/qgslayoutitemregistry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ class CORE_EXPORT QgsLayoutItemRegistry : public QObject
183183

184184
// known item types
185185
LayoutPage, //!< Page items
186+
LayoutRectangle, //!< Rectangular shape item
187+
LayoutEllipse, //!< Ellipse shape item
188+
LayoutTriangle, //!< Triangle shape item
186189

187190
// item types provided by plugins
188191
PluginItem, //!< Starting point for plugin item types
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/***************************************************************************
2+
qgslayoutitemshape.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 "qgslayoutitemshape.h"
18+
#include "qgslayout.h"
19+
#include "qgslayoututils.h"
20+
21+
#include <QPainter>
22+
23+
QgsLayoutItemShape::QgsLayoutItemShape( QgsLayout *layout )
24+
: QgsLayoutItem( layout )
25+
{
26+
QgsStringMap properties;
27+
properties.insert( QStringLiteral( "color" ), QStringLiteral( "white" ) );
28+
properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
29+
properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
30+
properties.insert( QStringLiteral( "color_border" ), QStringLiteral( "black" ) );
31+
properties.insert( QStringLiteral( "width_border" ), QStringLiteral( "0.3" ) );
32+
properties.insert( QStringLiteral( "joinstyle" ), QStringLiteral( "miter" ) );
33+
mShapeStyleSymbol.reset( QgsFillSymbol::createSimple( properties ) );
34+
}
35+
36+
void QgsLayoutItemShape::setSymbol( QgsFillSymbol *symbol )
37+
{
38+
if ( !symbol )
39+
return;
40+
41+
mShapeStyleSymbol.reset( symbol->clone() );
42+
}
43+
44+
//
45+
// QgsLayoutItemRectangularShape
46+
//
47+
48+
QgsLayoutItemRectangularShape::QgsLayoutItemRectangularShape( QgsLayout *layout )
49+
: QgsLayoutItemShape( layout )
50+
, mCornerRadius( 0.0 )
51+
{
52+
53+
}
54+
55+
QgsLayoutItemRectangularShape *QgsLayoutItemRectangularShape::create( QgsLayout *layout, const QVariantMap &settings )
56+
{
57+
Q_UNUSED( settings );
58+
return new QgsLayoutItemRectangularShape( layout );
59+
}
60+
61+
void QgsLayoutItemRectangularShape::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
62+
{
63+
QPainter *painter = context.painter();
64+
painter->setPen( Qt::NoPen );
65+
painter->setBrush( Qt::NoBrush );
66+
67+
double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
68+
69+
QPolygonF shapePolygon;
70+
if ( mCornerRadius.length() > 0 )
71+
{
72+
//shapes with curves must be enlarged before conversion to QPolygonF, or
73+
//the curves are approximated too much and appear jaggy
74+
QTransform t = QTransform::fromScale( 100, 100 );
75+
//inverse transform used to scale created polygons back to expected size
76+
QTransform ti = t.inverted();
77+
78+
QPainterPath roundedRectPath;
79+
double radius = mLayout->convertToLayoutUnits( mCornerRadius ) * scale;
80+
roundedRectPath.addRoundedRect( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ), radius, radius );
81+
QPolygonF roundedPoly = roundedRectPath.toFillPolygon( t );
82+
shapePolygon = ti.map( roundedPoly );
83+
}
84+
else
85+
{
86+
shapePolygon = QPolygonF( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
87+
}
88+
89+
QList<QPolygonF> rings; //empty list
90+
91+
symbol()->startRender( context );
92+
symbol()->renderPolygon( shapePolygon, &rings, nullptr, context );
93+
symbol()->stopRender( context );
94+
}
95+
96+
97+
//
98+
// QgsLayoutItemEllipseShape
99+
//
100+
101+
QgsLayoutItemEllipseShape::QgsLayoutItemEllipseShape( QgsLayout *layout )
102+
: QgsLayoutItemShape( layout )
103+
{
104+
105+
}
106+
107+
QgsLayoutItemEllipseShape *QgsLayoutItemEllipseShape::create( QgsLayout *layout, const QVariantMap &settings )
108+
{
109+
Q_UNUSED( settings );
110+
return new QgsLayoutItemEllipseShape( layout );
111+
}
112+
113+
void QgsLayoutItemEllipseShape::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
114+
{
115+
QPainter *painter = context.painter();
116+
painter->setPen( Qt::NoPen );
117+
painter->setBrush( Qt::NoBrush );
118+
119+
double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
120+
121+
//shapes with curves must be enlarged before conversion to QPolygonF, or
122+
//the curves are approximated too much and appear jaggy
123+
QTransform t = QTransform::fromScale( 100, 100 );
124+
//inverse transform used to scale created polygons back to expected size
125+
QTransform ti = t.inverted();
126+
127+
//create an ellipse
128+
QPainterPath ellipsePath;
129+
ellipsePath.addEllipse( QRectF( 0, 0, rect().width() * scale, rect().height() * scale ) );
130+
QPolygonF ellipsePoly = ellipsePath.toFillPolygon( t );
131+
QPolygonF shapePolygon = ti.map( ellipsePoly );
132+
133+
QList<QPolygonF> rings; //empty list
134+
135+
symbol()->startRender( context );
136+
symbol()->renderPolygon( shapePolygon, &rings, nullptr, context );
137+
symbol()->stopRender( context );
138+
}
139+
140+
//
141+
// QgsLayoutItemTriangleShape
142+
//
143+
144+
QgsLayoutItemTriangleShape::QgsLayoutItemTriangleShape( QgsLayout *layout )
145+
: QgsLayoutItemShape( layout )
146+
{
147+
148+
}
149+
150+
QgsLayoutItemTriangleShape *QgsLayoutItemTriangleShape::create( QgsLayout *layout, const QVariantMap &settings )
151+
{
152+
Q_UNUSED( settings );
153+
return new QgsLayoutItemTriangleShape( layout );
154+
}
155+
156+
void QgsLayoutItemTriangleShape::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
157+
{
158+
QPainter *painter = context.painter();
159+
painter->setPen( Qt::NoPen );
160+
painter->setBrush( Qt::NoBrush );
161+
162+
double scale = context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
163+
QPolygonF shapePolygon = QPolygonF() << QPointF( 0, rect().height() * scale )
164+
<< QPointF( rect().width() * scale, rect().height() * scale )
165+
<< QPointF( rect().width() / 2.0 * scale, 0 )
166+
<< QPointF( 0, rect().height() * scale );
167+
168+
QList<QPolygonF> rings; //empty list
169+
170+
symbol()->startRender( context );
171+
symbol()->renderPolygon( shapePolygon, &rings, nullptr, context );
172+
symbol()->stopRender( context );
173+
}

‎src/core/layout/qgslayoutitemshape.h

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/***************************************************************************
2+
qgslayoutitemshape.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+
17+
#ifndef QGSLAYOUTITEMSHAPE_H
18+
#define QGSLAYOUTITEMSHAPE_H
19+
20+
#include "qgis_core.h"
21+
#include "qgslayoutitem.h"
22+
#include "qgslayoutitemregistry.h"
23+
#include "qgssymbol.h"
24+
#include "qgslayoutmeasurement.h"
25+
26+
/**
27+
* \ingroup core
28+
* \class QgsLayoutItemShape
29+
* \brief Base class for layout items which are basic shapes (e.g. rectangles, ellipses).
30+
* \since QGIS 3.0
31+
*/
32+
class CORE_EXPORT QgsLayoutItemShape : public QgsLayoutItem
33+
{
34+
Q_OBJECT
35+
36+
public:
37+
38+
/**
39+
* Sets the fill \a symbol used to draw the shape. Ownership is not transferred
40+
* and a clone of the symbol is made.
41+
* \see symbol()
42+
*/
43+
void setSymbol( QgsFillSymbol *symbol );
44+
45+
/**
46+
* Returns the fill symbol used to draw the shape.
47+
* \see setSymbol()
48+
*/
49+
QgsFillSymbol *symbol() { return mShapeStyleSymbol.get(); }
50+
51+
protected:
52+
53+
/**
54+
* Constructor for QgsLayoutItemShape, with the specified parent \a layout.
55+
*/
56+
explicit QgsLayoutItemShape( QgsLayout *layout );
57+
58+
private:
59+
std::unique_ptr< QgsFillSymbol > mShapeStyleSymbol;
60+
};
61+
62+
63+
/**
64+
* \ingroup core
65+
* \class QgsLayoutItemRectangularShape
66+
* \brief A rectangular shape item for layouts.
67+
* \since QGIS 3.0
68+
*/
69+
class CORE_EXPORT QgsLayoutItemRectangularShape : public QgsLayoutItemShape
70+
{
71+
72+
Q_OBJECT
73+
74+
public:
75+
76+
/**
77+
* Constructor for QgsLayoutItemRectangularShape, with the specified parent \a layout.
78+
*/
79+
explicit QgsLayoutItemRectangularShape( QgsLayout *layout );
80+
virtual int type() const override { return QgsLayoutItemRegistry::LayoutRectangle; }
81+
82+
static QgsLayoutItemRectangularShape *create( QgsLayout *layout, const QVariantMap &settings ) SIP_FACTORY;
83+
84+
/**
85+
* Sets the corner \a radius for rounded rectangle corners.
86+
* \see cornerRadius()
87+
*/
88+
void setCornerRadius( QgsLayoutMeasurement radius ) { mCornerRadius = radius; }
89+
90+
/**
91+
* Returns the corner radius for rounded rectangle corners.
92+
* \see setCornerRadius()
93+
*/
94+
QgsLayoutMeasurement cornerRadius() const { return mCornerRadius; }
95+
96+
protected:
97+
98+
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
99+
100+
private:
101+
QgsLayoutMeasurement mCornerRadius;
102+
};
103+
104+
/**
105+
* \ingroup core
106+
* \class QgsLayoutItemEllipseShape
107+
* \brief A ellipse shape item for layouts.
108+
* \since QGIS 3.0
109+
*/
110+
class CORE_EXPORT QgsLayoutItemEllipseShape : public QgsLayoutItemShape
111+
{
112+
113+
Q_OBJECT
114+
115+
public:
116+
117+
/**
118+
* Constructor for QgsLayoutItemEllipseShape, with the specified parent \a layout.
119+
*/
120+
explicit QgsLayoutItemEllipseShape( QgsLayout *layout );
121+
virtual int type() const override { return QgsLayoutItemRegistry::LayoutEllipse; }
122+
123+
static QgsLayoutItemEllipseShape *create( QgsLayout *layout, const QVariantMap &settings ) SIP_FACTORY;
124+
125+
protected:
126+
127+
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
128+
129+
};
130+
131+
/**
132+
* \ingroup core
133+
* \class QgsLayoutItemTriangleShape
134+
* \brief A triangle shape item for layouts.
135+
* \since QGIS 3.0
136+
*/
137+
class CORE_EXPORT QgsLayoutItemTriangleShape : public QgsLayoutItemShape
138+
{
139+
140+
Q_OBJECT
141+
142+
public:
143+
144+
/**
145+
* Constructor for QgsLayoutItemTriangleShape, with the specified parent \a layout.
146+
*/
147+
explicit QgsLayoutItemTriangleShape( QgsLayout *layout );
148+
virtual int type() const override { return QgsLayoutItemRegistry::LayoutTriangle; }
149+
150+
static QgsLayoutItemTriangleShape *create( QgsLayout *layout, const QVariantMap &settings ) SIP_FACTORY;
151+
152+
protected:
153+
154+
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
155+
156+
};
157+
158+
#endif //QGSLAYOUTITEMSHAPE_H

‎src/gui/layout/qgslayoutitemguiregistry.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgslayoutitemguiregistry.h"
1818
#include "qgslayoutviewrubberband.h"
19+
#include "qgslayoutitemregistry.h"
1920
#include <QPainter>
2021

2122

@@ -40,13 +41,23 @@ bool QgsLayoutItemGuiRegistry::populate()
4041
if ( !mMetadata.isEmpty() )
4142
return false;
4243

43-
// add temporary item to register
4444
auto createRubberBand = ( []( QgsLayoutView * view )->QgsLayoutViewRubberBand *
4545
{
4646
return new QgsLayoutViewRectangularRubberBand( view );
4747
} );
48+
auto createEllipseBand = ( []( QgsLayoutView * view )->QgsLayoutViewRubberBand *
49+
{
50+
return new QgsLayoutViewEllipticalRubberBand( view );
51+
} );
52+
auto createTriangleBand = ( []( QgsLayoutView * view )->QgsLayoutViewRubberBand *
53+
{
54+
return new QgsLayoutViewTriangleRubberBand( view );
55+
} );
4856

4957
addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( 101, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), nullptr, createRubberBand ) );
58+
addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutRectangle, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), nullptr, createRubberBand ) );
59+
addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutEllipse, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), nullptr, createEllipseBand ) );
60+
addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutTriangle, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), nullptr, createTriangleBand ) );
5061
return true;
5162
}
5263

‎src/gui/layout/qgslayoutviewrubberband.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgslayoutview.h"
2020
#include <QGraphicsRectItem>
2121
#include <QGraphicsEllipseItem>
22+
#include <QGraphicsPolygonItem>
2223

2324
QgsLayoutViewRubberBand::QgsLayoutViewRubberBand( QgsLayoutView *view )
2425
: mView( view )
@@ -248,3 +249,78 @@ QRectF QgsLayoutViewEllipticalRubberBand::finish( QPointF position, Qt::Keyboard
248249
}
249250
return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
250251
}
252+
253+
//
254+
// QgsLayoutViewTriangleRubberBand
255+
//
256+
257+
QgsLayoutViewTriangleRubberBand::QgsLayoutViewTriangleRubberBand( QgsLayoutView *view )
258+
: QgsLayoutViewRubberBand( view )
259+
{
260+
261+
}
262+
263+
QgsLayoutViewTriangleRubberBand *QgsLayoutViewTriangleRubberBand::create( QgsLayoutView *view ) const
264+
{
265+
return new QgsLayoutViewTriangleRubberBand( view );
266+
}
267+
268+
QgsLayoutViewTriangleRubberBand::~QgsLayoutViewTriangleRubberBand()
269+
{
270+
if ( mRubberBandItem )
271+
{
272+
layout()->removeItem( mRubberBandItem );
273+
delete mRubberBandItem;
274+
}
275+
}
276+
277+
void QgsLayoutViewTriangleRubberBand::start( QPointF position, Qt::KeyboardModifiers )
278+
{
279+
QTransform t;
280+
mRubberBandItem = new QGraphicsPolygonItem();
281+
mRubberBandItem->setBrush( brush() );
282+
mRubberBandItem->setPen( pen() );
283+
mRubberBandStartPos = position;
284+
t.translate( position.x(), position.y() );
285+
mRubberBandItem->setTransform( t );
286+
mRubberBandItem->setZValue( QgsLayout::ZMapTool );
287+
layout()->addItem( mRubberBandItem );
288+
layout()->update();
289+
}
290+
291+
void QgsLayoutViewTriangleRubberBand::update( QPointF position, Qt::KeyboardModifiers modifiers )
292+
{
293+
if ( !mRubberBandItem )
294+
{
295+
return;
296+
}
297+
298+
bool constrainSquare = modifiers & Qt::ShiftModifier;
299+
bool fromCenter = modifiers & Qt::AltModifier;
300+
301+
QRectF newRect = updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
302+
303+
QPolygonF shapePolygon = QPolygonF() << QPointF( 0, newRect.height() )
304+
<< QPointF( newRect.width(), newRect.height() )
305+
<< QPointF( newRect.width() / 2.0, 0 )
306+
<< QPointF( 0, newRect.height() );
307+
308+
mRubberBandItem->setPolygon( shapePolygon );
309+
QTransform t;
310+
t.translate( newRect.x(), newRect.y() );
311+
mRubberBandItem->setTransform( t );
312+
}
313+
314+
QRectF QgsLayoutViewTriangleRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
315+
{
316+
bool constrainSquare = modifiers & Qt::ShiftModifier;
317+
bool fromCenter = modifiers & Qt::AltModifier;
318+
319+
if ( mRubberBandItem )
320+
{
321+
layout()->removeItem( mRubberBandItem );
322+
delete mRubberBandItem;
323+
mRubberBandItem = nullptr;
324+
}
325+
return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
326+
}

‎src/gui/layout/qgslayoutviewrubberband.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class QgsLayoutView;
2727
class QGraphicsRectItem;
2828
class QGraphicsEllipseItem;
29+
class QGraphicsPolygonItem;
2930
class QgsLayout;
3031

3132
/**
@@ -199,5 +200,36 @@ class GUI_EXPORT QgsLayoutViewEllipticalRubberBand : public QgsLayoutViewRubberB
199200
//! Start of rubber band creation
200201
QPointF mRubberBandStartPos;
201202

203+
};
204+
205+
/**
206+
* \ingroup gui
207+
* QgsLayoutViewTriangleRubberBand is triangular rubber band for use within QgsLayoutView widgets.
208+
* \since QGIS 3.0
209+
*/
210+
class GUI_EXPORT QgsLayoutViewTriangleRubberBand : public QgsLayoutViewRubberBand
211+
{
212+
public:
213+
214+
/**
215+
* Constructor for QgsLayoutViewTriangleRubberBand.
216+
*/
217+
QgsLayoutViewTriangleRubberBand( QgsLayoutView *view = nullptr );
218+
QgsLayoutViewTriangleRubberBand *create( QgsLayoutView *view ) const override SIP_FACTORY;
219+
220+
~QgsLayoutViewTriangleRubberBand();
221+
222+
void start( QPointF position, Qt::KeyboardModifiers modifiers ) override;
223+
void update( QPointF position, Qt::KeyboardModifiers modifiers ) override;
224+
QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 ) override;
225+
226+
private:
227+
228+
//! Rubber band item
229+
QGraphicsPolygonItem *mRubberBandItem = nullptr;
230+
231+
//! Start of rubber band creation
232+
QPointF mRubberBandStartPos;
233+
202234
};
203235
#endif // QGSLAYOUTVIEWRUBBERBAND_H

0 commit comments

Comments
 (0)
Please sign in to comment.