QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgslayoutviewtoolmoveitemcontent.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutviewtoolmoveitemcontent.cpp
3  ------------------------------------
4  Date : October 2017
5  Copyright : (C) 2017 Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
18 #include "qgslayoutview.h"
19 #include "qgslayout.h"
20 #include "qgslayoutitemnodeitem.h"
21 #include "qgssettings.h"
22 #include "qgslayoutundostack.h"
23 
25  : QgsLayoutViewTool( view, tr( "Select" ) )
26 {
27  setCursor( Qt::ArrowCursor );
28 }
29 
31 {
32  if ( event->button() != Qt::LeftButton )
33  {
34  event->ignore();
35  return;
36  }
37 
38  const QList<QGraphicsItem *> itemsAtCursorPos = view()->items( event->pos() );
39  if ( itemsAtCursorPos.isEmpty() )
40  return;
41 
42  //find highest non-locked QgsLayoutItem at clicked position
43  //(other graphics items may be higher, e.g., selection handles)
44  for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
45  {
46  QgsLayoutItem *item = dynamic_cast<QgsLayoutItem *>( graphicsItem );
47  if ( item && !item->isLocked() )
48  {
49  //we've found the highest QgsLayoutItem
50  mMoveContentStartPos = event->layoutPoint();
51  mMoveContentItem = item;
52  mMovingItemContent = true;
53  break;
54  }
55  }
56 }
57 
59 {
60  if ( !mMovingItemContent || !mMoveContentItem )
61  {
62  event->ignore();
63  return;
64  }
65 
66  //update item preview
67  mMoveContentItem->setMoveContentPreviewOffset( event->layoutPoint().x() - mMoveContentStartPos.x(),
68  event->layoutPoint().y() - mMoveContentStartPos.y() );
69  mMoveContentItem->update();
70 }
71 
73 {
74  if ( event->button() != Qt::LeftButton || !mMovingItemContent || !mMoveContentItem )
75  {
76  event->ignore();
77  return;
78  }
79 
80  //update item preview
81  mMoveContentItem->setMoveContentPreviewOffset( 0, 0 );
82 
83  double moveX = event->layoutPoint().x() - mMoveContentStartPos.x();
84  double moveY = event->layoutPoint().y() - mMoveContentStartPos.y();
85 
86  mMoveContentItem->layout()->undoStack()->beginCommand( mMoveContentItem, tr( "Move Item Content" ) );
87  mMoveContentItem->moveContent( -moveX, -moveY );
88  mMoveContentItem->layout()->undoStack()->endCommand();
89  mMoveContentItem = nullptr;
90  mMovingItemContent = false;
91 }
92 
94 {
95  event->accept();
96 
97  QPointF scenePoint = view()->mapToScene( event->pos() );
98  //select topmost item at position of event
99  QgsLayoutItem *item = layout()->layoutItemAt( scenePoint, true );
100  if ( !item || !item->isSelected() )
101  return;
102 
103  QgsSettings settings;
104  double zoomFactor = settings.value( QStringLiteral( "qgis/zoom_factor" ), 2.0 ).toDouble();
105 
106  // "Normal" mouse have an angle delta of 120, precision mouses provide data faster, in smaller steps
107  zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 120.0 * std::fabs( event->angleDelta().y() );
108 
109  if ( event->modifiers() & Qt::ControlModifier )
110  {
111  //holding ctrl while wheel zooming results in a finer zoom
112  zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.0;
113  }
114 
115  //calculate zoom scale factor
116  bool zoomIn = event->angleDelta().y() > 0;
117  double scaleFactor = ( zoomIn ? zoomFactor : 1 / zoomFactor );
118 
119  QPointF itemPoint = item->mapFromScene( scenePoint );
120  item->layout()->undoStack()->beginCommand( item, tr( "Zoom Item Content" ), QgsLayoutItem::UndoZoomContent );
121  item->zoomContent( scaleFactor, itemPoint );
122  item->layout()->undoStack()->endCommand();
123 }
QgsLayoutViewToolMoveItemContent::layoutMoveEvent
void layoutMoveEvent(QgsLayoutViewMouseEvent *event) override
Mouse move event for overriding.
Definition: qgslayoutviewtoolmoveitemcontent.cpp:58
QgsLayoutObject::layout
const QgsLayout * layout() const
Returns the layout the object is attached to.
Definition: qgslayoutobject.cpp:118
qgslayoutviewtoolmoveitemcontent.h
qgslayoutundostack.h
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsLayoutViewToolMoveItemContent::wheelEvent
void wheelEvent(QWheelEvent *event) override
Mouse wheel event for overriding.
Definition: qgslayoutviewtoolmoveitemcontent.cpp:93
qgslayoutview.h
QgsLayoutItem::UndoZoomContent
@ UndoZoomContent
Item content zoomed.
Definition: qgslayoutitem.h:231
QgsSettings
Definition: qgssettings.h:61
QgsLayoutViewToolMoveItemContent::layoutReleaseEvent
void layoutReleaseEvent(QgsLayoutViewMouseEvent *event) override
Mouse release event for overriding.
Definition: qgslayoutviewtoolmoveitemcontent.cpp:72
qgslayoutviewmouseevent.h
qgslayoutitemnodeitem.h
QgsLayout::layoutItemAt
QgsLayoutItem * layoutItemAt(QPointF position, bool ignoreLocked=false) const
Returns the topmost layout item at a specified position.
Definition: qgslayout.cpp:292
QgsLayoutItem::zoomContent
virtual void zoomContent(double factor, QPointF point)
Zooms content of item.
Definition: qgslayoutitem.cpp:963
QgsLayoutViewTool::view
QgsLayoutView * view() const
Returns the view associated with the tool.
Definition: qgslayoutviewtool.cpp:38
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsLayoutViewTool::layout
QgsLayout * layout() const
Returns the layout associated with the tool.
Definition: qgslayoutviewtool.cpp:43
qgslayout.h
QgsLayoutUndoStack::endCommand
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
Definition: qgslayoutundostack.cpp:53
QgsLayoutViewTool
Definition: qgslayoutviewtool.h:46
qgssettings.h
QgsLayoutUndoStack::beginCommand
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
Definition: qgslayoutundostack.cpp:42
QgsLayoutView
Definition: qgslayoutview.h:49
QgsLayoutViewMouseEvent::layoutPoint
QPointF layoutPoint() const
Returns the event point location in layout coordinates.
Definition: qgslayoutviewmouseevent.cpp:44
QgsLayoutViewToolMoveItemContent::layoutPressEvent
void layoutPressEvent(QgsLayoutViewMouseEvent *event) override
Mouse press event for overriding.
Definition: qgslayoutviewtoolmoveitemcontent.cpp:30
QgsLayoutItem::moveContent
virtual void moveContent(double dx, double dy)
Moves the content of the item, by a specified dx and dy in layout units.
Definition: qgslayoutitem.cpp:953
QgsLayoutItem::setMoveContentPreviewOffset
virtual void setMoveContentPreviewOffset(double dx, double dy)
Sets temporary offset for the item, by a specified dx and dy in layout units.
Definition: qgslayoutitem.cpp:958
QgsLayoutViewTool::setCursor
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
Definition: qgslayoutviewtool.cpp:115
QgsLayoutViewMouseEvent
Definition: qgslayoutviewmouseevent.h:35
QgsLayoutViewToolMoveItemContent::QgsLayoutViewToolMoveItemContent
QgsLayoutViewToolMoveItemContent(QgsLayoutView *view)
Constructor for QgsLayoutViewToolMoveItemContent.
Definition: qgslayoutviewtoolmoveitemcontent.cpp:24
QgsLayoutItem::isLocked
bool isLocked() const
Returns true if the item is locked, and cannot be interacted with using the mouse.
Definition: qgslayoutitem.h:397
QgsLayout::undoStack
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
Definition: qgslayout.cpp:685