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 ) );
123 double newScale =
toDouble(
string, &ok );
124 double oldScale = mScale;
125 if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
127 newScale = mMinScale;
138 if ( mScale != oldScale )
156 void QgsScaleComboBox::fixupScale()
158 QStringList txtList = currentText().split(
':' );
159 bool userSetScale = txtList.size() != 2;
162 double newScale =
toDouble( currentText(), &ok );
168 if ( userSetScale && newScale < 1.0 && !
qgsDoubleNear( newScale, 0.0 ) )
170 newScale = 1 / newScale;
184 return QStringLiteral(
"0" );
186 else if ( scale <= 1 )
188 return QStringLiteral(
"%1:1" ).arg( QLocale().
toString( static_cast< int >( std::round( 1.0 / scale ) ) ) );
192 return QStringLiteral(
"1:%1" ).arg( QLocale().
toString( static_cast< int >( std::round( scale ) ) ) );
199 QString scaleTxt( scaleString );
212 QStringList txtList = scaleTxt.split(
':' );
213 if ( 2 == txtList.size() )
219 if ( okX && okY && x != 0 )
222 scale =
static_cast< double >( y ) / static_cast< double >( x );
239 if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
This class is a composition of two QSettings instances:
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...
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.
const QString PROJECT_SCALES
void updateScales(const QStringList &scales=QStringList())
Sets the list of predefined scales to show in the combobox.
QgsScaleComboBox(QWidget *parent=nullptr)
Constructor for QgsScaleComboBox.