QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
20  : QComboBox( parent )
21 {
22  QList< QgsUnitTypes::LayoutUnit > units;
31 
32  Q_FOREACH ( QgsUnitTypes::LayoutUnit u, units )
33  {
34  addItem( QgsUnitTypes::toAbbreviatedString( u ), u );
35  setItemData( count() - 1, QgsUnitTypes::toString( u ), Qt::ToolTipRole );
36  }
37  connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, &QgsLayoutUnitsComboBox::indexChanged );
38 }
39 
41 {
42  return static_cast< QgsUnitTypes::LayoutUnit >( currentData().toInt() );
43 }
44 
46 {
47  setCurrentIndex( findData( unit ) );
48 }
49 
50 void QgsLayoutUnitsComboBox::linkToWidget( QDoubleSpinBox *widget )
51 {
52  mLinkedSpinBoxes << widget;
53 }
54 
55 void QgsLayoutUnitsComboBox::indexChanged( int )
56 {
57  QgsUnitTypes::LayoutUnit newUnit = unit();
58  if ( mConverter )
59  {
60  Q_FOREACH ( const QPointer< QDoubleSpinBox > &widget, mLinkedSpinBoxes )
61  {
62  if ( widget )
63  whileBlocking( widget.data() )->setValue( mConverter->convert( QgsLayoutMeasurement( widget->value(), mOldUnit ), newUnit ).length() );
64  }
65  }
66  emit changed( newUnit );
67  mOldUnit = newUnit;
68 }
69 
71 {
72  return mConverter;
73 }
74 
76 {
77  mConverter = converter;
78 }
79 
80 #include "qgslayoutunitscombobox.h"
QgsUnitTypes::LayoutUnit unit() const
Returns the unit currently selected in the combo box.
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
void setUnit(QgsUnitTypes::LayoutUnit unit)
Sets the unit currently selected in the combo box.
double length() const
Returns the length of the measurement.
Typographic points.
Definition: qgsunittypes.h:131
void linkToWidget(QDoubleSpinBox *widget)
Registers a spin box widget as linked with the combo box.
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
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.
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:225
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:124
QgsLayoutMeasurementConverter * converter() const
Returns the converter used when automatically converting units for linked widgets.
Typographic picas.
Definition: qgsunittypes.h:132