23 #include <QAbstractItemView> 33 setInsertPolicy( QComboBox::NoInsert );
34 setCompleter(
nullptr );
35 connect(
this,
static_cast<void ( QComboBox::* )(
const QString & )
>( &QComboBox::activated ),
this, &QgsScaleComboBox::fixupScale );
36 connect( lineEdit(), &QLineEdit::editingFinished,
this, &QgsScaleComboBox::fixupScale );
42 QStringList myScalesList;
43 QString oldScale = currentText();
45 if ( scales.isEmpty() )
48 QString myScales = settings.
value( QStringLiteral(
"Map/scales" ),
PROJECT_SCALES ).toString();
49 if ( !myScales.isEmpty() )
51 myScalesList = myScales.split(
',' );
56 QStringList::const_iterator scaleIt = scales.constBegin();
57 for ( ; scaleIt != scales.constEnd(); ++scaleIt )
59 myScalesList.append( *scaleIt );
66 for (
int i = 0; i < myScalesList.size(); ++i )
68 parts = myScalesList[ i ] .split(
':' );
69 denominator = QLocale().toDouble( parts[1], &ok );
72 myScalesList[ i ] =
toString( denominator );
78 addItems( myScalesList );
80 blockSignals(
false );
85 QComboBox::showPopup();
87 if ( !currentText().contains(
':' ) )
91 QStringList parts = currentText().split(
':' );
95 long currScale = parts.at( 1 ).toLong( &ok );
96 long nextScale, delta;
97 for (
int i = 0; i < count(); i++ )
99 parts = itemText( i ).split(
':' );
100 nextScale = parts.at( 1 ).toLong( &ok );
101 delta = std::labs( currScale - nextScale );
109 blockSignals(
true );
110 view()->setCurrentIndex( model()->index( idx, 0 ) );
111 blockSignals(
false );
112 view()->setMinimumWidth( view()->sizeHintForColumn( 0 ) );
122 double oldScale = mScale;
123 if ( mAllowNull &&
string.trimmed().isEmpty() )
125 mScale = std::numeric_limits< double >::quiet_NaN();
128 if ( !std::isnan( oldScale ) )
136 double newScale =
toDouble(
string, &ok );
137 if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
139 newScale = mMinScale;
150 if ( mScale != oldScale )
165 return std::isnan( mScale );
173 void QgsScaleComboBox::fixupScale()
175 if ( mAllowNull && currentText().trimmed().isEmpty() )
177 setScale( std::numeric_limits< double >::quiet_NaN() );
181 QStringList txtList = currentText().split(
':' );
182 bool userSetScale = txtList.size() != 2;
185 double newScale =
toDouble( currentText(), &ok );
191 if ( userSetScale && newScale < 1.0 && !
qgsDoubleNear( newScale, 0.0 ) )
193 newScale = 1 / newScale;
205 if ( std::isnan( scale ) )
211 return QStringLiteral(
"0" );
213 else if ( scale <= 1 )
215 return QStringLiteral(
"%1:1" ).arg( QLocale().
toString( static_cast< int >( std::round( 1.0 / scale ) ) ) );
219 return QStringLiteral(
"1:%1" ).arg( QLocale().
toString( static_cast< int >( std::round( scale ) ) ) );
226 QString scaleTxt( scaleString );
239 QStringList txtList = scaleTxt.split(
':' );
240 if ( 2 == txtList.size() )
246 if ( okX && okY && x != 0 )
249 scale =
static_cast< double >( y ) / static_cast< double >( x );
266 lineEdit()->setClearButtonEnabled( allowNull );
278 if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
287 setScale( std::numeric_limits< double >::quiet_NaN() );
This class is a composition of two QSettings instances:
void setAllowNull(bool allowNull)
Sets whether the scale combobox can be set to a NULL value.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QString scaleString() const
Returns the selected scale as a string, e.g.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
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...
bool isNull() const
Returns true if the combo box is currently set to a "null" value.
void setScale(double scale)
Set the selected scale from a double.
static double toDouble(const QString &string, bool *ok=nullptr)
Helper function to convert a scale string to double.
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...
void showPopup() override
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
double scale() const
Returns the selected scale as a double.
void setMinScale(double scale)
Set the minimum allowed scale.
bool setScaleString(const QString &string)
Set the selected scale from a string, e.g.
static QString toString(double scale)
Helper function to convert a scale double to scale string.
void setNull()
Sets the combo box to the null value.
const QString PROJECT_SCALES
void updateScales(const QStringList &scales=QStringList())
Sets the list of predefined scales to show in the combobox.
bool allowNull() const
Returns true if the combobox can be set to a NULL value.
QgsScaleComboBox(QWidget *parent=nullptr)
Constructor for QgsScaleComboBox.