QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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;
28 units
37
38 const auto constUnits = units;
39 for ( const Qgis::LayoutUnit u : constUnits )
40 {
41 addItem( QgsUnitTypes::toAbbreviatedString( u ), static_cast<int>( u ) );
42 setItemData( count() - 1, QgsUnitTypes::toString( u ), Qt::ToolTipRole );
43 }
44 connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, &QgsLayoutUnitsComboBox::indexChanged );
45}
46
48{
49 return static_cast<Qgis::LayoutUnit>( currentData().toInt() );
50}
51
53{
54 setCurrentIndex( findData( static_cast<int>( unit ) ) );
55}
56
57void QgsLayoutUnitsComboBox::linkToWidget( QDoubleSpinBox *widget )
58{
59 mLinkedSpinBoxes << widget;
60}
61
62void QgsLayoutUnitsComboBox::indexChanged( int )
63{
64 const Qgis::LayoutUnit newUnit = unit();
65 if ( mConverter )
66 {
67 const auto constMLinkedSpinBoxes = mLinkedSpinBoxes;
68 for ( const QPointer<QDoubleSpinBox> &widget : constMLinkedSpinBoxes )
69 {
70 if ( widget )
71 whileBlocking( widget.data() )->setValue( mConverter->convert( QgsLayoutMeasurement( widget->value(), mOldUnit ), newUnit ).length() );
72 }
73 }
74 emit unitChanged( newUnit );
75 emit changed( static_cast<int>( newUnit ) );
76 mOldUnit = newUnit;
77}
78
83
88
LayoutUnit
Layout measurement units.
Definition qgis.h:5360
@ Feet
Feet.
Definition qgis.h:5365
@ Centimeters
Centimeters.
Definition qgis.h:5362
@ Millimeters
Millimeters.
Definition qgis.h:5361
@ Points
Typographic points.
Definition qgis.h:5366
@ Meters
Meters.
Definition qgis.h:5363
@ Picas
Typographic picas.
Definition qgis.h:5367
@ Pixels
Pixels.
Definition qgis.h:5368
@ Inches
Inches.
Definition qgis.h:5364
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:6880