Skip to content
Permalink
Browse files

Allow deletion of guides by dragging outside of pages

  • Loading branch information
nyalldawson committed Jul 26, 2017
1 parent e06b0af commit da4382309eafe2addce7cfd55d089e67e4d99a75
@@ -183,6 +183,11 @@ class QgsLayoutGuideCollection : QAbstractTableModel
set.
%End

void removeGuide( QgsLayoutGuide *guide );
%Docstring
Removes the specified ``guide``, and deletes it.
%End

void update();
%Docstring
Updates the position (and visibility) of all guide line items.
@@ -338,6 +338,15 @@ void QgsLayoutGuideCollection::addGuide( QgsLayoutGuide *guide )
} );
}

void QgsLayoutGuideCollection::removeGuide( QgsLayoutGuide *guide )
{
int row = mGuides.indexOf( guide );
if ( row < 0 )
return;

removeRow( row );
}

void QgsLayoutGuideCollection::update()
{
Q_FOREACH ( QgsLayoutGuide *guide, mGuides )
@@ -207,6 +207,11 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
*/
void addGuide( QgsLayoutGuide *guide SIP_TRANSFER );

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

/**
* Updates the position (and visibility) of all guide line items.
*/
@@ -668,8 +668,28 @@ void QgsLayoutRuler::mouseReleaseEvent( QMouseEvent *event )
{
QApplication::restoreOverrideCursor();

// TODO - delete guide if outside of page
QPointF layoutPoint = convertLocalPointToLayout( event->pos() );

// delete guide if it ends outside of page
QgsLayoutItemPage *page = mView->currentLayout()->pageCollection()->page( mDraggingGuide->page() );
bool deleteGuide = false;
switch ( mDraggingGuide->orientation() )
{
case QgsLayoutGuide::Horizontal:
if ( layoutPoint.y() < page->scenePos().y() || layoutPoint.y() > page->scenePos().y() + page->rect().height() )
deleteGuide = true;
break;

case QgsLayoutGuide::Vertical:
if ( layoutPoint.x() < page->scenePos().x() || layoutPoint.x() > page->scenePos().x() + page->rect().width() )
deleteGuide = true;
break;
}

if ( deleteGuide )
{
mView->currentLayout()->guides().removeGuide( mDraggingGuide );
}
mDraggingGuide = nullptr;
}
else
@@ -104,7 +104,6 @@ def testUpdateGuide(self):
g.update()
self.assertFalse(g.item().isVisible())


def testCollection(self):
p = QgsProject()
l = QgsLayout(p)
@@ -220,6 +219,21 @@ def testQgsLayoutGuideProxyModel(self):
self.assertEqual(hoz_page_1_filter.rowCount(QModelIndex()), 1)
self.assertEqual(hoz_page_1_filter.data(hoz_page_1_filter.index(0, 0), QgsLayoutGuideCollection.PositionRole), 5)

def testRemoveGuide(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)
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1])
guides.removeGuide(None)
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1])
guides.removeGuide(g1)
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [])


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

0 comments on commit da43823

Please sign in to comment.
You can’t perform that action at this time.