QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
16 #include "qgslayoutunitscombobox.h"
18 #include "qgis.h"
19 
21  : QComboBox( parent )
22 {
23  QList< QgsUnitTypes::LayoutUnit > units;
32 
33  const auto constUnits = units;
34  for ( QgsUnitTypes::LayoutUnit u : constUnits )
35  {
36  addItem( QgsUnitTypes::toAbbreviatedString( u ), u );
37  setItemData( count() - 1, QgsUnitTypes::toString( u ), Qt::ToolTipRole );
38  }
39  connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, &QgsLayoutUnitsComboBox::indexChanged );
40 }
41 
43 {
44  return static_cast< QgsUnitTypes::LayoutUnit >( currentData().toInt() );
45 }
46 
48 {
49  setCurrentIndex( findData( unit ) );
50 }
51 
52 void QgsLayoutUnitsComboBox::linkToWidget( QDoubleSpinBox *widget )
53 {
54  mLinkedSpinBoxes << widget;
55 }
56 
57 void QgsLayoutUnitsComboBox::indexChanged( int )
58 {
59  QgsUnitTypes::LayoutUnit newUnit = unit();
60  if ( mConverter )
61  {
62  const auto constMLinkedSpinBoxes = mLinkedSpinBoxes;
63  for ( const QPointer< QDoubleSpinBox > &widget : constMLinkedSpinBoxes )
64  {
65  if ( widget )
66  whileBlocking( widget.data() )->setValue( mConverter->convert( QgsLayoutMeasurement( widget->value(), mOldUnit ), newUnit ).length() );
67  }
68  }
69  emit changed( newUnit );
70  mOldUnit = newUnit;
71 }
72 
74 {
75  return mConverter;
76 }
77 
79 {
80  mConverter = converter;
81 }
82 
83 #include "qgslayoutunitscombobox.h"
This class provides a method of converting QgsLayoutMeasurements from one unit to another.
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::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 changed(QgsUnitTypes::LayoutUnit unit)
Emitted when the unit is changed.
void setConverter(QgsLayoutMeasurementConverter *converter)
Sets a converter to use when automatically converting units for linked widgets.
QgsUnitTypes::LayoutUnit unit
void setUnit(QgsUnitTypes::LayoutUnit unit)
Sets the unit currently selected in the combo box.
QgsLayoutUnitsComboBox(QWidget *parent=nullptr)
Constructor for QgsLayoutUnitsComboBox.
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:182
@ LayoutInches
Inches.
Definition: qgsunittypes.h:186
@ LayoutMillimeters
Millimeters.
Definition: qgsunittypes.h:183
@ LayoutFeet
Feet.
Definition: qgsunittypes.h:187
@ LayoutCentimeters
Centimeters.
Definition: qgsunittypes.h:184
@ LayoutPixels
Pixels.
Definition: qgsunittypes.h:190
@ LayoutPicas
Typographic picas.
Definition: qgsunittypes.h:189
@ LayoutPoints
Typographic points.
Definition: qgsunittypes.h:188
@ LayoutMeters
Meters.
Definition: qgsunittypes.h:185
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::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:537