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() );
301 element.appendChild( styleElem );
307 QDomElement symbolElem = element.firstChildElement( QStringLiteral(
"symbol" ) );
308 if ( !symbolElem.isNull() )
310 mPageStyleSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
314 createDefaultPageStyleSymbol();
325 QgsLayoutItemPageGrid::QgsLayoutItemPageGrid(
double x,
double y,
double width,
double height,
QgsLayout *layout )
326 : QGraphicsRectItem( 0, 0, width, height )
330 setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption );
331 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
332 setFlag( QGraphicsItem::ItemIsSelectable,
false );
333 setFlag( QGraphicsItem::ItemIsMovable,
false );
338 void QgsLayoutItemPageGrid::paint( QPainter *painter,
const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
346 if ( !mLayout->renderContext().isPreviewRender() )
355 QPointF gridOffset = mLayout->convertToLayoutUnits( grid.
offset() );
356 double gridResolution = mLayout->convertToLayoutUnits( grid.
resolution() );
357 int gridMultiplyX =
static_cast< int >( gridOffset.x() / gridResolution );
358 int gridMultiplyY =
static_cast< int >( gridOffset.y() / gridResolution );
359 double currentXCoord = gridOffset.x() - gridMultiplyX * gridResolution;
360 double currentYCoord;
361 double minYCoord = gridOffset.y() - gridMultiplyY * gridResolution;
365 painter->setRenderHint( QPainter::Antialiasing,
false );
367 switch ( grid.
style() )
371 painter->setPen( grid.
pen() );
374 for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
376 painter->drawLine( QPointF( currentXCoord, 0 ), QPointF( currentXCoord, rect().height() ) );
380 currentYCoord = minYCoord;
381 for ( ; currentYCoord <= rect().height(); currentYCoord += gridResolution )
383 painter->drawLine( QPointF( 0, currentYCoord ), QPointF( rect().width(), currentYCoord ) );
391 QPen gridPen = grid.
pen();
392 painter->setPen( gridPen );
393 painter->setBrush( QBrush( gridPen.color() ) );
394 double halfCrossLength = 1;
403 halfCrossLength = gridResolution / 6;
406 for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
408 currentYCoord = minYCoord;
409 for ( ; currentYCoord <= rect().height(); currentYCoord += gridResolution )
411 painter->drawLine( QPointF( currentXCoord - halfCrossLength, currentYCoord ), QPointF( currentXCoord + halfCrossLength, currentYCoord ) );
412 painter->drawLine( QPointF( currentXCoord, currentYCoord - halfCrossLength ), QPointF( currentXCoord, currentYCoord + halfCrossLength ) );