30 #include <QStyleOptionGraphicsItem>
35 setFlag( QGraphicsItem::ItemIsSelectable,
false );
36 setFlag( QGraphicsItem::ItemIsMovable,
false );
41 mBoundingRect = QRectF();
42 prepareGeometryChange();
46 const QFontMetrics fm( font );
47 mMaximumShadowWidth = fm.boundingRect( QStringLiteral(
"X" ) ).width();
49 mGrid.reset(
new QgsLayoutItemPageGrid( pos().x(), pos().y(), rect().width(), rect().height(),
mLayout ) );
50 mGrid->setParentItem(
this );
52 createDefaultPageStyleSymbol();
69 return QObject::tr(
"Page" );
110 pageLayout.setMode( QPageLayout::FullPageMode );
115 pageLayout.setOrientation( QPageLayout::Landscape );
116 pageLayout.setPageSize( QPageSize( QSizeF( size.height(), size.width() ), QPageSize::Millimeter ) );
120 pageLayout.setOrientation( QPageLayout::Portrait );
121 pageLayout.setPageSize( QPageSize( size, QPageSize::Millimeter ) );
123 pageLayout.setUnits( QPageLayout::Millimeter );
142 mPageStyleSymbol.reset( symbol );
151 const QString trimmedString =
string.trimmed();
152 if ( trimmedString.compare( QLatin1String(
"portrait" ), Qt::CaseInsensitive ) == 0 )
158 else if ( trimmedString.compare( QLatin1String(
"landscape" ), Qt::CaseInsensitive ) == 0 )
169 if ( mBoundingRect.isNull() )
171 const double shadowWidth =
mLayout->pageCollection()->pageShadowWidth();
172 mBoundingRect = rect();
173 mBoundingRect.adjust( 0, 0, shadowWidth, shadowWidth );
175 return mBoundingRect;
182 mGrid->setRect( 0, 0, rect().width(), rect().height() );
187 void QgsLayoutItemPage::createDefaultPageStyleSymbol()
189 QVariantMap properties;
190 properties.insert( QStringLiteral(
"color" ), QStringLiteral(
"white" ) );
191 properties.insert( QStringLiteral(
"style" ), QStringLiteral(
"solid" ) );
192 properties.insert( QStringLiteral(
"style_border" ), QStringLiteral(
"no" ) );
193 properties.insert( QStringLiteral(
"joinstyle" ), QStringLiteral(
"miter" ) );
200 class QgsLayoutItemPageUndoCommand:
public QgsLayoutItemUndoCommand
205 : QgsLayoutItemUndoCommand( page, text, id, parent )
208 void restoreState( QDomDocument &stateDoc )
override
210 QgsLayoutItemUndoCommand::restoreState( stateDoc );
211 layout()->pageCollection()->reflow();
227 return new QgsLayoutItemPageUndoCommand(
this, text,
id, parent );
264 if (
mLayout->renderContext().isPreviewRender() )
268 painter->setRenderHint( QPainter::Antialiasing,
false );
270 const QRectF pageRect = QRectF( 0, 0, scale * rect().width(), scale * rect().height() );
273 painter->setBrush( QBrush( QColor( 150, 150, 150 ) ) );
274 painter->setPen( Qt::NoPen );
275 painter->drawRect( pageRect.translated( std::min( scale *
mLayout->pageCollection()->pageShadowWidth(), mMaximumShadowWidth ),
276 std::min( scale *
mLayout->pageCollection()->pageShadowWidth(), mMaximumShadowWidth ) ) );
279 painter->setBrush( QColor( 215, 215, 215 ) );
280 QPen pagePen = QPen( QColor( 100, 100, 100 ), 0 );
281 pagePen.setJoinStyle( Qt::MiterJoin );
282 pagePen.setCosmetic(
true );
283 painter->setPen( pagePen );
284 painter->drawRect( pageRect );
287 if ( mPageStyleSymbol )
289 std::unique_ptr< QgsFillSymbol > symbol( mPageStyleSymbol->clone() );
300 maxBleedPixels = std::floor( maxBleedPixels - 2 );
304 const QPolygonF pagePolygon = QPolygonF( QRectF( maxBleedPixels, maxBleedPixels,
305 std::ceil( rect().width() * scale ) - 2 * maxBleedPixels, std::ceil( rect().height() * scale ) - 2 * maxBleedPixels ) );
306 const QVector<QPolygonF> rings;
308 symbol->renderPolygon( pagePolygon, &rings,
nullptr, context.
renderContext() );
322 element.appendChild( styleElem );
328 const QDomElement symbolElem = element.firstChildElement( QStringLiteral(
"symbol" ) );
329 if ( !symbolElem.isNull() )
331 mPageStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
335 createDefaultPageStyleSymbol();
346 QgsLayoutItemPageGrid::QgsLayoutItemPageGrid(
double x,
double y,
double width,
double height,
QgsLayout *layout )
347 : QGraphicsRectItem( 0, 0, width, height )
351 setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption );
352 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
353 setFlag( QGraphicsItem::ItemIsSelectable,
false );
354 setFlag( QGraphicsItem::ItemIsMovable,
false );
359 void QgsLayoutItemPageGrid::paint( QPainter *painter,
const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
367 if ( !mLayout->renderContext().isPreviewRender() )
376 const QPointF gridOffset = mLayout->convertToLayoutUnits( grid.
offset() );
377 const double gridResolution = mLayout->convertToLayoutUnits( grid.
resolution() );
378 const int gridMultiplyX =
static_cast< int >( gridOffset.x() / gridResolution );
379 const int gridMultiplyY =
static_cast< int >( gridOffset.y() / gridResolution );
380 double currentXCoord = gridOffset.x() - gridMultiplyX * gridResolution;
381 double currentYCoord;
382 const double minYCoord = gridOffset.y() - gridMultiplyY * gridResolution;
386 painter->setRenderHint( QPainter::Antialiasing,
false );
388 switch ( grid.
style() )
392 painter->setPen( grid.
pen() );
395 for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
397 painter->drawLine( QPointF( currentXCoord, 0 ), QPointF( currentXCoord, rect().height() ) );
401 currentYCoord = minYCoord;
402 for ( ; currentYCoord <= rect().height(); currentYCoord += gridResolution )
404 painter->drawLine( QPointF( 0, currentYCoord ), QPointF( rect().width(), currentYCoord ) );
412 const QPen gridPen = grid.
pen();
413 painter->setPen( gridPen );
414 painter->setBrush( QBrush( gridPen.color() ) );
415 double halfCrossLength = 1;
424 halfCrossLength = gridResolution / 6;
427 for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
429 currentYCoord = minYCoord;
430 for ( ; currentYCoord <= rect().height(); currentYCoord += gridResolution )
432 painter->drawLine( QPointF( currentXCoord - halfCrossLength, currentYCoord ), QPointF( currentXCoord + halfCrossLength, currentYCoord ) );
433 painter->drawLine( QPointF( currentXCoord, currentYCoord - halfCrossLength ), QPointF( currentXCoord, currentYCoord + halfCrossLength ) );