16 #include <QDoubleValidator> 17 #include <QIntValidator> 20 #include <QTextStream> 21 #include <QMessageBox> 22 #include <QFileDialog> 41 , TRSTRING_NOT_SET( tr(
"Not Set" ) )
42 , mRasterLayer( layer )
43 , mMapCanvas( canvas )
50 connect( cboxTransparencyBand, SIGNAL( currentIndexChanged(
int ) ),
this, SIGNAL(
widgetChanged() ) );
51 connect( sliderTransparency, SIGNAL( valueChanged(
int ) ),
this, SLOT( sliderTransparency_valueChanged(
int ) ) );
53 mPixelSelectorTool =
nullptr;
57 connect( mPixelSelectorTool, SIGNAL( canvasClicked(
const QgsPoint&, Qt::MouseButton ) ),
this, SLOT( pixelSelected(
const QgsPoint& ) ) );
61 pbnAddValuesFromDisplay->setEnabled(
false );
78 gboxNoDataValue->setEnabled(
false );
79 gboxCustomTransparency->setEnabled(
false );
82 cboxTransparencyBand->addItem(
tr(
"None" ), -1 );
85 for (
int i = 1; i <= nBands; ++i )
90 if ( colorInterp !=
"Undefined" )
94 cboxTransparencyBand->addItem( bandName, i );
97 sliderTransparency->setValue(( 1.0 - renderer->
opacity() ) * 255 );
99 sliderTransparency_valueChanged(( 1.0 - renderer->
opacity() ) * 255 );
104 cboxTransparencyBand->setCurrentIndex( myIndex );
108 cboxTransparencyBand->setCurrentIndex( cboxTransparencyBand->findText( TRSTRING_NOT_SET ) );
118 lblSrcNoDataValue->setText(
tr(
"not defined" ) );
125 mSrcNoDataValueCheckBox->setEnabled( enableSrcNoData );
126 lblSrcNoDataValue->setEnabled( enableSrcNoData );
128 populateTransparencyTable( mRasterLayer->
renderer() );
131 void QgsRasterTransparencyWidget::transparencyCellTextEdited(
const QString &text )
144 if ( !lineEdit )
return;
147 for (
int r = 0 ; r < tableTransparency->rowCount(); r++ )
149 for (
int c = 0 ; c < tableTransparency->columnCount(); c++ )
151 if ( tableTransparency->cellWidget( r, c ) ==
sender() )
158 if ( row != -1 )
break;
164 QLineEdit *toLineEdit =
dynamic_cast<QLineEdit *
>( tableTransparency->cellWidget( row, 1 ) );
165 if ( !toLineEdit )
return;
166 bool toChanged = mTransparencyToEdited.
value( row );
173 else if ( column == 1 )
175 setTransparencyToEdited( row );
181 void QgsRasterTransparencyWidget::sliderTransparency_valueChanged(
int theValue )
184 int myInt = static_cast <
int >(( theValue / 255.0 ) * 100 );
188 void QgsRasterTransparencyWidget::on_pbnAddValuesFromDisplay_clicked()
190 if ( mMapCanvas && mPixelSelectorTool )
196 void QgsRasterTransparencyWidget::on_pbnAddValuesManually_clicked()
204 tableTransparency->insertRow( tableTransparency->rowCount() );
209 for (
int i = 0; i < n; i++ )
211 setTransparencyCell( tableTransparency->rowCount() - 1, i, std::numeric_limits<double>::quiet_NaN() );
214 setTransparencyCell( tableTransparency->rowCount() - 1, n, 100 );
216 tableTransparency->resizeColumnsToContents();
217 tableTransparency->resizeRowsToContents();
220 void QgsRasterTransparencyWidget::on_pbnDefaultValues_clicked()
230 setupTransparencyTable( nBands );
232 tableTransparency->resizeColumnsToContents();
233 tableTransparency->resizeRowsToContents();
237 void QgsRasterTransparencyWidget::on_pbnExportTransparentPixelValues_clicked()
244 if ( !myFileName.
endsWith(
".txt", Qt::CaseInsensitive ) )
246 myFileName = myFileName +
".txt";
249 QFile myOutputFile( myFileName );
250 if ( myOutputFile.
open( QFile::WriteOnly ) )
253 myOutputStream <<
"# " <<
tr(
"QGIS Generated Transparent Pixel Value Export File" ) <<
'\n';
254 if ( rasterIsMultiBandColor() )
256 myOutputStream <<
"#\n#\n# " <<
tr(
"Red" ) <<
"\t" <<
tr(
"Green" ) <<
"\t" <<
tr(
"Blue" ) <<
"\t" <<
tr(
"Percent Transparent" );
257 for (
int myTableRunner = 0; myTableRunner < tableTransparency->rowCount(); myTableRunner++ )
259 myOutputStream <<
'\n' <<
QString::number( transparencyCellValue( myTableRunner, 0 ) ) <<
"\t" 260 <<
QString::number( transparencyCellValue( myTableRunner, 1 ) ) <<
"\t" 261 <<
QString::number( transparencyCellValue( myTableRunner, 2 ) ) <<
"\t" 267 myOutputStream <<
"#\n#\n# " <<
tr(
"Value" ) <<
"\t" <<
tr(
"Percent Transparent" );
269 for (
int myTableRunner = 0; myTableRunner < tableTransparency->rowCount(); myTableRunner++ )
271 myOutputStream <<
'\n' <<
QString::number( transparencyCellValue( myTableRunner, 0 ) ) <<
"\t" 272 <<
QString::number( transparencyCellValue( myTableRunner, 1 ) ) <<
"\t" 279 QMessageBox::warning(
this,
tr(
"Write access denied" ),
tr(
"Write access denied. Adjust the file permissions and try again.\n\n" ) );
284 void QgsRasterTransparencyWidget::on_pbnImportTransparentPixelValues_clicked()
286 int myLineCounter = 0;
287 bool myImportError =
false;
292 QFile myInputFile( myFileName );
293 if ( myInputFile.
open( QFile::ReadOnly ) )
297 if ( rasterIsMultiBandColor() )
299 for (
int myTableRunner = tableTransparency->rowCount() - 1; myTableRunner >= 0; myTableRunner-- )
301 tableTransparency->removeRow( myTableRunner );
304 while ( !myInputStream.
atEnd() )
307 myInputLine = myInputStream.
readLine();
313 if ( myTokens.
count() != 4 )
315 myImportError =
true;
316 myBadLines = myBadLines +
QString::number( myLineCounter ) +
":\t[" + myInputLine +
"]\n";
320 tableTransparency->insertRow( tableTransparency->rowCount() );
321 for (
int col = 0; col < 4; col++ )
323 setTransparencyCell( tableTransparency->rowCount() - 1, col, myTokens[col].toDouble() );
332 for (
int myTableRunner = tableTransparency->rowCount() - 1; myTableRunner >= 0; myTableRunner-- )
334 tableTransparency->removeRow( myTableRunner );
337 while ( !myInputStream.
atEnd() )
340 myInputLine = myInputStream.
readLine();
346 if ( myTokens.
count() != 3 && myTokens.
count() != 2 )
348 myImportError =
true;
349 myBadLines = myBadLines +
QString::number( myLineCounter ) +
":\t[" + myInputLine +
"]\n";
353 if ( myTokens.
count() == 2 )
355 myTokens.
insert( 1, myTokens[0] );
357 tableTransparency->insertRow( tableTransparency->rowCount() );
358 for (
int col = 0; col < 3; col++ )
360 setTransparencyCell( tableTransparency->rowCount() - 1, col, myTokens[col].toDouble() );
370 QMessageBox::warning(
this,
tr(
"Import Error" ),
tr(
"The following lines contained errors\n\n%1" ).arg( myBadLines ) );
373 else if ( !myFileName.
isEmpty() )
375 QMessageBox::warning(
this,
tr(
"Read access denied" ),
tr(
"Read access denied. Adjust the file permissions and try again.\n\n" ) );
377 tableTransparency->resizeColumnsToContents();
378 tableTransparency->resizeRowsToContents();
382 void QgsRasterTransparencyWidget::on_pbnRemoveSelectedRow_clicked()
384 if ( 0 < tableTransparency->rowCount() )
386 tableTransparency->removeRow( tableTransparency->currentRow() );
391 bool QgsRasterTransparencyWidget::rasterIsMultiBandColor()
399 if ( rasterRenderer )
401 rasterRenderer->
setAlphaBand( cboxTransparencyBand->itemData( cboxTransparencyBand->currentIndex() ).toInt() );
405 if ( tableTransparency->columnCount() == 4 )
409 for (
int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
411 myTransparentPixel.
red = transparencyCellValue( myListRunner, 0 );
412 myTransparentPixel.
green = transparencyCellValue( myListRunner, 1 );
413 myTransparentPixel.
blue = transparencyCellValue( myListRunner, 2 );
415 myTransparentThreeValuePixelList.
append( myTransparentPixel );
419 else if ( tableTransparency->columnCount() == 3 )
423 for (
int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
425 myTransparentPixel.
min = transparencyCellValue( myListRunner, 0 );
426 myTransparentPixel.
max = transparencyCellValue( myListRunner, 1 );
429 myTransparentSingleValuePixelList.
append( myTransparentPixel );
437 rasterRenderer->
setOpacity(( 255 - sliderTransparency->value() ) / 255.0 );
441 void QgsRasterTransparencyWidget::pixelSelected(
const QgsPoint & canvasPoint )
450 if ( mMapCanvas && mPixelSelectorTool )
459 int myWidth = mMapCanvas->
extent().
width() / mapUnitsPerPixel;
460 int myHeight = mMapCanvas->
extent().
height() / mapUnitsPerPixel;
467 for (
int i = 0; i < bands.
size(); ++i )
469 int bandNo = bands.
value( i );
470 if ( myPixelMap.
count( bandNo ) == 1 )
472 if ( myPixelMap.
value( bandNo ).isNull() )
476 double value = myPixelMap.
value( bandNo ).toDouble();
481 if ( bands.
size() == 1 )
486 tableTransparency->insertRow( tableTransparency->rowCount() );
487 for (
int i = 0; i < values.
size(); i++ )
489 setTransparencyCell( tableTransparency->rowCount() - 1, i, values.
value( i ) );
491 setTransparencyCell( tableTransparency->rowCount() - 1, tableTransparency->columnCount() - 1, 100 );
494 tableTransparency->resizeColumnsToContents();
495 tableTransparency->resizeRowsToContents();
498 void QgsRasterTransparencyWidget::populateTransparencyTable(
QgsRasterRenderer *renderer )
511 setupTransparencyTable( nBands );
514 if ( !rasterTransparency )
522 for (
int i = 0; i < pixelList.
size(); ++i )
524 tableTransparency->insertRow( i );
525 setTransparencyCell( i, 0, pixelList[i].
min );
526 setTransparencyCell( i, 1, pixelList[i].
max );
527 setTransparencyCell( i, 2, pixelList[i].percentTransparent );
529 if ( pixelList[i].min != pixelList[i].max )
531 setTransparencyToEdited( i );
535 else if ( nBands == 3 )
538 for (
int i = 0; i < pixelList.
size(); ++i )
540 tableTransparency->insertRow( i );
541 setTransparencyCell( i, 0, pixelList[i].red );
542 setTransparencyCell( i, 1, pixelList[i].green );
543 setTransparencyCell( i, 2, pixelList[i].blue );
544 setTransparencyCell( i, 3, pixelList[i].percentTransparent );
548 tableTransparency->resizeColumnsToContents();
549 tableTransparency->resizeRowsToContents();
553 void QgsRasterTransparencyWidget::setupTransparencyTable(
int nBands )
555 tableTransparency->clear();
556 tableTransparency->setColumnCount( 0 );
557 tableTransparency->setRowCount( 0 );
558 mTransparencyToEdited.
clear();
562 tableTransparency->setColumnCount( 4 );
564 tableTransparency->setHorizontalHeaderItem( 1,
new QTableWidgetItem(
tr(
"Green" ) ) );
565 tableTransparency->setHorizontalHeaderItem( 2,
new QTableWidgetItem(
tr(
"Blue" ) ) );
566 tableTransparency->setHorizontalHeaderItem( 3,
new QTableWidgetItem(
tr(
"Percent Transparent" ) ) );
570 tableTransparency->setColumnCount( 3 );
573 if ( QgsRasterLayer::PalettedColor != mRasterLayer->drawingStyle() &&
574 QgsRasterLayer::PalettedSingleBandGray != mRasterLayer->drawingStyle() &&
575 QgsRasterLayer::PalettedSingleBandPseudoColor != mRasterLayer->drawingStyle() &&
576 QgsRasterLayer::PalettedMultiBandColor != mRasterLayer->drawingStyle() )
578 tableTransparency->setHorizontalHeaderItem( 0,
new QTableWidgetItem(
tr(
"Gray" ) ) );
582 tableTransparency->setHorizontalHeaderItem( 0,
new QTableWidgetItem(
tr(
"Indexed Value" ) ) );
585 tableTransparency->setHorizontalHeaderItem( 0,
new QTableWidgetItem(
tr(
"From" ) ) );
587 tableTransparency->setHorizontalHeaderItem( 2,
new QTableWidgetItem(
tr(
"Percent Transparent" ) ) );
591 void QgsRasterTransparencyWidget::setTransparencyCell(
int row,
int column,
double value )
595 if ( !provider )
return;
598 if ( !renderer )
return;
606 if ( column == tableTransparency->columnCount() - 1 )
622 if ( !qIsNaN( value ) )
629 if ( !qIsNaN( value ) )
635 lineEdit->
setText( valueString );
638 tableTransparency->setCellWidget( row, column, lineEdit );
639 adjustTransparencyCellWidth( row, column );
641 if ( nBands == 1 && ( column == 0 || column == 1 ) )
643 connect( lineEdit, SIGNAL( textEdited(
const QString & ) ),
this, SLOT( transparencyCellTextEdited(
const QString & ) ) );
645 tableTransparency->resizeColumnsToContents();
649 void QgsRasterTransparencyWidget::adjustTransparencyCellWidth(
int row,
int column )
651 QLineEdit *lineEdit =
dynamic_cast<QLineEdit *
>( tableTransparency->cellWidget( row, column ) );
652 if ( !lineEdit )
return;
655 width = qMax( width, tableTransparency->columnWidth( column ) );
660 void QgsRasterTransparencyWidget::setTransparencyToEdited(
int row )
662 if ( row >= mTransparencyToEdited.
size() )
664 mTransparencyToEdited.
resize( row + 1 );
666 mTransparencyToEdited[row] =
true;
669 double QgsRasterTransparencyWidget::transparencyCellValue(
int row,
int column )
671 QLineEdit *lineEdit =
dynamic_cast<QLineEdit *
>( tableTransparency->cellWidget( row, column ) );
672 if ( !lineEdit || lineEdit->
text().isEmpty() )
674 std::numeric_limits<double>::quiet_NaN();
676 return lineEdit->
text().toDouble();
void unsetMapTool(QgsMapTool *mapTool)
Unset the current map tool or last non zoom tool.
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
QString & append(QChar ch)
static QString printValue(double value)
Print double value with all necessary significant digits.
QString readLine(qint64 maxlen)
void setText(const QString &)
virtual QList< int > usesBands() const
Returns a list of band numbers used by the renderer.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
virtual QString generateBandName(int theBandNumber) const
helper function to create zero padded band names
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QString simplified() const
void setTransparentThreeValuePixelList(const QList< TransparentThreeValuePixel > &theNewList)
Mutator for transparentThreeValuePixelList.
Thirty two bit floating point (float)
QgsRasterRenderer * renderer() const
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsRasterTransparency * rasterTransparency() const
The QgsMapSettings class contains configuration for rendering of the map.
double ANALYSIS_EXPORT max(double x, double y)
Returns the maximum of two doubles or the first argument if both are equal.
void setMapTool(QgsMapTool *mapTool)
Sets the map tool currently being used on the canvas.
QString number(int n, int base)
int count(const T &value) const
virtual QgsRasterIdentifyResult identify(const QgsPoint &thePoint, QgsRaster::IdentifyFormat theFormat, const QgsRectangle &theExtent=QgsRectangle(), int theWidth=0, int theHeight=0, int theDpi=96)
Identify raster value(s) found on the point position.
void append(const T &value)
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
Sixty four bit floating point (double)
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
virtual QGis::DataType srcDataType(int bandNo) const override=0
Returns source data type for the band specified by number, source data type may be shorter than dataT...
double percentTransparent
double width() const
Width of the rectangle.
QgsRectangle extent() const
Returns the current zoom exent of the map canvas.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
void setAlphaBand(int band)
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
A class to represent a point.
virtual bool useSrcNoDataValue(int bandNo) const
Get source nodata value usage.
QList< QgsRasterTransparency::TransparentSingleValuePixel > transparentSingleValuePixelList() const
Accessor for transparentSingleValuePixelList.
QMap< int, QVariant > results() const
Get results.
int width(const QString &text, int len) const
virtual QString colorInterpretationName(int theBandNo) const
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
virtual QGis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
QVariant value(const QString &key, const QVariant &defaultValue) const
void setTransparentSingleValuePixelList(const QList< TransparentSingleValuePixel > &theNewList)
Mutator for transparentSingleValuePixelList.
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from output CRS to layer's CRS
void insert(int i, const T &value)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
Renderer for multiband images with the color components.
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
QgsRasterDataProvider * dataProvider()
Returns the data provider.
Defines the list of pixel values to be considered as transparent or semi transparent when rendering r...
void setOpacity(double opacity)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
virtual bool srcHasNoDataValue(int bandNo) const
Return true if source band has no data value.
double ANALYSIS_EXPORT min(double x, double y)
Returns the minimum of two doubles or the first argument if both are equal.
virtual double srcNoDataValue(int bandNo) const
Value representing no data value.
QList< QgsRasterTransparency::TransparentThreeValuePixel > transparentThreeValuePixelList() const
Accessor for transparentThreeValuePixelList.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setRasterTransparency(QgsRasterTransparency *t)
double percentTransparent
void setValidator(const QValidator *v)
int count(const Key &key) const
Raster renderer pipe that applies colors to a raster.
double height() const
Height of the rectangle.
void setContentsMargins(int left, int top, int right, int bottom)
const T value(const Key &key) const
Base class for raster data providers.