QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgslayoutunitscombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutunitscombobox.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
18#include "qgsunittypes.h"
19#include "qgis.h"
20
22 : QComboBox( parent )
23{
24 QList< Qgis::LayoutUnit > units;
25 units << Qgis::LayoutUnit::Millimeters
26 << Qgis::LayoutUnit::Centimeters
27 << Qgis::LayoutUnit::Meters
28 << Qgis::LayoutUnit::Inches
29 << Qgis::LayoutUnit::Feet
30 << Qgis::LayoutUnit::Points
31 << Qgis::LayoutUnit::Picas
32 << Qgis::LayoutUnit::Pixels;
33
34 const auto constUnits = units;
35 for ( const Qgis::LayoutUnit u : constUnits )
36 {
37 addItem( QgsUnitTypes::toAbbreviatedString( u ), static_cast< int >( u ) );
38 setItemData( count() - 1, QgsUnitTypes::toString( u ), Qt::ToolTipRole );
39 }
40 connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, &QgsLayoutUnitsComboBox::indexChanged );
41}
42
44{
45 return static_cast< Qgis::LayoutUnit >( currentData().toInt() );
46}
47
49{
50 setCurrentIndex( findData( static_cast< int >( unit ) ) );
51}
52
53void QgsLayoutUnitsComboBox::linkToWidget( QDoubleSpinBox *widget )
54{
55 mLinkedSpinBoxes << widget;
56}
57
58void QgsLayoutUnitsComboBox::indexChanged( int )
59{
60 const Qgis::LayoutUnit newUnit = unit();
61 if ( mConverter )
62 {
63 const auto constMLinkedSpinBoxes = mLinkedSpinBoxes;
64 for ( const QPointer< QDoubleSpinBox > &widget : constMLinkedSpinBoxes )
65 {
66 if ( widget )
67 whileBlocking( widget.data() )->setValue( mConverter->convert( QgsLayoutMeasurement( widget->value(), mOldUnit ), newUnit ).length() );
68 }
69 }
70 emit unitChanged( newUnit );
71 emit changed( static_cast<int>( newUnit ) );
72 mOldUnit = newUnit;
73}
74
76{
77 return mConverter;
78}
79
81{
82 mConverter = converter;
83}
84
LayoutUnit
Layout measurement units.
Definition: qgis.h:3461
This class provides a method of converting QgsLayoutMeasurements from one unit to another.
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, Qgis::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
double length() const
Returns the length of the measurement.
A custom combo box for selecting units for layout settings.
void linkToWidget(QDoubleSpinBox *widget)
Registers a spin box widget as linked with the combo box.
QgsLayoutMeasurementConverter * converter() const
Returns the converter used when automatically converting units for linked widgets.
void unitChanged(Qgis::LayoutUnit unit)
Emitted when the unit is changed.
void setConverter(QgsLayoutMeasurementConverter *converter)
Sets a converter to use when automatically converting units for linked widgets.
void setUnit(Qgis::LayoutUnit unit)
Sets the unit currently selected in the combo box.
QgsLayoutUnitsComboBox(QWidget *parent=nullptr)
Constructor for QgsLayoutUnitsComboBox.
void changed(int)
Emitted when the unit is changed.
static Q_INVOKABLE QString toString(Qgis::DistanceUnit unit)
Returns a translated string representing a distance unit.
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:3914