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