Allow setting pen/brush for QgsLayoutViewRubberBand

This commit is contained in:
Nyall Dawson 2017-07-05 17:55:16 +10:00
parent a346736df2
commit 5cac2f71f0
4 changed files with 97 additions and 4 deletions

View File

@ -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 );

View File

@ -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 );

View File

@ -20,6 +20,8 @@
#include "qgis_gui.h"
#include "qgis_sip.h"
#include <QBrush>
#include <QPen>
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 );
};

View File

@ -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"