From 5cac2f71f00dd7981c82e7aa5ee9c330e9c000d1 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 5 Jul 2017 17:55:16 +1000 Subject: [PATCH] Allow setting pen/brush for QgsLayoutViewRubberBand --- python/gui/layout/qgslayoutviewrubberband.sip | 30 +++++++++++++++++ src/gui/layout/qgslayoutviewrubberband.cpp | 28 +++++++++++++--- src/gui/layout/qgslayoutviewrubberband.h | 33 +++++++++++++++++++ tests/src/gui/testqgslayoutview.cpp | 10 ++++++ 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/python/gui/layout/qgslayoutviewrubberband.sip b/python/gui/layout/qgslayoutviewrubberband.sip index e86cb8de2c2..fae3771f666 100644 --- a/python/gui/layout/qgslayoutviewrubberband.sip +++ b/python/gui/layout/qgslayoutviewrubberband.sip @@ -77,6 +77,36 @@ class QgsLayoutViewRubberBand :rtype: QgsLayout %End + QBrush brush() const; +%Docstring + Returns the brush used for drawing the rubber band. +.. seealso:: setBrush() +.. seealso:: pen() + :rtype: QBrush +%End + + void setBrush( const QBrush &brush ); +%Docstring + Sets the ``brush`` used for drawing the rubber band. +.. seealso:: brush() +.. seealso:: setPen() +%End + + QPen pen() const; +%Docstring + Returns the pen used for drawing the rubber band. +.. seealso:: setPen() +.. seealso:: brush() + :rtype: QPen +%End + + void setPen( const QPen &pen ); +%Docstring + Sets the ``pen`` used for drawing the rubber band. +.. seealso:: pen() +.. seealso:: setBrush() +%End + protected: QRectF updateRect( QPointF start, QPointF position, bool constrainSquare, bool fromCenter ); diff --git a/src/gui/layout/qgslayoutviewrubberband.cpp b/src/gui/layout/qgslayoutviewrubberband.cpp index c2abd6ba416..73500903416 100644 --- a/src/gui/layout/qgslayoutviewrubberband.cpp +++ b/src/gui/layout/qgslayoutviewrubberband.cpp @@ -99,6 +99,26 @@ QRectF QgsLayoutViewRubberBand::updateRect( QPointF start, QPointF position, boo return QRectF( x, y, width, height ); } +QPen QgsLayoutViewRubberBand::pen() const +{ + return mPen; +} + +void QgsLayoutViewRubberBand::setPen( const QPen &pen ) +{ + mPen = pen; +} + +QBrush QgsLayoutViewRubberBand::brush() const +{ + return mBrush; +} + +void QgsLayoutViewRubberBand::setBrush( const QBrush &brush ) +{ + mBrush = brush; +} + QgsLayoutViewRectangularRubberBand::QgsLayoutViewRectangularRubberBand( QgsLayoutView *view ) : QgsLayoutViewRubberBand( view ) @@ -123,8 +143,8 @@ void QgsLayoutViewRectangularRubberBand::start( QPointF position, Qt::KeyboardMo { QTransform t; mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 ); - mRubberBandItem->setBrush( Qt::NoBrush ); - mRubberBandItem->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) ); + mRubberBandItem->setBrush( brush() ); + mRubberBandItem->setPen( pen() ); mRubberBandStartPos = position; t.translate( position.x(), position.y() ); mRubberBandItem->setTransform( t ); @@ -188,8 +208,8 @@ void QgsLayoutViewEllipticalRubberBand::start( QPointF position, Qt::KeyboardMod { QTransform t; mRubberBandItem = new QGraphicsEllipseItem( 0, 0, 0, 0 ); - mRubberBandItem->setBrush( Qt::NoBrush ); - mRubberBandItem->setPen( QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ) ); + mRubberBandItem->setBrush( brush() ); + mRubberBandItem->setPen( pen() ); mRubberBandStartPos = position; t.translate( position.x(), position.y() ); mRubberBandItem->setTransform( t ); diff --git a/src/gui/layout/qgslayoutviewrubberband.h b/src/gui/layout/qgslayoutviewrubberband.h index c0fe5ad7f8b..703c3ab6bd5 100644 --- a/src/gui/layout/qgslayoutviewrubberband.h +++ b/src/gui/layout/qgslayoutviewrubberband.h @@ -20,6 +20,8 @@ #include "qgis_gui.h" #include "qgis_sip.h" +#include +#include class QgsLayoutView; class QGraphicsRectItem; @@ -89,6 +91,34 @@ class GUI_EXPORT QgsLayoutViewRubberBand */ QgsLayout *layout() const; + /** + * Returns the brush used for drawing the rubber band. + * \see setBrush() + * \see pen() + */ + QBrush brush() const; + + /** + * Sets the \a brush used for drawing the rubber band. + * \see brush() + * \see setPen() + */ + void setBrush( const QBrush &brush ); + + /** + * Returns the pen used for drawing the rubber band. + * \see setPen() + * \see brush() + */ + QPen pen() const; + + /** + * Sets the \a pen used for drawing the rubber band. + * \see pen() + * \see setBrush() + */ + void setPen( const QPen &pen ); + protected: /** @@ -103,6 +133,9 @@ class GUI_EXPORT QgsLayoutViewRubberBand QgsLayoutView *mView = nullptr; + QBrush mBrush = Qt::NoBrush; + QPen mPen = QPen( QBrush( QColor( 227, 22, 22, 200 ) ), 0 ); + }; diff --git a/tests/src/gui/testqgslayoutview.cpp b/tests/src/gui/testqgslayoutview.cpp index 18ecec2e5c4..14be1cac475 100644 --- a/tests/src/gui/testqgslayoutview.cpp +++ b/tests/src/gui/testqgslayoutview.cpp @@ -36,6 +36,7 @@ class TestQgsLayoutView: public QObject void tool(); void events(); void registryUtils(); + void rubberBand(); private: @@ -271,5 +272,14 @@ void TestQgsLayoutView::registryUtils() } +void TestQgsLayoutView::rubberBand() +{ + QgsLayoutViewRectangularRubberBand band; + band.setBrush( QBrush( QColor( 255, 0, 0 ) ) ); + QCOMPARE( band.brush().color(), QColor( 255, 0, 0 ) ); + band.setPen( QPen( QColor( 0, 255, 0 ) ) ); + QCOMPARE( band.pen().color(), QColor( 0, 255, 0 ) ); +} + QGSTEST_MAIN( TestQgsLayoutView ) #include "testqgslayoutview.moc"