Skip to content
Permalink
Browse files

Add a temporary layout item type for testing

  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent 86bc8af commit db62a745ebf0cf70051b7b4c599dcacec09f46b0
@@ -8,6 +8,7 @@




class QgsLayoutItemAbstractMetadata
{
%Docstring
@@ -172,6 +173,7 @@ class QgsLayoutItemRegistry : QObject




/************************************************************************
* This file has been generated automatically from *
* *
@@ -55,10 +55,12 @@ class QgsLayoutViewRubberBand
ending ``position`` (in layout coordinate space).
%End

virtual void finish( QPointF position, Qt::KeyboardModifiers modifiers ) = 0;
virtual QRectF finish( QPointF position, Qt::KeyboardModifiers modifiers ) = 0;
%Docstring
Called when a rubber band use has finished and the rubber
band is no longer required.
Returns the final bounding box of the rubber band.
:rtype: QRectF
%End

QgsLayoutView *view() const;
@@ -114,7 +116,7 @@ class QgsLayoutViewRectangularRubberBand : QgsLayoutViewRubberBand

virtual void update( QPointF position, Qt::KeyboardModifiers modifiers );

virtual void finish( QPointF, Qt::KeyboardModifiers );
virtual QRectF finish( QPointF, Qt::KeyboardModifiers );


};
@@ -144,7 +146,7 @@ class QgsLayoutViewEllipticalRubberBand : QgsLayoutViewRubberBand

virtual void update( QPointF position, Qt::KeyboardModifiers modifiers );

virtual void finish( QPointF, Qt::KeyboardModifiers );
virtual QRectF finish( QPointF, Qt::KeyboardModifiers );


};
@@ -19,7 +19,6 @@

#include "qgis_core.h"
#include "qgslayoutobject.h"
#include "qgslayoutitemregistry.h"
#include <QGraphicsRectItem>

class QgsLayout;
@@ -15,11 +15,19 @@
***************************************************************************/

#include "qgslayoutitemregistry.h"
#include <QPainter>

QgsLayoutItemRegistry::QgsLayoutItemRegistry( QObject *parent )
: QObject( parent )
{

// add temporary item to register
auto createTemporaryItem = []( QgsLayout * layout, const QVariantMap & )->QgsLayoutItem*
{
return new TestLayoutItem( layout );
};

addLayoutItemType( new QgsLayoutItemMetadata( 101, QStringLiteral( "temp type" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), createTemporaryItem ) );
}

QgsLayoutItemRegistry::~QgsLayoutItemRegistry()
@@ -88,3 +96,26 @@ QMap<int, QString> QgsLayoutItemRegistry::itemTypes() const
}
return types;
}

///@cond TEMPORARY
TestLayoutItem::TestLayoutItem( QgsLayout *layout )
: QgsLayoutItem( layout )
{
int h = static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
int s = ( qrand() % ( 200 - 100 + 1 ) ) + 100;
int v = ( qrand() % ( 130 - 255 + 1 ) ) + 130;
mColor = QColor::fromHsv( h, s, v );
}

void TestLayoutItem::draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
{
Q_UNUSED( itemStyle );
Q_UNUSED( pWidget );
painter->save();
painter->setRenderHint( QPainter::Antialiasing, false );
painter->setPen( Qt::NoPen );
painter->setBrush( mColor );
painter->drawRect( rect() );
painter->restore();
}
///@endcond
@@ -24,6 +24,8 @@
#include <QIcon>
#include <functional>

#include "qgslayoutitem.h" // temporary

class QgsLayout;
class QgsLayoutView;
class QgsLayoutItem;
@@ -304,6 +306,27 @@ class CORE_EXPORT QgsLayoutItemRegistry : public QObject

};

#ifndef SIP_RUN
///@cond TEMPORARY
//simple item for testing
class TestLayoutItem : public QgsLayoutItem
{
public:

TestLayoutItem( QgsLayout *layout );
~TestLayoutItem() {}

//implement pure virtual methods
int type() const { return QgsLayoutItemRegistry::LayoutItem + 102; }
void draw( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget );

private:
QColor mColor;
};

///@endcond
#endif

#endif //QGSLAYOUTITEMREGISTRY_H


@@ -150,14 +150,18 @@ void QgsLayoutViewRectangularRubberBand::update( QPointF position, Qt::KeyboardM
mRubberBandItem->setTransform( t );
}

void QgsLayoutViewRectangularRubberBand::finish( QPointF, Qt::KeyboardModifiers )
QRectF QgsLayoutViewRectangularRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
{
bool constrainSquare = modifiers & Qt::ShiftModifier;
bool fromCenter = modifiers & Qt::AltModifier;

if ( mRubberBandItem )
{
layout()->removeItem( mRubberBandItem );
delete mRubberBandItem;
mRubberBandItem = nullptr;
}
return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
}

QgsLayoutViewEllipticalRubberBand::QgsLayoutViewEllipticalRubberBand( QgsLayoutView *view )
@@ -211,12 +215,16 @@ void QgsLayoutViewEllipticalRubberBand::update( QPointF position, Qt::KeyboardMo
mRubberBandItem->setTransform( t );
}

void QgsLayoutViewEllipticalRubberBand::finish( QPointF, Qt::KeyboardModifiers )
QRectF QgsLayoutViewEllipticalRubberBand::finish( QPointF position, Qt::KeyboardModifiers modifiers )
{
bool constrainSquare = modifiers & Qt::ShiftModifier;
bool fromCenter = modifiers & Qt::AltModifier;

if ( mRubberBandItem )
{
layout()->removeItem( mRubberBandItem );
delete mRubberBandItem;
mRubberBandItem = nullptr;
}
return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
}
@@ -73,8 +73,9 @@ class GUI_EXPORT QgsLayoutViewRubberBand
/**
* Called when a rubber band use has finished and the rubber
* band is no longer required.
* Returns the final bounding box of the rubber band.
*/
virtual void finish( QPointF position, Qt::KeyboardModifiers modifiers ) = 0;
virtual QRectF finish( QPointF position, Qt::KeyboardModifiers modifiers ) = 0;

/**
* Returns the view associated with the rubber band.
@@ -124,7 +125,7 @@ class GUI_EXPORT QgsLayoutViewRectangularRubberBand : public QgsLayoutViewRubber

void start( QPointF position, Qt::KeyboardModifiers modifiers ) override;
void update( QPointF position, Qt::KeyboardModifiers modifiers ) override;
void finish( QPointF, Qt::KeyboardModifiers ) override;
QRectF finish( QPointF, Qt::KeyboardModifiers ) override;

private:

@@ -155,7 +156,7 @@ class GUI_EXPORT QgsLayoutViewEllipticalRubberBand : public QgsLayoutViewRubberB

void start( QPointF position, Qt::KeyboardModifiers modifiers ) override;
void update( QPointF position, Qt::KeyboardModifiers modifiers ) override;
void finish( QPointF, Qt::KeyboardModifiers ) override;
QRectF finish( QPointF, Qt::KeyboardModifiers ) override;

private:

@@ -68,9 +68,11 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
return;
}

QRectF rect = QRectF( view()->mapToScene( mMousePressStartPos ),
event->layoutPoint() );
if ( mRubberBand )
{
mRubberBand->finish( event->layoutPoint(), event->modifiers() );
rect = mRubberBand->finish( event->layoutPoint(), event->modifiers() );
}

// click? or click-and-drag?
@@ -84,8 +86,9 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
}
Q_UNUSED( clickOnly );


QgsLogger::debug( QStringLiteral( "creating new %1 item " ).arg( QgsApplication::layoutItemRegistry()->itemMetadata( mItemType )->visibleName() ) );
QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );
item->setRect( rect );
layout()->addItem( item );
}

int QgsLayoutViewToolAddItem::itemType() const
@@ -19,6 +19,8 @@
#include "qgseditorwidgetregistry.h"
#include "qgslayertreeembeddedwidgetregistry.h"
#include "qgsmaplayeractionregistry.h"
#include "qgslayoutitemregistry.h"
#include "qgslayoutviewrubberband.h"
#ifdef Q_OS_MACX
#include "qgsmacnative.h"
#else
@@ -78,4 +80,13 @@ QgsGui::QgsGui()
mShortcutsManager = new QgsShortcutsManager();
mLayerTreeEmbeddedWidgetRegistry = new QgsLayerTreeEmbeddedWidgetRegistry();
mMapLayerActionRegistry = new QgsMapLayerActionRegistry();


QgsLayoutItemAbstractMetadata *abstractMetadata = QgsApplication::layoutItemRegistry()->itemMetadata( 101 );
QgsLayoutItemMetadata *metadata = dynamic_cast<QgsLayoutItemMetadata *>( abstractMetadata );
metadata->setRubberBandCreationFunction( []( QgsLayoutView * view )->QgsLayoutViewRubberBand *
{
return new QgsLayoutViewRectangularRubberBand( view );
} );

}

0 comments on commit db62a74

Please sign in to comment.
You can’t perform that action at this time.