Skip to content

Commit

Permalink
Allow layout items to be created without rubber bands, e.g. so that
Browse files Browse the repository at this point in the history
marker items are placed with a single click instead of click-and-drag
  • Loading branch information
nyalldawson committed Apr 5, 2020
1 parent 632448c commit 0fe2ec2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/gui/layout/qgslayoutguiutils.cpp
Expand Up @@ -310,7 +310,7 @@ void QgsLayoutGuiUtils::registerGuiForKnownItemTypes( QgsMapCanvas *mapCanvas )
[ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
{
return new QgsLayoutMarkerWidget( qobject_cast< QgsLayoutItemMarker * >( item ) );
}, createRubberBand ) );
}, nullptr ) );

// arrow
std::unique_ptr< QgsLayoutItemGuiMetadata > arrowMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata>(
Expand Down
26 changes: 18 additions & 8 deletions src/gui/layout/qgslayoutviewtooladditem.cpp
Expand Up @@ -53,6 +53,7 @@ void QgsLayoutViewToolAddItem::layoutPressEvent( QgsLayoutViewMouseEvent *event

mDrawing = true;
mMousePressStartPos = event->pos();
mMousePressStartLayoutPos = event->layoutPoint();
mRubberBand.reset( QgsGui::layoutItemGuiRegistry()->createItemRubberBand( mItemMetadataId, view() ) );
if ( mRubberBand )
{
Expand Down Expand Up @@ -85,7 +86,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
}
mDrawing = false;

QRectF rect = mRubberBand->finish( event->snappedPoint(), event->modifiers() );
QRectF rect = mRubberBand ? mRubberBand->finish( event->snappedPoint(), event->modifiers() ) : QRectF();

QString undoText;
if ( QgsLayoutItemAbstractGuiMetadata *metadata = QgsGui::layoutItemGuiRegistry()->itemMetadata( mItemMetadataId ) )
Expand All @@ -107,7 +108,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even

// click? or click-and-drag?
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
if ( clickOnly )
if ( clickOnly && mRubberBand )
{
QgsLayoutItemPropertiesDialog dlg( view() );
dlg.setLayout( layout() );
Expand All @@ -125,17 +126,25 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
return;
}
}
else
else if ( mRubberBand )
{
item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), QgsUnitTypes::LayoutMillimeters ) );
item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), QgsUnitTypes::LayoutMillimeters ) );
}
else
{
// item type doesn't use rubber bands -- e.g. marker items
item->attemptMove( QgsLayoutPoint( mMousePressStartLayoutPos, layout()->units() ) );
}

// record last created item size
QgsSettings settings;
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
settings.setEnumValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), item->sizeWithUnits().units() );
if ( mRubberBand )
{
QgsSettings settings;
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
settings.setEnumValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), item->sizeWithUnits().units() );
}

QgsGui::layoutItemGuiRegistry()->newItemAddedToLayout( mItemMetadataId, item );

Expand All @@ -154,7 +163,8 @@ void QgsLayoutViewToolAddItem::deactivate()
if ( mDrawing )
{
// canceled mid operation
mRubberBand->finish();
if ( mRubberBand )
mRubberBand->finish();
mDrawing = false;
}
QgsLayoutViewTool::deactivate();
Expand Down
3 changes: 3 additions & 0 deletions src/gui/layout/qgslayoutviewtooladditem.h
Expand Up @@ -77,6 +77,9 @@ class GUI_EXPORT QgsLayoutViewToolAddItem : public QgsLayoutViewTool
//! Start position for mouse press
QPoint mMousePressStartPos;

//! Start position for mouse press in layout coordinates
QPointF mMousePressStartLayoutPos;

//! Start of rubber band creation
QPointF mRubberBandStartPos;

Expand Down

0 comments on commit 0fe2ec2

Please sign in to comment.