QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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 "moc_qgslayoutviewtooladditem.cpp"
18#include "qgslayoutview.h"
19#include "qgslayout.h"
22#include "qgsgui.h"
25#include "qgssettings.h"
26#include "qgslayoutundostack.h"
27#include <QGraphicsRectItem>
28#include <QPen>
29#include <QBrush>
30#include <QMouseEvent>
31
38
40{
41 mItemMetadataId = metadataId;
42}
43
45{
46 if ( event->button() != Qt::LeftButton )
47 {
48 event->ignore();
49 return;
50 }
51
52 mDrawing = true;
53 mMousePressStartPos = event->pos();
54 mMousePressStartLayoutPos = event->layoutPoint();
55 mRubberBand.reset( QgsGui::layoutItemGuiRegistry()->createItemRubberBand( mItemMetadataId, view() ) );
56 if ( mRubberBand )
57 {
58 connect( mRubberBand.get(), &QgsLayoutViewRubberBand::sizeChanged, this, [ = ]( const QString & size )
59 {
60 view()->pushStatusMessage( size );
61 } );
62 mRubberBand->start( event->snappedPoint(), event->modifiers() );
63 }
64}
65
67{
68 if ( mDrawing && mRubberBand )
69 {
70 mRubberBand->update( event->snappedPoint(), event->modifiers() );
71 }
72 else
73 {
74 event->ignore();
75 }
76}
77
79{
80 if ( event->button() != Qt::LeftButton || !mDrawing )
81 {
82 event->ignore();
83 return;
84 }
85 mDrawing = false;
86
87 const QRectF rect = mRubberBand ? mRubberBand->finish( event->snappedPoint(), event->modifiers() ) : QRectF();
88
89 QString undoText;
90 if ( QgsLayoutItemAbstractGuiMetadata *metadata = QgsGui::layoutItemGuiRegistry()->itemMetadata( mItemMetadataId ) )
91 {
92 undoText = tr( "Create %1" ).arg( metadata->visibleName() );
93 }
94 else
95 {
96 undoText = tr( "Create Item" );
97 }
98 layout()->undoStack()->beginMacro( undoText );
99
100 QgsLayoutItem *item = QgsGui::layoutItemGuiRegistry()->createItem( mItemMetadataId, layout() );
101 if ( !item )
102 {
103 layout()->undoStack()->endMacro();
104 return;
105 }
106
107 // click? or click-and-drag?
108 const bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
109 if ( clickOnly && mRubberBand )
110 {
112 dlg.setLayout( layout() );
113 dlg.setItemPosition( QgsLayoutPoint( event->snappedPoint(), layout()->units() ) );
114 if ( dlg.exec() )
115 {
116 item->setReferencePoint( dlg.referencePoint() );
117 item->attemptResize( dlg.itemSize() );
118 item->attemptMove( dlg.itemPosition(), true, false, dlg.page() );
119 }
120 else
121 {
122 delete item;
123 layout()->undoStack()->endMacro();
124 return;
125 }
126 }
127 else if ( mRubberBand )
128 {
129 item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), Qgis::LayoutUnit::Millimeters ) );
130 item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), Qgis::LayoutUnit::Millimeters ) );
131 }
132 else
133 {
134 // item type doesn't use rubber bands -- e.g. marker items
135 item->attemptMove( QgsLayoutPoint( mMousePressStartLayoutPos, layout()->units() ) );
136 }
137
138 // record last created item size
139 if ( mRubberBand )
140 {
141 QgsSettings settings;
142 settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
143 settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
144 settings.setEnumValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), item->sizeWithUnits().units() );
145 }
146
147 QgsGui::layoutItemGuiRegistry()->newItemAddedToLayout( mItemMetadataId, item, mCustomProperties );
148
149 // it's possible (in certain circumstances, e.g. when adding frame items) that this item
150 // has already been added to the layout
151 if ( item->scene() != layout() )
152 layout()->addLayoutItem( item );
153 layout()->setSelectedItem( item );
154
155 layout()->undoStack()->endMacro();
156 emit createdItem();
157}
158
160{
162 mCustomProperties.clear();
163}
164
166{
167 if ( mDrawing )
168 {
169 // canceled mid operation
170 if ( mRubberBand )
171 mRubberBand->finish();
172 mDrawing = false;
173 }
175}
176
178{
179 return mCustomProperties;
180}
181
182void QgsLayoutViewToolAddItem::setCustomProperties( const QVariantMap &customProperties )
183{
184 mCustomProperties = customProperties;
185}
186
188{
189 return mItemMetadataId;
190}
@ Millimeters
Millimeters.
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Definition qgsgui.cpp:139
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...
double height() const
Returns the height of the size.
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.
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.
void setSelectedItem(QgsLayoutItem *item)
Clears any selected items and sets item as the current selection.
void addLayoutItem(QgsLayoutItem *item)
Adds an item to the layout.
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
void setEnumValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on an enum.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.