Skip to content

Commit de2626d

Browse files
committedJul 18, 2017
Add unit types to new item properties dialog, handle different reference
point settings
1 parent 19a7863 commit de2626d

File tree

7 files changed

+293
-52
lines changed

7 files changed

+293
-52
lines changed
 

‎python/gui/layout/qgslayoutnewitempropertiesdialog.sip

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,66 @@
99

1010

1111

12-
class QgsLayoutNewItemPropertiesDialog : QDialog
12+
class QgsLayoutItemPropertiesDialog : QDialog
1313
{
1414
%Docstring
15-
A dialog for configuring properties like the size and position of new layout items.
15+
A dialog for configuring properties like the size and position of layout items.
16+
17+
This is usually used only when constructing new layout items, allowing users to precisely
18+
enter their sizes and positions.
19+
20+
.. versionadded:: 3.0
1621
%End
1722

1823
%TypeHeaderCode
1924
#include "qgslayoutnewitempropertiesdialog.h"
2025
%End
2126
public:
2227

23-
QgsLayoutNewItemPropertiesDialog( QWidget *parent = 0, Qt::WindowFlags flags = 0 );
24-
28+
QgsLayoutItemPropertiesDialog( QWidget *parent = 0, Qt::WindowFlags flags = 0 );
29+
%Docstring
30+
Constructor for QgsLayoutNewItemPropertiesDialog.
31+
%End
2532

26-
void setInitialItemPosition( QPointF position );
33+
void setItemPosition( QgsLayoutPoint position );
34+
%Docstring
35+
Sets the item ``position`` to show in the dialog.
36+
.. seealso:: itemPosition()
37+
%End
2738

2839
QgsLayoutPoint itemPosition() const;
2940
%Docstring
41+
Returns the current item position defined by the dialog.
42+
.. seealso:: setItemPosition()
3043
:rtype: QgsLayoutPoint
3144
%End
3245

46+
void setItemSize( QgsLayoutSize size );
47+
%Docstring
48+
Sets the item ``size`` to show in the dialog.
49+
.. seealso:: itemSize()
50+
%End
51+
3352
QgsLayoutSize itemSize() const;
3453
%Docstring
54+
Returns the item size defined by the dialog.
55+
.. seealso:: setItemSize()
3556
:rtype: QgsLayoutSize
3657
%End
3758

59+
QgsLayoutItem::ReferencePoint referencePoint() const;
60+
%Docstring
61+
Returns the item reference point defined by the dialog.
62+
.. seealso:: setReferencePoint()
63+
:rtype: QgsLayoutItem.ReferencePoint
64+
%End
65+
66+
void setReferencePoint( QgsLayoutItem::ReferencePoint point );
67+
%Docstring
68+
Sets the item reference ``point`` defined to show in the dialog.
69+
.. seealso:: referencePoint()
70+
%End
71+
3872
};
3973

4074
/************************************************************************

‎src/gui/layout/qgslayoutnewitempropertiesdialog.cpp

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,134 @@
1616
#include "qgslayoutnewitempropertiesdialog.h"
1717
#include "qgssettings.h"
1818

19-
QgsLayoutNewItemPropertiesDialog::QgsLayoutNewItemPropertiesDialog( QWidget *parent, Qt::WindowFlags flags )
19+
QgsLayoutItemPropertiesDialog::QgsLayoutItemPropertiesDialog( QWidget *parent, Qt::WindowFlags flags )
2020
: QDialog( parent, flags )
2121
{
2222
setupUi( this );
23+
24+
//make button exclusive
25+
QButtonGroup *buttonGroup = new QButtonGroup( this );
26+
buttonGroup->addButton( mUpperLeftCheckBox );
27+
buttonGroup->addButton( mUpperMiddleCheckBox );
28+
buttonGroup->addButton( mUpperRightCheckBox );
29+
buttonGroup->addButton( mMiddleLeftCheckBox );
30+
buttonGroup->addButton( mMiddleCheckBox );
31+
buttonGroup->addButton( mMiddleRightCheckBox );
32+
buttonGroup->addButton( mLowerLeftCheckBox );
33+
buttonGroup->addButton( mLowerMiddleCheckBox );
34+
buttonGroup->addButton( mLowerRightCheckBox );
35+
buttonGroup->setExclusive( true );
36+
2337
QgsSettings settings;
2438
double lastWidth = settings.value( QStringLiteral( "LayoutDesigner/lastItemWidth" ), QStringLiteral( "50" ) ).toDouble();
2539
double lastHeight = settings.value( QStringLiteral( "LayoutDesigner/lastItemHeight" ), QStringLiteral( "50" ) ).toDouble();
26-
mWidthSpin->setValue( lastWidth );
27-
mHeightSpin->setValue( lastHeight );
40+
QgsUnitTypes::LayoutUnit lastSizeUnit = static_cast< QgsUnitTypes::LayoutUnit >( settings.value( QStringLiteral( "LayoutDesigner/lastSizeUnit" ) ).toInt() );
41+
setItemSize( QgsLayoutSize( lastWidth, lastHeight, lastSizeUnit ) );
2842
}
2943

30-
void QgsLayoutNewItemPropertiesDialog::setInitialItemPosition( QPointF position )
44+
void QgsLayoutItemPropertiesDialog::setItemPosition( QgsLayoutPoint position )
3145
{
3246
mXPosSpin->setValue( position.x() );
3347
mYPosSpin->setValue( position.y() );
48+
mPosUnitsComboBox->setUnit( position.units() );
49+
}
50+
51+
QgsLayoutPoint QgsLayoutItemPropertiesDialog::itemPosition() const
52+
{
53+
return QgsLayoutPoint( mXPosSpin->value(), mYPosSpin->value(), mPosUnitsComboBox->unit() );
3454
}
3555

36-
QgsLayoutPoint QgsLayoutNewItemPropertiesDialog::itemPosition() const
56+
void QgsLayoutItemPropertiesDialog::setItemSize( QgsLayoutSize size )
3757
{
38-
return QgsLayoutPoint( mXPosSpin->value(), mYPosSpin->value() );
58+
mWidthSpin->setValue( size.width() );
59+
mHeightSpin->setValue( size.height() );
60+
mSizeUnitsComboBox->setUnit( size.units() );
3961
}
4062

41-
QgsLayoutSize QgsLayoutNewItemPropertiesDialog::itemSize() const
63+
QgsLayoutSize QgsLayoutItemPropertiesDialog::itemSize() const
4264
{
43-
return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value() );
65+
return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
66+
}
67+
68+
QgsLayoutItem::ReferencePoint QgsLayoutItemPropertiesDialog::referencePoint() const
69+
{
70+
if ( mUpperLeftCheckBox->checkState() == Qt::Checked )
71+
{
72+
return QgsLayoutItem::UpperLeft;
73+
}
74+
else if ( mUpperMiddleCheckBox->checkState() == Qt::Checked )
75+
{
76+
return QgsLayoutItem::UpperMiddle;
77+
}
78+
else if ( mUpperRightCheckBox->checkState() == Qt::Checked )
79+
{
80+
return QgsLayoutItem::UpperRight;
81+
}
82+
else if ( mMiddleLeftCheckBox->checkState() == Qt::Checked )
83+
{
84+
return QgsLayoutItem::MiddleLeft;
85+
}
86+
else if ( mMiddleCheckBox->checkState() == Qt::Checked )
87+
{
88+
return QgsLayoutItem::Middle;
89+
}
90+
else if ( mMiddleRightCheckBox->checkState() == Qt::Checked )
91+
{
92+
return QgsLayoutItem::MiddleRight;
93+
}
94+
else if ( mLowerLeftCheckBox->checkState() == Qt::Checked )
95+
{
96+
return QgsLayoutItem::LowerLeft;
97+
}
98+
else if ( mLowerMiddleCheckBox->checkState() == Qt::Checked )
99+
{
100+
return QgsLayoutItem::LowerMiddle;
101+
}
102+
else if ( mLowerRightCheckBox->checkState() == Qt::Checked )
103+
{
104+
return QgsLayoutItem::LowerRight;
105+
}
106+
return QgsLayoutItem::UpperLeft;
107+
}
108+
109+
void QgsLayoutItemPropertiesDialog::setReferencePoint( QgsLayoutItem::ReferencePoint point )
110+
{
111+
switch ( point )
112+
{
113+
case QgsLayoutItem::UpperLeft:
114+
mUpperLeftCheckBox->setChecked( true );
115+
break;
116+
117+
case QgsLayoutItem::UpperMiddle:
118+
mUpperMiddleCheckBox->setChecked( true );
119+
break;
120+
121+
case QgsLayoutItem::UpperRight:
122+
mUpperRightCheckBox->setChecked( true );
123+
break;
124+
125+
case QgsLayoutItem::MiddleLeft:
126+
mMiddleLeftCheckBox->setChecked( true );
127+
break;
128+
129+
case QgsLayoutItem::Middle:
130+
mMiddleCheckBox->setChecked( true );
131+
break;
132+
133+
case QgsLayoutItem::MiddleRight:
134+
mMiddleRightCheckBox->setChecked( true );
135+
break;
136+
137+
case QgsLayoutItem::LowerLeft:
138+
mLowerLeftCheckBox->setChecked( true );
139+
break;
140+
141+
case QgsLayoutItem::LowerMiddle:
142+
mLowerMiddleCheckBox->setChecked( true );
143+
break;
144+
145+
case QgsLayoutItem::LowerRight:
146+
mLowerRightCheckBox->setChecked( true );
147+
break;
148+
}
44149
}

‎src/gui/layout/qgslayoutnewitempropertiesdialog.h

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,64 @@
2222

2323
#include "qgslayoutsize.h"
2424
#include "qgslayoutpoint.h"
25+
#include "qgslayoutitem.h"
2526

2627
/**
2728
* \ingroup gui
28-
* \brief A dialog for configuring properties like the size and position of new layout items.
29+
* \brief A dialog for configuring properties like the size and position of layout items.
30+
*
31+
* This is usually used only when constructing new layout items, allowing users to precisely
32+
* enter their sizes and positions.
33+
*
34+
* \since QGIS 3.0
2935
*/
30-
class GUI_EXPORT QgsLayoutNewItemPropertiesDialog : public QDialog, private Ui::QgsLayoutNewItemPropertiesDialog
36+
class GUI_EXPORT QgsLayoutItemPropertiesDialog : public QDialog, private Ui::QgsLayoutNewItemPropertiesDialog
3137
{
3238
Q_OBJECT
3339

3440
public:
3541

36-
QgsLayoutNewItemPropertiesDialog( QWidget *parent = nullptr, Qt::WindowFlags flags = 0 );
42+
/**
43+
* Constructor for QgsLayoutNewItemPropertiesDialog.
44+
*/
45+
QgsLayoutItemPropertiesDialog( QWidget *parent = nullptr, Qt::WindowFlags flags = 0 );
3746

47+
/**
48+
* Sets the item \a position to show in the dialog.
49+
* \see itemPosition()
50+
*/
51+
void setItemPosition( QgsLayoutPoint position );
3852

39-
void setInitialItemPosition( QPointF position );
40-
53+
/**
54+
* Returns the current item position defined by the dialog.
55+
* \see setItemPosition()
56+
*/
4157
QgsLayoutPoint itemPosition() const;
4258

59+
/**
60+
* Sets the item \a size to show in the dialog.
61+
* \see itemSize()
62+
*/
63+
void setItemSize( QgsLayoutSize size );
64+
65+
/**
66+
* Returns the item size defined by the dialog.
67+
* \see setItemSize()
68+
*/
4369
QgsLayoutSize itemSize() const;
4470

71+
/**
72+
* Returns the item reference point defined by the dialog.
73+
* \see setReferencePoint()
74+
*/
75+
QgsLayoutItem::ReferencePoint referencePoint() const;
76+
77+
/**
78+
* Sets the item reference \a point defined to show in the dialog.
79+
* \see referencePoint()
80+
*/
81+
void setReferencePoint( QgsLayoutItem::ReferencePoint point );
82+
4583
};
4684

4785
#endif // QGSLAYOUTNEWITEMPROPERTIESDIALOG_H

‎src/gui/layout/qgslayoutviewtooladditem.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
8989
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
9090
if ( clickOnly )
9191
{
92-
QgsLayoutNewItemPropertiesDialog dlg( view() );
93-
dlg.setInitialItemPosition( event->layoutPoint() );
92+
QgsLayoutItemPropertiesDialog dlg( view() );
93+
dlg.setItemPosition( QgsLayoutPoint( event->layoutPoint(), layout()->units() ) );
9494
if ( dlg.exec() )
9595
{
96+
item->setReferencePoint( dlg.referencePoint() );
9697
item->attemptResize( dlg.itemSize() );
9798
item->attemptMove( dlg.itemPosition() );
9899
}
@@ -112,7 +113,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
112113
QgsSettings settings;
113114
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
114115
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
115-
116+
settings.setValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), static_cast< int >( item->sizeWithUnits().units() ) );
116117

117118
layout()->addItem( item );
118119
}

‎src/ui/layout/qgslayoutnewitemproperties.ui

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>391</width>
9+
<width>468</width>
1010
<height>263</height>
1111
</rect>
1212
</property>
@@ -21,11 +21,18 @@
2121
</property>
2222
<layout class="QVBoxLayout" name="verticalLayout">
2323
<item>
24-
<layout class="QGridLayout" name="gridLayout_3" columnstretch="0,0,0">
24+
<layout class="QGridLayout" name="gridLayout_3" columnstretch="0,1,0,0">
25+
<item row="3" column="0">
26+
<widget class="QLabel" name="mHeightLabel">
27+
<property name="text">
28+
<string>Height</string>
29+
</property>
30+
</widget>
31+
</item>
2532
<item row="3" column="1" colspan="2">
2633
<widget class="QgsDoubleSpinBox" name="mHeightSpin">
2734
<property name="suffix">
28-
<string> mm</string>
35+
<string/>
2936
</property>
3037
<property name="decimals">
3138
<number>3</number>
@@ -44,7 +51,7 @@
4451
<item row="1" column="1" colspan="2">
4552
<widget class="QgsDoubleSpinBox" name="mYPosSpin">
4653
<property name="suffix">
47-
<string> mm</string>
54+
<string/>
4855
</property>
4956
<property name="decimals">
5057
<number>3</number>
@@ -60,46 +67,53 @@
6067
</property>
6168
</widget>
6269
</item>
63-
<item row="3" column="0">
64-
<widget class="QLabel" name="mHeightLabel">
70+
<item row="0" column="0">
71+
<widget class="QLabel" name="mXLabel">
6572
<property name="text">
66-
<string>Height</string>
73+
<string>X</string>
6774
</property>
6875
</widget>
6976
</item>
70-
<item row="2" column="1" colspan="2">
71-
<widget class="QgsDoubleSpinBox" name="mWidthSpin">
77+
<item row="0" column="1" colspan="2">
78+
<widget class="QgsDoubleSpinBox" name="mXPosSpin">
7279
<property name="suffix">
73-
<string> mm</string>
80+
<string/>
7481
</property>
7582
<property name="decimals">
7683
<number>3</number>
7784
</property>
85+
<property name="minimum">
86+
<double>-9999999.000000000000000</double>
87+
</property>
7888
<property name="maximum">
7989
<double>9999999.000000000000000</double>
8090
</property>
81-
<property name="value">
82-
<double>100.000000000000000</double>
83-
</property>
8491
<property name="showClearButton" stdset="0">
8592
<bool>false</bool>
8693
</property>
8794
</widget>
8895
</item>
89-
<item row="0" column="1" colspan="2">
90-
<widget class="QgsDoubleSpinBox" name="mXPosSpin">
96+
<item row="1" column="0">
97+
<widget class="QLabel" name="mYLabel">
98+
<property name="text">
99+
<string>Y</string>
100+
</property>
101+
</widget>
102+
</item>
103+
<item row="2" column="1" colspan="2">
104+
<widget class="QgsDoubleSpinBox" name="mWidthSpin">
91105
<property name="suffix">
92-
<string> mm</string>
106+
<string/>
93107
</property>
94108
<property name="decimals">
95109
<number>3</number>
96110
</property>
97-
<property name="minimum">
98-
<double>-9999999.000000000000000</double>
99-
</property>
100111
<property name="maximum">
101112
<double>9999999.000000000000000</double>
102113
</property>
114+
<property name="value">
115+
<double>100.000000000000000</double>
116+
</property>
103117
<property name="showClearButton" stdset="0">
104118
<bool>false</bool>
105119
</property>
@@ -112,19 +126,11 @@
112126
</property>
113127
</widget>
114128
</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>
129+
<item row="0" column="3" rowspan="2">
130+
<widget class="QgsLayoutUnitsComboBox" name="mPosUnitsComboBox"/>
121131
</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>
132+
<item row="2" column="3" rowspan="2">
133+
<widget class="QgsLayoutUnitsComboBox" name="mSizeUnitsComboBox"/>
128134
</item>
129135
</layout>
130136
</item>
@@ -347,12 +353,19 @@
347353
<extends>QDoubleSpinBox</extends>
348354
<header>qgsdoublespinbox.h</header>
349355
</customwidget>
356+
<customwidget>
357+
<class>QgsLayoutUnitsComboBox</class>
358+
<extends>QComboBox</extends>
359+
<header>qgslayoutunitscombobox.h</header>
360+
</customwidget>
350361
</customwidgets>
351362
<tabstops>
352363
<tabstop>mXPosSpin</tabstop>
353364
<tabstop>mYPosSpin</tabstop>
365+
<tabstop>mPosUnitsComboBox</tabstop>
354366
<tabstop>mWidthSpin</tabstop>
355367
<tabstop>mHeightSpin</tabstop>
368+
<tabstop>mSizeUnitsComboBox</tabstop>
356369
<tabstop>mUpperLeftCheckBox</tabstop>
357370
<tabstop>mUpperMiddleCheckBox</tabstop>
358371
<tabstop>mUpperRightCheckBox</tabstop>

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ ADD_PYTHON_TEST(PyQgsLayerTreeMapCanvasBridge test_qgslayertreemapcanvasbridge.p
7575
ADD_PYTHON_TEST(PyQgsLayerTree test_qgslayertree.py)
7676
ADD_PYTHON_TEST(PyQgsLayoutManager test_qgslayoutmanager.py)
7777
ADD_PYTHON_TEST(PyQgsLayoutView test_qgslayoutview.py)
78+
ADD_PYTHON_TEST(PyQgsLayoutItemPropertiesDialog test_qgslayoutitempropertiesdialog.py)
7879
ADD_PYTHON_TEST(PyQgsLayoutUnitsComboBox test_qgslayoutunitscombobox.py)
7980
ADD_PYTHON_TEST(PyQgsLineSymbolLayers test_qgslinesymbollayers.py)
8081
ADD_PYTHON_TEST(PyQgsLocator test_qgslocator.py)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsLayoutItemPropertiesDialog
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '18/07/2017'
11+
__copyright__ = 'Copyright 2017, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
from qgis.core import QgsUnitTypes, QgsLayoutSize, QgsLayoutPoint, QgsLayoutItem
18+
from qgis.gui import QgsLayoutItemPropertiesDialog
19+
20+
from qgis.testing import start_app, unittest
21+
22+
start_app()
23+
24+
25+
class TestQgsLayoutItemPropertiesDialog(unittest.TestCase):
26+
27+
def testGettersSetters(self):
28+
""" test dialog getters/setters """
29+
dlg = qgis.gui.QgsLayoutItemPropertiesDialog()
30+
31+
dlg.setItemPosition(QgsLayoutPoint(5, 6, QgsUnitTypes.LayoutPixels))
32+
self.assertEqual(dlg.itemPosition().x(), 5.0)
33+
self.assertEqual(dlg.itemPosition().y(), 6.0)
34+
self.assertEqual(dlg.itemPosition().units(), QgsUnitTypes.LayoutPixels)
35+
36+
dlg.setItemSize(QgsLayoutSize(15, 16, QgsUnitTypes.LayoutInches))
37+
self.assertEqual(dlg.itemSize().width(), 15.0)
38+
self.assertEqual(dlg.itemSize().height(), 16.0)
39+
self.assertEqual(dlg.itemSize().units(), QgsUnitTypes.LayoutInches)
40+
41+
for p in [QgsLayoutItem.UpperLeft, QgsLayoutItem.UpperMiddle, QgsLayoutItem.UpperRight,
42+
QgsLayoutItem.MiddleLeft, QgsLayoutItem.Middle, QgsLayoutItem.MiddleRight,
43+
QgsLayoutItem.LowerLeft, QgsLayoutItem.LowerMiddle, QgsLayoutItem.LowerRight]:
44+
dlg.setReferencePoint(p)
45+
self.assertEqual(dlg.referencePoint(), p)
46+
47+
48+
if __name__ == '__main__':
49+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.