Skip to content

Commit

Permalink
Pasting items from layout context menu pastes them at menu origin
Browse files Browse the repository at this point in the history
instead of final cursor position
  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent ac6c131 commit 17656f2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
12 changes: 12 additions & 0 deletions python/gui/layout/qgslayoutview.sip
Expand Up @@ -212,6 +212,18 @@ class QgsLayoutView: QGraphicsView

A list of pasted items is returned.

.. seealso:: copySelectedItems()
.. seealso:: hasItemsInClipboard()
:rtype: list of QgsLayoutItem
%End

QList< QgsLayoutItem * > pasteItems( QPointF layoutPoint );
%Docstring
Pastes items from clipboard, at the specified ``layoutPoint``,
in layout units.

A list of pasted items is returned.

.. seealso:: copySelectedItems()
.. seealso:: hasItemsInClipboard()
:rtype: list of QgsLayoutItem
Expand Down
5 changes: 3 additions & 2 deletions src/app/layout/qgslayoutappmenuprovider.cpp
Expand Up @@ -94,9 +94,10 @@ QMenu *QgsLayoutAppMenuProvider::createContextMenu( QWidget *parent, QgsLayout *
else if ( mDesigner->view()->hasItemsInClipboard() )
{
QAction *pasteAction = new QAction( tr( "Paste" ), menu );
connect( pasteAction, &QAction::triggered, this, [this]()
connect( pasteAction, &QAction::triggered, this, [this, menu]()
{
mDesigner->paste();
QPointF pt = mDesigner->view()->mapToScene( mDesigner->view()->mapFromGlobal( menu->pos() ) );
mDesigner->view()->pasteItems( pt );
} );
menu->addAction( pasteAction );
menu->addSeparator();
Expand Down
20 changes: 20 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -369,6 +369,26 @@ QList< QgsLayoutItem * > QgsLayoutView::pasteItems( QgsLayoutView::PasteMode mod
return pastedItems;
}

QList<QgsLayoutItem *> QgsLayoutView::pasteItems( QPointF layoutPoint )
{
QList< QgsLayoutItem * > pastedItems;
QDomDocument doc;
QClipboard *clipboard = QApplication::clipboard();
if ( doc.setContent( clipboard->mimeData()->data( QStringLiteral( "text/xml" ) ) ) )
{
QDomElement docElem = doc.documentElement();
if ( docElem.tagName() == QLatin1String( "LayoutItemClipboard" ) )
{
currentLayout()->undoStack()->beginMacro( tr( "Paste Items" ) );
currentLayout()->undoStack()->beginCommand( currentLayout(), tr( "Paste Items" ) );
pastedItems = currentLayout()->addItemsFromXml( docElem, doc, QgsReadWriteContext(), &layoutPoint, false );
currentLayout()->undoStack()->endCommand();
currentLayout()->undoStack()->endMacro();
}
}
return pastedItems;
}

bool QgsLayoutView::hasItemsInClipboard() const
{
QDomDocument doc;
Expand Down
11 changes: 11 additions & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -247,6 +247,17 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/
QList< QgsLayoutItem * > pasteItems( PasteMode mode );

/**
* Pastes items from clipboard, at the specified \a layoutPoint,
* in layout units.
*
* A list of pasted items is returned.
*
* \see copySelectedItems()
* \see hasItemsInClipboard()
*/
QList< QgsLayoutItem * > pasteItems( QPointF layoutPoint );

/**
* Returns true if the current clipboard contains layout items.
* \see pasteItems()
Expand Down

0 comments on commit 17656f2

Please sign in to comment.