From 9267f8676cb6fba3602816b3d9a86551bd1f4b37 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 27 Jul 2017 12:01:59 +1000 Subject: [PATCH] Readd menu option to show/hide guides --- .../core/layout/qgslayoutguidecollection.sip | 14 ++++++++++++++ src/app/layout/qgslayoutdesignerdialog.cpp | 16 ++++++++++++++++ src/app/layout/qgslayoutdesignerdialog.h | 10 ++++++++++ src/core/layout/qgslayoutguidecollection.cpp | 17 +++++++++++++++-- src/core/layout/qgslayoutguidecollection.h | 15 +++++++++++++++ src/ui/layout/qgslayoutdesignerbase.ui | 12 +++++++++++- tests/src/python/test_qgslayoutguides.py | 19 +++++++++++++++++++ 7 files changed, 100 insertions(+), 3 deletions(-) diff --git a/python/core/layout/qgslayoutguidecollection.sip b/python/core/layout/qgslayoutguidecollection.sip index 115300bb05f..33abad2937c 100644 --- a/python/core/layout/qgslayoutguidecollection.sip +++ b/python/core/layout/qgslayoutguidecollection.sip @@ -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 + }; diff --git a/src/app/layout/qgslayoutdesignerdialog.cpp b/src/app/layout/qgslayoutdesignerdialog.cpp index d13b81bb5cf..0cfd47a9ad5 100644 --- a/src/app/layout/qgslayoutdesignerdialog.cpp +++ b/src/app/layout/qgslayoutdesignerdialog.cpp @@ -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 ); @@ -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(); } @@ -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(); diff --git a/src/app/layout/qgslayoutdesignerdialog.h b/src/app/layout/qgslayoutdesignerdialog.h index 45eab77854c..1d4cf6c484e 100644 --- a/src/app/layout/qgslayoutdesignerdialog.h +++ b/src/app/layout/qgslayoutdesignerdialog.h @@ -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: /** diff --git a/src/core/layout/qgslayoutguidecollection.cpp b/src/core/layout/qgslayoutguidecollection.cpp index 9edd71d5747..2f62f8b1e7a 100644 --- a/src/core/layout/qgslayoutguidecollection.cpp +++ b/src/core/layout/qgslayoutguidecollection.cpp @@ -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: @@ -87,7 +88,7 @@ void QgsLayoutGuide::update() else { mLineItem->setLine( 0, layoutPos, page->rect().width(), layoutPos ); - mLineItem->show(); + mLineItem->setVisible( showGuide ); } break; @@ -100,7 +101,7 @@ void QgsLayoutGuide::update() else { mLineItem->setLine( layoutPos, 0, layoutPos, page->rect().height() ); - mLineItem->show(); + mLineItem->setVisible( showGuide ); } break; @@ -404,6 +405,18 @@ QList QgsLayoutGuideCollection::guides( QgsLayoutGuide::Orient return res; } +bool QgsLayoutGuideCollection::visible() const +{ + return mGuidesVisible; +} + +void QgsLayoutGuideCollection::setVisible( bool visible ) +{ + mGuidesVisible = visible; + update(); +} + + // // QgsLayoutGuideProxyModel diff --git a/src/core/layout/qgslayoutguidecollection.h b/src/core/layout/qgslayoutguidecollection.h index 9b058f58882..7e4c78f9987 100644 --- a/src/core/layout/qgslayoutguidecollection.h +++ b/src/core/layout/qgslayoutguidecollection.h @@ -236,6 +236,19 @@ 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; @@ -243,6 +256,8 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel QList< QgsLayoutGuide * > mGuides; int mHeaderSize = 0; + bool mGuidesVisible = true; + }; diff --git a/src/ui/layout/qgslayoutdesignerbase.ui b/src/ui/layout/qgslayoutdesignerbase.ui index 207f28e0924..2aaafc00198 100644 --- a/src/ui/layout/qgslayoutdesignerbase.ui +++ b/src/ui/layout/qgslayoutdesignerbase.ui @@ -83,7 +83,7 @@ 0 0 1083 - 42 + 25 @@ -353,6 +353,9 @@ + + true + Show G&uides @@ -364,6 +367,9 @@ + + true + &Snap to Guides @@ -433,6 +439,10 @@ + + + + diff --git a/tests/src/python/test_qgslayoutguides.py b/tests/src/python/test_qgslayoutguides.py index f8d18998025..5444f88f8d7 100644 --- a/tests/src/python/test_qgslayoutguides.py +++ b/tests/src/python/test_qgslayoutguides.py @@ -293,6 +293,25 @@ class TestQgsLayoutGuide(unittest.TestCase): 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()