qgslayoutitempage.h 5.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/***************************************************************************
                              qgslayoutitempage.h
                             --------------------
    begin                : July 2017
    copyright            : (C) 2017 by Nyall Dawson
    email                : nyall dot dawson at gmail dot com
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef QGSLAYOUTITEMPAGE_H
#define QGSLAYOUTITEMPAGE_H

#include "qgis_core.h"
#include "qgslayoutitem.h"
#include "qgslayoutitemregistry.h"
#include "qgis_sip.h"


///@cond PRIVATE
#ifndef SIP_RUN

/**
 * \ingroup core
 * Item representing a grid. This is drawn separately to the underlying page item since the grid needs to be
 * drawn above all other layout items, while the paper item is drawn below all others.
 * \since QGIS 3.0
 */
class CORE_EXPORT QgsLayoutItemPageGrid: public QGraphicsRectItem
{
  public:
    QgsLayoutItemPageGrid( double x, double y, double width, double height, QgsLayout *layout );

    void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;

  private:
    QgsLayout *mLayout = nullptr;
};
#endif
///@endcond

/**
 * \ingroup core
 * \class QgsLayoutItemPage
 * \brief Item representing the paper in a layout.
 * \since QGIS 3.0
 */
class CORE_EXPORT QgsLayoutItemPage : public QgsLayoutItem
{

    Q_OBJECT

  public:

    //! Page orientation
    enum Orientation
    {
      Portrait, //!< Portrait orientation
      Landscape //!< Landscape orientation
    };

    //! Page item undo commands, used for collapsing undo commands
    enum UndoCommand
    {
      UndoPageSymbol = 3000, //!< Layout page symbol change
    };

    /**
     * Constructor for QgsLayoutItemPage, with the specified parent \a layout.
     */
    explicit QgsLayoutItemPage( QgsLayout *layout );
    ~QgsLayoutItemPage() override;

    /**
     * Returns a new page item for the specified \a layout.
     *
     * The caller takes responsibility for deleting the returned object.
     */
    static QgsLayoutItemPage *create( QgsLayout *layout ) SIP_FACTORY;

    int type() const override;
    QString displayName() const override;

    /**
     * Sets the \a size of the page.
     * \see pageSize()
     */
    void setPageSize( const QgsLayoutSize &size );

    /**
     * Sets the page size to a known page \a size, e.g. "A4" and \a orientation.
     * The known page sizes are managed by QgsPageSizeRegistry. Valid page sizes
     * can be retrieved via QgsPageSizeRegistry::entries().
     * The function returns TRUE if \a size was a valid page size and the page
     * size was changed. If FALSE is returned then \a size could not be matched
     * to a known page size.
     * \see pageSize()
     */
    bool setPageSize( const QString &size, Orientation orientation = Portrait );

    /**
     * Returns the size of the page.
     * \see setPageSize()
     */
    QgsLayoutSize pageSize() const;

    /**
     * Returns the page orientation.
     * \note There is no direct setter for page orientation - use setPageSize() instead.
     */
    Orientation orientation() const;

    /**
     * Sets the \a symbol to use for drawing the page background.
     *
     * Ownership of \a symbol is transferred to the page.
     *
     * \see pageStyleSymbol()
     *
     * \since QGIS 3.10
     */
    void setPageStyleSymbol( QgsFillSymbol *symbol SIP_TRANSFER );

    /**
     * Returns the symbol to use for drawing the page background.
     *
     * \see setPageStyleSymbol()
     *
     * \since QGIS 3.10
     */
    const QgsFillSymbol *pageStyleSymbol() const { return mPageStyleSymbol.get(); }

    /**
     * Decodes a \a string representing a page orientation. If specified, \a ok
     * will be set to TRUE if string could be successfully interpreted as a
     * page orientation.
    */
    static QgsLayoutItemPage::Orientation decodePageOrientation( const QString &string, bool *ok SIP_OUT = nullptr );

    QRectF boundingRect() const override;
    void attemptResize( const QgsLayoutSize &size, bool includesFrame = false ) override;
    QgsAbstractLayoutUndoCommand *createCommand( const QString &text, int id, QUndoCommand *parent = nullptr ) override SIP_FACTORY;
    ExportLayerBehavior exportLayerBehavior() const override;
    bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;

  public slots:

    void redraw() override;

  protected:

    void draw( QgsLayoutItemRenderContext &context ) override;
    void drawFrame( QgsRenderContext &context ) override;
    void drawBackground( QgsRenderContext &context ) override;
    bool writePropertiesToElement( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const override;
    bool readPropertiesFromElement( const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context ) override;

  private:

    double mMaximumShadowWidth = -1;

    std::unique_ptr< QgsLayoutItemPageGrid > mGrid;
    mutable QRectF mBoundingRect;

    //! Symbol for drawing page
    std::unique_ptr< QgsFillSymbol > mPageStyleSymbol;

    void createDefaultPageStyleSymbol();

    friend class TestQgsLayoutPage;
};

#endif //QGSLAYOUTITEMPAGE_H