Skip to content
Permalink
Browse files

Fix raster paletted and pseudocolor various issues

  • Loading branch information
elpaso committed Nov 5, 2020
1 parent 72fe363 commit 414d9381176e4cbe2aa6a006aa0d7f80511e59c8
@@ -1,2 +1,3 @@
# The following has been generated automatically from src/core/qgis.h
Qgis.DataType.baseClass = Qgis
Qgis.PythonMacroMode.baseClass = Qgis
@@ -55,10 +55,19 @@ Constructor for QgsDoubleValidator.

:param bottom: the minimal range limit accepted by the validator
:param top: the maximal range limit accepted by the validator
:param decimal: the number of decimal accepted by the validator
:param decimal: the number of decimals accepted by the validator
:param parent: parent object
%End

QgsDoubleValidator( int decimal, QObject *parent );
%Docstring
Constructor for QgsDoubleValidator.

:param decimal: the number of decimals accepted by the validator
:param parent: parent object

.. versionadded:: 3.16
%End


QValidator::State validate( QString &input ) const;
@@ -11,8 +11,6 @@





class QgsColorRampShaderWidget: QWidget, protected Ui::QgsColorRampShaderWidgetBase
{
%Docstring
@@ -85,8 +83,6 @@ Returns shared function used in the renderer
Sets widget state from the color ramp shader
%End

static int VALUE_ROLE;

signals:
void minimumMaximumChangedFromTree( double minimum, double maximum );
%Docstring
@@ -115,6 +115,7 @@ class CORE_EXPORT Qgis
ARGB32 = 12, //!< Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32
ARGB32_Premultiplied = 13 //!< Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied
};
Q_ENUM( DataType )

/**
* Authorisation to run Python Macros
@@ -67,7 +67,7 @@ QgsRasterRenderer *QgsPalettedRasterRenderer::create( const QDomElement &elem, Q
QDomNodeList paletteEntries = paletteElem.elementsByTagName( QStringLiteral( "paletteEntry" ) );

QDomElement entryElem;
int value;
double value;

for ( int i = 0; i < paletteEntries.size(); ++i )
{
@@ -353,8 +353,7 @@ QgsPalettedRasterRenderer::ClassData QgsPalettedRasterRenderer::colorTableToClas
QgsPalettedRasterRenderer::ClassData classes;
for ( ; colorIt != table.constEnd(); ++colorIt )
{
int idx = ( int )( colorIt->value );
classes << QgsPalettedRasterRenderer::Class( idx, colorIt->color, colorIt->label );
classes << QgsPalettedRasterRenderer::Class( colorIt->value, colorIt->color, colorIt->label );
}
return classes;
}
@@ -499,7 +498,7 @@ QgsPalettedRasterRenderer::ClassData QgsPalettedRasterRenderer::classDataFromRas
QgsRasterIterator iter( raster );
iter.startRasterRead( bandNumber, raster->xSize(), raster->ySize(), raster->extent(), feedback );

int nbBlocksWidth = static_cast< int >( std::ceil( 1.0 * raster->ySize() / maxWidth ) );
int nbBlocksWidth = static_cast< int >( std::ceil( 1.0 * raster->xSize() / maxWidth ) );
int nbBlocksHeight = static_cast< int >( std::ceil( 1.0 * raster->ySize() / maxHeight ) );
int nbBlocks = nbBlocksWidth * nbBlocksHeight;

@@ -609,11 +608,9 @@ QgsPalettedRasterRenderer::ClassData QgsPalettedRasterRenderer::classDataFromRas
void QgsPalettedRasterRenderer::updateArrays()
{
mColors.clear();
int i = 0;
ClassData::const_iterator it = mClassData.constBegin();
for ( ; it != mClassData.constEnd(); ++it )
{
mColors[it->value] = qPremultiply( it->color.rgba() );
i++;
}
}
@@ -471,6 +471,7 @@ SET(QGIS_GUI_SRCS
qgslegendpatchshapebutton.cpp
qgslegendpatchshapewidget.cpp
qgslimitedrandomcolorrampdialog.cpp
qgslocaleawarenumericlineeditdelegate.cpp
qgsratiolockbutton.cpp
qgsludialog.cpp
qgsmanageconnectionsdialog.cpp
@@ -275,11 +275,23 @@ namespace QgsGuiUtils
return QSize( adjustedSize, adjustedSize );
}

QString displayValueWithMaximumDecimals( const Qgis::DataType rasterDataType, const double value )
QString displayValueWithMaximumDecimals( const Qgis::DataType dataType, const double value, bool displayTrailingZeroes )
{
const int precision { significantDigits( rasterDataType ) };
// Reduce
return QLocale().toString( value, 'f', precision );
const int precision { significantDigits( dataType ) };
QString result { QLocale().toString( value, 'f', precision ) };
if ( ! displayTrailingZeroes )
{
const QRegularExpression zeroesRe { QStringLiteral( R"raw(\%1\d*?(0+$))raw" ).arg( QLocale().decimalPoint() ) };
if ( zeroesRe.match( result ).hasMatch() )
{
result.truncate( zeroesRe.match( result ).capturedStart( 1 ) );
if ( result.endsWith( QLocale().decimalPoint( ) ) )
{
result.chop( 1 );
}
}
}
return result;
}

int significantDigits( const Qgis::DataType rasterDataType )
@@ -193,19 +193,20 @@ namespace QgsGuiUtils

/**
* Returns a localized string representation of the \a value with the appropriate number of
* decimals supported by the \a rasterDataType.
* decimals supported by the \a dataType. Traling zeroes after decimal separator are not
* show unless \a displayTrailingZeroes is set.
* Note that for floating point types the number of decimals may exceed the actual internal
* precision because the precision is always calculated on the mantissa and the conversion to
* string interprets the precision as decimal places.
* \since QGIS 3.16
*/
QString displayValueWithMaximumDecimals( const Qgis::DataType rasterDataType, const double value );
QString GUI_EXPORT displayValueWithMaximumDecimals( const Qgis::DataType dataType, const double value, bool displayTrailingZeroes = false );

/**
* Returns the maximum number of significant digits a for the given \a rasterDataType.
* \since QGIS 3.16
*/
int significantDigits( const Qgis::DataType rasterDataType );
int GUI_EXPORT significantDigits( const Qgis::DataType rasterDataType );

}

@@ -13,9 +13,61 @@
* (at your option) any later version. *
* *
***************************************************************************/

#include <QLineEdit>

#include "qgslocaleawarenumericlineeditdelegate.h"
#include "qgsdoublevalidator.h"
#include "qgsguiutils.h"

/// @cond PRIVATE

QgsLocaleAwareNumericLineEditDelegate::QgsLocaleAwareNumericLineEditDelegate( Qgis::DataType dataType, QWidget *parent )
: QStyledItemDelegate( parent )
, mDataType( dataType )
{
}

QgsLocaleAwareNumericLineEditDelegate::QgsLocaleAwareNumericLineEditDelegate()
QWidget *QgsLocaleAwareNumericLineEditDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
Q_UNUSED( option )
Q_UNUSED( index )
auto editor = new QLineEdit{ parent };
editor->setValidator( new QgsDoubleValidator( QgsGuiUtils::significantDigits( mDataType ), editor ) );
return editor;
}

void QgsLocaleAwareNumericLineEditDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QLineEdit *lineEdit { qobject_cast<QLineEdit *>( editor ) };
if ( ! lineEdit )
{
QStyledItemDelegate::setEditorData( editor, index );
}

const QVariant value { index.data( ) };
return lineEdit->setText( displayText( value, QLocale() ) );
}

void QgsLocaleAwareNumericLineEditDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
QLineEdit *lineEdit { qobject_cast<QLineEdit *>( editor ) };
if ( ! editor )
{
QStyledItemDelegate::setModelData( editor, model, index );
}
const double value { QgsDoubleValidator::toDouble( lineEdit->text() ) };
model->setData( index, value );
}

QString QgsLocaleAwareNumericLineEditDelegate::displayText( const QVariant &value, const QLocale & ) const
{
return QgsGuiUtils::displayValueWithMaximumDecimals( mDataType, value.toDouble( ) );
}

void QgsLocaleAwareNumericLineEditDelegate::setDataType( const Qgis::DataType &dataType )
{
mDataType = dataType;
}

///@endcond
@@ -16,11 +16,56 @@
#ifndef QGSLOCALEAWARENUMERICLINEEDITDELEGATE_H
#define QGSLOCALEAWARENUMERICLINEEDITDELEGATE_H

#include <QStyledItemDelegate>

class QgsLocaleAwareNumericLineEditDelegate
#include "qgis.h"

#define SIP_NO_FILE

/// @cond PRIVATE

/**
* QgsLocaleAwareNumericLineEditDelegate class provides a QLineEdit editor
* for QgsColorRampShaderWidget and QgsPalettedRendererWidget value columns, it accepts
* localized numeric inputs and displays the proper number of decimals according to the
* raster band data type.
*
* This delegate assumes that the value stored in the DisplayRole is numeric (can be converted to double).
*
*/
class QgsLocaleAwareNumericLineEditDelegate: public QStyledItemDelegate
{

Q_OBJECT

public:

/**
* QgsLocaleAwareNumericLineEditDelegate
* \param dataType raster band data type
* \param parent
*/
QgsLocaleAwareNumericLineEditDelegate( Qgis::DataType dataType, QWidget *parent = nullptr );

QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;

// QAbstractItemDelegate interface
public:
QgsLocaleAwareNumericLineEditDelegate();
void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
QString displayText( const QVariant &value, const QLocale &locale ) const override;

/**
* Sets data type to \a dataType
*/
void setDataType( const Qgis::DataType &dataType );

private:

Qgis::DataType mDataType;

};

///@endcond

#endif // QGSLOCALEAWARENUMERICLINEEDITDELEGATE_H

0 comments on commit 414d938

Please sign in to comment.
You can’t perform that action at this time.