QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
17#include "moc_qgslayoutunitscombobox.cpp"
19#include "qgsunittypes.h"
20#include "qgis.h"
21
23 : QComboBox( parent )
24{
25 QList<Qgis::LayoutUnit> units;
34
35 const auto constUnits = units;
36 for ( const Qgis::LayoutUnit u : constUnits )
37 {
38 addItem( QgsUnitTypes::toAbbreviatedString( u ), static_cast<int>( u ) );
39 setItemData( count() - 1, QgsUnitTypes::toString( u ), Qt::ToolTipRole );
40 }
41 connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, &QgsLayoutUnitsComboBox::indexChanged );
42}
43
45{
46 return static_cast<Qgis::LayoutUnit>( currentData().toInt() );
47}
48
50{
51 setCurrentIndex( findData( static_cast<int>( unit ) ) );
52}
53
54void QgsLayoutUnitsComboBox::linkToWidget( QDoubleSpinBox *widget )
55{
56 mLinkedSpinBoxes << widget;
57}
58
59void QgsLayoutUnitsComboBox::indexChanged( int )
60{
61 const Qgis::LayoutUnit newUnit = unit();
62 if ( mConverter )
63 {
64 const auto constMLinkedSpinBoxes = mLinkedSpinBoxes;
65 for ( const QPointer<QDoubleSpinBox> &widget : constMLinkedSpinBoxes )
66 {
67 if ( widget )
68 whileBlocking( widget.data() )->setValue( mConverter->convert( QgsLayoutMeasurement( widget->value(), mOldUnit ), newUnit ).length() );
69 }
70 }
71 emit unitChanged( newUnit );
72 emit changed( static_cast<int>( newUnit ) );
73 mOldUnit = newUnit;
74}
75
80
85
LayoutUnit
Layout measurement units.
Definition qgis.h:4912
@ Centimeters
Centimeters.
@ Millimeters
Millimeters.
@ Points
Typographic points.
@ Picas
Typographic picas.
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 changed(int unit)
Emitted when the unit is changed.
void setUnit(Qgis::LayoutUnit unit)
Sets the unit currently selected in the combo box.
QgsLayoutUnitsComboBox(QWidget *parent=nullptr)
Constructor for QgsLayoutUnitsComboBox.
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:5928