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() )
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< float >( std::round(
scale ) ),
'f', 0 ) );
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() );