25#include <QAbstractItemView>
30#include "moc_qgsscalecombobox.cpp"
32using namespace Qt::StringLiterals;
40 setInsertPolicy( QComboBox::NoInsert );
41 setCompleter(
nullptr );
42 connect(
this, qOverload<int>( &QComboBox::activated ),
this, &QgsScaleComboBox::fixupScale );
43 connect( lineEdit(), &QLineEdit::editingFinished,
this, &QgsScaleComboBox::fixupScale );
49 QStringList scalesList;
50 const QString oldScale = currentText();
52 if ( scales.isEmpty() )
61 QStringList cleanedScalesList;
62 for (
const QString &
scale : std::as_const( scalesList ) )
64 const QStringList parts =
scale.split(
':' );
65 if ( parts.size() < 2 )
69 const double denominator = QLocale().toDouble( parts[1], &ok );
72 cleanedScalesList.push_back(
toString( denominator ) );
76 const double denominator = parts[1].toDouble( &ok );
79 cleanedScalesList.push_back(
toString( denominator ) );
86 addItems( cleanedScalesList );
88 blockSignals(
false );
93 if ( scales.isEmpty() )
99 const QString oldScale = currentText();
101 QStringList scalesStringList;
102 scalesStringList.reserve( scales.size() );
103 for (
double denominator : scales )
105 scalesStringList.push_back(
toString( denominator ) );
108 blockSignals(
true );
110 addItems( scalesStringList );
112 blockSignals(
false );
117 QComboBox::showPopup();
119 if ( !currentText().contains(
':' ) )
123 QStringList parts = currentText().split(
':' );
127 const long currScale = parts.at( 1 ).toLong( &ok );
128 long nextScale, delta;
129 for (
int i = 0; i < count(); i++ )
131 parts = itemText( i ).split(
':' );
132 nextScale = parts.at( 1 ).toLong( &ok );
133 delta = std::labs( currScale - nextScale );
141 blockSignals(
true );
142 view()->setCurrentIndex( model()->index( idx, 0 ) );
143 blockSignals(
false );
144 view()->setMinimumWidth( view()->sizeHintForColumn( 0 ) );
154 const double oldScale = mScale;
155 if ( mAllowNull &&
string.trimmed().isEmpty() )
157 mScale = std::numeric_limits<double>::quiet_NaN();
160 if ( !std::isnan( oldScale ) )
168 double newScale =
toDouble(
string, &ok );
169 if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
171 newScale = mMinScale;
180 setEditText(
toString( mScale, mMode ) );
182 if ( mScale != oldScale )
197 return std::isnan( mScale );
205void QgsScaleComboBox::fixupScale()
207 if ( mAllowNull && currentText().trimmed().isEmpty() )
209 setScale( std::numeric_limits<double>::quiet_NaN() );
213 const QStringList txtList = currentText().split(
':' );
214 const bool userSetScale = txtList.size() != 2;
217 double newScale =
toDouble( currentText(), &ok );
227 if ( userSetScale && newScale < 1.0 && !
qgsDoubleNear( newScale, 0.0 ) )
229 newScale = 1 / newScale;
262 if ( std::isnan(
scale ) )
276 return u
"%1:1"_s.arg( QLocale().
toString(
static_cast<int>( std::round( 1.0 /
scale ) ) ) );
280 return u
"1:%1"_s.arg( QLocale().
toString(
static_cast<float>( std::round(
scale ) ),
'f', 0 ) );
285 qlonglong numerator = 0;
286 qlonglong denominator = 0;
288 return u
"%1:%2"_s.arg(
313 QStringList txtList = scaleTxt.split(
':' );
314 if ( 2 == txtList.size() )
320 if ( okX && okY && x != 0 )
323 scale =
static_cast<double>( y ) /
static_cast<double>( x );
340 lineEdit()->setClearButtonEnabled(
allowNull );
352 if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
361 setScale( std::numeric_limits<double>::quiet_NaN() );
static Q_INVOKABLE void doubleToRational(double value, qlonglong &numerator, qlonglong &denominator, double tolerance=1.0e-9, int maxIterations=100)
Converts a double value to a rational fraction.
void setPredefinedScales(const QVector< double > &scales)
Sets the list of predefined scales to show in the combobox.
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 ratioModeChanged(QgsScaleComboBox::RatioMode mode)
Emitted when the ratio mode for the widget is changed.
void showPopup() override
void setNull()
Sets the combo box to the null value.
RatioMode
Scale ratio modes.
@ Flexible
Allows numerator values other than 1, e.g: "2:3".
@ ForceUnitNumerator
Default mode, forces the scale numerator to be 1, e.g. "1:1000".
void setRatioMode(QgsScaleComboBox::RatioMode mode)
Sets the ratio mode for the scale.
void setMinScale(double scale)
Set the minimum allowed scale.
static QString toString(double scale, QgsScaleComboBox::RatioMode mode=QgsScaleComboBox::RatioMode::ForceUnitNumerator)
Helper function to convert a scale double to scale string.
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
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).