Skip to content
Permalink
Browse files

Add actions to switch grid display/snapping on/off

  • Loading branch information
nyalldawson committed Jul 24, 2017
1 parent 798ec83 commit d0dfec72cb952cfebf69975e4e49ad9797099296
@@ -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

};


@@ -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
@@ -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 );
@@ -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();
@@ -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();
@@ -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:

/**
@@ -80,5 +80,10 @@ double QgsLayoutContext::dpi() const

bool QgsLayoutContext::gridVisible() const
{
return true;
return mGridVisible;
}

void QgsLayoutContext::setGridVisible( bool visible )
{
mGridVisible = visible;
}
@@ -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;


};

@@ -374,6 +374,11 @@ void QgsLayoutItem::refresh()
refreshDataDefinedProperty();
}

void QgsLayoutItem::redraw()
{
update();
}

void QgsLayoutItem::drawDebugRect( QPainter *painter )
{
if ( !painter )
@@ -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
@@ -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() */ )
@@ -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;
@@ -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;
@@ -191,6 +191,13 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject
*/
double pageShadowWidth() const;

public slots:

/**
* Triggers a redraw for all pages.
*/
void redraw();

signals:

/**
@@ -118,6 +118,9 @@
<addaction name="mActionZoomAll"/>
<addaction name="mActionZoomToWidth"/>
<addaction name="separator"/>
<addaction name="mActionShowGrid"/>
<addaction name="mActionSnapGrid"/>
<addaction name="separator"/>
<addaction name="mActionShowRulers"/>
<addaction name="separator"/>
<addaction name="mToolbarMenu"/>
@@ -307,6 +310,38 @@
<string>Add Pages…</string>
</property>
</action>
<action name="mActionShowGrid">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/vector_grid.png</normaloff>:/images/themes/default/vector_grid.png</iconset>
</property>
<property name="text">
<string>Show &amp;Grid</string>
</property>
<property name="toolTip">
<string>Show grid</string>
</property>
<property name="shortcut">
<string>Ctrl+'</string>
</property>
</action>
<action name="mActionSnapGrid">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>S&amp;nap to Grid</string>
</property>
<property name="toolTip">
<string>Snap to grid</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+'</string>
</property>
</action>
</widget>
<resources>
<include location="../../../images/images.qrc"/>

0 comments on commit d0dfec7

Please sign in to comment.
You can’t perform that action at this time.