QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
33QWidget *QgsLocaleAwareNumericLineEditDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
34{
35 Q_UNUSED( option )
36 Q_UNUSED( index )
37 auto editor = new QLineEdit { parent };
38 editor->setValidator( new QgsDoubleValidator( QgsGuiUtils::significantDigits( mDataType ), editor ) );
39 return editor;
40}
41
42void QgsLocaleAwareNumericLineEditDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
43{
44 QLineEdit *lineEdit { qobject_cast<QLineEdit *>( editor ) };
45 if ( lineEdit )
46 {
47 const QVariant value = index.data();
48 lineEdit->setText( displayText( value, QLocale() ) );
49 }
50 else
51 {
52 QStyledItemDelegate::setEditorData( editor, index );
53 }
54}
55
56void QgsLocaleAwareNumericLineEditDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
57{
58 QLineEdit *lineEdit { qobject_cast<QLineEdit *>( editor ) };
59 if ( !editor )
60 {
61 QStyledItemDelegate::setModelData( editor, model, index );
62 }
63 const double value { QgsDoubleValidator::toDouble( lineEdit->text() ) };
64 model->setData( index, value );
65}
66
67QString QgsLocaleAwareNumericLineEditDelegate::displayText( const QVariant &value, const QLocale & ) const
68{
69 return QgsGuiUtils::displayValueWithMaximumDecimals( mDataType, value.toDouble() );
70}
71
72void QgsLocaleAwareNumericLineEditDelegate::setDataType( const Qgis::DataType &dataType )
73{
74 mDataType = dataType;
75}
76
DataType
Raster data types.
Definition qgis.h:393
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...