QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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"
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
void setUnit(QgsUnitTypes::LayoutUnit unit)
Sets the unit currently selected in the combo box.
Typographic points.
Definition: qgsunittypes.h:132
void linkToWidget(QDoubleSpinBox *widget)
Registers a spin box widget as linked with the combo box.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
void changed(QgsUnitTypes::LayoutUnit unit)
Emitted when the unit is changed.
QgsUnitTypes::LayoutUnit unit() const
Returns the unit currently selected in the combo box.
QgsLayoutUnitsComboBox(QWidget *parent=nullptr)
Constructor for QgsLayoutUnitsComboBox.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:212
double length() const
Returns the length of the measurement.
A custom combo box for selecting units for layout settings.
This class provides a method of converting QgsLayoutMeasurements from one unit to another...
void setConverter(QgsLayoutMeasurementConverter *converter)
Sets a converter to use when automatically converting units for linked widgets.
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:125
QgsLayoutMeasurementConverter * converter() const
Returns the converter used when automatically converting units for linked widgets.
Typographic picas.
Definition: qgsunittypes.h:133