QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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
17#include "moc_qgslayoutviewtoolmoveitemcontent.cpp"
19#include "qgslayoutview.h"
20#include "qgslayout.h"
22#include "qgssettings.h"
23#include "qgslayoutundostack.h"
24
26 : QgsLayoutViewTool( view, tr( "Select" ) )
27{
28 setCursor( Qt::ArrowCursor );
29}
30
32{
33 if ( event->button() != Qt::LeftButton )
34 {
35 event->ignore();
36 return;
37 }
38
39 const QList<QGraphicsItem *> itemsAtCursorPos = view()->items( event->pos() );
40 if ( itemsAtCursorPos.isEmpty() )
41 return;
42
43 //find highest non-locked QgsLayoutItem at clicked position
44 //(other graphics items may be higher, e.g., selection handles)
45 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
46 {
47 QgsLayoutItem *item = dynamic_cast<QgsLayoutItem *>( graphicsItem );
48 if ( item && !item->isLocked() )
49 {
50 //we've found the highest QgsLayoutItem
51 mMoveContentStartPos = event->layoutPoint();
52 mMoveContentItem = item;
53 mMovingItemContent = true;
54 break;
55 }
56 }
57}
58
60{
61 if ( !mMovingItemContent || !mMoveContentItem )
62 {
63 event->ignore();
64 return;
65 }
66
67 //update item preview
68 mMoveContentItem->setMoveContentPreviewOffset( event->layoutPoint().x() - mMoveContentStartPos.x(),
69 event->layoutPoint().y() - mMoveContentStartPos.y() );
70 mMoveContentItem->update();
71}
72
74{
75 if ( event->button() != Qt::LeftButton || !mMovingItemContent || !mMoveContentItem )
76 {
77 event->ignore();
78 return;
79 }
80
81 //update item preview
82 mMoveContentItem->setMoveContentPreviewOffset( 0, 0 );
83
84 const double moveX = event->layoutPoint().x() - mMoveContentStartPos.x();
85 const double moveY = event->layoutPoint().y() - mMoveContentStartPos.y();
86
87 mMoveContentItem->layout()->undoStack()->beginCommand( mMoveContentItem, tr( "Move Item Content" ) );
88 mMoveContentItem->moveContent( -moveX, -moveY );
89 mMoveContentItem->layout()->undoStack()->endCommand();
90 mMoveContentItem = nullptr;
91 mMovingItemContent = false;
92}
93
95{
96 event->accept();
97
98 const QPointF scenePoint = view()->mapToScene( event->position().x(), event->position().y() );
99
100 //select topmost item at position of event
101 QgsLayoutItem *item = layout()->layoutItemAt( scenePoint, true );
102 if ( !item || !item->isSelected() )
103 return;
104
105 const QgsSettings settings;
106 double zoomFactor = settings.value( QStringLiteral( "qgis/zoom_factor" ), 2.0 ).toDouble();
107 bool reverseZoom = settings.value( QStringLiteral( "qgis/reverse_wheel_zoom" ), false ).toBool();
108 bool zoomIn = reverseZoom ? event->angleDelta().y() < 0 : event->angleDelta().y() > 0;
109
110 // "Normal" mouse have an angle delta of 120, precision mouses provide data faster, in smaller steps
111 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 120.0 * std::fabs( event->angleDelta().y() );
112
113 if ( event->modifiers() & Qt::ControlModifier )
114 {
115 //holding ctrl while wheel zooming results in a finer zoom
116 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.0;
117 }
118
119 //calculate zoom scale factor
120 const double scaleFactor = ( zoomIn ? zoomFactor : 1 / zoomFactor );
121
122 const QPointF itemPoint = item->mapFromScene( scenePoint );
123 item->layout()->undoStack()->beginCommand( item, tr( "Zoom Item Content" ), QgsLayoutItem::UndoZoomContent );
124 item->zoomContent( scaleFactor, itemPoint );
125 item->layout()->undoStack()->endCommand();
126}
Base class for graphical items within a QgsLayout.
@ UndoZoomContent
Item content zoomed.
virtual void zoomContent(double factor, QPointF point)
Zooms content of item.
bool isLocked() const
Returns true if the item is locked, and cannot be interacted with using the mouse.
virtual void moveContent(double dx, double dy)
Moves the content of the item, by a specified dx and dy in layout units.
virtual void setMoveContentPreviewOffset(double dx, double dy)
Sets temporary offset for the item, by a specified dx and dy in layout units.
const QgsLayout * layout() const
Returns the layout the object is attached to.
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
A QgsLayoutViewMouseEvent is the result of a user interaction with the mouse on a QgsLayoutView.
QPointF layoutPoint() const
Returns the event point location in layout coordinates.
void wheelEvent(QWheelEvent *event) override
Mouse wheel event for overriding.
QgsLayoutViewToolMoveItemContent(QgsLayoutView *view)
Constructor for QgsLayoutViewToolMoveItemContent.
void layoutMoveEvent(QgsLayoutViewMouseEvent *event) override
Mouse move event for overriding.
void layoutPressEvent(QgsLayoutViewMouseEvent *event) override
Mouse press event for overriding.
void layoutReleaseEvent(QgsLayoutViewMouseEvent *event) override
Mouse release event for overriding.
Abstract base class for all layout view tools.
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
QgsLayoutView * view() const
Returns the view associated with the tool.
QgsLayout * layout() const
Returns the layout associated with the tool.
A graphical widget to display and interact with QgsLayouts.
QgsLayoutItem * layoutItemAt(QPointF position, bool ignoreLocked=false, double searchTolerance=0) const
Returns the topmost layout item at a specified position.
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.