Skip to content

Commit

Permalink
Resurrect locking/unlocking item actions
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent 683a869 commit dbb3125
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
13 changes: 13 additions & 0 deletions python/core/layout/qgslayout.sip
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
not correctly emit signals to allow the layout's model to update.
%End

void lockSelectedItems();
%Docstring
Locks any selected items, preventing them from being interacted with
by mouse interactions.
.. seealso:: unlockAllItems()
%End

void unlockAllItems();
%Docstring
Unlocks all locked items in the layout.
.. seealso:: lockSelectedItems()
%End

QgsLayoutItem *itemByUuid( const QString &uuid );
%Docstring
Returns the layout item with matching ``uuid`` unique identifier, or a None
Expand Down
41 changes: 41 additions & 0 deletions src/core/layout/qgslayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,47 @@ void QgsLayout::deselectAll()
emit selectedItemChanged( nullptr );
}

void QgsLayout::lockSelectedItems()
{
mUndoStack->beginMacro( tr( "Items locked" ) );
const QList<QgsLayoutItem *> selectionList = selectedLayoutItems();
for ( QgsLayoutItem *item : selectionList )
{
mUndoStack->beginCommand( item, QString() );
item->setLocked( true );
mUndoStack->endCommand();
}

deselectAll();
mUndoStack->endMacro();
}

void QgsLayout::unlockAllItems()
{
//unlock all items in composer

mUndoStack->beginMacro( tr( "Items unlocked" ) );

//first, clear the selection
deselectAll();

const QList<QGraphicsItem *> itemList = items();
for ( QGraphicsItem *graphicItem : itemList )
{
QgsLayoutItem *item = dynamic_cast<QgsLayoutItem *>( graphicItem );
if ( item && item->isLocked() )
{
mUndoStack->beginCommand( item, QString() );
item->setLocked( false );
//select unlocked items, same behavior as illustrator
item->setSelected( true );
emit selectedItemChanged( item );
mUndoStack->endCommand();
}
}
mUndoStack->endMacro();
}

QgsLayoutItem *QgsLayout::itemByUuid( const QString &uuid )
{
QList<QgsLayoutItem *> itemList;
Expand Down
13 changes: 13 additions & 0 deletions src/core/layout/qgslayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
*/
void deselectAll();

/**
* Locks any selected items, preventing them from being interacted with
* by mouse interactions.
* \see unlockAllItems()
*/
void lockSelectedItems();

/**
* Unlocks all locked items in the layout.
* \see lockSelectedItems()
*/
void unlockAllItems();

/**
* Returns the layout item with matching \a uuid unique identifier, or a nullptr
* if a matching item could not be found.
Expand Down
3 changes: 1 addition & 2 deletions src/core/layout/qgslayoutitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,12 @@ void QgsLayoutItem::setLocked( const bool locked )

mIsLocked = locked;

#if 0 //TODO
//inform model that id data has changed
if ( mLayout )
{
mLayout->itemsModel()->updateItemLockStatus( this );
}
#endif

update();
emit lockChanged();
}
Expand Down
34 changes: 34 additions & 0 deletions tests/src/python/test_qgslayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,40 @@ def testLayoutItemAt(self):
item2.setLocked(True)
self.assertEqual(l.layoutItemAt(QPointF(9, 13), item3, True), item1)

def testLockActions(self):
p = QgsProject()
l = QgsLayout(p)

# add some items
item1 = QgsLayoutItemMap(l)
l.addItem(item1)
item2 = QgsLayoutItemMap(l)
l.addItem(item2)
item3 = QgsLayoutItemMap(l)
l.addItem(item3)

item1.setLocked(True)
item3.setLocked(True)
self.assertTrue(item1.isLocked())
self.assertFalse(item2.isLocked())
self.assertTrue(item3.isLocked())

l.unlockAllItems()
self.assertFalse(item1.isLocked())
self.assertFalse(item2.isLocked())
self.assertFalse(item3.isLocked())
self.assertTrue(item1.isSelected())
self.assertFalse(item2.isSelected())
self.assertTrue(item3.isSelected())

l.lockSelectedItems()
self.assertTrue(item1.isLocked())
self.assertFalse(item2.isLocked())
self.assertTrue(item3.isLocked())
self.assertFalse(item1.isSelected())
self.assertFalse(item2.isSelected())
self.assertFalse(item3.isSelected())


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

0 comments on commit dbb3125

Please sign in to comment.