Add actions to switch grid display/snapping on/off

This commit is contained in:
Nyall Dawson 2017-07-24 21:56:13 +10:00
parent 798ec83681
commit d0dfec72cb
15 changed files with 133 additions and 1 deletions

View File

@ -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
};

View File

@ -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

View File

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

View File

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

View File

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

View File

@ -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:
/**

View File

@ -80,5 +80,10 @@ double QgsLayoutContext::dpi() const
bool QgsLayoutContext::gridVisible() const
{
return true;
return mGridVisible;
}
void QgsLayoutContext::setGridVisible( bool visible )
{
mGridVisible = visible;
}

View File

@ -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;
};

View File

@ -374,6 +374,11 @@ void QgsLayoutItem::refresh()
refreshDataDefinedProperty();
}
void QgsLayoutItem::redraw()
{
update();
}
void QgsLayoutItem::drawDebugRect( QPainter *painter )
{
if ( !painter )

View File

@ -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

View File

@ -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() */ )

View File

@ -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;

View File

@ -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;

View File

@ -191,6 +191,13 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject
*/
double pageShadowWidth() const;
public slots:
/**
* Triggers a redraw for all pages.
*/
void redraw();
signals:
/**

View File

@ -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"/>