Skip to content

Commit cdec70b

Browse files
committed
[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.
1 parent b4f5025 commit cdec70b

File tree

7 files changed

+571
-4
lines changed

7 files changed

+571
-4
lines changed

python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
%Include layertree/qgslayertreeviewdefaultactions.sip
279279
%Include layout/qgslayoutdesignerinterface.sip
280280
%Include layout/qgslayoutitemguiregistry.sip
281+
%Include layout/qgslayoutnewitempropertiesdialog.sip
281282
%Include layout/qgslayoutruler.sip
282283
%Include layout/qgslayoutview.sip
283284
%Include layout/qgslayoutviewtool.sip
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/layout/qgslayoutnewitempropertiesdialog.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsLayoutNewItemPropertiesDialog : QDialog
13+
{
14+
%Docstring
15+
A dialog for configuring properties like the size and position of new layout items.
16+
%End
17+
18+
%TypeHeaderCode
19+
#include "qgslayoutnewitempropertiesdialog.h"
20+
%End
21+
public:
22+
23+
QgsLayoutNewItemPropertiesDialog( QWidget *parent = 0, Qt::WindowFlags flags = 0 );
24+
25+
26+
void setInitialItemPosition( QPointF position );
27+
28+
QgsLayoutPoint itemPosition() const;
29+
%Docstring
30+
:rtype: QgsLayoutPoint
31+
%End
32+
33+
QgsLayoutSize itemSize() const;
34+
%Docstring
35+
:rtype: QgsLayoutSize
36+
%End
37+
38+
};
39+
40+
/************************************************************************
41+
* This file has been generated automatically from *
42+
* *
43+
* src/gui/layout/qgslayoutnewitempropertiesdialog.h *
44+
* *
45+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
46+
************************************************************************/

src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ SET(QGIS_GUI_SRCS
159159
layertree/qgslayertreeviewdefaultactions.cpp
160160

161161
layout/qgslayoutitemguiregistry.cpp
162+
layout/qgslayoutnewitempropertiesdialog.cpp
162163
layout/qgslayoutruler.cpp
163164
layout/qgslayoutview.cpp
164165
layout/qgslayoutviewmouseevent.cpp
@@ -647,6 +648,7 @@ SET(QGIS_GUI_MOC_HDRS
647648

648649
layout/qgslayoutdesignerinterface.h
649650
layout/qgslayoutitemguiregistry.h
651+
layout/qgslayoutnewitempropertiesdialog.h
650652
layout/qgslayoutruler.h
651653
layout/qgslayoutview.h
652654
layout/qgslayoutviewtool.h
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***************************************************************************
2+
qgslayoutnewitempropertiesdialog.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+
16+
#include "qgslayoutnewitempropertiesdialog.h"
17+
#include "qgssettings.h"
18+
19+
QgsLayoutNewItemPropertiesDialog::QgsLayoutNewItemPropertiesDialog( QWidget *parent, Qt::WindowFlags flags )
20+
: QDialog( parent, flags )
21+
{
22+
setupUi( this );
23+
QgsSettings settings;
24+
double lastWidth = settings.value( QStringLiteral( "LayoutDesigner/lastItemWidth" ), QStringLiteral( "50" ) ).toDouble();
25+
double lastHeight = settings.value( QStringLiteral( "LayoutDesigner/lastItemHeight" ), QStringLiteral( "50" ) ).toDouble();
26+
mWidthSpin->setValue( lastWidth );
27+
mHeightSpin->setValue( lastHeight );
28+
}
29+
30+
void QgsLayoutNewItemPropertiesDialog::setInitialItemPosition( QPointF position )
31+
{
32+
mXPosSpin->setValue( position.x() );
33+
mYPosSpin->setValue( position.y() );
34+
}
35+
36+
QgsLayoutPoint QgsLayoutNewItemPropertiesDialog::itemPosition() const
37+
{
38+
return QgsLayoutPoint( mXPosSpin->value(), mYPosSpin->value() );
39+
}
40+
41+
QgsLayoutSize QgsLayoutNewItemPropertiesDialog::itemSize() const
42+
{
43+
return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value() );
44+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***************************************************************************
2+
qgslayoutnewitempropertiesdialog.h
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+
16+
#ifndef QGSLAYOUTNEWITEMPROPERTIESDIALOG_H
17+
#define QGSLAYOUTNEWITEMPROPERTIESDIALOG_H
18+
19+
#include "qgis.h"
20+
#include "qgis_gui.h"
21+
#include "ui_qgslayoutnewitemproperties.h"
22+
23+
#include "qgslayoutsize.h"
24+
#include "qgslayoutpoint.h"
25+
26+
/**
27+
* \ingroup gui
28+
* \brief A dialog for configuring properties like the size and position of new layout items.
29+
*/
30+
class GUI_EXPORT QgsLayoutNewItemPropertiesDialog : public QDialog, private Ui::QgsLayoutNewItemPropertiesDialog
31+
{
32+
Q_OBJECT
33+
34+
public:
35+
36+
QgsLayoutNewItemPropertiesDialog( QWidget *parent = nullptr, Qt::WindowFlags flags = 0 );
37+
38+
39+
void setInitialItemPosition( QPointF position );
40+
41+
QgsLayoutPoint itemPosition() const;
42+
43+
QgsLayoutSize itemSize() const;
44+
45+
};
46+
47+
#endif // QGSLAYOUTNEWITEMPROPERTIESDIALOG_H

src/gui/layout/qgslayoutviewtooladditem.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "qgslayoutviewrubberband.h"
2525
#include "qgsgui.h"
2626
#include "qgslayoutitemguiregistry.h"
27+
#include "qgslayoutnewitempropertiesdialog.h"
28+
#include "qgssettings.h"
2729
#include <QGraphicsRectItem>
2830
#include <QPen>
2931
#include <QBrush>
@@ -81,13 +83,37 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
8183

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

86+
QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );
87+
8488
// click? or click-and-drag?
8589
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
86-
Q_UNUSED( clickOnly );
90+
if ( clickOnly )
91+
{
92+
QgsLayoutNewItemPropertiesDialog dlg( view() );
93+
dlg.setInitialItemPosition( event->layoutPoint() );
94+
if ( dlg.exec() )
95+
{
96+
item->attemptResize( dlg.itemSize() );
97+
item->attemptMove( dlg.itemPosition() );
98+
}
99+
else
100+
{
101+
delete item;
102+
return;
103+
}
104+
}
105+
else
106+
{
107+
item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), QgsUnitTypes::LayoutMillimeters ) );
108+
item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), QgsUnitTypes::LayoutMillimeters ) );
109+
}
110+
111+
// record last created item size
112+
QgsSettings settings;
113+
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
114+
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
115+
87116

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

0 commit comments

Comments
 (0)