QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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"
QgsLayoutUnitsComboBox::QgsLayoutUnitsComboBox
QgsLayoutUnitsComboBox(QWidget *parent=nullptr)
Constructor for QgsLayoutUnitsComboBox.
Definition: qgslayoutunitscombobox.cpp:20
QgsUnitTypes::LayoutPixels
@ LayoutPixels
Pixels.
Definition: qgsunittypes.h:189
QgsLayoutUnitsComboBox::changed
void changed(QgsUnitTypes::LayoutUnit unit)
Emitted when the unit is changed.
QgsLayoutMeasurement::length
double length() const
Returns the length of the measurement.
Definition: qgslayoutmeasurement.h:48
QgsUnitTypes::toAbbreviatedString
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
Definition: qgsunittypes.cpp:269
QgsUnitTypes::LayoutUnit
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:181
qgis.h
QgsUnitTypes::LayoutFeet
@ LayoutFeet
Feet.
Definition: qgsunittypes.h:186
QgsUnitTypes::LayoutCentimeters
@ LayoutCentimeters
Centimeters.
Definition: qgsunittypes.h:183
QgsLayoutMeasurementConverter::convert
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
Definition: qgslayoutmeasurementconverter.cpp:21
QgsUnitTypes::LayoutInches
@ LayoutInches
Inches.
Definition: qgsunittypes.h:185
QgsLayoutUnitsComboBox::setConverter
void setConverter(QgsLayoutMeasurementConverter *converter)
Sets a converter to use when automatically converting units for linked widgets.
Definition: qgslayoutunitscombobox.cpp:78
qgslayoutmeasurementconverter.h
QgsLayoutUnitsComboBox::converter
QgsLayoutMeasurementConverter * converter() const
Returns the converter used when automatically converting units for linked widgets.
Definition: qgslayoutunitscombobox.cpp:73
QgsLayoutUnitsComboBox::unit
QgsUnitTypes::LayoutUnit unit
Definition: qgslayoutunitscombobox.h:36
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsUnitTypes::toString
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
Definition: qgsunittypes.cpp:199
QgsUnitTypes::LayoutPicas
@ LayoutPicas
Typographic picas.
Definition: qgsunittypes.h:188
qgslayoutunitscombobox.h
QgsLayoutUnitsComboBox
A custom combo box for selecting units for layout settings.
Definition: qgslayoutunitscombobox.h:34
QgsLayoutMeasurementConverter
This class provides a method of converting QgsLayoutMeasurements from one unit to another.
Definition: qgslayoutmeasurementconverter.h:41
QgsLayoutUnitsComboBox::setUnit
void setUnit(QgsUnitTypes::LayoutUnit unit)
Sets the unit currently selected in the combo box.
Definition: qgslayoutunitscombobox.cpp:47
QgsUnitTypes::LayoutMillimeters
@ LayoutMillimeters
Millimeters.
Definition: qgsunittypes.h:182
QgsLayoutUnitsComboBox::linkToWidget
void linkToWidget(QDoubleSpinBox *widget)
Registers a spin box widget as linked with the combo box.
Definition: qgslayoutunitscombobox.cpp:52
QgsUnitTypes::LayoutPoints
@ LayoutPoints
Typographic points.
Definition: qgsunittypes.h:187
QgsUnitTypes::LayoutMeters
@ LayoutMeters
Meters.
Definition: qgsunittypes.h:184
QgsLayoutMeasurement
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
Definition: qgslayoutmeasurement.h:34