QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgsgui.h"
19#include "qgslayout.h"
22#include "qgslayoutundostack.h"
23#include "qgslayoutview.h"
26#include "qgssettings.h"
27
28#include <QBrush>
29#include <QGraphicsRectItem>
30#include <QMouseEvent>
31#include <QPen>
32#include <QString>
33
34#include "moc_qgslayoutviewtooladditem.cpp"
35
36using namespace Qt::StringLiterals;
37
44
46{
47 mItemMetadataId = metadataId;
48}
49
51{
52 if ( event->button() != Qt::LeftButton )
53 {
54 event->ignore();
55 return;
56 }
57
58 mDrawing = true;
59 mMousePressStartPos = event->pos();
60 mMousePressStartLayoutPos = event->layoutPoint();
61 mRubberBand.reset( QgsGui::layoutItemGuiRegistry()->createItemRubberBand( mItemMetadataId, view() ) );
62 if ( mRubberBand )
63 {
64 connect( mRubberBand.get(), &QgsLayoutViewRubberBand::sizeChanged, this, [this]( const QString &size ) {
65 view()->pushStatusMessage( size );
66 } );
67 mRubberBand->start( event->snappedPoint(), event->modifiers() );
68 }
69}
70
72{
73 if ( mDrawing && mRubberBand )
74 {
75 mRubberBand->update( event->snappedPoint(), event->modifiers() );
76 }
77 else
78 {
79 event->ignore();
80 }
81}
82
84{
85 if ( event->button() != Qt::LeftButton || !mDrawing )
86 {
87 event->ignore();
88 return;
89 }
90 mDrawing = false;
91
92 const QRectF rect = mRubberBand ? mRubberBand->finish( event->snappedPoint(), event->modifiers() ) : QRectF();
93
94 QString undoText;
95 if ( QgsLayoutItemAbstractGuiMetadata *metadata = QgsGui::layoutItemGuiRegistry()->itemMetadata( mItemMetadataId ) )
96 {
97 undoText = tr( "Create %1" ).arg( metadata->visibleName() );
98 }
99 else
100 {
101 undoText = tr( "Create Item" );
102 }
103 layout()->undoStack()->beginMacro( undoText );
104
105 QgsLayoutItem *item = QgsGui::layoutItemGuiRegistry()->createItem( mItemMetadataId, layout() );
106 if ( !item )
107 {
108 layout()->undoStack()->endMacro();
109 return;
110 }
111
112 // click? or click-and-drag?
113 const bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
114 if ( clickOnly && mRubberBand )
115 {
117 dlg.setLayout( layout() );
118 dlg.setItemPosition( QgsLayoutPoint( event->snappedPoint(), layout()->units() ) );
119 if ( dlg.exec() )
120 {
121 item->setReferencePoint( dlg.referencePoint() );
122 item->attemptResize( dlg.itemSize() );
123 item->attemptMove( dlg.itemPosition(), true, false, dlg.page() );
124 }
125 else
126 {
127 delete item;
128 layout()->undoStack()->endMacro();
129 return;
130 }
131 }
132 else if ( mRubberBand )
133 {
134 item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), Qgis::LayoutUnit::Millimeters ) );
135 item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), Qgis::LayoutUnit::Millimeters ) );
136 }
137 else
138 {
139 // item type doesn't use rubber bands -- e.g. marker items
140 item->attemptMove( QgsLayoutPoint( mMousePressStartLayoutPos, layout()->units() ) );
141 }
142
143 // record last created item size
144 if ( mRubberBand )
145 {
146 QgsSettings settings;
147 settings.setValue( u"LayoutDesigner/lastItemWidth"_s, item->sizeWithUnits().width() );
148 settings.setValue( u"LayoutDesigner/lastItemHeight"_s, item->sizeWithUnits().height() );
149 settings.setEnumValue( u"LayoutDesigner/lastSizeUnit"_s, item->sizeWithUnits().units() );
150 }
151
152 QgsGui::layoutItemGuiRegistry()->newItemAddedToLayout( mItemMetadataId, item, mCustomProperties );
153
154 // it's possible (in certain circumstances, e.g. when adding frame items) that this item
155 // has already been added to the layout
156 if ( item->scene() != layout() )
157 layout()->addLayoutItem( item );
158 layout()->setSelectedItem( item );
159
160 layout()->undoStack()->endMacro();
161 emit createdItem();
162}
163
165{
167 mCustomProperties.clear();
168}
169
171{
172 if ( mDrawing )
173 {
174 // canceled mid operation
175 if ( mRubberBand )
176 mRubberBand->finish();
177 mDrawing = false;
178 }
180}
181
183{
184 return mCustomProperties;
185}
186
188{
189 mCustomProperties = customProperties;
190}
191
193{
194 return mItemMetadataId;
195}
@ Millimeters
Millimeters.
Definition qgis.h:5276
static QgsLayoutItemGuiRegistry * layoutItemGuiRegistry()
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
Definition qgsgui.cpp:154
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.
Provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
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 mouse event which is the result of a user interaction with 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.
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.
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.
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 ...
Stores settings for use within QGIS.
Definition qgssettings.h:68
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.