21#include "moc_qgsscalecombobox.cpp"
24#include <QAbstractItemView>
34 setInsertPolicy( QComboBox::NoInsert );
35 setCompleter(
nullptr );
36 connect(
this, qOverload<int>( &QComboBox::activated ),
this, &QgsScaleComboBox::fixupScale );
37 connect( lineEdit(), &QLineEdit::editingFinished,
this, &QgsScaleComboBox::fixupScale );
43 QStringList scalesList;
44 const QString oldScale = currentText();
46 if ( scales.isEmpty() )
55 QStringList cleanedScalesList;
56 for (
const QString &
scale : std::as_const( scalesList ) )
58 const QStringList parts =
scale.split(
':' );
59 if ( parts.size() < 2 )
63 const double denominator = QLocale().toDouble( parts[1], &ok );
66 cleanedScalesList.push_back(
toString( denominator ) );
70 const double denominator = parts[1].toDouble( &ok );
73 cleanedScalesList.push_back(
toString( denominator ) );
80 addItems( cleanedScalesList );
82 blockSignals(
false );
87 if ( scales.isEmpty() )
93 const QString oldScale = currentText();
95 QStringList scalesStringList;
96 scalesStringList.reserve( scales.size() );
97 for (
double denominator : scales )
99 scalesStringList.push_back(
toString( denominator ) );
102 blockSignals(
true );
104 addItems( scalesStringList );
106 blockSignals(
false );
111 QComboBox::showPopup();
113 if ( !currentText().contains(
':' ) )
117 QStringList parts = currentText().split(
':' );
121 const long currScale = parts.at( 1 ).toLong( &ok );
122 long nextScale, delta;
123 for (
int i = 0; i < count(); i++ )
125 parts = itemText( i ).split(
':' );
126 nextScale = parts.at( 1 ).toLong( &ok );
127 delta = std::labs( currScale - nextScale );
135 blockSignals(
true );
136 view()->setCurrentIndex( model()->index( idx, 0 ) );
137 blockSignals(
false );
138 view()->setMinimumWidth( view()->sizeHintForColumn( 0 ) );
148 const double oldScale = mScale;
149 if ( mAllowNull &&
string.trimmed().isEmpty() )
151 mScale = std::numeric_limits<double>::quiet_NaN();
154 if ( !std::isnan( oldScale ) )
162 double newScale =
toDouble(
string, &ok );
163 if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
165 newScale = mMinScale;
176 if ( mScale != oldScale )
191 return std::isnan( mScale );
199void QgsScaleComboBox::fixupScale()
201 if ( mAllowNull && currentText().trimmed().isEmpty() )
203 setScale( std::numeric_limits<double>::quiet_NaN() );
207 const QStringList txtList = currentText().split(
':' );
208 const bool userSetScale = txtList.size() != 2;
211 double newScale =
toDouble( currentText(), &ok );
217 if ( userSetScale && newScale < 1.0 && !
qgsDoubleNear( newScale, 0.0 ) )
219 newScale = 1 / newScale;
231 if ( std::isnan(
scale ) )
237 return QStringLiteral(
"0" );
239 else if (
scale <= 1 )
241 return QStringLiteral(
"%1:1" ).arg( QLocale().
toString(
static_cast<int>( std::round( 1.0 /
scale ) ) ) );
245 return QStringLiteral(
"1:%1" ).arg( QLocale().
toString(
static_cast<float>( std::round(
scale ) ),
'f', 0 ) );
265 QStringList txtList = scaleTxt.split(
':' );
266 if ( 2 == txtList.size() )
272 if ( okX && okY && x != 0 )
275 scale =
static_cast<double>( y ) /
static_cast<double>( x );
292 lineEdit()->setClearButtonEnabled(
allowNull );
304 if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
313 setScale( std::numeric_limits<double>::quiet_NaN() );
void setPredefinedScales(const QVector< double > &scales)
Sets the list of predefined scales to show in the combobox.
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)