QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgslayoutnewitempropertiesdialog.cpp
Go to the documentation of this file.
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 
17 #include "qgssettings.h"
18 #include "qgslayout.h"
20 #include "qgshelp.h"
21 #include "qgsgui.h"
22 
23 #include <QButtonGroup>
24 
25 QgsLayoutItemPropertiesDialog::QgsLayoutItemPropertiesDialog( QWidget *parent, Qt::WindowFlags flags )
26  : QDialog( parent, flags )
27 {
28  setupUi( this );
29 
31 
32  //make button exclusive
33  QButtonGroup *buttonGroup = new QButtonGroup( this );
34  buttonGroup->addButton( mUpperLeftRadioButton );
35  buttonGroup->addButton( mUpperMiddleRadioButton );
36  buttonGroup->addButton( mUpperRightRadioButton );
37  buttonGroup->addButton( mMiddleLeftRadioButton );
38  buttonGroup->addButton( mMiddleRadioButton );
39  buttonGroup->addButton( mMiddleRightRadioButton );
40  buttonGroup->addButton( mLowerLeftRadioButton );
41  buttonGroup->addButton( mLowerMiddleRadioButton );
42  buttonGroup->addButton( mLowerRightRadioButton );
43  buttonGroup->setExclusive( true );
44 
45  QgsSettings settings;
46  double lastWidth = settings.value( QStringLiteral( "LayoutDesigner/lastItemWidth" ), QStringLiteral( "50" ) ).toDouble();
47  double lastHeight = settings.value( QStringLiteral( "LayoutDesigner/lastItemHeight" ), QStringLiteral( "50" ) ).toDouble();
48  QgsUnitTypes::LayoutUnit lastSizeUnit = settings.enumValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), QgsUnitTypes::LayoutMillimeters );
49  setItemSize( QgsLayoutSize( lastWidth, lastHeight, lastSizeUnit ) );
50 
51  mPosUnitsComboBox->linkToWidget( mXPosSpin );
52  mPosUnitsComboBox->linkToWidget( mYPosSpin );
53  mSizeUnitsComboBox->linkToWidget( mWidthSpin );
54  mSizeUnitsComboBox->linkToWidget( mHeightSpin );
55 
56  mLockAspectRatio->setWidthSpinBox( mWidthSpin );
57  mLockAspectRatio->setHeightSpinBox( mHeightSpin );
58 
59  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutItemPropertiesDialog::showHelp );
60 }
61 
63 {
64  // page number
65  QPointF layoutPoint = mLayout->convertToLayoutUnits( position );
66  int page = mLayout->pageCollection()->pageNumberForPoint( layoutPoint );
67 
68  // convert position to relative for current page
69  position = mLayout->convertFromLayoutUnits( mLayout->pageCollection()->positionOnPage( layoutPoint ), position.units() );
70 
71  mPageSpin->setValue( page + 1 );
72  whileBlocking( mPosUnitsComboBox )->setUnit( position.units() );
73  mXPosSpin->setValue( position.x() );
74  mYPosSpin->setValue( position.y() );
75 }
76 
78 {
79  return QgsLayoutPoint( mXPosSpin->value(), mYPosSpin->value(), mPosUnitsComboBox->unit() );
80 }
81 
83 {
84  return mPageSpin->value() - 1;
85 }
86 
88 {
89  mWidthSpin->setValue( size.width() );
90  mHeightSpin->setValue( size.height() );
91  whileBlocking( mSizeUnitsComboBox )->setUnit( size.units() );
92 }
93 
95 {
96  return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
97 }
98 
100 {
101  if ( mUpperLeftRadioButton->isChecked() )
102  {
104  }
105  else if ( mUpperMiddleRadioButton->isChecked() )
106  {
108  }
109  else if ( mUpperRightRadioButton->isChecked() )
110  {
112  }
113  else if ( mMiddleLeftRadioButton->isChecked() )
114  {
116  }
117  else if ( mMiddleRadioButton->isChecked() )
118  {
119  return QgsLayoutItem::Middle;
120  }
121  else if ( mMiddleRightRadioButton->isChecked() )
122  {
124  }
125  else if ( mLowerLeftRadioButton->isChecked() )
126  {
128  }
129  else if ( mLowerMiddleRadioButton->isChecked() )
130  {
132  }
133  else if ( mLowerRightRadioButton->isChecked() )
134  {
136  }
138 }
139 
141 {
142  switch ( point )
143  {
145  mUpperLeftRadioButton->setChecked( true );
146  break;
147 
149  mUpperMiddleRadioButton->setChecked( true );
150  break;
151 
153  mUpperRightRadioButton->setChecked( true );
154  break;
155 
157  mMiddleLeftRadioButton->setChecked( true );
158  break;
159 
161  mMiddleRadioButton->setChecked( true );
162  break;
163 
165  mMiddleRightRadioButton->setChecked( true );
166  break;
167 
169  mLowerLeftRadioButton->setChecked( true );
170  break;
171 
173  mLowerMiddleRadioButton->setChecked( true );
174  break;
175 
177  mLowerRightRadioButton->setChecked( true );
178  break;
179  }
180 }
181 
183 {
184  mSizeUnitsComboBox->setConverter( &layout->renderContext().measurementConverter() );
185  mPosUnitsComboBox->setConverter( &layout->renderContext().measurementConverter() );
186  mLayout = layout;
187 }
188 
189 void QgsLayoutItemPropertiesDialog::showHelp()
190 {
191  QgsHelp::openHelp( QStringLiteral( "print_composer/composer_items/composer_items_options.html#creating-a-layout-item" ) );
192 }
QgsLayoutRenderContext::measurementConverter
const QgsLayoutMeasurementConverter & measurementConverter() const
Returns the layout measurement converter to be used in the layout.
Definition: qgslayoutrendercontext.h:129
QgsLayoutSize::width
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76
QgsLayoutSize::units
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
Definition: qgslayoutsize.h:103
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsLayoutItem::Middle
@ Middle
Center of item.
Definition: qgslayoutitem.h:207
QgsLayoutItem::MiddleRight
@ MiddleRight
Middle right of item.
Definition: qgslayoutitem.h:208
qgsgui.h
QgsLayoutItem::MiddleLeft
@ MiddleLeft
Middle left of item.
Definition: qgslayoutitem.h:206
QgsUnitTypes::LayoutUnit
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:181
QgsLayoutPoint::y
double y() const
Returns y coordinate of point.
Definition: qgslayoutpoint.h:86
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsLayoutItem::ReferencePoint
ReferencePoint
Fixed position reference point.
Definition: qgslayoutitem.h:202
qgslayoutnewitempropertiesdialog.h
QgsLayoutItemPropertiesDialog::setLayout
void setLayout(QgsLayout *layout)
Sets the layout associated with the dialog.
Definition: qgslayoutnewitempropertiesdialog.cpp:182
QgsLayoutItemPropertiesDialog::setReferencePoint
void setReferencePoint(QgsLayoutItem::ReferencePoint point)
Sets the item reference point defined to show in the dialog.
Definition: qgslayoutnewitempropertiesdialog.cpp:140
QgsLayoutItemPropertiesDialog::setItemPosition
void setItemPosition(QgsLayoutPoint position)
Sets the item position to show in the dialog.
Definition: qgslayoutnewitempropertiesdialog.cpp:62
QgsLayoutItemPropertiesDialog::page
int page() const
Returns the page number for the new item.
Definition: qgslayoutnewitempropertiesdialog.cpp:82
QgsGui::enableAutoGeometryRestore
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:139
QgsLayoutPageCollection::pageNumberForPoint
int pageNumberForPoint(QPointF point) const
Returns the page number corresponding to a point in the layout (in layout units).
Definition: qgslayoutpagecollection.cpp:155
QgsLayoutItemPropertiesDialog::itemPosition
QgsLayoutPoint itemPosition() const
Returns the current item position defined by the dialog.
Definition: qgslayoutnewitempropertiesdialog.cpp:77
QgsLayoutPageCollection::positionOnPage
QPointF positionOnPage(QPointF point) const
Returns the position within a page of a point in the layout (in layout units).
Definition: qgslayoutpagecollection.cpp:243
QgsLayout::renderContext
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
Definition: qgslayout.cpp:359
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsLayoutItemPropertiesDialog::referencePoint
QgsLayoutItem::ReferencePoint referencePoint() const
Returns the item reference point defined by the dialog.
Definition: qgslayoutnewitempropertiesdialog.cpp:99
QgsLayoutItemPropertiesDialog::QgsLayoutItemPropertiesDialog
QgsLayoutItemPropertiesDialog(QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsLayoutNewItemPropertiesDialog.
Definition: qgslayoutnewitempropertiesdialog.cpp:25
qgslayout.h
QgsLayoutItem::LowerRight
@ LowerRight
Lower right corner of item.
Definition: qgslayoutitem.h:211
QgsLayoutSize::height
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
QgsLayoutItem::LowerLeft
@ LowerLeft
Lower left corner of item.
Definition: qgslayoutitem.h:209
QgsLayout::pageCollection
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
Definition: qgslayout.cpp:459
QgsLayoutItemPropertiesDialog::itemSize
QgsLayoutSize itemSize() const
Returns the item size defined by the dialog.
Definition: qgslayoutnewitempropertiesdialog.cpp:94
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
qgssettings.h
QgsLayoutItem::UpperRight
@ UpperRight
Upper right corner of item.
Definition: qgslayoutitem.h:205
qgslayoutpagecollection.h
QgsLayoutSize
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:41
QgsLayoutItemPropertiesDialog::setItemSize
void setItemSize(QgsLayoutSize size)
Sets the item size to show in the dialog.
Definition: qgslayoutnewitempropertiesdialog.cpp:87
QgsLayoutItem::UpperMiddle
@ UpperMiddle
Upper center of item.
Definition: qgslayoutitem.h:204
QgsUnitTypes::LayoutMillimeters
@ LayoutMillimeters
Millimeters.
Definition: qgsunittypes.h:182
QgsSettings::enumValue
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
Definition: qgssettings.h:252
QgsLayoutPoint
This class provides a method of storing points, consisting of an x and y coordinate,...
Definition: qgslayoutpoint.h:40
QgsLayout::convertToLayoutUnits
double convertToLayoutUnits(QgsLayoutMeasurement measurement) const
Converts a measurement into the layout's native units.
Definition: qgslayout.cpp:329
QgsLayoutItem::UpperLeft
@ UpperLeft
Upper left corner of item.
Definition: qgslayoutitem.h:203
qgshelp.h
QgsLayoutPoint::x
double x() const
Returns x coordinate of point.
Definition: qgslayoutpoint.h:72
QgsLayout::convertFromLayoutUnits
QgsLayoutMeasurement convertFromLayoutUnits(double length, QgsUnitTypes::LayoutUnit unit) const
Converts a length measurement from the layout's native units to a specified target unit.
Definition: qgslayout.cpp:344
QgsLayoutPoint::units
QgsUnitTypes::LayoutUnit units() const
Returns the units for the point.
Definition: qgslayoutpoint.h:99
QgsLayoutItem::LowerMiddle
@ LowerMiddle
Lower center of item.
Definition: qgslayoutitem.h:210