Skip to content
Permalink
Browse files
[FEATURE] Allow setting composer page background style (fix #7846)
  • Loading branch information
nyalldawson committed Dec 17, 2013
1 parent a152312 commit d0b84e67962a89752230a99dcfd3729f508ea18f
@@ -19,6 +19,9 @@
#include "qgscomposition.h"
#include "qgscomposermap.h"
#include "qgscomposeritem.h"
#include "qgsstylev2.h"
#include "qgssymbolv2selectordialog.h"
#include "qgssymbollayerv2utils.h"
#include <QColorDialog>
#include <QWidget>
#include <QPrinter> //for screen resolution
@@ -45,6 +48,8 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
{
mNumPagesSpinBox->setValue( mComposition->numPages() );

updatePageStyle();

//read printout resolution from composition
mResolutionSpinBox->setValue( mComposition->printResolution() );

@@ -374,6 +379,30 @@ void QgsCompositionWidget::displayCompositionWidthHeight()
}
}

void QgsCompositionWidget::on_mPageStyleButton_clicked()
{
if ( !mComposition )
{
return;
}

QgsSymbolV2SelectorDialog d( mComposition->pageStyleSymbol(), QgsStyleV2::defaultStyle(), 0 );

if ( d.exec() == QDialog::Accepted )
{
updatePageStyle();
}
}

void QgsCompositionWidget::updatePageStyle()
{
if ( mComposition )
{
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mComposition->pageStyleSymbol(), mPageStyleButton->iconSize() );
mPageStyleButton->setIcon( icon );
}
}

void QgsCompositionWidget::setPrintAsRasterCheckBox( bool state )
{
mPrintAsRasterCheckBox->blockSignals( true );
@@ -520,6 +549,7 @@ void QgsCompositionWidget::blockSignals( bool block )
mPaperHeightDoubleSpinBox->blockSignals( block );
mNumPagesSpinBox->blockSignals( block );
mPaperOrientationComboBox->blockSignals( block );
mPageStyleButton->blockSignals( block );
mResolutionSpinBox->blockSignals( block );
mPrintAsRasterCheckBox->blockSignals( block );
mGridResolutionSpinBox->blockSignals( block );
@@ -48,6 +48,7 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mPaperWidthDoubleSpinBox_editingFinished();
void on_mPaperHeightDoubleSpinBox_editingFinished();
void on_mNumPagesSpinBox_valueChanged( int value );
void on_mPageStyleButton_clicked();
void on_mResolutionSpinBox_valueChanged( const int value );
void on_mPrintAsRasterCheckBox_toggled( bool state );
void on_mGenerateWorldFileCheckBox_toggled( bool state );
@@ -84,6 +85,8 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
/**Sets GUI elements to snaping distances of composition*/
void displaySnapingSettings();

void updatePageStyle();

void createPaperEntries();
void insertPaperEntries();

@@ -37,6 +37,8 @@
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsexpression.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"

#include <QDomDocument>
#include <QDomElement>
@@ -54,6 +56,7 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
, mPageWidth( 297 )
, mPageHeight( 210 )
, mSpaceBetweenPages( 10 )
, mPageStyleSymbol( 0 )
, mPrintAsRaster( false )
, mGenerateWorldFile( false )
, mWorldFileMap( 0 )
@@ -75,6 +78,7 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
, mPreventCursorChange( false )
{
setBackgroundBrush( Qt::gray );
createDefaultPageStyleSymbol();
addPaperItem();

//add mouse selection handles to composition, and initially hide
@@ -97,6 +101,7 @@ QgsComposition::QgsComposition()
mPageWidth( 297 ),
mPageHeight( 210 ),
mSpaceBetweenPages( 10 ),
mPageStyleSymbol( 0 ),
mPrintAsRaster( false ),
mGenerateWorldFile( false ),
mWorldFileMap( 0 ),
@@ -133,6 +138,7 @@ QgsComposition::~QgsComposition()
clear();
delete mActiveItemCommand;
delete mActiveMultiFrameCommand;
delete mPageStyleSymbol;
}

void QgsComposition::loadDefaults()
@@ -200,6 +206,22 @@ int QgsComposition::numPages() const
return mPages.size();
}

void QgsComposition::setPageStyleSymbol( QgsFillSymbolV2* symbol )
{
delete mPageStyleSymbol;
mPageStyleSymbol = symbol;
}

void QgsComposition::createDefaultPageStyleSymbol()
{
delete mPageStyleSymbol;
QgsStringMap properties;
properties.insert( "color", "white" );
properties.insert( "style", "solid" );
properties.insert( "style_border", "no" );
mPageStyleSymbol = QgsFillSymbolV2::createSimple( properties );
}

QPointF QgsComposition::positionOnPage( const QPointF & position ) const
{
double y;
@@ -469,6 +491,9 @@ bool QgsComposition::writeXML( QDomElement& composerElem, QDomDocument& doc )
compositionElem.setAttribute( "paperHeight", QString::number( mPageHeight ) );
compositionElem.setAttribute( "numPages", mPages.size() );

QDomElement pageStyleElem = QgsSymbolLayerV2Utils::saveSymbol( QString(), mPageStyleSymbol, doc );
compositionElem.appendChild( pageStyleElem );

//snapping
if ( mSnapToGrid )
{
@@ -559,6 +584,13 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
emit paperSizeChanged();
int numPages = compositionElem.attribute( "numPages", "1" ).toInt();

QDomElement pageStyleSymbolElem = compositionElem.firstChildElement( "symbol" );
if ( !pageStyleSymbolElem.isNull() )
{
delete mPageStyleSymbol;
mPageStyleSymbol = dynamic_cast<QgsFillSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol( pageStyleSymbolElem ) );
}

if ( widthConversionOk && heightConversionOk )
{
removePaperItems();
@@ -55,6 +55,7 @@ class QgsComposerMultiFrame;
class QgsComposerMultiFrameCommand;
class QgsVectorLayer;
class QgsComposer;
class QgsFillSymbolV2;

/** \ingroup MapComposer
* Graphics scene for map printing. The class manages the paper item which always
@@ -108,6 +109,11 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
/**Note: added in version 1.9*/
int numPages() const;

/**Note: added in version 2.1*/
void setPageStyleSymbol( QgsFillSymbolV2* symbol );
/**Note: added in version 2.1*/
QgsFillSymbolV2* pageStyleSymbol() { return mPageStyleSymbol; }

/**Returns the position within a page of a point in the composition
@note Added in QGIS 2.1
*/
@@ -432,6 +438,10 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
QList< QgsPaperItem* > mPages;
double mSpaceBetweenPages; //space in preview between pages

/**Drawing style for page*/
QgsFillSymbolV2* mPageStyleSymbol;
void createDefaultPageStyleSymbol();

/**Maintains z-Order of items. Starts with item at position 1 (position 0 is always paper item)*/
QLinkedList<QgsComposerItem*> mItemZList;

@@ -17,6 +17,7 @@

#include "qgspaperitem.h"
#include "qgscomposition.h"
#include "qgsstylev2.h"
#include "qgslogger.h"
#include <QGraphicsRectItem>
#include <QPainter>
@@ -152,7 +153,27 @@ void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* ite
return;
}

drawBackground( painter );
QgsRenderContext context;
context.setPainter( painter );
context.setScaleFactor( 1.0 );
if ( mComposition->plotStyle() == QgsComposition::Preview )
{
context.setRasterScaleFactor( horizontalViewScaleFactor() );
}
else
{
context.setRasterScaleFactor( mComposition->printResolution() / 25.4 );
}

painter->save();
painter->setRenderHint( QPainter::Antialiasing );
QPolygonF pagePolygon = QPolygonF( QRectF( 0, 0, rect().width(), rect().height() ) );
mComposition->pageStyleSymbol()->startRender( context );
QList<QPolygonF> rings; //empty list
mComposition->pageStyleSymbol()->renderPolygon( pagePolygon, &rings, 0, context );
mComposition->pageStyleSymbol()->stopRender( context );
painter->restore();

}

bool QgsPaperItem::writeXML( QDomElement& elem, QDomDocument & doc ) const
@@ -223,7 +223,7 @@
</property>
</widget>
</item>
<item row="6" column="1">
<item row="7" column="1">
<widget class="QSpinBox" name="mResolutionSpinBox">
<property name="suffix">
<string> dpi</string>
@@ -236,14 +236,14 @@
</property>
</widget>
</item>
<item row="6" column="0">
<item row="7" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Export resolution</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<item row="9" column="0" colspan="2">
<widget class="QCheckBox" name="mPrintAsRasterCheckBox">
<property name="text">
<string>Print as raster</string>
@@ -256,14 +256,14 @@
</property>
</widget>
</item>
<item row="9" column="0">
<item row="10" column="0">
<widget class="QCheckBox" name="mGenerateWorldFileCheckBox">
<property name="text">
<string>World file on</string>
</property>
</widget>
</item>
<item row="9" column="1">
<item row="10" column="1">
<widget class="QComboBox" name="mWorldFileMapComboBox">
<property name="enabled">
<bool>false</bool>
@@ -273,6 +273,20 @@
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QPushButton" name="mPageStyleButton">
<property name="text">
<string>Change...</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Page background</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>

0 comments on commit d0b84e6

Please sign in to comment.