QGIS API Documentation  3.6.0-Noosa (5873452)
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  Q_FOREACH ( QgsUnitTypes::LayoutUnit u, units )
34  {
35  addItem( QgsUnitTypes::toAbbreviatedString( u ), u );
36  setItemData( count() - 1, QgsUnitTypes::toString( u ), Qt::ToolTipRole );
37  }
38  connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, &QgsLayoutUnitsComboBox::indexChanged );
39 }
40 
42 {
43  return static_cast< QgsUnitTypes::LayoutUnit >( currentData().toInt() );
44 }
45 
47 {
48  setCurrentIndex( findData( unit ) );
49 }
50 
51 void QgsLayoutUnitsComboBox::linkToWidget( QDoubleSpinBox *widget )
52 {
53  mLinkedSpinBoxes << widget;
54 }
55 
56 void QgsLayoutUnitsComboBox::indexChanged( int )
57 {
58  QgsUnitTypes::LayoutUnit newUnit = unit();
59  if ( mConverter )
60  {
61  Q_FOREACH ( const QPointer< QDoubleSpinBox > &widget, mLinkedSpinBoxes )
62  {
63  if ( widget )
64  whileBlocking( widget.data() )->setValue( mConverter->convert( QgsLayoutMeasurement( widget->value(), mOldUnit ), newUnit ).length() );
65  }
66  }
67  emit changed( newUnit );
68  mOldUnit = newUnit;
69 }
70 
72 {
73  return mConverter;
74 }
75 
77 {
78  mConverter = converter;
79 }
80 
81 #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