23#include <QAbstractItemView>
33 setInsertPolicy( QComboBox::NoInsert );
34 setCompleter(
nullptr );
35 connect(
this, qOverload< int >( &QComboBox::activated ),
this, &QgsScaleComboBox::fixupScale );
36 connect( lineEdit(), &QLineEdit::editingFinished,
this, &QgsScaleComboBox::fixupScale );
42 QStringList myScalesList;
43 const QString oldScale = currentText();
45 if ( scales.isEmpty() )
51 QStringList::const_iterator scaleIt = scales.constBegin();
52 for ( ; scaleIt != scales.constEnd(); ++scaleIt )
54 myScalesList.append( *scaleIt );
61 for (
int i = 0; i < myScalesList.size(); ++i )
63 parts = myScalesList[ i ] .split(
':' );
64 denominator = QLocale().toDouble( parts[1], &ok );
67 myScalesList[ i ] =
toString( denominator );
73 addItems( myScalesList );
75 blockSignals(
false );
80 QComboBox::showPopup();
82 if ( !currentText().contains(
':' ) )
86 QStringList parts = currentText().split(
':' );
90 const long currScale = parts.at( 1 ).toLong( &ok );
91 long nextScale, delta;
92 for (
int i = 0; i < count(); i++ )
94 parts = itemText( i ).split(
':' );
95 nextScale = parts.at( 1 ).toLong( &ok );
96 delta = std::labs( currScale - nextScale );
104 blockSignals(
true );
105 view()->setCurrentIndex( model()->index( idx, 0 ) );
106 blockSignals(
false );
107 view()->setMinimumWidth( view()->sizeHintForColumn( 0 ) );
117 const double oldScale = mScale;
118 if ( mAllowNull &&
string.trimmed().isEmpty() )
120 mScale = std::numeric_limits< double >::quiet_NaN();
123 if ( !std::isnan( oldScale ) )
131 double newScale =
toDouble(
string, &ok );
132 if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
134 newScale = mMinScale;
145 if ( mScale != oldScale )
160 return std::isnan( mScale );
168void QgsScaleComboBox::fixupScale()
170 if ( mAllowNull && currentText().trimmed().isEmpty() )
172 setScale( std::numeric_limits< double >::quiet_NaN() );
176 const QStringList txtList = currentText().split(
':' );
177 const bool userSetScale = txtList.size() != 2;
180 double newScale =
toDouble( currentText(), &ok );
186 if ( userSetScale && newScale < 1.0 && !
qgsDoubleNear( newScale, 0.0 ) )
188 newScale = 1 / newScale;
200 if ( std::isnan(
scale ) )
206 return QStringLiteral(
"0" );
208 else if (
scale <= 1 )
210 return QStringLiteral(
"%1:1" ).arg( QLocale().
toString(
static_cast< int >( std::round( 1.0 /
scale ) ) ) );
214 return QStringLiteral(
"1:%1" ).arg( QLocale().
toString(
static_cast< float >( std::round(
scale ) ),
'f', 0 ) );
234 QStringList txtList = scaleTxt.split(
':' );
235 if ( 2 == txtList.size() )
241 if ( okX && okY && x != 0 )
244 scale =
static_cast< double >( y ) /
static_cast< double >( x );
261 lineEdit()->setClearButtonEnabled(
allowNull );
273 if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
282 setScale( std::numeric_limits< double >::quiet_NaN() );
static QString toString(double scale)
Helper function to convert a scale double to scale string.
void updateScales(const QStringList &scales=QStringList())
Sets the list of predefined scales to show in the combobox.
QString scaleString() const
Returns the selected scale as a string, e.g.
bool setScaleString(const QString &string)
Set the selected scale from a string, e.g.
void setAllowNull(bool allowNull)
Sets whether the scale combobox can be set to a NULL value.
QgsScaleComboBox(QWidget *parent=nullptr)
Constructor for QgsScaleComboBox.
bool isNull() const
Returns true if the combo box is currently set to a "null" value.
bool allowNull() const
Returns true if the combobox can be set to a NULL value.
static double toDouble(const QString &string, bool *ok=nullptr)
Helper function to convert a scale string to double.
void setScale(double scale)
Set the selected scale from a double.
void showPopup() override
void setNull()
Sets the combo box to the null value.
void setMinScale(double scale)
Set the minimum allowed scale.
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static const QgsSettingsEntryStringList * settingsMapScales
double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
int qgsPermissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, e.g., allowing for incorrect numbers of digits b...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)