QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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
17#include <QLineEdit>
18
20#include "qgsdoublevalidator.h"
21#include "qgsguiutils.h"
22
24
25QgsLocaleAwareNumericLineEditDelegate::QgsLocaleAwareNumericLineEditDelegate( Qgis::DataType dataType, QWidget *parent )
26 : QStyledItemDelegate( parent )
27 , mDataType( dataType )
28{
29}
30
31QWidget *QgsLocaleAwareNumericLineEditDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
32{
33 Q_UNUSED( option )
34 Q_UNUSED( index )
35 auto editor = new QLineEdit{ parent };
36 editor->setValidator( new QgsDoubleValidator( QgsGuiUtils::significantDigits( mDataType ), editor ) );
37 return editor;
38}
39
40void QgsLocaleAwareNumericLineEditDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
41{
42 QLineEdit *lineEdit { qobject_cast<QLineEdit *>( editor ) };
43 if ( lineEdit )
44 {
45 const QVariant value = index.data( );
46 lineEdit->setText( displayText( value, QLocale() ) );
47 }
48 else
49 {
50 QStyledItemDelegate::setEditorData( editor, index );
51 }
52}
53
54void QgsLocaleAwareNumericLineEditDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
55{
56 QLineEdit *lineEdit { qobject_cast<QLineEdit *>( editor ) };
57 if ( ! editor )
58 {
59 QStyledItemDelegate::setModelData( editor, model, index );
60 }
61 const double value { QgsDoubleValidator::toDouble( lineEdit->text() ) };
62 model->setData( index, value );
63}
64
65QString QgsLocaleAwareNumericLineEditDelegate::displayText( const QVariant &value, const QLocale & ) const
66{
67 return QgsGuiUtils::displayValueWithMaximumDecimals( mDataType, value.toDouble( ) );
68}
69
70void QgsLocaleAwareNumericLineEditDelegate::setDataType( const Qgis::DataType &dataType )
71{
72 mDataType = dataType;
73}
74
DataType
Raster data types.
Definition qgis.h:351
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator and QRegularExpressionVali...
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...