Skip to content

Commit

Permalink
Port 'exclude from exports' functionality from composer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 7, 2017
1 parent 4e8878d commit 75898d8
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 8 deletions.
61 changes: 61 additions & 0 deletions python/core/layout/qgslayouteffect.sip
@@ -0,0 +1,61 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/layout/qgslayouteffect.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsLayoutEffect : QGraphicsEffect
{
%Docstring

A QGraphicsEffect subclass used for rendering layout items
onto a scene with custom composition modes.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayouteffect.h"
%End
public:

QgsLayoutEffect();
%Docstring
Constructor for QgsLayoutEffect.
%End

void setCompositionMode( QPainter::CompositionMode mode );
%Docstring
Sets the composition (blending) ``mode`` used for rendering
the item.
.. seealso:: compositionMode()
%End

QPainter::CompositionMode compositionMode() const;
%Docstring
Returns the composition (blending) mode used for rendering
the item.
.. seealso:: setCompositionMode()
:rtype: QPainter.CompositionMode
%End

protected:

virtual void draw( QPainter *painter );

};


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/layout/qgslayouteffect.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
13 changes: 13 additions & 0 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -425,6 +425,19 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
%Docstring
Sets the item's composition blending ``mode``.
.. seealso:: blendMode()
%End

bool excludeFromExports() const;
%Docstring
Returns whether the item should be excluded from layout exports and prints.
.. seealso:: setExcludeFromExports()
:rtype: bool
%End

void setExcludeFromExports( bool exclude );
%Docstring
Sets whether the item should be excluded from composer exports and prints.
.. seealso:: excludeFromExports()
%End

virtual double estimatedFrameBleed() const;
Expand Down
43 changes: 38 additions & 5 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -224,7 +224,7 @@ void QgsLayoutItem::setParentGroup( QgsLayoutItemGroup *group )

void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget * )
{
if ( !painter || !painter->device() )
if ( !painter || !painter->device() || !shouldDrawItem() )
{
return;
}
Expand Down Expand Up @@ -411,6 +411,20 @@ bool QgsLayoutItem::shouldBlockUndoCommands() const
return !mLayout || mLayout != scene() || mBlockUndoCommands;
}

bool QgsLayoutItem::shouldDrawItem() const
{
#if 0 //TODO
if ( ( mLayout && mLayout->plotStyle() == QgsComposition::Preview ) || !mLayout )
{
//preview mode or no composition, so OK to draw item
return true;
}
#endif

//exporting layout, so check if item is excluded from exports
return !mEvaluatedExcludeFromExports;
}

double QgsLayoutItem::itemRotation() const
{
return rotation();
Expand Down Expand Up @@ -518,6 +532,17 @@ void QgsLayoutItem::setBlendMode( const QPainter::CompositionMode mode )
refreshBlendMode();
}

bool QgsLayoutItem::excludeFromExports() const
{
return mExcludeFromExports;
}

void QgsLayoutItem::setExcludeFromExports( bool exclude )
{
mExcludeFromExports = exclude;
refreshDataDefinedProperty( QgsLayoutObject::ExcludeFromExports );
}

double QgsLayoutItem::estimatedFrameBleed() const
{
if ( !hasFrame() )
Expand Down Expand Up @@ -617,6 +642,14 @@ void QgsLayoutItem::refreshDataDefinedProperty( const QgsLayoutObject::DataDefin
{
refreshBlendMode();
}
if ( property == QgsLayoutObject::ExcludeFromExports || property == QgsLayoutObject::AllProperties )
{
bool exclude = mExcludeFromExports;
//data defined exclude from exports set?
mEvaluatedExcludeFromExports = mDataDefinedProperties.valueAsBool( QgsLayoutObject::ExcludeFromExports, createExpressionContext(), exclude );
}

update();
}

void QgsLayoutItem::setItemRotation( const double angle )
Expand Down Expand Up @@ -842,10 +875,10 @@ bool QgsLayoutItem::writePropertiesToElement( QDomElement &element, QDomDocument
#if 0
//transparency
// composerItemElem.setAttribute( "transparency", QString::number( mTransparency ) );

// composerItemElem.setAttribute( "excludeFromExports", mExcludeFromExports );
#endif

element.setAttribute( "excludeFromExports", mExcludeFromExports );

writeObjectPropertiesToElement( element, document, context );
return true;
}
Expand Down Expand Up @@ -968,9 +1001,9 @@ bool QgsLayoutItem::readPropertiesFromElement( const QDomElement &element, const
#if 0 //TODO
//transparency
setTransparency( itemElem.attribute( "transparency", "0" ).toInt() );
mExcludeFromExports = itemElem.attribute( "excludeFromExports", "0" ).toInt();
mEvaluatedExcludeFromExports = mExcludeFromExports;
#endif
mExcludeFromExports = element.attribute( QStringLiteral( "excludeFromExports" ), QStringLiteral( "0" ) ).toInt();
mEvaluatedExcludeFromExports = mExcludeFromExports;

mBlockUndoCommands = false;
return true;
Expand Down
26 changes: 26 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -429,6 +429,18 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
*/
void setBlendMode( const QPainter::CompositionMode mode );

/**
* Returns whether the item should be excluded from layout exports and prints.
* \see setExcludeFromExports()
*/
bool excludeFromExports() const;

/**
* Sets whether the item should be excluded from composer exports and prints.
* \see excludeFromExports()
*/
void setExcludeFromExports( bool exclude );

/**
* Returns the estimated amount the item's frame bleeds outside the item's
* actual rectangle. For instance, if the item has a 2mm frame stroke, then
Expand Down Expand Up @@ -657,6 +669,15 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
QgsLayoutPoint mItemPosition;
double mItemRotation = 0.0;

//! Whether item should be excluded in exports
bool mExcludeFromExports = false;

/**
* Temporary evaluated item exclusion. Data defined properties may mean
* this value differs from mExcludeFromExports.
*/
bool mEvaluatedExcludeFromExports = false;

//! Composition blend mode for item
QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
std::unique_ptr< QgsLayoutEffect > mEffect;
Expand Down Expand Up @@ -699,6 +720,11 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
void setScenePos( const QPointF &destinationPos );
bool shouldBlockUndoCommands() const;

/**
* Returns whether the item should be drawn in the current context
*/
bool shouldDrawItem() const;

friend class TestQgsLayoutItem;
friend class QgsLayoutItemGroup;
};
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutitemgroup.cpp
Expand Up @@ -129,6 +129,7 @@ void QgsLayoutItemGroup::setVisibility( const bool visible )

void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point, bool useReferencePoint )
{
Q_UNUSED( useReferencePoint ); //groups should always have reference point in top left
if ( !mLayout )
return;

Expand Down
4 changes: 1 addition & 3 deletions src/gui/layout/qgslayoutitemwidget.cpp
Expand Up @@ -644,8 +644,8 @@ void QgsLayoutItemPropertiesWidget::setValuesForGuiNonPositionElements()
#if 0//TODO
mOpacityWidget->setOpacity( mItem->itemOpacity() );
mItemRotationSpinBox->setValue( mItem->itemRotation( QgsComposerObject::OriginalValue ) );
mExcludeFromPrintsCheckBox->setChecked( mItem->excludeFromExports( QgsComposerObject::OriginalValue ) );
#endif
mExcludeFromPrintsCheckBox->setChecked( mItem->excludeFromExports() );

block( false );
}
Expand Down Expand Up @@ -889,9 +889,7 @@ void QgsLayoutItemPropertiesWidget::mExcludeFromPrintsCheckBox_toggled( bool che
if ( mItem )
{
mItem->layout()->undoStack()->beginCommand( mItem, checked ? tr( "Exclude from Exports" ) : tr( "Include in Exports" ) );
#if 0 //TODO
mItem->setExcludeFromExports( checked );
#endif
mItem->layout()->undoStack()->endCommand();
}
}
34 changes: 34 additions & 0 deletions tests/src/core/testqgslayoutitem.cpp
Expand Up @@ -152,6 +152,7 @@ class TestQgsLayoutItem: public QObject
void multiItemUndo();
void overlappingUndo();
void blendMode();
void excludeFromExports();

private:

Expand Down Expand Up @@ -1355,6 +1356,7 @@ void TestQgsLayoutItem::writeReadXmlProperties()
original->setBackgroundEnabled( false );
original->setBackgroundColor( QColor( 200, 150, 100 ) );
original->setBlendMode( QPainter::CompositionMode_Darken );
original->setExcludeFromExports( true );

QgsLayoutItem *copy = createCopyViaXml( &l, original );

Expand All @@ -1378,6 +1380,7 @@ void TestQgsLayoutItem::writeReadXmlProperties()
QVERIFY( !copy->hasBackground() );
QCOMPARE( copy->backgroundColor(), QColor( 200, 150, 100 ) );
QCOMPARE( copy->blendMode(), QPainter::CompositionMode_Darken );
QVERIFY( copy->excludeFromExports( ) );

delete copy;
delete original;
Expand Down Expand Up @@ -1556,6 +1559,37 @@ void TestQgsLayoutItem::blendMode()
QCOMPARE( item->mEffect->compositionMode(), QPainter::CompositionMode_Lighten );
}

void TestQgsLayoutItem::excludeFromExports()
{
QgsProject proj;
QgsLayout l( &proj );

QgsLayoutItemRectangularShape *item = new QgsLayoutItemRectangularShape( &l );
l.addLayoutItem( item );

item->setExcludeFromExports( true );
QVERIFY( item->excludeFromExports() );
item->setExcludeFromExports( false );
QVERIFY( !item->excludeFromExports() );

item->dataDefinedProperties().setProperty( QgsLayoutObject::ExcludeFromExports, QgsProperty::fromExpression( "1" ) );
item->refreshDataDefinedProperty();
QVERIFY( !item->excludeFromExports() ); // should not change
QVERIFY( item->mEvaluatedExcludeFromExports );

item->setPos( 100, 100 );
item->setRect( 0, 0, 200, 200 );
l.setSceneRect( 0, 0, 400, 400 );
l.context().setFlag( QgsLayoutContext::FlagDebug, true );
QImage image( l.sceneRect().size().toSize(), QImage::Format_ARGB32 );
image.fill( 0 );
QPainter painter( &image );
l.render( &painter );
painter.end();

QVERIFY( renderCheck( QStringLiteral( "layoutitem_excluded" ), image, 0 ) );
}

QgsLayoutItem *TestQgsLayoutItem::createCopyViaXml( QgsLayout *layout, QgsLayoutItem *original )
{
//save original item to xml
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 75898d8

Please sign in to comment.