Skip to content

Commit

Permalink
Allow setting pen/brush for QgsLayoutViewRubberBand
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent a346736 commit 5cac2f7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
30 changes: 30 additions & 0 deletions python/gui/layout/qgslayoutviewrubberband.sip
Expand Up @@ -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 );
Expand Down
28 changes: 24 additions & 4 deletions src/gui/layout/qgslayoutviewrubberband.cpp
Expand Up @@ -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 )
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
33 changes: 33 additions & 0 deletions src/gui/layout/qgslayoutviewrubberband.h
Expand Up @@ -20,6 +20,8 @@

#include "qgis_gui.h"
#include "qgis_sip.h"
#include <QBrush>
#include <QPen>

class QgsLayoutView;
class QGraphicsRectItem;
Expand Down Expand Up @@ -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:

/**
Expand All @@ -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 );

};


Expand Down
10 changes: 10 additions & 0 deletions tests/src/gui/testqgslayoutview.cpp
Expand Up @@ -36,6 +36,7 @@ class TestQgsLayoutView: public QObject
void tool();
void events();
void registryUtils();
void rubberBand();

private:

Expand Down Expand Up @@ -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"

0 comments on commit 5cac2f7

Please sign in to comment.