Allow layout items to be created without rubber bands, e.g. so that

marker items are placed with a single click instead of click-and-drag
This commit is contained in:
Nyall Dawson 2020-04-05 08:31:36 +10:00
parent 632448c447
commit 0fe2ec2f70
3 changed files with 22 additions and 9 deletions

View File

@ -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>(

View File

@ -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 )
{
@ -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 ) )
@ -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() );
@ -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 );
@ -154,7 +163,8 @@ void QgsLayoutViewToolAddItem::deactivate()
if ( mDrawing )
{
// canceled mid operation
mRubberBand->finish();
if ( mRubberBand )
mRubberBand->finish();
mDrawing = false;
}
QgsLayoutViewTool::deactivate();

View File

@ -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;