QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgslocaleawarenumericlineeditdelegate.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslocaleawarenumericlineeditdelegate.cpp - QgsLocaleAwareNumericLineEditDelegate
3
4 ---------------------
5 begin : 5.11.2020
6 copyright : (C) 2020 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgsdoublevalidator.h"
20#include "qgsguiutils.h"
21
22#include <QLineEdit>
23
24#include "moc_qgslocaleawarenumericlineeditdelegate.cpp"
25
27
28QgsLocaleAwareNumericLineEditDelegate::QgsLocaleAwareNumericLineEditDelegate( Qgis::DataType dataType, QWidget *parent )
29 : QStyledItemDelegate( parent )
30 , mDataType( dataType )
31{
32}
33
34QWidget *QgsLocaleAwareNumericLineEditDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
35{
36 Q_UNUSED( option )
37 Q_UNUSED( index )
38 auto editor = new QLineEdit { parent };
39 editor->setValidator( new QgsDoubleValidator( QgsGuiUtils::significantDigits( mDataType ), editor ) );
40 return editor;
41}
42
43void QgsLocaleAwareNumericLineEditDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
44{
45 QLineEdit *lineEdit { qobject_cast<QLineEdit *>( editor ) };
46 if ( lineEdit )
47 {
48 const QVariant value = index.data();
49 lineEdit->setText( displayText( value, QLocale() ) );
50 }
51 else
52 {
53 QStyledItemDelegate::setEditorData( editor, index );
54 }
55}
56
57void QgsLocaleAwareNumericLineEditDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
58{
59 QLineEdit *lineEdit { qobject_cast<QLineEdit *>( editor ) };
60 if ( !editor )
61 {
62 QStyledItemDelegate::setModelData( editor, model, index );
63 }
64 const double value { QgsDoubleValidator::toDouble( lineEdit->text() ) };
65 model->setData( index, value );
66}
67
68QString QgsLocaleAwareNumericLineEditDelegate::displayText( const QVariant &value, const QLocale & ) const
69{
70 return QgsGuiUtils::displayValueWithMaximumDecimals( mDataType, value.toDouble() );
71}
72
73void QgsLocaleAwareNumericLineEditDelegate::setDataType( const Qgis::DataType &dataType )
74{
75 mDataType = dataType;
76}
77
DataType
Raster data types.
Definition qgis.h:372
A custom validator which allows entry of doubles in a locale-tolerant way.
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
int significantDigits(const Qgis::DataType rasterDataType)
Returns the maximum number of significant digits a for the given rasterDataType.
QString displayValueWithMaximumDecimals(const Qgis::DataType dataType, const double value, bool displayTrailingZeroes)
Returns a localized string representation of the value with the appropriate number of decimals suppor...