Skip to content

Commit 17656f2

Browse files
committedDec 6, 2017
Pasting items from layout context menu pastes them at menu origin
instead of final cursor position
1 parent ac6c131 commit 17656f2

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed
 

‎python/gui/layout/qgslayoutview.sip

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ class QgsLayoutView: QGraphicsView
212212

213213
A list of pasted items is returned.
214214

215+
.. seealso:: copySelectedItems()
216+
.. seealso:: hasItemsInClipboard()
217+
:rtype: list of QgsLayoutItem
218+
%End
219+
220+
QList< QgsLayoutItem * > pasteItems( QPointF layoutPoint );
221+
%Docstring
222+
Pastes items from clipboard, at the specified ``layoutPoint``,
223+
in layout units.
224+
225+
A list of pasted items is returned.
226+
215227
.. seealso:: copySelectedItems()
216228
.. seealso:: hasItemsInClipboard()
217229
:rtype: list of QgsLayoutItem

‎src/app/layout/qgslayoutappmenuprovider.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ QMenu *QgsLayoutAppMenuProvider::createContextMenu( QWidget *parent, QgsLayout *
9494
else if ( mDesigner->view()->hasItemsInClipboard() )
9595
{
9696
QAction *pasteAction = new QAction( tr( "Paste" ), menu );
97-
connect( pasteAction, &QAction::triggered, this, [this]()
97+
connect( pasteAction, &QAction::triggered, this, [this, menu]()
9898
{
99-
mDesigner->paste();
99+
QPointF pt = mDesigner->view()->mapToScene( mDesigner->view()->mapFromGlobal( menu->pos() ) );
100+
mDesigner->view()->pasteItems( pt );
100101
} );
101102
menu->addAction( pasteAction );
102103
menu->addSeparator();

‎src/gui/layout/qgslayoutview.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,26 @@ QList< QgsLayoutItem * > QgsLayoutView::pasteItems( QgsLayoutView::PasteMode mod
369369
return pastedItems;
370370
}
371371

372+
QList<QgsLayoutItem *> QgsLayoutView::pasteItems( QPointF layoutPoint )
373+
{
374+
QList< QgsLayoutItem * > pastedItems;
375+
QDomDocument doc;
376+
QClipboard *clipboard = QApplication::clipboard();
377+
if ( doc.setContent( clipboard->mimeData()->data( QStringLiteral( "text/xml" ) ) ) )
378+
{
379+
QDomElement docElem = doc.documentElement();
380+
if ( docElem.tagName() == QLatin1String( "LayoutItemClipboard" ) )
381+
{
382+
currentLayout()->undoStack()->beginMacro( tr( "Paste Items" ) );
383+
currentLayout()->undoStack()->beginCommand( currentLayout(), tr( "Paste Items" ) );
384+
pastedItems = currentLayout()->addItemsFromXml( docElem, doc, QgsReadWriteContext(), &layoutPoint, false );
385+
currentLayout()->undoStack()->endCommand();
386+
currentLayout()->undoStack()->endMacro();
387+
}
388+
}
389+
return pastedItems;
390+
}
391+
372392
bool QgsLayoutView::hasItemsInClipboard() const
373393
{
374394
QDomDocument doc;

‎src/gui/layout/qgslayoutview.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
247247
*/
248248
QList< QgsLayoutItem * > pasteItems( PasteMode mode );
249249

250+
/**
251+
* Pastes items from clipboard, at the specified \a layoutPoint,
252+
* in layout units.
253+
*
254+
* A list of pasted items is returned.
255+
*
256+
* \see copySelectedItems()
257+
* \see hasItemsInClipboard()
258+
*/
259+
QList< QgsLayoutItem * > pasteItems( QPointF layoutPoint );
260+
250261
/**
251262
* Returns true if the current clipboard contains layout items.
252263
* \see pasteItems()

0 commit comments

Comments
 (0)
Please sign in to comment.