Skip to content

Commit

Permalink
Hook up clearing guides
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent da43823 commit df3bcdd
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/layout/qgslayoutguidecollection.sip
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ class QgsLayoutGuideCollection : QAbstractTableModel
void removeGuide( QgsLayoutGuide *guide );
%Docstring
Removes the specified ``guide``, and deletes it.
.. seealso:: clear()
%End

void clear();
%Docstring
Removes all guides from the collection.
.. seealso:: removeGuide()
%End

void update();
Expand Down
6 changes: 6 additions & 0 deletions python/gui/layout/qgslayoutruler.sip
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class QgsLayoutRuler: QWidget
:rtype: int
%End

void setContextMenu( QMenu *menu );
%Docstring
Sets a context ``menu`` to show when right clicking occurs on the ruler.
Ownership of ``menu`` is unchanged.
%End

public slots:

void setCursorPosition( QPointF position );
Expand Down
12 changes: 12 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
mActionShowRulers->blockSignals( false );
connect( mActionShowRulers, &QAction::triggered, this, &QgsLayoutDesignerDialog::showRulers );

QMenu *rulerMenu = new QMenu( this );
rulerMenu->addAction( mActionManageGuides );
rulerMenu->addAction( mActionClearGuides );
mHorizontalRuler->setContextMenu( rulerMenu );
mVerticalRuler->setContextMenu( rulerMenu );

connect( mActionShowGrid, &QAction::triggered, this, &QgsLayoutDesignerDialog::showGrid );
connect( mActionSnapGrid, &QAction::triggered, this, &QgsLayoutDesignerDialog::snapToGrid );

Expand Down Expand Up @@ -297,6 +303,12 @@ void QgsLayoutDesignerDialog::setCurrentLayout( QgsLayout *layout )
{
mLayout = layout;
mView->setCurrentLayout( layout );

connect( mActionClearGuides, &QAction::triggered, &mLayout->guides(), [ = ]
{
mLayout->guides().clear();
} );

createLayoutPropertiesWidget();
}

Expand Down
8 changes: 8 additions & 0 deletions src/core/layout/qgslayoutguidecollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ void QgsLayoutGuideCollection::removeGuide( QgsLayoutGuide *guide )
removeRow( row );
}

void QgsLayoutGuideCollection::clear()
{
beginResetModel();
qDeleteAll( mGuides );
mGuides.clear();
endResetModel();
}

void QgsLayoutGuideCollection::update()
{
Q_FOREACH ( QgsLayoutGuide *guide, mGuides )
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoutguidecollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,16 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel

/**
* Removes the specified \a guide, and deletes it.
* \see clear()
*/
void removeGuide( QgsLayoutGuide *guide );

/**
* Removes all guides from the collection.
* \see removeGuide()
*/
void clear();

/**
* Updates the position (and visibility) of all guide line items.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/gui/layout/qgslayoutruler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QDragEnterEvent>
#include <QGraphicsLineItem>
#include <QPainter>
#include <QMenu>
#include <cmath>

const int RULER_FONT_SIZE = 8;
Expand Down Expand Up @@ -510,6 +511,11 @@ void QgsLayoutRuler::setLayoutView( QgsLayoutView *view )
connect( mView, &QgsLayoutView::cursorPosChanged, this, &QgsLayoutRuler::setCursorPosition );
}

void QgsLayoutRuler::setContextMenu( QMenu *menu )
{
mMenu = menu;
}

void QgsLayoutRuler::setCursorPosition( QPointF position )
{
mMarkerPos = mView->mapFromScene( position );
Expand Down Expand Up @@ -745,4 +751,9 @@ void QgsLayoutRuler::mouseReleaseEvent( QMouseEvent *event )
mView->currentLayout()->guides().addGuide( guide.release() );
}
}
else if ( event->button() == Qt::RightButton )
{
if ( mMenu )
mMenu->popup( event->globalPos() );
}
}
8 changes: 8 additions & 0 deletions src/gui/layout/qgslayoutruler.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class GUI_EXPORT QgsLayoutRuler: public QWidget
*/
int rulerSize() const { return mRulerMinSize; }

/**
* Sets a context \a menu to show when right clicking occurs on the ruler.
* Ownership of \a menu is unchanged.
*/
void setContextMenu( QMenu *menu );

public slots:

/**
Expand Down Expand Up @@ -118,6 +124,8 @@ class GUI_EXPORT QgsLayoutRuler: public QWidget
//! Polygon for drawing guide markers
QPolygonF mGuideMarker;

QMenu *mMenu = nullptr;

//! Calculates the optimum labeled units for ruler so that labels are a good distance apart
int optimumScale( double minPixelDiff, int &magnitude, int &multiple );

Expand Down
4 changes: 4 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ void QgsLayoutView::setCurrentLayout( QgsLayout *layout )
connect( &layout->guides(), &QAbstractItemModel::dataChanged, mHorizontalRuler, [ = ] { mHorizontalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::rowsInserted, mHorizontalRuler, [ = ] { mHorizontalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::rowsRemoved, mHorizontalRuler, [ = ] { mHorizontalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::modelReset, mHorizontalRuler, [ = ] { mHorizontalRuler->update(); } );
}
if ( mVerticalRuler )
{
connect( &layout->guides(), &QAbstractItemModel::dataChanged, mVerticalRuler, [ = ] { mVerticalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::rowsInserted, mVerticalRuler, [ = ] { mVerticalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::rowsRemoved, mVerticalRuler, [ = ] { mVerticalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::modelReset, mVerticalRuler, [ = ] { mVerticalRuler->update(); } );
}

//emit layoutSet, so that designer dialogs can update for the new layout
Expand Down Expand Up @@ -166,6 +168,7 @@ void QgsLayoutView::setHorizontalRuler( QgsLayoutRuler *ruler )
connect( &layout->guides(), &QAbstractItemModel::dataChanged, ruler, [ = ] { mHorizontalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::rowsInserted, ruler, [ = ] { mHorizontalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::rowsRemoved, ruler, [ = ] { mHorizontalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::modelReset, ruler, [ = ] { mHorizontalRuler->update(); } );
}
viewChanged();
}
Expand All @@ -179,6 +182,7 @@ void QgsLayoutView::setVerticalRuler( QgsLayoutRuler *ruler )
connect( &layout->guides(), &QAbstractItemModel::dataChanged, ruler, [ = ] { mVerticalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::rowsInserted, ruler, [ = ] { mVerticalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::rowsRemoved, ruler, [ = ] { mVerticalRuler->update(); } );
connect( &layout->guides(), &QAbstractItemModel::modelReset, ruler, [ = ] { mVerticalRuler->update(); } );
}
viewChanged();
}
Expand Down
9 changes: 9 additions & 0 deletions src/ui/layout/qgslayoutdesignerbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<addaction name="mActionShowGuides"/>
<addaction name="mActionSnapGuides"/>
<addaction name="mActionManageGuides"/>
<addaction name="mActionClearGuides"/>
<addaction name="separator"/>
<addaction name="mActionShowRulers"/>
<addaction name="separator"/>
Expand Down Expand Up @@ -373,6 +374,14 @@
<string>Ctrl+Shift+;</string>
</property>
</action>
<action name="mActionClearGuides">
<property name="text">
<string>&amp;Clear Guides</string>
</property>
<property name="toolTip">
<string>Clear guides</string>
</property>
</action>
</widget>
<resources>
<include location="../../../images/images.qrc"/>
Expand Down
15 changes: 15 additions & 0 deletions tests/src/python/test_qgslayoutguides.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ def testRemoveGuide(self):
guides.removeGuide(g1)
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [])

def testClear(self):
p = QgsProject()
l = QgsLayout(p)
l.initializeDefaults() # add a page
guides = l.guides()

# add a guide
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
guides.addGuide(g1)
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
guides.addGuide(g2)
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1, g2])
guides.clear()
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [])


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

0 comments on commit df3bcdd

Please sign in to comment.