QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 ) { view()->pushStatusMessage( size ); } );
65 mRubberBand->start( event->snappedPoint(), event->modifiers() );
66 }
67}
68
70{
71 if ( mDrawing && mRubberBand )
72 {
73 mRubberBand->update( event->snappedPoint(), event->modifiers() );
74 }
75 else
76 {
77 event->ignore();
78 }
79}
80
82{
83 if ( event->button() != Qt::LeftButton || !mDrawing )
84 {
85 event->ignore();
86 return;
87 }
88 mDrawing = false;
89
90 const QRectF rect = mRubberBand ? mRubberBand->finish( event->snappedPoint(), event->modifiers() ) : QRectF();
91
92 QString undoText;
93 if ( QgsLayoutItemAbstractGuiMetadata *metadata = QgsGui::layoutItemGuiRegistry()->itemMetadata( mItemMetadataId ) )
94 {
95 undoText = tr( "Create %1" ).arg( metadata->visibleName() );
96 }
97 else
98 {
99 undoText = tr( "Create Item" );
100 }
101 layout()->undoStack()->beginMacro( undoText );
102
103 QgsLayoutItem *item = QgsGui::layoutItemGuiRegistry()->createItem( mItemMetadataId, layout() );
104 if ( !item )
105 {
106 layout()->undoStack()->endMacro();
107 return;
108 }
109
110 // click? or click-and-drag?
111 const bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
112 if ( clickOnly && mRubberBand )
113 {
115 dlg.setLayout( layout() );
116 dlg.setItemPosition( QgsLayoutPoint( event->snappedPoint(), layout()->units() ) );
117 if ( dlg.exec() )
118 {
119 item->setReferencePoint( dlg.referencePoint() );
120 item->attemptResize( dlg.itemSize() );
121 item->attemptMove( dlg.itemPosition(), true, false, dlg.page() );
122 }
123 else
124 {
125 delete item;
126 layout()->undoStack()->endMacro();
127 return;
128 }
129 }
130 else if ( mRubberBand )
131 {
132 item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), Qgis::LayoutUnit::Millimeters ) );
133 item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), Qgis::LayoutUnit::Millimeters ) );
134 }
135 else
136 {
137 // item type doesn't use rubber bands -- e.g. marker items
138 item->attemptMove( QgsLayoutPoint( mMousePressStartLayoutPos, layout()->units() ) );
139 }
140
141 // record last created item size
142 if ( mRubberBand )
143 {
144 QgsSettings settings;
145 settings.setValue( u"LayoutDesigner/lastItemWidth"_s, item->sizeWithUnits().width() );
146 settings.setValue( u"LayoutDesigner/lastItemHeight"_s, item->sizeWithUnits().height() );
147 settings.setEnumValue( u"LayoutDesigner/lastSizeUnit"_s, item->sizeWithUnits().units() );
148 }
149
150 QgsGui::layoutItemGuiRegistry()->newItemAddedToLayout( mItemMetadataId, item, mCustomProperties );
151
152 // it's possible (in certain circumstances, e.g. when adding frame items) that this item
153 // has already been added to the layout
154 if ( item->scene() != layout() )
155 layout()->addLayoutItem( item );
156 layout()->setSelectedItem( item );
157
158 layout()->undoStack()->endMacro();
159 emit createdItem();
160}
161
163{
165 mCustomProperties.clear();
166}
167
169{
170 if ( mDrawing )
171 {
172 // canceled mid operation
173 if ( mRubberBand )
174 mRubberBand->finish();
175 mDrawing = false;
176 }
178}
179
181{
182 return mCustomProperties;
183}
184
186{
187 mCustomProperties = customProperties;
188}
189
191{
192 return mItemMetadataId;
193}
@ Millimeters
Millimeters.
Definition qgis.h:5361
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.