30 #include <QStyleOptionGraphicsItem>
35 setFlag( QGraphicsItem::ItemIsSelectable,
false );
36 setFlag( QGraphicsItem::ItemIsMovable,
false );
41 mBoundingRect = QRectF();
42 prepareGeometryChange();
46 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 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 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 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 QPolygonF pagePolygon = QPolygonF( QRectF( maxBleedPixels, maxBleedPixels,
305 std::ceil( rect().width() * scale ) - 2 * maxBleedPixels, std::ceil( rect().height() * scale ) - 2 * maxBleedPixels ) );
306 QVector<QPolygonF> rings;
308 symbol->renderPolygon( pagePolygon, &rings,
nullptr, context.
renderContext() );
322 element.appendChild( styleElem );
328 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 QPointF gridOffset = mLayout->convertToLayoutUnits( grid.
offset() );
377 double gridResolution = mLayout->convertToLayoutUnits( grid.
resolution() );
378 int gridMultiplyX =
static_cast< int >( gridOffset.x() / gridResolution );
379 int gridMultiplyY =
static_cast< int >( gridOffset.y() / gridResolution );
380 double currentXCoord = gridOffset.x() - gridMultiplyX * gridResolution;
381 double currentYCoord;
382 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 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 ) );
Base class for commands to undo/redo layout and layout object changes.
static QgsPageSizeRegistry * pageSizeRegistry()
Returns the application's page size registry, used for managing layout page sizes.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static QgsFillSymbol * createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
Contains settings relating to the appearance, spacing and offset for layout grids.
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
QgsLayoutPoint offset() const
Returns the offset of the page/snap grid.
Style style() const
Returns the style used for drawing the page/snap grids.
QPen pen() const
Returns the pen used for drawing page/snap grids.
Item representing the paper in a layout.
~QgsLayoutItemPage() override
bool readPropertiesFromElement(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
QRectF boundingRect() const override
void setPageSize(const QgsLayoutSize &size)
Sets the size of the page.
int type() const override
QgsLayoutSize pageSize() const
Returns the size of the page.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
void attemptResize(const QgsLayoutSize &size, bool includesFrame=false) override
Attempts to resize the item to a specified target size.
Orientation orientation() const
Returns the page orientation.
QString displayName() const override
Gets item display name.
void drawBackground(QgsRenderContext &context) override
Draws the background for the item.
QgsAbstractLayoutUndoCommand * createCommand(const QString &text, int id, QUndoCommand *parent=nullptr) override
Creates a new layout undo command with the specified text and parent.
static QgsLayoutItemPage * create(QgsLayout *layout)
Returns a new page item for the specified layout.
QgsLayoutItemPage(QgsLayout *layout)
Constructor for QgsLayoutItemPage, with the specified parent layout.
void setPageStyleSymbol(QgsFillSymbol *symbol)
Sets the symbol to use for drawing the page background.
Orientation
Page orientation.
@ Landscape
Landscape orientation.
@ Portrait
Portrait orientation.
void drawFrame(QgsRenderContext &context) override
Draws the frame around the item.
ExportLayerBehavior exportLayerBehavior() const override
Returns the behavior of this item during exporting to layered exports (e.g.
static QgsLayoutItemPage::Orientation decodePageOrientation(const QString &string, bool *ok=nullptr)
Decodes a string representing a page orientation.
bool writePropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
QPageLayout pageLayout() const
Returns the page layout for the page, suitable to pass to QPrinter::setPageLayout.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Base class for graphical items within a QgsLayout.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
virtual void redraw()
Triggers a redraw (update) of the item.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
void sizePositionChanged()
Emitted when the item's size or position changes.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
@ CanGroupWithItemsOfSameType
Item can only be placed on layers with other items of the same type, but multiple items of this type ...
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
double length() const
Returns the length of the measurement.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QPointer< QgsLayout > mLayout
void addPage(QgsLayoutItemPage *page)
Adds a page to the collection.
Stores information relating to the current rendering settings for a layout.
bool gridVisible() const
Returns true if the page grid should be drawn.
const QgsLayoutMeasurementConverter & measurementConverter() const
Returns the layout measurement converter to be used in the layout.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
double height() const
Returns the height of the size.
void setWidth(const double width)
Sets the width for the size.
double width() const
Returns the width of the size.
void setHeight(const double height)
Sets the height for the size.
static Q_DECL_DEPRECATED double scaleFactorFromItemStyle(const QStyleOptionGraphicsItem *style)
Extracts the scale factor from an item style.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
@ ZPage
Z-value for page (paper) items.
@ ZGrid
Z-value for page grids.
A named page size for layouts.
QgsLayoutSize size
Page size.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
Scoped object for saving and restoring a QPainter object's state.
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A symbol entity for QgsStyle databases.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
@ LayoutMillimeters
Millimeters.
@ RenderMillimeters
Millimeters.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Contains information relating to the style entity currently being visited.