Skip to content

Commit

Permalink
Support deletion of guides through manager
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent 6138b97 commit 9458f1f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/core/layout/qgslayoutguidecollection.sip
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class QgsLayoutGuideCollection : QAbstractTableModel

virtual QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const;
virtual bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() );


void addGuide( QgsLayoutGuide *guide /Transfer/ );
%Docstring
Expand Down
20 changes: 17 additions & 3 deletions src/core/layout/qgslayoutguidecollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant

QgsLayoutMeasurement m = guide->position();
m.setLength( newPos );
guide->setPosition( m );
whileBlocking( guide )->setPosition( m );
guide->update();
return true;
}
Expand All @@ -248,7 +248,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant

QgsLayoutMeasurement m = guide->position();
m.setLength( newPos );
guide->setPosition( m );
whileBlocking( guide )->setPosition( m );
guide->update();
return true;
}
Expand All @@ -261,7 +261,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant

QgsLayoutMeasurement m = guide->position();
m.setUnits( static_cast< QgsUnitTypes::LayoutUnit >( units ) );
guide->setPosition( m );
whileBlocking( guide )->setPosition( m );
guide->update();
return true;
}
Expand All @@ -288,6 +288,20 @@ QVariant QgsLayoutGuideCollection::headerData( int section, Qt::Orientation orie
return QAbstractTableModel::headerData( section, orientation, role );
}

bool QgsLayoutGuideCollection::removeRows( int row, int count, const QModelIndex &parent )
{
if ( parent.isValid() )
return false;

beginRemoveRows( parent, row, row + count - 1 );
for ( int i = 0; i < count; ++ i )
{
delete mGuides.takeAt( row );
}
endRemoveRows();
return true;
}

void QgsLayoutGuideCollection::addGuide( QgsLayoutGuide *guide )
{
guide->setLayout( mLayout );
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutguidecollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
Qt::ItemFlags flags( const QModelIndex &index ) const override;
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const override;
bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;

/**
* Adds a \a guide to the collection. Ownership of the guide is transferred to the
Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutitempage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ void QgsLayoutItemPage::attemptResize( const QgsLayoutSize &size )
QgsLayoutItem::attemptResize( size );
//update size of attached grid to reflect new page size and position
mGrid->setRect( 0, 0, rect().width(), rect().height() );

mLayout->guides().update();
}

void QgsLayoutItemPage::redraw()
Expand Down
21 changes: 21 additions & 0 deletions tests/src/python/test_qgslayoutguides.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ def testCollection(self):
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1, g2])
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical), [g3])

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

g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
guides.addGuide(g1)
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(15))
guides.addGuide(g2)
g3 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(35))
guides.addGuide(g3)

self.assertTrue(guides.removeRows(1, 1))
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1])
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical), [g3])

self.assertTrue(guides.removeRows(0, 2))
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [])
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical), [])

def testQgsLayoutGuideProxyModel(self):
p = QgsProject()
l = QgsLayout(p)
Expand Down

0 comments on commit 9458f1f

Please sign in to comment.