QGIS API Documentation 3.99.0-Master (357b655ed83)
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
18#include "qgslayout.h"
20#include "qgslayoutundostack.h"
21#include "qgslayoutview.h"
23#include "qgssettings.h"
24
25#include <QString>
26
27#include "moc_qgslayoutviewtoolmoveitemcontent.cpp"
28
29using namespace Qt::StringLiterals;
30
36
38{
39 if ( event->button() != Qt::LeftButton )
40 {
41 event->ignore();
42 return;
43 }
44
45 const QList<QGraphicsItem *> itemsAtCursorPos = view()->items( event->pos() );
46 if ( itemsAtCursorPos.isEmpty() )
47 return;
48
49 //find highest non-locked QgsLayoutItem at clicked position
50 //(other graphics items may be higher, e.g., selection handles)
51 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
52 {
53 QgsLayoutItem *item = dynamic_cast<QgsLayoutItem *>( graphicsItem );
54 if ( item && !item->isLocked() )
55 {
56 //we've found the highest QgsLayoutItem
57 mMoveContentStartPos = event->layoutPoint();
58 mMoveContentItem = item;
59 mMovingItemContent = true;
60 break;
61 }
62 }
63}
64
66{
67 if ( !mMovingItemContent || !mMoveContentItem )
68 {
69 event->ignore();
70 return;
71 }
72
73 //update item preview
74 mMoveContentItem->setMoveContentPreviewOffset( event->layoutPoint().x() - mMoveContentStartPos.x(), event->layoutPoint().y() - mMoveContentStartPos.y() );
75 mMoveContentItem->update();
76}
77
79{
80 if ( event->button() != Qt::LeftButton || !mMovingItemContent || !mMoveContentItem )
81 {
82 event->ignore();
83 return;
84 }
85
86 //update item preview
87 mMoveContentItem->setMoveContentPreviewOffset( 0, 0 );
88
89 const double moveX = event->layoutPoint().x() - mMoveContentStartPos.x();
90 const double moveY = event->layoutPoint().y() - mMoveContentStartPos.y();
91
92 mMoveContentItem->layout()->undoStack()->beginCommand( mMoveContentItem, tr( "Move Item Content" ) );
93 mMoveContentItem->moveContent( -moveX, -moveY );
94 mMoveContentItem->layout()->undoStack()->endCommand();
95 mMoveContentItem = nullptr;
96 mMovingItemContent = false;
97}
98
100{
101 event->accept();
102
103 const QPointF scenePoint = view()->mapToScene( event->position().x(), event->position().y() );
104
105 //select topmost item at position of event
106 QgsLayoutItem *item = layout()->layoutItemAt( scenePoint, true );
107 if ( !item || !item->isSelected() )
108 return;
109
110 const QgsSettings settings;
111 double zoomFactor = settings.value( u"qgis/zoom_factor"_s, 2.0 ).toDouble();
112 bool reverseZoom = settings.value( u"qgis/reverse_wheel_zoom"_s, false ).toBool();
113 bool zoomIn = reverseZoom ? event->angleDelta().y() < 0 : event->angleDelta().y() > 0;
114
115 // "Normal" mouse have an angle delta of 120, precision mouses provide data faster, in smaller steps
116 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 120.0 * std::fabs( event->angleDelta().y() );
117
118 if ( event->modifiers() & Qt::ControlModifier )
119 {
120 //holding ctrl while wheel zooming results in a finer zoom
121 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.0;
122 }
123
124 //calculate zoom scale factor
125 const double scaleFactor = ( zoomIn ? zoomFactor : 1 / zoomFactor );
126
127 const QPointF itemPoint = item->mapFromScene( scenePoint );
128 item->layout()->undoStack()->beginCommand( item, tr( "Zoom Item Content" ), QgsLayoutItem::UndoZoomContent );
129 item->zoomContent( scaleFactor, itemPoint );
130 item->layout()->undoStack()->endCommand();
131}
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.
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 mouse event which is the result of a user interaction with 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.
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.
QgsLayoutViewTool(QgsLayoutView *view, const QString &name)
Constructor for QgsLayoutViewTool, taking a layout view and tool name as parameters.
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 ...
Stores settings for use within QGIS.
Definition qgssettings.h:68
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.