Skip to content

Commit 5cac2f7

Browse files
committedJul 11, 2017
Allow setting pen/brush for QgsLayoutViewRubberBand
1 parent a346736 commit 5cac2f7

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed
 

‎python/gui/layout/qgslayoutviewrubberband.sip

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,36 @@ class QgsLayoutViewRubberBand
7777
:rtype: QgsLayout
7878
%End
7979

80+
QBrush brush() const;
81+
%Docstring
82+
Returns the brush used for drawing the rubber band.
83+
.. seealso:: setBrush()
84+
.. seealso:: pen()
85+
:rtype: QBrush
86+
%End
87+
88+
void setBrush( const QBrush &brush );
89+
%Docstring
90+
Sets the ``brush`` used for drawing the rubber band.
91+
.. seealso:: brush()
92+
.. seealso:: setPen()
93+
%End
94+
95+
QPen pen() const;
96+
%Docstring
97+
Returns the pen used for drawing the rubber band.
98+
.. seealso:: setPen()
99+
.. seealso:: brush()
100+
:rtype: QPen
101+
%End
102+
103+
void setPen( const QPen &pen );
104+
%Docstring
105+
Sets the ``pen`` used for drawing the rubber band.
106+
.. seealso:: pen()
107+
.. seealso:: setBrush()
108+
%End
109+
80110
protected:
81111

82112
QRectF updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter );

‎src/gui/layout/qgslayoutviewrubberband.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ QRectF QgsLayoutViewRubberBand::updateRect( QPointF start, QPointF position, boo
9999
return QRectF( x, y, width, height );
100100
}
101101

102+
QPen QgsLayoutViewRubberBand::pen() const
103+
{
104+
return mPen;
105+
}
106+
107+
void QgsLayoutViewRubberBand::setPen( const QPen &pen )
108+
{
109+
mPen = pen;
110+
}
111+
112+
QBrush QgsLayoutViewRubberBand::brush() const
113+
{
114+
return mBrush;
115+
}
116+
117+
void QgsLayoutViewRubberBand::setBrush( const QBrush &brush )
118+
{
119+
mBrush = brush;
120+
}
121+
102122

103123
QgsLayoutViewRectangularRubberBand::QgsLayoutViewRectangularRubberBand( QgsLayoutView *view )
104124
: QgsLayoutViewRubberBand( view )
@@ -123,8 +143,8 @@ void QgsLayoutViewRectangularRubberBand::start( QPointF position, Qt::KeyboardMo
123143
{
124144
QTransform t;
125145
mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
126-
mRubberBandItem->setBrush( Qt::NoBrush );
127-
mRubberBandItem->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
146+
mRubberBandItem->setBrush( brush() );
147+
mRubberBandItem->setPen( pen() );
128148
mRubberBandStartPos = position;
129149
t.translate( position.x(), position.y() );
130150
mRubberBandItem->setTransform( t );
@@ -188,8 +208,8 @@ void QgsLayoutViewEllipticalRubberBand::start( QPointF position, Qt::KeyboardMod
188208
{
189209
QTransform t;
190210
mRubberBandItem = new QGraphicsEllipseItem( 0, 0, 0, 0 );
191-
mRubberBandItem->setBrush( Qt::NoBrush );
192-
mRubberBandItem->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) );
211+
mRubberBandItem->setBrush( brush() );
212+
mRubberBandItem->setPen( pen() );
193213
mRubberBandStartPos = position;
194214
t.translate( position.x(), position.y() );
195215
mRubberBandItem->setTransform( t );

‎src/gui/layout/qgslayoutviewrubberband.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include "qgis_gui.h"
2222
#include "qgis_sip.h"
23+
#include <QBrush>
24+
#include <QPen>
2325

2426
class QgsLayoutView;
2527
class QGraphicsRectItem;
@@ -89,6 +91,34 @@ class GUI_EXPORT QgsLayoutViewRubberBand
8991
*/
9092
QgsLayout *layout() const;
9193

94+
/**
95+
* Returns the brush used for drawing the rubber band.
96+
* \see setBrush()
97+
* \see pen()
98+
*/
99+
QBrush brush() const;
100+
101+
/**
102+
* Sets the \a brush used for drawing the rubber band.
103+
* \see brush()
104+
* \see setPen()
105+
*/
106+
void setBrush( const QBrush &brush );
107+
108+
/**
109+
* Returns the pen used for drawing the rubber band.
110+
* \see setPen()
111+
* \see brush()
112+
*/
113+
QPen pen() const;
114+
115+
/**
116+
* Sets the \a pen used for drawing the rubber band.
117+
* \see pen()
118+
* \see setBrush()
119+
*/
120+
void setPen( const QPen &pen );
121+
92122
protected:
93123

94124
/**
@@ -103,6 +133,9 @@ class GUI_EXPORT QgsLayoutViewRubberBand
103133

104134
QgsLayoutView *mView = nullptr;
105135

136+
QBrush mBrush = Qt::NoBrush;
137+
QPen mPen = QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 );
138+
106139
};
107140

108141

‎tests/src/gui/testqgslayoutview.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TestQgsLayoutView: public QObject
3636
void tool();
3737
void events();
3838
void registryUtils();
39+
void rubberBand();
3940

4041
private:
4142

@@ -271,5 +272,14 @@ void TestQgsLayoutView::registryUtils()
271272

272273
}
273274

275+
void TestQgsLayoutView::rubberBand()
276+
{
277+
QgsLayoutViewRectangularRubberBand band;
278+
band.setBrush( QBrush( QColor( 255, 0, 0 ) ) );
279+
QCOMPARE( band.brush().color(), QColor( 255, 0, 0 ) );
280+
band.setPen( QPen( QColor( 0, 255, 0 ) ) );
281+
QCOMPARE( band.pen().color(), QColor( 0, 255, 0 ) );
282+
}
283+
274284
QGSTEST_MAIN( TestQgsLayoutView )
275285
#include "testqgslayoutview.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.