26 #include <QStyleOptionGraphicsItem> 31 setFlag( QGraphicsItem::ItemIsSelectable,
false );
32 setFlag( QGraphicsItem::ItemIsMovable,
false );
37 mBoundingRect = QRectF();
38 prepareGeometryChange();
42 QFontMetrics fm( font );
43 mMaximumShadowWidth = fm.width( QStringLiteral(
"X" ) );
45 mGrid.reset(
new QgsLayoutItemPageGrid( pos().x(), pos().y(), rect().width(), rect().height(),
mLayout ) );
46 mGrid->setParentItem(
this );
69 switch ( orientation )
111 QString trimmedString =
string.trimmed();
112 if ( trimmedString.compare( QLatin1String(
"portrait" ), Qt::CaseInsensitive ) == 0 )
118 else if ( trimmedString.compare( QLatin1String(
"landscape" ), Qt::CaseInsensitive ) == 0 )
129 if ( mBoundingRect.isNull() )
131 double shadowWidth =
mLayout->pageCollection()->pageShadowWidth();
132 mBoundingRect = rect();
133 mBoundingRect.adjust( 0, 0, shadowWidth, shadowWidth );
135 return mBoundingRect;
142 mGrid->setRect( 0, 0, rect().width(), rect().height() );
148 class QgsLayoutItemPageUndoCommand:
public QgsLayoutItemUndoCommand
153 : QgsLayoutItemUndoCommand( page, text,
id, parent )
156 void restoreState( QDomDocument &stateDoc )
override 158 QgsLayoutItemUndoCommand::restoreState( stateDoc );
175 return new QgsLayoutItemPageUndoCommand(
this, text,
id, parent );
199 if (
mLayout->renderContext().isPreviewRender() )
203 painter->setRenderHint( QPainter::Antialiasing,
false );
205 QRectF pageRect = QRectF( 0, 0, scale * rect().width(), scale * rect().height() );
208 painter->setBrush( QBrush( QColor( 150, 150, 150 ) ) );
209 painter->setPen( Qt::NoPen );
210 painter->drawRect( pageRect.translated( std::min( scale *
mLayout->pageCollection()->pageShadowWidth(), mMaximumShadowWidth ),
211 std::min( scale *
mLayout->pageCollection()->pageShadowWidth(), mMaximumShadowWidth ) ) );
214 painter->setBrush( QColor( 215, 215, 215 ) );
215 QPen pagePen = QPen( QColor( 100, 100, 100 ), 0 );
216 pagePen.setJoinStyle( Qt::MiterJoin );
217 pagePen.setCosmetic(
true );
218 painter->setPen( pagePen );
219 painter->drawRect( pageRect );
222 std::unique_ptr< QgsFillSymbol > symbol(
mLayout->pageCollection()->pageStyleSymbol()->clone() );
233 maxBleedPixels = std::floor( maxBleedPixels - 2 );
237 QPolygonF pagePolygon = QPolygonF( QRectF( maxBleedPixels, maxBleedPixels,
238 std::ceil( rect().width() * scale ) - 2 * maxBleedPixels, std::ceil( rect().height() * scale ) - 2 * maxBleedPixels ) );
239 QList<QPolygonF> rings;
241 symbol->renderPolygon( pagePolygon, &rings,
nullptr, context.
renderContext() );
258 QgsLayoutItemPageGrid::QgsLayoutItemPageGrid(
double x,
double y,
double width,
double height,
QgsLayout *
layout )
259 : QGraphicsRectItem( 0, 0, width, height )
263 setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption );
264 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
265 setFlag( QGraphicsItem::ItemIsSelectable,
false );
266 setFlag( QGraphicsItem::ItemIsMovable,
false );
271 void QgsLayoutItemPageGrid::paint( QPainter *painter,
const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
279 if ( !
mLayout->renderContext().isPreviewRender() )
288 QPointF gridOffset =
mLayout->convertToLayoutUnits( grid.
offset() );
290 int gridMultiplyX =
static_cast< int >( gridOffset.x() / gridResolution );
291 int gridMultiplyY =
static_cast< int >( gridOffset.y() / gridResolution );
292 double currentXCoord = gridOffset.x() - gridMultiplyX * gridResolution;
293 double currentYCoord;
294 double minYCoord = gridOffset.y() - gridMultiplyY * gridResolution;
298 painter->setRenderHint( QPainter::Antialiasing,
false );
300 switch ( grid.
style() )
304 painter->setPen( grid.
pen() );
307 for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
309 painter->drawLine( QPointF( currentXCoord, 0 ), QPointF( currentXCoord, rect().height() ) );
313 currentYCoord = minYCoord;
314 for ( ; currentYCoord <= rect().height(); currentYCoord += gridResolution )
316 painter->drawLine( QPointF( 0, currentYCoord ), QPointF( rect().width(), currentYCoord ) );
324 QPen gridPen = grid.
pen();
325 painter->setPen( gridPen );
326 painter->setBrush( QBrush( gridPen.color() ) );
327 double halfCrossLength = 1;
336 halfCrossLength = gridResolution / 6;
339 for ( ; currentXCoord <= rect().width(); currentXCoord += gridResolution )
341 currentYCoord = minYCoord;
342 for ( ; currentYCoord <= rect().height(); currentYCoord += gridResolution )
344 painter->drawLine( QPointF( currentXCoord - halfCrossLength, currentYCoord ), QPointF( currentXCoord + halfCrossLength, currentYCoord ) );
345 painter->drawLine( QPointF( currentXCoord, currentYCoord - halfCrossLength ), QPointF( currentXCoord, currentYCoord + halfCrossLength ) );
int type() const override
static double scaleFactorFromItemStyle(const QStyleOptionGraphicsItem *style)
Extracts the scale factor from an item style.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsLayoutPoint offset() const
Returns the offset of the page/snap grid.
Style style() const
Returns the style used for drawing the page/snap grids.
Base class for graphical items within a QgsLayout.
void drawFrame(QgsRenderContext &context) override
Draws the frame around the item.
Base class for commands to undo/redo layout and layout object changes.
QgsLayoutSize pageSize() const
Returns the size of the page.
QRectF boundingRect() const override
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
double length() const
Returns the length of the measurement.
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
Z-value for page (paper) items.
double height() const
Returns the height of the size.
QPen pen() const
Returns the pen used for drawing page/snap grids.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
void sizePositionChanged()
Emitted when the item's size or position changes.
double width() const
Returns the width of the size.
static QgsLayoutItemPage::Orientation decodePageOrientation(const QString &string, bool *ok=nullptr)
Decodes a string representing a page orientation.
void attemptResize(const QgsLayoutSize &size, bool includesFrame=false) override
Attempts to resize the item to a specified target size.
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout...
QgsAbstractLayoutUndoCommand * createCommand(const QString &text, int id, QUndoCommand *parent=nullptr) override
Creates a new layout undo command with the specified text and parent.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
QPointer< QgsLayout > mLayout
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
bool gridVisible() const
Returns true if the page grid should be drawn.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
void setHeight(const double height)
Sets the height for the size.
Contains settings relating to the appearance, spacing and offset for layout grids.
virtual void redraw()
Triggers a redraw (update) of the item.
Contains settings and helpers relating to a render of a QgsLayoutItem.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
int page() const
Returns the page the item is currently on, with the first page returning 0.
QgsLayoutItemPage(QgsLayout *layout)
Constructor for QgsLayoutItemPage, with the specified parent layout.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
static QgsPageSizeRegistry * pageSizeRegistry()
Returns the application's page size registry, used for managing layout page sizes.
A named page size for layouts.
Orientation orientation() const
Returns the page orientiation.
Orientation
Page orientiation.
Stores information relating to the current rendering settings for a layout.
static QgsLayoutItemPage * create(QgsLayout *layout)
Returns a new page item for the specified layout.
void drawBackground(QgsRenderContext &context) override
Draws the background for the item.
void addPage(QgsLayoutItemPage *page)
Adds a page to the collection.
void setPageSize(const QgsLayoutSize &size)
Sets the size of the page.
const QgsLayout * layout() const
Returns the layout the object is attached to.
void reflow()
Forces the page collection to reflow the arrangement of pages, e.g.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
QgsLayoutSize size
Page size.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setWidth(const double width)
Sets the width for the size.
Item representing the paper in a layout.