Skip to content
Permalink
Browse files

Hook up clearing guides

  • Loading branch information
nyalldawson committed Jul 26, 2017
1 parent da43823 commit df3bcddab7e2717f9abf8d5b9b25ba445b4b7035
@@ -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();
@@ -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 );
@@ -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 );

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

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

createLayoutPropertiesWidget();
}

@@ -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 )
@@ -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.
*/
@@ -20,6 +20,7 @@
#include <QDragEnterEvent>
#include <QGraphicsLineItem>
#include <QPainter>
#include <QMenu>
#include <cmath>

const int RULER_FONT_SIZE = 8;
@@ -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 );
@@ -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() );
}
}
@@ -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:

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

@@ -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
@@ -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();
}
@@ -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();
}
@@ -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"/>
@@ -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"/>
@@ -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.
You can’t perform that action at this time.