Skip to content

Commit 9458f1f

Browse files
committedAug 7, 2017
Support deletion of guides through manager
1 parent 6138b97 commit 9458f1f

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed
 

‎python/core/layout/qgslayoutguidecollection.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ class QgsLayoutGuideCollection : QAbstractTableModel
166166

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

170172
void addGuide( QgsLayoutGuide *guide /Transfer/ );
171173
%Docstring

‎src/core/layout/qgslayoutguidecollection.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant
235235

236236
QgsLayoutMeasurement m = guide->position();
237237
m.setLength( newPos );
238-
guide->setPosition( m );
238+
whileBlocking( guide )->setPosition( m );
239239
guide->update();
240240
return true;
241241
}
@@ -248,7 +248,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant
248248

249249
QgsLayoutMeasurement m = guide->position();
250250
m.setLength( newPos );
251-
guide->setPosition( m );
251+
whileBlocking( guide )->setPosition( m );
252252
guide->update();
253253
return true;
254254
}
@@ -261,7 +261,7 @@ bool QgsLayoutGuideCollection::setData( const QModelIndex &index, const QVariant
261261

262262
QgsLayoutMeasurement m = guide->position();
263263
m.setUnits( static_cast< QgsUnitTypes::LayoutUnit >( units ) );
264-
guide->setPosition( m );
264+
whileBlocking( guide )->setPosition( m );
265265
guide->update();
266266
return true;
267267
}
@@ -288,6 +288,20 @@ QVariant QgsLayoutGuideCollection::headerData( int section, Qt::Orientation orie
288288
return QAbstractTableModel::headerData( section, orientation, role );
289289
}
290290

291+
bool QgsLayoutGuideCollection::removeRows( int row, int count, const QModelIndex &parent )
292+
{
293+
if ( parent.isValid() )
294+
return false;
295+
296+
beginRemoveRows( parent, row, row + count - 1 );
297+
for ( int i = 0; i < count; ++ i )
298+
{
299+
delete mGuides.takeAt( row );
300+
}
301+
endRemoveRows();
302+
return true;
303+
}
304+
291305
void QgsLayoutGuideCollection::addGuide( QgsLayoutGuide *guide )
292306
{
293307
guide->setLayout( mLayout );

‎src/core/layout/qgslayoutguidecollection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class CORE_EXPORT QgsLayoutGuideCollection : public QAbstractTableModel
191191
Qt::ItemFlags flags( const QModelIndex &index ) const override;
192192
QVariant headerData( int section, Qt::Orientation orientation,
193193
int role = Qt::DisplayRole ) const override;
194+
bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
194195

195196
/**
196197
* Adds a \a guide to the collection. Ownership of the guide is transferred to the

‎src/core/layout/qgslayoutitempage.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ void QgsLayoutItemPage::attemptResize( const QgsLayoutSize &size )
115115
QgsLayoutItem::attemptResize( size );
116116
//update size of attached grid to reflect new page size and position
117117
mGrid->setRect( 0, 0, rect().width(), rect().height() );
118+
119+
mLayout->guides().update();
118120
}
119121

120122
void QgsLayoutItemPage::redraw()

‎tests/src/python/test_qgslayoutguides.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ def testCollection(self):
151151
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1, g2])
152152
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical), [g3])
153153

154+
def testDeleteRows(self):
155+
p = QgsProject()
156+
l = QgsLayout(p)
157+
l.initializeDefaults()
158+
guides = l.guides()
159+
160+
g1 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(5, QgsUnitTypes.LayoutCentimeters))
161+
guides.addGuide(g1)
162+
g2 = QgsLayoutGuide(QgsLayoutGuide.Horizontal, QgsLayoutMeasurement(15))
163+
guides.addGuide(g2)
164+
g3 = QgsLayoutGuide(QgsLayoutGuide.Vertical, QgsLayoutMeasurement(35))
165+
guides.addGuide(g3)
166+
167+
self.assertTrue(guides.removeRows(1, 1))
168+
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [g1])
169+
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical), [g3])
170+
171+
self.assertTrue(guides.removeRows(0, 2))
172+
self.assertEqual(guides.guides(QgsLayoutGuide.Horizontal), [])
173+
self.assertEqual(guides.guides(QgsLayoutGuide.Vertical), [])
174+
154175
def testQgsLayoutGuideProxyModel(self):
155176
p = QgsProject()
156177
l = QgsLayout(p)

0 commit comments

Comments
 (0)
Please sign in to comment.