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.width( QStringLiteral(
"X" ) );
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" );
80 switch ( orientation )
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 );
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 QList<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() );
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 ) );
The class is used as a container of context for various read/write operations on other objects...
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...
A symbol entity for QgsStyle databases.
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.
QString displayName() const override
Gets item display name.
QRectF boundingRect() const override
static QgsFillSymbol * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
~QgsLayoutItemPage() override
void setPageStyleSymbol(QgsFillSymbol *symbol)
Sets the symbol to use for drawing the page background.
QPen pen() const
Returns the pen used for drawing page/snap grids.
Z-value for page (paper) items.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Item can only be placed on layers with other items of the same type, but multiple items of this type ...
An interface for classes which can visit style entity (e.g.
QMap< QString, QString > QgsStringMap
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
bool gridVisible() const
Returns true if the page grid should be drawn.
void sizePositionChanged()
Emitted when the item's size or position changes.
static QgsLayoutItemPage::Orientation decodePageOrientation(const QString &string, bool *ok=nullptr)
Decodes a string representing a page orientation.
const QgsFillSymbol * pageStyleSymbol() const
Returns the symbol to use for drawing the page background.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
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
bool writePropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
bool readPropertiesFromElement(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
Orientation orientation() const
Returns the page orientation.
int page() const
Returns the page the item is currently on, with the first page returning 0.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
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.
QgsLayoutPoint offset() const
Returns the offset of the page/snap grid.
QgsLayoutItemPage(QgsLayout *layout)
Constructor for QgsLayoutItemPage, with the specified parent layout.
QgsLayoutSize pageSize() const
Returns the size of the page.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
double length() const
Returns the length of the measurement.
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.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
Style style() const
Returns the style used for drawing the page/snap grids.
Orientation
Page orientation.
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.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
void setPageSize(const QgsLayoutSize &size)
Sets the size of the page.
ExportLayerBehavior exportLayerBehavior() const override
Returns the behavior of this item during exporting to layered exports (e.g.
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.
double height() const
Returns the height of the size.
Contains information relating to the style entity currently being visited.
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.
double width() const
Returns the width of the size.