Skip to content

Commit

Permalink
Readd menu option to show/hide guides
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent 98ff702 commit 9267f86
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
14 changes: 14 additions & 0 deletions python/core/layout/qgslayoutguidecollection.sip
Expand Up @@ -213,6 +213,20 @@ class QgsLayoutGuideCollection : QAbstractTableModel
:rtype: list of QgsLayoutGuide
%End


bool visible() const;
%Docstring
Returns true if the guide lines should be drawn.
.. seealso:: setVisible()
:rtype: bool
%End

void setVisible( bool visible );
%Docstring
Sets whether the guide lines should be ``visible``.
.. seealso:: visible()
%End

};


Expand Down
16 changes: 16 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -133,6 +133,8 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
connect( mActionShowGrid, &QAction::triggered, this, &QgsLayoutDesignerDialog::showGrid );
connect( mActionSnapGrid, &QAction::triggered, this, &QgsLayoutDesignerDialog::snapToGrid );

connect( mActionShowGuides, &QAction::triggered, this, &QgsLayoutDesignerDialog::showGuides );

mView = new QgsLayoutView();
//mView->setMapCanvas( mQgis->mapCanvas() );
mView->setContentsMargins( 0, 0, 0, 0 );
Expand Down Expand Up @@ -309,6 +311,10 @@ void QgsLayoutDesignerDialog::setCurrentLayout( QgsLayout *layout )
mLayout->guides().clear();
} );

mActionShowGrid->setChecked( mLayout->context().gridVisible() );
mActionSnapGrid->setChecked( mLayout->snapper().snapToGrid() );
mActionShowGuides->setChecked( mLayout->guides().visible() );

createLayoutPropertiesWidget();
}

Expand Down Expand Up @@ -393,6 +399,16 @@ void QgsLayoutDesignerDialog::snapToGrid( bool enabled )
mLayout->snapper().setSnapToGrid( enabled );
}

void QgsLayoutDesignerDialog::showGuides( bool visible )
{
mLayout->guides().setVisible( visible );
}

void QgsLayoutDesignerDialog::snapToGuides( bool enabled )
{

}

void QgsLayoutDesignerDialog::closeEvent( QCloseEvent * )
{
emit aboutToClose();
Expand Down
10 changes: 10 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.h
Expand Up @@ -125,6 +125,16 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
*/
void snapToGrid( bool enabled );

/**
* Toggles whether the page guides should be \a visible.
*/
void showGuides( bool visible );

/**
* Toggles whether snapping to the page guides is \a enabled.
*/
void snapToGuides( bool enabled );

signals:

/**
Expand Down
17 changes: 15 additions & 2 deletions src/core/layout/qgslayoutguidecollection.cpp
Expand Up @@ -77,6 +77,7 @@ void QgsLayoutGuide::update()
QgsLayoutItemPage *page = mLayout->pageCollection()->page( mPage );
mLineItem->setParentItem( page );
double layoutPos = mLayout->convertToLayoutUnits( mPosition );
bool showGuide = mLayout->guides().visible();
switch ( mOrientation )
{
case Horizontal:
Expand All @@ -87,7 +88,7 @@ void QgsLayoutGuide::update()
else
{
mLineItem->setLine( 0, layoutPos, page->rect().width(), layoutPos );
mLineItem->show();
mLineItem->setVisible( showGuide );
}

break;
Expand All @@ -100,7 +101,7 @@ void QgsLayoutGuide::update()
else
{
mLineItem->setLine( layoutPos, 0, layoutPos, page->rect().height() );
mLineItem->show();
mLineItem->setVisible( showGuide );
}

break;
Expand Down Expand Up @@ -404,6 +405,18 @@ QList<QgsLayoutGuide *> QgsLayoutGuideCollection::guides( QgsLayoutGuide::Orient
return res;
}

bool QgsLayoutGuideCollection::visible() const
{
return mGuidesVisible;
}

void QgsLayoutGuideCollection::setVisible( bool visible )
{
mGuidesVisible = visible;
update();
}



//
// QgsLayoutGuideProxyModel
Expand Down
15 changes: 15 additions & 0 deletions src/core/layout/qgslayoutguidecollection.h
Expand Up @@ -236,13 +236,28 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
*/
QList< QgsLayoutGuide * > guides( QgsLayoutGuide::Orientation orientation, int page = -1 );


/**
* Returns true if the guide lines should be drawn.
* \see setVisible()
*/
bool visible() const;

/**
* Sets whether the guide lines should be \a visible.
* \see visible()
*/
void setVisible( bool visible );

private:

QgsLayout *mLayout = nullptr;

QList< QgsLayoutGuide * > mGuides;
int mHeaderSize = 0;

bool mGuidesVisible = true;

};


Expand Down
12 changes: 11 additions & 1 deletion src/ui/layout/qgslayoutdesignerbase.ui
Expand Up @@ -83,7 +83,7 @@
<x>0</x>
<y>0</y>
<width>1083</width>
<height>42</height>
<height>25</height>
</rect>
</property>
<widget class="QMenu" name="mLayoutMenu">
Expand Down Expand Up @@ -353,6 +353,9 @@
</property>
</action>
<action name="mActionShowGuides">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show G&amp;uides</string>
</property>
Expand All @@ -364,6 +367,9 @@
</property>
</action>
<action name="mActionSnapGuides">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>&amp;Snap to Guides</string>
</property>
Expand Down Expand Up @@ -433,6 +439,10 @@
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
<include location="../../../images/images.qrc"/>
</resources>
<connections/>
</ui>
19 changes: 19 additions & 0 deletions tests/src/python/test_qgslayoutguides.py
Expand Up @@ -293,6 +293,25 @@ def testApplyToOtherPages(self):
self.assertEqual(len(guides.guides(QgsLayoutGuide.Vertical, 0)), 1)
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical, 0)[0].position().length(), 6)

def testSetVisible(self):
p = QgsProject()
l = QgsLayout(p)
l.initializeDefaults()
guides = l.guides()

# add some guides
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5))
guides.addGuide(g1)
g2 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(6))
guides.addGuide(g2)

guides.setVisible(False)
self.assertFalse(g1.item().isVisible())
self.assertFalse(g2.item().isVisible())
guides.setVisible(True)
self.assertTrue(g1.item().isVisible())
self.assertTrue(g2.item().isVisible())


if __name__ == '__main__':
unittest.main()

0 comments on commit 9267f86

Please sign in to comment.