diff --git a/python/core/layout/qgslayoutcontext.sip b/python/core/layout/qgslayoutcontext.sip index 9be6f4f75af..63b8ad93ab0 100644 --- a/python/core/layout/qgslayoutcontext.sip +++ b/python/core/layout/qgslayoutcontext.sip @@ -128,9 +128,16 @@ class QgsLayoutContext bool gridVisible() const; %Docstring Returns true if the page grid should be drawn. +.. seealso:: setGridVisible() :rtype: bool %End + void setGridVisible( bool visible ); +%Docstring + Sets whether the page grid should be ``visible``. +.. seealso:: gridVisible() +%End + }; diff --git a/python/core/layout/qgslayoutitem.sip b/python/core/layout/qgslayoutitem.sip index 8cac4f72a5c..90fc48998d7 100644 --- a/python/core/layout/qgslayoutitem.sip +++ b/python/core/layout/qgslayoutitem.sip @@ -228,6 +228,11 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem recalculation of its position and size. %End + virtual void redraw(); +%Docstring + Triggers a redraw (update) of the item. +%End + virtual void refreshDataDefinedProperty( const QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ); %Docstring Refreshes a data defined ``property`` for the item by reevaluating the property's value diff --git a/python/core/layout/qgslayoutitempage.sip b/python/core/layout/qgslayoutitempage.sip index e1ef47a6242..dd7a2c85d34 100644 --- a/python/core/layout/qgslayoutitempage.sip +++ b/python/core/layout/qgslayoutitempage.sip @@ -80,6 +80,11 @@ class QgsLayoutItemPage : QgsLayoutItem virtual void attemptResize( const QgsLayoutSize &size ); + public slots: + + virtual void redraw(); + + protected: virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 ); diff --git a/python/core/layout/qgslayoutpagecollection.sip b/python/core/layout/qgslayoutpagecollection.sip index 8700d67e88e..5c8e388faba 100644 --- a/python/core/layout/qgslayoutpagecollection.sip +++ b/python/core/layout/qgslayoutpagecollection.sip @@ -188,6 +188,13 @@ class QgsLayoutPageCollection : QObject :rtype: float %End + public slots: + + void redraw(); +%Docstring + Triggers a redraw for all pages. +%End + signals: void changed(); diff --git a/src/app/layout/qgslayoutdesignerdialog.cpp b/src/app/layout/qgslayoutdesignerdialog.cpp index cc5497512fe..82e234d0797 100644 --- a/src/app/layout/qgslayoutdesignerdialog.cpp +++ b/src/app/layout/qgslayoutdesignerdialog.cpp @@ -122,6 +122,9 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla mActionShowRulers->blockSignals( false ); connect( mActionShowRulers, &QAction::triggered, this, &QgsLayoutDesignerDialog::showRulers ); + connect( mActionShowGrid, &QAction::triggered, this, &QgsLayoutDesignerDialog::showGrid ); + connect( mActionSnapGrid, &QAction::triggered, this, &QgsLayoutDesignerDialog::snapToGrid ); + mView = new QgsLayoutView(); //mView->setMapCanvas( mQgis->mapCanvas() ); mView->setContentsMargins( 0, 0, 0, 0 ); @@ -337,6 +340,17 @@ void QgsLayoutDesignerDialog::showRulers( bool visible ) settings.setValue( QStringLiteral( "LayoutDesigner/showRulers" ), visible ); } +void QgsLayoutDesignerDialog::showGrid( bool visible ) +{ + mLayout->context().setGridVisible( visible ); + mLayout->pageCollection()->redraw(); +} + +void QgsLayoutDesignerDialog::snapToGrid( bool enabled ) +{ + mLayout->snapper().setSnapToGrid( enabled ); +} + void QgsLayoutDesignerDialog::closeEvent( QCloseEvent * ) { emit aboutToClose(); diff --git a/src/app/layout/qgslayoutdesignerdialog.h b/src/app/layout/qgslayoutdesignerdialog.h index 0a93533ae06..3a3e88c81c8 100644 --- a/src/app/layout/qgslayoutdesignerdialog.h +++ b/src/app/layout/qgslayoutdesignerdialog.h @@ -115,6 +115,16 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner */ void showRulers( bool visible ); + /** + * Toggles whether the page grid should be \a visible. + */ + void showGrid( bool visible ); + + /** + * Toggles whether snapping to the page grid is \a enabled. + */ + void snapToGrid( bool enabled ); + signals: /** diff --git a/src/core/layout/qgslayoutcontext.cpp b/src/core/layout/qgslayoutcontext.cpp index 989164562bf..1887e5b4acb 100644 --- a/src/core/layout/qgslayoutcontext.cpp +++ b/src/core/layout/qgslayoutcontext.cpp @@ -80,5 +80,10 @@ double QgsLayoutContext::dpi() const bool QgsLayoutContext::gridVisible() const { - return true; + return mGridVisible; +} + +void QgsLayoutContext::setGridVisible( bool visible ) +{ + mGridVisible = visible; } diff --git a/src/core/layout/qgslayoutcontext.h b/src/core/layout/qgslayoutcontext.h index 00566165739..4f66fa13b37 100644 --- a/src/core/layout/qgslayoutcontext.h +++ b/src/core/layout/qgslayoutcontext.h @@ -141,9 +141,16 @@ class CORE_EXPORT QgsLayoutContext /** * Returns true if the page grid should be drawn. + * \see setGridVisible() */ bool gridVisible() const; + /** + * Sets whether the page grid should be \a visible. + * \see gridVisible() + */ + void setGridVisible( bool visible ); + private: Flags mFlags = 0; @@ -153,6 +160,8 @@ class CORE_EXPORT QgsLayoutContext QgsLayoutMeasurementConverter mMeasurementConverter; + bool mGridVisible = false; + }; diff --git a/src/core/layout/qgslayoutitem.cpp b/src/core/layout/qgslayoutitem.cpp index 650ef0fa93a..7da8eeadff6 100644 --- a/src/core/layout/qgslayoutitem.cpp +++ b/src/core/layout/qgslayoutitem.cpp @@ -374,6 +374,11 @@ void QgsLayoutItem::refresh() refreshDataDefinedProperty(); } +void QgsLayoutItem::redraw() +{ + update(); +} + void QgsLayoutItem::drawDebugRect( QPainter *painter ) { if ( !painter ) diff --git a/src/core/layout/qgslayoutitem.h b/src/core/layout/qgslayoutitem.h index 0c8edb7fc7a..6e561bd532a 100644 --- a/src/core/layout/qgslayoutitem.h +++ b/src/core/layout/qgslayoutitem.h @@ -235,6 +235,11 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt */ void refresh() override; + /** + * Triggers a redraw (update) of the item. + */ + virtual void redraw(); + /** * Refreshes a data defined \a property for the item by reevaluating the property's value * and redrawing the item with this new value. If \a property is set to diff --git a/src/core/layout/qgslayoutitempage.cpp b/src/core/layout/qgslayoutitempage.cpp index 242b1575d3c..8cda23869cf 100644 --- a/src/core/layout/qgslayoutitempage.cpp +++ b/src/core/layout/qgslayoutitempage.cpp @@ -117,6 +117,12 @@ void QgsLayoutItemPage::attemptResize( const QgsLayoutSize &size ) mGrid->setRect( 0, 0, rect().width(), rect().height() ); } +void QgsLayoutItemPage::redraw() +{ + QgsLayoutItem::redraw(); + mGrid->update(); +} + void QgsLayoutItemPage::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * ) { if ( !context.painter() || !mLayout /*|| !mLayout->pagesVisible() */ ) diff --git a/src/core/layout/qgslayoutitempage.h b/src/core/layout/qgslayoutitempage.h index 4ac180465a4..62c2e846241 100644 --- a/src/core/layout/qgslayoutitempage.h +++ b/src/core/layout/qgslayoutitempage.h @@ -110,6 +110,10 @@ class CORE_EXPORT QgsLayoutItemPage : public QgsLayoutItem void attemptResize( const QgsLayoutSize &size ) override; + public slots: + + void redraw() override; + protected: void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override; diff --git a/src/core/layout/qgslayoutpagecollection.cpp b/src/core/layout/qgslayoutpagecollection.cpp index 7ba9f3c5881..5aae95110e6 100644 --- a/src/core/layout/qgslayoutpagecollection.cpp +++ b/src/core/layout/qgslayoutpagecollection.cpp @@ -135,6 +135,14 @@ double QgsLayoutPageCollection::pageShadowWidth() const return spaceBetweenPages() / 2; } +void QgsLayoutPageCollection::redraw() +{ + Q_FOREACH ( QgsLayoutItemPage *page, mPages ) + { + page->redraw(); + } +} + QgsLayout *QgsLayoutPageCollection::layout() const { return mLayout; diff --git a/src/core/layout/qgslayoutpagecollection.h b/src/core/layout/qgslayoutpagecollection.h index 98259ff92b5..20024de3d80 100644 --- a/src/core/layout/qgslayoutpagecollection.h +++ b/src/core/layout/qgslayoutpagecollection.h @@ -191,6 +191,13 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject */ double pageShadowWidth() const; + public slots: + + /** + * Triggers a redraw for all pages. + */ + void redraw(); + signals: /** diff --git a/src/ui/layout/qgslayoutdesignerbase.ui b/src/ui/layout/qgslayoutdesignerbase.ui index 980818247a0..655d3405600 100644 --- a/src/ui/layout/qgslayoutdesignerbase.ui +++ b/src/ui/layout/qgslayoutdesignerbase.ui @@ -118,6 +118,9 @@ + + + @@ -307,6 +310,38 @@ Add Pages… + + + true + + + + :/images/themes/default/vector_grid.png:/images/themes/default/vector_grid.png + + + Show &Grid + + + Show grid + + + Ctrl+' + + + + + true + + + S&nap to Grid + + + Snap to grid + + + Ctrl+Shift+' + +