Skip to content
Permalink
Browse files

[needs-docs] Add a new item properties dialog

When adding a new item to a layout, if the user just does a single
click of the mouse (vs a click and drag to set the new item
position/size), then a dialog is shown asking to user for
the new item size/position/reference point.

This allows easy creation of items with an exact position/size
and is common behavior in most proper DTP/illustration apps.
  • Loading branch information
nyalldawson committed Jul 17, 2017
1 parent b4f5025 commit cdec70babe3f924f8f408b267823fdb1595fc9ee
@@ -278,6 +278,7 @@
%Include layertree/qgslayertreeviewdefaultactions.sip
%Include layout/qgslayoutdesignerinterface.sip
%Include layout/qgslayoutitemguiregistry.sip
%Include layout/qgslayoutnewitempropertiesdialog.sip
%Include layout/qgslayoutruler.sip
%Include layout/qgslayoutview.sip
%Include layout/qgslayoutviewtool.sip
@@ -0,0 +1,46 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutnewitempropertiesdialog.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsLayoutNewItemPropertiesDialog : QDialog
{
%Docstring
A dialog for configuring properties like the size and position of new layout items.
%End

%TypeHeaderCode
#include "qgslayoutnewitempropertiesdialog.h"
%End
public:

QgsLayoutNewItemPropertiesDialog( QWidget *parent = 0, Qt::WindowFlags flags = 0 );


void setInitialItemPosition( QPointF position );

QgsLayoutPoint itemPosition() const;
%Docstring
:rtype: QgsLayoutPoint
%End

QgsLayoutSize itemSize() const;
%Docstring
:rtype: QgsLayoutSize
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutnewitempropertiesdialog.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -159,6 +159,7 @@ SET(QGIS_GUI_SRCS
layertree/qgslayertreeviewdefaultactions.cpp

layout/qgslayoutitemguiregistry.cpp
layout/qgslayoutnewitempropertiesdialog.cpp
layout/qgslayoutruler.cpp
layout/qgslayoutview.cpp
layout/qgslayoutviewmouseevent.cpp
@@ -647,6 +648,7 @@ SET(QGIS_GUI_MOC_HDRS

layout/qgslayoutdesignerinterface.h
layout/qgslayoutitemguiregistry.h
layout/qgslayoutnewitempropertiesdialog.h
layout/qgslayoutruler.h
layout/qgslayoutview.h
layout/qgslayoutviewtool.h
@@ -0,0 +1,44 @@
/***************************************************************************
qgslayoutnewitempropertiesdialog.cpp
------------------------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayoutnewitempropertiesdialog.h"
#include "qgssettings.h"

QgsLayoutNewItemPropertiesDialog::QgsLayoutNewItemPropertiesDialog( QWidget *parent, Qt::WindowFlags flags )
: QDialog( parent, flags )
{
setupUi( this );
QgsSettings settings;
double lastWidth = settings.value( QStringLiteral( "LayoutDesigner/lastItemWidth" ), QStringLiteral( "50" ) ).toDouble();
double lastHeight = settings.value( QStringLiteral( "LayoutDesigner/lastItemHeight" ), QStringLiteral( "50" ) ).toDouble();
mWidthSpin->setValue( lastWidth );
mHeightSpin->setValue( lastHeight );
}

void QgsLayoutNewItemPropertiesDialog::setInitialItemPosition( QPointF position )
{
mXPosSpin->setValue( position.x() );
mYPosSpin->setValue( position.y() );
}

QgsLayoutPoint QgsLayoutNewItemPropertiesDialog::itemPosition() const
{
return QgsLayoutPoint( mXPosSpin->value(), mYPosSpin->value() );
}

QgsLayoutSize QgsLayoutNewItemPropertiesDialog::itemSize() const
{
return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value() );
}
@@ -0,0 +1,47 @@
/***************************************************************************
qgslayoutnewitempropertiesdialog.h
----------------------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLAYOUTNEWITEMPROPERTIESDIALOG_H
#define QGSLAYOUTNEWITEMPROPERTIESDIALOG_H

#include "qgis.h"
#include "qgis_gui.h"
#include "ui_qgslayoutnewitemproperties.h"

#include "qgslayoutsize.h"
#include "qgslayoutpoint.h"

/**
* \ingroup gui
* \brief A dialog for configuring properties like the size and position of new layout items.
*/
class GUI_EXPORT QgsLayoutNewItemPropertiesDialog : public QDialog, private Ui::QgsLayoutNewItemPropertiesDialog
{
Q_OBJECT

public:

QgsLayoutNewItemPropertiesDialog( QWidget *parent = nullptr, Qt::WindowFlags flags = 0 );


void setInitialItemPosition( QPointF position );

QgsLayoutPoint itemPosition() const;

QgsLayoutSize itemSize() const;

};

#endif // QGSLAYOUTNEWITEMPROPERTIESDIALOG_H
@@ -24,6 +24,8 @@
#include "qgslayoutviewrubberband.h"
#include "qgsgui.h"
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutnewitempropertiesdialog.h"
#include "qgssettings.h"
#include <QGraphicsRectItem>
#include <QPen>
#include <QBrush>
@@ -81,13 +83,37 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even

QRectF rect = mRubberBand->finish( event->layoutPoint(), event->modifiers() );

QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );

// click? or click-and-drag?
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
Q_UNUSED( clickOnly );
if ( clickOnly )
{
QgsLayoutNewItemPropertiesDialog dlg( view() );
dlg.setInitialItemPosition( event->layoutPoint() );
if ( dlg.exec() )
{
item->attemptResize( dlg.itemSize() );
item->attemptMove( dlg.itemPosition() );
}
else
{
delete item;
return;
}
}
else
{
item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), QgsUnitTypes::LayoutMillimeters ) );
item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), QgsUnitTypes::LayoutMillimeters ) );
}

// record last created item size
QgsSettings settings;
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );


QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );
item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), QgsUnitTypes::LayoutMillimeters ) );
item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), QgsUnitTypes::LayoutMillimeters ) );
layout()->addItem( item );
}

0 comments on commit cdec70b

Please sign in to comment.
You can’t perform that action at this time.