QGIS API Documentation  3.6.0-Noosa (5873452)
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 "qgsgui.h"
21 
22 #include <QButtonGroup>
23 
24 QgsLayoutItemPropertiesDialog::QgsLayoutItemPropertiesDialog( QWidget *parent, Qt::WindowFlags flags )
25  : QDialog( parent, flags )
26 {
27  setupUi( this );
28 
30 
31  //make button exclusive
32  QButtonGroup *buttonGroup = new QButtonGroup( this );
33  buttonGroup->addButton( mUpperLeftRadioButton );
34  buttonGroup->addButton( mUpperMiddleRadioButton );
35  buttonGroup->addButton( mUpperRightRadioButton );
36  buttonGroup->addButton( mMiddleLeftRadioButton );
37  buttonGroup->addButton( mMiddleRadioButton );
38  buttonGroup->addButton( mMiddleRightRadioButton );
39  buttonGroup->addButton( mLowerLeftRadioButton );
40  buttonGroup->addButton( mLowerMiddleRadioButton );
41  buttonGroup->addButton( mLowerRightRadioButton );
42  buttonGroup->setExclusive( true );
43 
44  QgsSettings settings;
45  double lastWidth = settings.value( QStringLiteral( "LayoutDesigner/lastItemWidth" ), QStringLiteral( "50" ) ).toDouble();
46  double lastHeight = settings.value( QStringLiteral( "LayoutDesigner/lastItemHeight" ), QStringLiteral( "50" ) ).toDouble();
47  QgsUnitTypes::LayoutUnit lastSizeUnit = settings.enumValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), QgsUnitTypes::LayoutMillimeters );
48  setItemSize( QgsLayoutSize( lastWidth, lastHeight, lastSizeUnit ) );
49 
50  mPosUnitsComboBox->linkToWidget( mXPosSpin );
51  mPosUnitsComboBox->linkToWidget( mYPosSpin );
52  mSizeUnitsComboBox->linkToWidget( mWidthSpin );
53  mSizeUnitsComboBox->linkToWidget( mHeightSpin );
54 
55  mLockAspectRatio->setWidthSpinBox( mWidthSpin );
56  mLockAspectRatio->setHeightSpinBox( mHeightSpin );
57 }
58 
60 {
61  // page number
62  QPointF layoutPoint = mLayout->convertToLayoutUnits( position );
63  int page = mLayout->pageCollection()->pageNumberForPoint( layoutPoint );
64 
65  // convert position to relative for current page
66  position = mLayout->convertFromLayoutUnits( mLayout->pageCollection()->positionOnPage( layoutPoint ), position.units() );
67 
68  mPageSpin->setValue( page + 1 );
69  whileBlocking( mPosUnitsComboBox )->setUnit( position.units() );
70  mXPosSpin->setValue( position.x() );
71  mYPosSpin->setValue( position.y() );
72 }
73 
75 {
76  return QgsLayoutPoint( mXPosSpin->value(), mYPosSpin->value(), mPosUnitsComboBox->unit() );
77 }
78 
80 {
81  return mPageSpin->value() - 1;
82 }
83 
85 {
86  mWidthSpin->setValue( size.width() );
87  mHeightSpin->setValue( size.height() );
88  whileBlocking( mSizeUnitsComboBox )->setUnit( size.units() );
89 }
90 
92 {
93  return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
94 }
95 
97 {
98  if ( mUpperLeftRadioButton->isChecked() )
99  {
101  }
102  else if ( mUpperMiddleRadioButton->isChecked() )
103  {
105  }
106  else if ( mUpperRightRadioButton->isChecked() )
107  {
109  }
110  else if ( mMiddleLeftRadioButton->isChecked() )
111  {
113  }
114  else if ( mMiddleRadioButton->isChecked() )
115  {
116  return QgsLayoutItem::Middle;
117  }
118  else if ( mMiddleRightRadioButton->isChecked() )
119  {
121  }
122  else if ( mLowerLeftRadioButton->isChecked() )
123  {
125  }
126  else if ( mLowerMiddleRadioButton->isChecked() )
127  {
129  }
130  else if ( mLowerRightRadioButton->isChecked() )
131  {
133  }
135 }
136 
138 {
139  switch ( point )
140  {
142  mUpperLeftRadioButton->setChecked( true );
143  break;
144 
146  mUpperMiddleRadioButton->setChecked( true );
147  break;
148 
150  mUpperRightRadioButton->setChecked( true );
151  break;
152 
154  mMiddleLeftRadioButton->setChecked( true );
155  break;
156 
158  mMiddleRadioButton->setChecked( true );
159  break;
160 
162  mMiddleRightRadioButton->setChecked( true );
163  break;
164 
166  mLowerLeftRadioButton->setChecked( true );
167  break;
168 
170  mLowerMiddleRadioButton->setChecked( true );
171  break;
172 
174  mLowerRightRadioButton->setChecked( true );
175  break;
176  }
177 }
178 
180 {
181  mSizeUnitsComboBox->setConverter( &layout->renderContext().measurementConverter() );
182  mPosUnitsComboBox->setConverter( &layout->renderContext().measurementConverter() );
183  mLayout = layout;
184 }
void setItemPosition(QgsLayoutPoint position)
Sets the item position to show in the dialog.
Lower left corner of item.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
int pageNumberForPoint(QPointF point) const
Returns the page number corresponding to a point in the layout (in layout units). ...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setReferencePoint(QgsLayoutItem::ReferencePoint point)
Sets the item reference point defined to show in the dialog.
Upper center of item.
void setLayout(QgsLayout *layout)
Sets the layout associated with the dialog.
Lower right corner of item.
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout&#39;s render context, which stores information relating to the current ...
Definition: qgslayout.cpp:357
QgsLayoutItemPropertiesDialog(QWidget *parent=nullptr, Qt::WindowFlags flags=nullptr)
Constructor for QgsLayoutNewItemPropertiesDialog.
This class provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
ReferencePoint
Fixed position reference point.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the point.
Upper right corner of item.
QgsLayoutPoint itemPosition() const
Returns the current item position defined by the dialog.
Lower center of item.
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout&#39;s page collection, which stores and manages page items in the layout...
Definition: qgslayout.cpp:457
Middle right of item.
double y() const
Returns y coordinate of point.
QgsLayoutItem::ReferencePoint referencePoint() const
Returns the item reference point defined by the dialog.
double x() const
Returns x coordinate of point.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
Upper left corner of item.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
Middle left of item.
QPointF positionOnPage(QPointF point) const
Returns the position within a page of a point in the layout (in layout units).
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:212
const QgsLayoutMeasurementConverter & measurementConverter() const
Returns the layout measurement converter to be used in the layout.
Center of item.
QgsLayoutSize itemSize() const
Returns the item size defined by the dialog.
QgsLayoutMeasurement convertFromLayoutUnits(double length, QgsUnitTypes::LayoutUnit unit) const
Converts a length measurement from the layout&#39;s native units to a specified target unit...
Definition: qgslayout.cpp:342
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:104
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:244
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:125
double convertToLayoutUnits(QgsLayoutMeasurement measurement) const
Converts a measurement into the layout&#39;s native units.
Definition: qgslayout.cpp:327
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
double height() const
Returns the height of the size.
Definition: qgslayoutsize.h:90
int page() const
Returns the page number for the new item.
void setItemSize(QgsLayoutSize size)
Sets the item size to show in the dialog.
double width() const
Returns the width of the size.
Definition: qgslayoutsize.h:76