Skip to content

Commit

Permalink
Fix raster paletted and pseudocolor various issues
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 5, 2020
1 parent 72fe363 commit 414d938
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 176 deletions.
1 change: 1 addition & 0 deletions python/core/auto_additions/qgis.py
@@ -1,2 +1,3 @@
# The following has been generated automatically from src/core/qgis.h
Qgis.DataType.baseClass = Qgis
Qgis.PythonMacroMode.baseClass = Qgis
11 changes: 10 additions & 1 deletion python/gui/auto_generated/qgsdoublevalidator.sip.in
Expand Up @@ -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;
Expand Down
Expand Up @@ -11,8 +11,6 @@





class QgsColorRampShaderWidget: QWidget, protected Ui::QgsColorRampShaderWidgetBase
{
%Docstring
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/core/qgis.h
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/core/raster/qgspalettedrasterrenderer.cpp
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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++;
}
}
1 change: 1 addition & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -471,6 +471,7 @@ SET(QGIS_GUI_SRCS
qgslegendpatchshapebutton.cpp
qgslegendpatchshapewidget.cpp
qgslimitedrandomcolorrampdialog.cpp
qgslocaleawarenumericlineeditdelegate.cpp
qgsratiolockbutton.cpp
qgsludialog.cpp
qgsmanageconnectionsdialog.cpp
Expand Down
20 changes: 16 additions & 4 deletions src/gui/qgsguiutils.cpp
Expand Up @@ -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 )
Expand Down
7 changes: 4 additions & 3 deletions src/gui/qgsguiutils.h
Expand Up @@ -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 );

}

Expand Down
54 changes: 53 additions & 1 deletion src/gui/qgslocaleawarenumericlineeditdelegate.cpp
Expand Up @@ -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
49 changes: 47 additions & 2 deletions src/gui/qgslocaleawarenumericlineeditdelegate.h
Expand Up @@ -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.