28 #include <QStyleOptionGraphicsItem>
33 setFlag( QGraphicsItem::ItemIsSelectable,
false );
34 setFlag( QGraphicsItem::ItemIsMovable,
false );
39 mBoundingRect = QRectF();
40 prepareGeometryChange();
44 QFontMetrics fm( font );
45 mMaximumShadowWidth = fm.boundingRect( QStringLiteral(
"X" ) ).width();
47 mGrid.reset(
new QgsLayoutItemPageGrid( pos().x(), pos().y(), rect().width(), rect().height(),
mLayout ) );
48 mGrid->setParentItem(
this );
50 createDefaultPageStyleSymbol();
67 return QObject::tr(
"Page" );
119 mPageStyleSymbol.reset( symbol );
128 QString trimmedString =
string.trimmed();
129 if ( trimmedString.compare( QLatin1String(
"portrait" ), Qt::CaseInsensitive ) == 0 )
135 else if ( trimmedString.compare( QLatin1String(
"landscape" ), Qt::CaseInsensitive ) == 0 )
146 if ( mBoundingRect.isNull() )
148 double shadowWidth =
mLayout->pageCollection()->pageShadowWidth();
149 mBoundingRect = rect();
150 mBoundingRect.adjust( 0, 0, shadowWidth, shadowWidth );
152 return mBoundingRect;
159 mGrid->setRect( 0, 0, rect().width(), rect().height() );
164 void QgsLayoutItemPage::createDefaultPageStyleSymbol()
167 properties.insert( QStringLiteral(
"color" ), QStringLiteral(
"white" ) );
168 properties.insert( QStringLiteral(
"style" ), QStringLiteral(
"solid" ) );
169 properties.insert( QStringLiteral(
"style_border" ), QStringLiteral(
"no" ) );
170 properties.insert( QStringLiteral(
"joinstyle" ), QStringLiteral(
"miter" ) );
177 class QgsLayoutItemPageUndoCommand:
public QgsLayoutItemUndoCommand
182 : QgsLayoutItemUndoCommand( page, text, id, parent )
185 void restoreState( QDomDocument &stateDoc )
override
187 QgsLayoutItemUndoCommand::restoreState( stateDoc );
188 layout()->pageCollection()->reflow();
204 return new QgsLayoutItemPageUndoCommand(
this, text,
id, parent );
241 if (
mLayout->renderContext().isPreviewRender() )
245 painter->setRenderHint( QPainter::Antialiasing,
false );
247 QRectF pageRect = QRectF( 0, 0, scale * rect().width(), scale * rect().height() );
250 painter->setBrush( QBrush( QColor( 150, 150, 150 ) ) );
251 painter->setPen( Qt::NoPen );
252 painter->drawRect( pageRect.translated( std::min( scale *
mLayout->pageCollection()->pageShadowWidth(), mMaximumShadowWidth ),
253 std::min( scale *
mLayout->pageCollection()->pageShadowWidth(), mMaximumShadowWidth ) ) );
256 painter->setBrush( QColor( 215, 215, 215 ) );
257 QPen pagePen = QPen( QColor( 100, 100, 100 ), 0 );
258 pagePen.setJoinStyle( Qt::MiterJoin );
259 pagePen.setCosmetic(
true );
260 painter->setPen( pagePen );
261 painter->drawRect( pageRect );
264 if ( mPageStyleSymbol )
266 std::unique_ptr< QgsFillSymbol > symbol( mPageStyleSymbol->clone() );
277 maxBleedPixels = std::floor( maxBleedPixels - 2 );
281 QPolygonF pagePolygon = QPolygonF( QRectF( maxBleedPixels, maxBleedPixels,
282 std::ceil( rect().width() * scale ) - 2 * maxBleedPixels, std::ceil( rect().height() * scale ) - 2 * maxBleedPixels ) );
283 QVector<QPolygonF> rings;
285 symbol->renderPolygon( pagePolygon, &rings,
nullptr, context.
renderContext() );
299 element.appendChild( styleElem );
305 QDomElement symbolElem = element.firstChildElement( QStringLiteral(
"symbol" ) );
306 if ( !symbolElem.isNull() )
308 mPageStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
312 createDefaultPageStyleSymbol();
323 QgsLayoutItemPageGrid::QgsLayoutItemPageGrid(
double x,
double y,
double width,
double height,
QgsLayout *layout )
324 : QGraphicsRectItem( 0, 0, width, height )
328 setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption );
329 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
330 setFlag( QGraphicsItem::ItemIsSelectable,
false );
331 setFlag( QGraphicsItem::ItemIsMovable,
false );
336 void QgsLayoutItemPageGrid::paint( QPainter *painter,
const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
344 if ( !mLayout->renderContext().isPreviewRender() )
353 QPointF gridOffset = mLayout->convertToLayoutUnits( grid.
offset() );
354 double gridResolution = mLayout->convertToLayoutUnits( grid.
resolution() );
355 int gridMultiplyX =
static_cast< int >( gridOffset.x() / gridResolution );
356 int gridMultiplyY =
static_cast< int >( gridOffset.y() / gridResolution );
357 double currentXCoord = gridOffset.x() - gridMultiplyX * gridResolution;
358 double currentYCoord;
359 double minYCoord = gridOffset.y() - gridMultiplyY * gridResolution;
363 painter->setRenderHint( QPainter::Antialiasing,
false );
365 switch ( grid.
style() )
369 painter->setPen( grid.
pen() );
372 for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
374 painter->drawLine( QPointF( currentXCoord, 0 ), QPointF( currentXCoord, rect().height() ) );
378 currentYCoord = minYCoord;
379 for ( ; currentYCoord <= rect().height(); currentYCoord += gridResolution )
381 painter->drawLine( QPointF( 0, currentYCoord ), QPointF( rect().width(), currentYCoord ) );
389 QPen gridPen = grid.
pen();
390 painter->setPen( gridPen );
391 painter->setBrush( QBrush( gridPen.color() ) );
392 double halfCrossLength = 1;
401 halfCrossLength = gridResolution / 6;
404 for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
406 currentYCoord = minYCoord;
407 for ( ; currentYCoord <= rect().height(); currentYCoord += gridResolution )
409 painter->drawLine( QPointF( currentXCoord - halfCrossLength, currentYCoord ), QPointF( currentXCoord + halfCrossLength, currentYCoord ) );
410 painter->drawLine( QPointF( currentXCoord, currentYCoord - halfCrossLength ), QPointF( currentXCoord, currentYCoord + halfCrossLength ) );