QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgslayoutviewtooladditem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutviewtooladditem.cpp
3 ----------------------------
4 Date : July 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 "qgslayoutview.h"
18#include "qgslayout.h"
21#include "qgsgui.h"
24#include "qgssettings.h"
25#include "qgslayoutundostack.h"
26#include <QGraphicsRectItem>
27#include <QPen>
28#include <QBrush>
29#include <QMouseEvent>
30
32 : QgsLayoutViewTool( view, tr( "Add item" ) )
33{
35 setCursor( Qt::CrossCursor );
36}
37
39{
40 mItemMetadataId = metadataId;
41}
42
44{
45 if ( event->button() != Qt::LeftButton )
46 {
47 event->ignore();
48 return;
49 }
50
51 mDrawing = true;
52 mMousePressStartPos = event->pos();
53 mMousePressStartLayoutPos = event->layoutPoint();
54 mRubberBand.reset( QgsGui::layoutItemGuiRegistry()->createItemRubberBand( mItemMetadataId, view() ) );
55 if ( mRubberBand )
56 {
57 connect( mRubberBand.get(), &QgsLayoutViewRubberBand::sizeChanged, this, [ = ]( const QString & size )
58 {
59 view()->pushStatusMessage( size );
60 } );
61 mRubberBand->start( event->snappedPoint(), event->modifiers() );
62 }
63}
64
66{
67 if ( mDrawing && mRubberBand )
68 {
69 mRubberBand->update( event->snappedPoint(), event->modifiers() );
70 }
71 else
72 {
73 event->ignore();
74 }
75}
76
78{
79 if ( event->button() != Qt::LeftButton || !mDrawing )
80 {
81 event->ignore();
82 return;
83 }
84 mDrawing = false;
85
86 const QRectF rect = mRubberBand ? mRubberBand->finish( event->snappedPoint(), event->modifiers() ) : QRectF();
87
88 QString undoText;
89 if ( QgsLayoutItemAbstractGuiMetadata *metadata = QgsGui::layoutItemGuiRegistry()->itemMetadata( mItemMetadataId ) )
90 {
91 undoText = tr( "Create %1" ).arg( metadata->visibleName() );
92 }
93 else
94 {
95 undoText = tr( "Create Item" );
96 }
97 layout()->undoStack()->beginMacro( undoText );
98
99 QgsLayoutItem *item = QgsGui::layoutItemGuiRegistry()->createItem( mItemMetadataId, layout() );
100 if ( !item )
101 {
102 layout()->undoStack()->endMacro();
103 return;
104 }
105
106 // click? or click-and-drag?
107 const bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
108 if ( clickOnly && mRubberBand )
109 {
111 dlg.setLayout( layout() );
112 dlg.setItemPosition( QgsLayoutPoint( event->snappedPoint(), layout()->units() ) );
113 if ( dlg.exec() )
114 {
115 item->setReferencePoint( dlg.referencePoint() );
116 item->attemptResize( dlg.itemSize() );
117 item->attemptMove( dlg.itemPosition(), true, false, dlg.page() );
118 }
119 else
120 {
121 delete item;
122 layout()->undoStack()->endMacro();
123 return;
124 }
125 }
126 else if ( mRubberBand )
127 {
128 item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), Qgis::LayoutUnit::Millimeters ) );
129 item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), Qgis::LayoutUnit::Millimeters ) );
130 }
131 else
132 {
133 // item type doesn't use rubber bands -- e.g. marker items
134 item->attemptMove( QgsLayoutPoint( mMousePressStartLayoutPos, layout()->units() ) );
135 }
136
137 // record last created item size
138 if ( mRubberBand )
139 {
140 QgsSettings settings;
141 settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
142 settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
143 settings.setEnumValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), item->sizeWithUnits().units() );
144 }
145
146 QgsGui::layoutItemGuiRegistry()->newItemAddedToLayout( mItemMetadataId, item, mCustomProperties );
147
148 // it's possible (in certain circumstances, e.g. when adding frame items) that this item
149 // has already been added to the layout
150 if ( item->scene() != layout() )
151 layout()->addLayoutItem( item );
152 layout()->setSelectedItem( item );
153
154 layout()->undoStack()->endMacro();
155 emit createdItem();
156}
157
159{
161 mCustomProperties.clear();
162}
163
165{
166 if ( mDrawing )
167 {
168 // canceled mid operation
169 if ( mRubberBand )
170 mRubberBand->finish();
171 mDrawing = false;
172 }
174}
175
177{
178 return mCustomProperties;
179}
180
181void QgsLayoutViewToolAddItem::setCustomProperties( const QVariantMap &customProperties )
182{
183 mCustomProperties = customProperties;
184}
185
187{
188 return mItemMetadataId;
189}
@ Millimeters
Millimeters.
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Definition: qgsgui.cpp:134
Stores GUI metadata about one layout item class.
void newItemAddedToLayout(int metadataId, QgsLayoutItem *item, const QVariantMap &properties=QVariantMap())
Called when a newly created item of the associated metadata metadataId has been added to a layout.
QgsLayoutItem * createItem(int metadataId, QgsLayout *layout) const
Creates a new instance of a layout item given the item metadata metadataId, target layout.
A dialog for configuring properties like the size and position of layout items.
QgsLayoutSize itemSize() const
Returns the item size defined by the dialog.
QgsLayoutItem::ReferencePoint referencePoint() const
Returns the item reference point defined by the dialog.
void setLayout(QgsLayout *layout)
Sets the layout associated with the dialog.
QgsLayoutPoint itemPosition() const
Returns the current item position defined by the dialog.
int page() const
Returns the page number for the new item.
void setItemPosition(QgsLayoutPoint position)
Sets the item position to show in the dialog.
Base class for graphical items within a QgsLayout.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
virtual void attemptMove(const QgsLayoutPoint &point, bool useReferencePoint=true, bool includesFrame=false, int page=-1)
Attempts to move the item to a specified point.
void setReferencePoint(ReferencePoint point)
Sets the reference point for positioning of the layout item.
This class provides a method of storing points, consisting of an x and y coordinate,...
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:89
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:75
void beginMacro(const QString &commandText)
Starts a macro command, with the given descriptive commandText.
void endMacro()
Ends a macro command.
A QgsLayoutViewMouseEvent is the result of a user interaction with the mouse on a QgsLayoutView.
QPointF snappedPoint() const
Returns the snapped event point location in layout coordinates.
void sizeChanged(const QString &size)
Emitted when the size of the rubber band is changed.
void layoutMoveEvent(QgsLayoutViewMouseEvent *event) override
Mouse move event for overriding.
QVariantMap customProperties() const
Returns any custom properties set for the tool.
int itemMetadataId() const
Returns the item metadata id for items created by the tool.
QgsLayoutViewToolAddItem(QgsLayoutView *view)
Constructs a QgsLayoutViewToolAddItem for the given layout view.
void activate() override
Called when tool is set as the currently active layout tool.
void deactivate() override
Called when tool is deactivated.
void layoutPressEvent(QgsLayoutViewMouseEvent *event) override
Mouse press event for overriding.
void layoutReleaseEvent(QgsLayoutViewMouseEvent *event) override
Mouse release event for overriding.
void createdItem()
Emitted when an item has been created using the tool.
void setItemMetadataId(int metadataId)
Sets the item metadata metadataId for items created by the tool.
void setCustomProperties(const QVariantMap &properties)
Sets custom properties for the tool.
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.
virtual void deactivate()
Called when tool is deactivated.
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
void setFlags(QgsLayoutViewTool::Flags flags)
Sets the combination of flags that will be used for the tool.
@ FlagSnaps
Tool utilizes snapped coordinates.
virtual void activate()
Called when tool is set as the currently active layout tool.
QgsLayout * layout() const
Returns the layout associated with the tool.
A graphical widget to display and interact with QgsLayouts.
Definition: qgslayoutview.h:50
void setSelectedItem(QgsLayoutItem *item)
Clears any selected items and sets item as the current selection.
Definition: qgslayout.cpp:168
void addLayoutItem(QgsLayoutItem *item)
Adds an item to the layout.
Definition: qgslayout.cpp:557
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:703
This class is a composition of two QSettings instances:
Definition: qgssettings.h:64
void setEnumValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on an enum.
Definition: qgssettings.h:315
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.