Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add actions to switch grid display/snapping on/off
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent 798ec83 commit d0dfec7
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/core/layout/qgslayoutcontext.sip
Expand Up @@ -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

};


Expand Down
5 changes: 5 additions & 0 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions python/core/layout/qgslayoutitempage.sip
Expand Up @@ -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 );
Expand Down
7 changes: 7 additions & 0 deletions python/core/layout/qgslayoutpagecollection.sip
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.h
Expand Up @@ -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:

/**
Expand Down
7 changes: 6 additions & 1 deletion src/core/layout/qgslayoutcontext.cpp
Expand Up @@ -80,5 +80,10 @@ double QgsLayoutContext::dpi() const

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

void QgsLayoutContext::setGridVisible( bool visible )
{
mGridVisible = visible;
}
9 changes: 9 additions & 0 deletions src/core/layout/qgslayoutcontext.h
Expand Up @@ -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;
Expand All @@ -153,6 +160,8 @@ class CORE_EXPORT QgsLayoutContext

QgsLayoutMeasurementConverter mMeasurementConverter;

bool mGridVisible = false;


};

Expand Down
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -374,6 +374,11 @@ void QgsLayoutItem::refresh()
refreshDataDefinedProperty();
}

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

void QgsLayoutItem::drawDebugRect( QPainter *painter )
{
if ( !painter )
Expand Down
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/core/layout/qgslayoutitempage.cpp
Expand Up @@ -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() */ )
Expand Down
4 changes: 4 additions & 0 deletions src/core/layout/qgslayoutitempage.h
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -191,6 +191,13 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject
*/
double pageShadowWidth() const;

public slots:

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

signals:

/**
Expand Down
35 changes: 35 additions & 0 deletions src/ui/layout/qgslayoutdesignerbase.ui
Expand Up @@ -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"/>
Expand Down Expand Up @@ -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"/>
Expand Down

0 comments on commit d0dfec7

Please sign in to comment.