QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include "qgis.h"
20#include "qgsunittypes.h"
21
22#include "moc_qgslayoutunitscombobox.cpp"
23
25 : QComboBox( parent )
26{
27 QList<Qgis::LayoutUnit> units;
36
37 const auto constUnits = units;
38 for ( const Qgis::LayoutUnit u : constUnits )
39 {
40 addItem( QgsUnitTypes::toAbbreviatedString( u ), static_cast<int>( u ) );
41 setItemData( count() - 1, QgsUnitTypes::toString( u ), Qt::ToolTipRole );
42 }
43 connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, &QgsLayoutUnitsComboBox::indexChanged );
44}
45
47{
48 return static_cast<Qgis::LayoutUnit>( currentData().toInt() );
49}
50
52{
53 setCurrentIndex( findData( static_cast<int>( unit ) ) );
54}
55
56void QgsLayoutUnitsComboBox::linkToWidget( QDoubleSpinBox *widget )
57{
58 mLinkedSpinBoxes << widget;
59}
60
61void QgsLayoutUnitsComboBox::indexChanged( int )
62{
63 const Qgis::LayoutUnit newUnit = unit();
64 if ( mConverter )
65 {
66 const auto constMLinkedSpinBoxes = mLinkedSpinBoxes;
67 for ( const QPointer<QDoubleSpinBox> &widget : constMLinkedSpinBoxes )
68 {
69 if ( widget )
70 whileBlocking( widget.data() )->setValue( mConverter->convert( QgsLayoutMeasurement( widget->value(), mOldUnit ), newUnit ).length() );
71 }
72 }
73 emit unitChanged( newUnit );
74 emit changed( static_cast<int>( newUnit ) );
75 mOldUnit = newUnit;
76}
77
82
87
LayoutUnit
Layout measurement units.
Definition qgis.h:5203
@ Feet
Feet.
Definition qgis.h:5208
@ Centimeters
Centimeters.
Definition qgis.h:5205
@ Millimeters
Millimeters.
Definition qgis.h:5204
@ Points
Typographic points.
Definition qgis.h:5209
@ Meters
Meters.
Definition qgis.h:5206
@ Picas
Typographic picas.
Definition qgis.h:5210
@ Pixels
Pixels.
Definition qgis.h:5211
@ Inches
Inches.
Definition qgis.h:5207
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.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
double length() const
Returns the length of the measurement.
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:6511