Skip to content

Commit cdec70b

Browse files
committedJul 18, 2017
[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

Lines changed: 401 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,401 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>QgsLayoutNewItemPropertiesDialog</class>
4+
<widget class="QDialog" name="QgsLayoutNewItemPropertiesDialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>391</width>
10+
<height>263</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>New Item Properties</string>
15+
</property>
16+
<layout class="QGridLayout" name="gridLayout_4" rowstretch="0,1,0" columnstretch="1,0">
17+
<item row="0" column="0">
18+
<widget class="QGroupBox" name="groupBox">
19+
<property name="title">
20+
<string>Position and size</string>
21+
</property>
22+
<layout class="QVBoxLayout" name="verticalLayout">
23+
<item>
24+
<layout class="QGridLayout" name="gridLayout_3" columnstretch="0,0,0">
25+
<item row="3" column="1" colspan="2">
26+
<widget class="QgsDoubleSpinBox" name="mHeightSpin">
27+
<property name="suffix">
28+
<string> mm</string>
29+
</property>
30+
<property name="decimals">
31+
<number>3</number>
32+
</property>
33+
<property name="maximum">
34+
<double>9999999.000000000000000</double>
35+
</property>
36+
<property name="value">
37+
<double>100.000000000000000</double>
38+
</property>
39+
<property name="showClearButton" stdset="0">
40+
<bool>false</bool>
41+
</property>
42+
</widget>
43+
</item>
44+
<item row="1" column="1" colspan="2">
45+
<widget class="QgsDoubleSpinBox" name="mYPosSpin">
46+
<property name="suffix">
47+
<string> mm</string>
48+
</property>
49+
<property name="decimals">
50+
<number>3</number>
51+
</property>
52+
<property name="minimum">
53+
<double>-9999999.000000000000000</double>
54+
</property>
55+
<property name="maximum">
56+
<double>9999999.000000000000000</double>
57+
</property>
58+
<property name="showClearButton" stdset="0">
59+
<bool>false</bool>
60+
</property>
61+
</widget>
62+
</item>
63+
<item row="3" column="0">
64+
<widget class="QLabel" name="mHeightLabel">
65+
<property name="text">
66+
<string>Height</string>
67+
</property>
68+
</widget>
69+
</item>
70+
<item row="2" column="1" colspan="2">
71+
<widget class="QgsDoubleSpinBox" name="mWidthSpin">
72+
<property name="suffix">
73+
<string> mm</string>
74+
</property>
75+
<property name="decimals">
76+
<number>3</number>
77+
</property>
78+
<property name="maximum">
79+
<double>9999999.000000000000000</double>
80+
</property>
81+
<property name="value">
82+
<double>100.000000000000000</double>
83+
</property>
84+
<property name="showClearButton" stdset="0">
85+
<bool>false</bool>
86+
</property>
87+
</widget>
88+
</item>
89+
<item row="0" column="1" colspan="2">
90+
<widget class="QgsDoubleSpinBox" name="mXPosSpin">
91+
<property name="suffix">
92+
<string> mm</string>
93+
</property>
94+
<property name="decimals">
95+
<number>3</number>
96+
</property>
97+
<property name="minimum">
98+
<double>-9999999.000000000000000</double>
99+
</property>
100+
<property name="maximum">
101+
<double>9999999.000000000000000</double>
102+
</property>
103+
<property name="showClearButton" stdset="0">
104+
<bool>false</bool>
105+
</property>
106+
</widget>
107+
</item>
108+
<item row="2" column="0">
109+
<widget class="QLabel" name="mWidthLabel">
110+
<property name="text">
111+
<string>Width</string>
112+
</property>
113+
</widget>
114+
</item>
115+
<item row="1" column="0">
116+
<widget class="QLabel" name="mYLabel">
117+
<property name="text">
118+
<string>Y</string>
119+
</property>
120+
</widget>
121+
</item>
122+
<item row="0" column="0">
123+
<widget class="QLabel" name="mXLabel">
124+
<property name="text">
125+
<string>X</string>
126+
</property>
127+
</widget>
128+
</item>
129+
</layout>
130+
</item>
131+
</layout>
132+
</widget>
133+
</item>
134+
<item row="0" column="1">
135+
<widget class="QGroupBox" name="groupBox_2">
136+
<property name="title">
137+
<string>Reference point</string>
138+
</property>
139+
<layout class="QGridLayout" name="gridLayout_2">
140+
<item row="1" column="2">
141+
<spacer name="horizontalSpacer">
142+
<property name="orientation">
143+
<enum>Qt::Horizontal</enum>
144+
</property>
145+
<property name="sizeHint" stdset="0">
146+
<size>
147+
<width>40</width>
148+
<height>20</height>
149+
</size>
150+
</property>
151+
</spacer>
152+
</item>
153+
<item row="1" column="1">
154+
<layout class="QGridLayout" name="gridLayout">
155+
<item row="0" column="1">
156+
<widget class="QCheckBox" name="mUpperMiddleCheckBox">
157+
<property name="sizePolicy">
158+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
159+
<horstretch>0</horstretch>
160+
<verstretch>0</verstretch>
161+
</sizepolicy>
162+
</property>
163+
<property name="text">
164+
<string/>
165+
</property>
166+
</widget>
167+
</item>
168+
<item row="1" column="2">
169+
<widget class="QCheckBox" name="mMiddleRightCheckBox">
170+
<property name="sizePolicy">
171+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
172+
<horstretch>0</horstretch>
173+
<verstretch>0</verstretch>
174+
</sizepolicy>
175+
</property>
176+
<property name="text">
177+
<string/>
178+
</property>
179+
</widget>
180+
</item>
181+
<item row="1" column="1">
182+
<widget class="QCheckBox" name="mMiddleCheckBox">
183+
<property name="sizePolicy">
184+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
185+
<horstretch>0</horstretch>
186+
<verstretch>0</verstretch>
187+
</sizepolicy>
188+
</property>
189+
<property name="text">
190+
<string/>
191+
</property>
192+
</widget>
193+
</item>
194+
<item row="0" column="2">
195+
<widget class="QCheckBox" name="mUpperRightCheckBox">
196+
<property name="sizePolicy">
197+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
198+
<horstretch>0</horstretch>
199+
<verstretch>0</verstretch>
200+
</sizepolicy>
201+
</property>
202+
<property name="text">
203+
<string/>
204+
</property>
205+
</widget>
206+
</item>
207+
<item row="2" column="1">
208+
<widget class="QCheckBox" name="mLowerMiddleCheckBox">
209+
<property name="sizePolicy">
210+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
211+
<horstretch>0</horstretch>
212+
<verstretch>0</verstretch>
213+
</sizepolicy>
214+
</property>
215+
<property name="text">
216+
<string/>
217+
</property>
218+
</widget>
219+
</item>
220+
<item row="2" column="2">
221+
<widget class="QCheckBox" name="mLowerRightCheckBox">
222+
<property name="sizePolicy">
223+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
224+
<horstretch>0</horstretch>
225+
<verstretch>0</verstretch>
226+
</sizepolicy>
227+
</property>
228+
<property name="text">
229+
<string/>
230+
</property>
231+
</widget>
232+
</item>
233+
<item row="0" column="0">
234+
<widget class="QCheckBox" name="mUpperLeftCheckBox">
235+
<property name="sizePolicy">
236+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
237+
<horstretch>0</horstretch>
238+
<verstretch>0</verstretch>
239+
</sizepolicy>
240+
</property>
241+
<property name="text">
242+
<string/>
243+
</property>
244+
<property name="checked">
245+
<bool>true</bool>
246+
</property>
247+
</widget>
248+
</item>
249+
<item row="2" column="0">
250+
<widget class="QCheckBox" name="mLowerLeftCheckBox">
251+
<property name="sizePolicy">
252+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
253+
<horstretch>0</horstretch>
254+
<verstretch>0</verstretch>
255+
</sizepolicy>
256+
</property>
257+
<property name="text">
258+
<string/>
259+
</property>
260+
</widget>
261+
</item>
262+
<item row="1" column="0">
263+
<widget class="QCheckBox" name="mMiddleLeftCheckBox">
264+
<property name="sizePolicy">
265+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
266+
<horstretch>0</horstretch>
267+
<verstretch>0</verstretch>
268+
</sizepolicy>
269+
</property>
270+
<property name="text">
271+
<string/>
272+
</property>
273+
</widget>
274+
</item>
275+
</layout>
276+
</item>
277+
<item row="1" column="0">
278+
<spacer name="horizontalSpacer_2">
279+
<property name="orientation">
280+
<enum>Qt::Horizontal</enum>
281+
</property>
282+
<property name="sizeHint" stdset="0">
283+
<size>
284+
<width>40</width>
285+
<height>20</height>
286+
</size>
287+
</property>
288+
</spacer>
289+
</item>
290+
<item row="2" column="1">
291+
<spacer name="verticalSpacer_2">
292+
<property name="orientation">
293+
<enum>Qt::Vertical</enum>
294+
</property>
295+
<property name="sizeHint" stdset="0">
296+
<size>
297+
<width>20</width>
298+
<height>40</height>
299+
</size>
300+
</property>
301+
</spacer>
302+
</item>
303+
<item row="0" column="1">
304+
<spacer name="verticalSpacer_3">
305+
<property name="orientation">
306+
<enum>Qt::Vertical</enum>
307+
</property>
308+
<property name="sizeHint" stdset="0">
309+
<size>
310+
<width>20</width>
311+
<height>40</height>
312+
</size>
313+
</property>
314+
</spacer>
315+
</item>
316+
</layout>
317+
</widget>
318+
</item>
319+
<item row="1" column="0">
320+
<spacer name="verticalSpacer">
321+
<property name="orientation">
322+
<enum>Qt::Vertical</enum>
323+
</property>
324+
<property name="sizeHint" stdset="0">
325+
<size>
326+
<width>20</width>
327+
<height>40</height>
328+
</size>
329+
</property>
330+
</spacer>
331+
</item>
332+
<item row="2" column="0" colspan="2">
333+
<widget class="QDialogButtonBox" name="buttonBox">
334+
<property name="orientation">
335+
<enum>Qt::Horizontal</enum>
336+
</property>
337+
<property name="standardButtons">
338+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
339+
</property>
340+
</widget>
341+
</item>
342+
</layout>
343+
</widget>
344+
<customwidgets>
345+
<customwidget>
346+
<class>QgsDoubleSpinBox</class>
347+
<extends>QDoubleSpinBox</extends>
348+
<header>qgsdoublespinbox.h</header>
349+
</customwidget>
350+
</customwidgets>
351+
<tabstops>
352+
<tabstop>mXPosSpin</tabstop>
353+
<tabstop>mYPosSpin</tabstop>
354+
<tabstop>mWidthSpin</tabstop>
355+
<tabstop>mHeightSpin</tabstop>
356+
<tabstop>mUpperLeftCheckBox</tabstop>
357+
<tabstop>mUpperMiddleCheckBox</tabstop>
358+
<tabstop>mUpperRightCheckBox</tabstop>
359+
<tabstop>mMiddleLeftCheckBox</tabstop>
360+
<tabstop>mMiddleCheckBox</tabstop>
361+
<tabstop>mMiddleRightCheckBox</tabstop>
362+
<tabstop>mLowerLeftCheckBox</tabstop>
363+
<tabstop>mLowerMiddleCheckBox</tabstop>
364+
<tabstop>mLowerRightCheckBox</tabstop>
365+
</tabstops>
366+
<resources/>
367+
<connections>
368+
<connection>
369+
<sender>buttonBox</sender>
370+
<signal>accepted()</signal>
371+
<receiver>QgsLayoutNewItemPropertiesDialog</receiver>
372+
<slot>accept()</slot>
373+
<hints>
374+
<hint type="sourcelabel">
375+
<x>248</x>
376+
<y>254</y>
377+
</hint>
378+
<hint type="destinationlabel">
379+
<x>157</x>
380+
<y>274</y>
381+
</hint>
382+
</hints>
383+
</connection>
384+
<connection>
385+
<sender>buttonBox</sender>
386+
<signal>rejected()</signal>
387+
<receiver>QgsLayoutNewItemPropertiesDialog</receiver>
388+
<slot>reject()</slot>
389+
<hints>
390+
<hint type="sourcelabel">
391+
<x>316</x>
392+
<y>260</y>
393+
</hint>
394+
<hint type="destinationlabel">
395+
<x>286</x>
396+
<y>274</y>
397+
</hint>
398+
</hints>
399+
</connection>
400+
</connections>
401+
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.