QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsrastertransparencywidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrastertransparencywidget.cpp
3  ---------------------
4  begin : May 2016
5  copyright : (C) 2016 by Nathan Woodrow
6  email : woodrow dot nathan at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include <QWidget>
16 #include <QDoubleValidator>
17 #include <QIntValidator>
18 #include <QSettings>
19 #include <QFile>
20 #include <QTextStream>
21 #include <QMessageBox>
22 #include <QFileDialog>
23 
25 
26 #include "qgsrasterlayer.h"
27 #include "qgsraster.h"
28 #include "qgsrasterlayerrenderer.h"
29 #include "qgsrasterdataprovider.h"
30 #include "qgsrastertransparency.h"
31 #include "qgsmaptoolemitpoint.h"
32 #include "qgsmapsettings.h"
33 #include "qgsrectangle.h"
34 #include "qgsmapcanvas.h"
37 
38 
40  : QgsMapLayerConfigWidget( layer, canvas, parent )
41  , TRSTRING_NOT_SET( tr( "Not Set" ) )
42  , mRasterLayer( layer )
43  , mMapCanvas( canvas )
44 {
45  setupUi( this );
46 
47  syncToLayer();
48 
49  connect( sliderTransparency, SIGNAL( valueChanged( int ) ), this, SIGNAL( widgetChanged() ) );
50  connect( cboxTransparencyBand, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( widgetChanged() ) );
51  connect( sliderTransparency, SIGNAL( valueChanged( int ) ), this, SLOT( sliderTransparency_valueChanged( int ) ) );
52 
53  mPixelSelectorTool = nullptr;
54  if ( mMapCanvas )
55  {
56  mPixelSelectorTool = new QgsMapToolEmitPoint( mMapCanvas );
57  connect( mPixelSelectorTool, SIGNAL( canvasClicked( const QgsPoint&, Qt::MouseButton ) ), this, SLOT( pixelSelected( const QgsPoint& ) ) );
58  }
59  else
60  {
61  pbnAddValuesFromDisplay->setEnabled( false );
62  }
63 }
64 
66 {
67 }
68 
70 {
71  QgsRasterDataProvider* provider = mRasterLayer->dataProvider();
72  QgsRasterRenderer* renderer = mRasterLayer->renderer();
73  if ( provider )
74  {
75  if ( provider->dataType( 1 ) == QGis::ARGB32
76  || provider->dataType( 1 ) == QGis::ARGB32_Premultiplied )
77  {
78  gboxNoDataValue->setEnabled( false );
79  gboxCustomTransparency->setEnabled( false );
80  }
81 
82  cboxTransparencyBand->addItem( tr( "None" ), -1 );
83  int nBands = provider->bandCount();
84  QString bandName;
85  for ( int i = 1; i <= nBands; ++i ) //band numbering seem to start at 1
86  {
87  bandName = provider->generateBandName( i );
88 
89  QString colorInterp = provider->colorInterpretationName( i );
90  if ( colorInterp != "Undefined" )
91  {
92  bandName.append( QString( " (%1)" ).arg( colorInterp ) );
93  }
94  cboxTransparencyBand->addItem( bandName, i );
95  }
96 
97  sliderTransparency->setValue(( 1.0 - renderer->opacity() ) * 255 );
98  //update the transparency percentage label
99  sliderTransparency_valueChanged(( 1.0 - renderer->opacity() ) * 255 );
100 
101  int myIndex = renderer->alphaBand();
102  if ( -1 != myIndex )
103  {
104  cboxTransparencyBand->setCurrentIndex( myIndex );
105  }
106  else
107  {
108  cboxTransparencyBand->setCurrentIndex( cboxTransparencyBand->findText( TRSTRING_NOT_SET ) );
109  }
110  }
111 
112  if ( mRasterLayer->dataProvider()->srcHasNoDataValue( 1 ) )
113  {
114  lblSrcNoDataValue->setText( QgsRasterBlock::printValue( mRasterLayer->dataProvider()->srcNoDataValue( 1 ) ) );
115  }
116  else
117  {
118  lblSrcNoDataValue->setText( tr( "not defined" ) );
119  }
120 
121  mSrcNoDataValueCheckBox->setChecked( mRasterLayer->dataProvider()->useSrcNoDataValue( 1 ) );
122 
123  bool enableSrcNoData = mRasterLayer->dataProvider()->srcHasNoDataValue( 1 ) && !qIsNaN( mRasterLayer->dataProvider()->srcNoDataValue( 1 ) );
124 
125  mSrcNoDataValueCheckBox->setEnabled( enableSrcNoData );
126  lblSrcNoDataValue->setEnabled( enableSrcNoData );
127 
128  populateTransparencyTable( mRasterLayer->renderer() );
129 }
130 
131 void QgsRasterTransparencyWidget::transparencyCellTextEdited( const QString &text )
132 {
133  Q_UNUSED( text );
134  QgsDebugMsg( QString( "text = %1" ).arg( text ) );
135  QgsRasterRenderer* renderer = mRasterLayer->renderer();
136  if ( !renderer )
137  {
138  return;
139  }
140  int nBands = renderer->usesBands().size();
141  if ( nBands == 1 )
142  {
143  QLineEdit *lineEdit = dynamic_cast<QLineEdit *>( sender() );
144  if ( !lineEdit ) return;
145  int row = -1;
146  int column = -1;
147  for ( int r = 0 ; r < tableTransparency->rowCount(); r++ )
148  {
149  for ( int c = 0 ; c < tableTransparency->columnCount(); c++ )
150  {
151  if ( tableTransparency->cellWidget( r, c ) == sender() )
152  {
153  row = r;
154  column = c;
155  break;
156  }
157  }
158  if ( row != -1 ) break;
159  }
160  QgsDebugMsg( QString( "row = %1 column =%2" ).arg( row ).arg( column ) );
161 
162  if ( column == 0 )
163  {
164  QLineEdit *toLineEdit = dynamic_cast<QLineEdit *>( tableTransparency->cellWidget( row, 1 ) );
165  if ( !toLineEdit ) return;
166  bool toChanged = mTransparencyToEdited.value( row );
167  QgsDebugMsg( QString( "toChanged = %1" ).arg( toChanged ) );
168  if ( !toChanged )
169  {
170  toLineEdit->setText( lineEdit->text() );
171  }
172  }
173  else if ( column == 1 )
174  {
175  setTransparencyToEdited( row );
176  }
177  }
178  emit widgetChanged();
179 }
180 
181 void QgsRasterTransparencyWidget::sliderTransparency_valueChanged( int theValue )
182 {
183  //set the transparency percentage label to a suitable value
184  int myInt = static_cast < int >(( theValue / 255.0 ) * 100 ); //255.0 to prevent integer division
185  lblTransparencyPercent->setText( QString::number( myInt ) + '%' );
186 }
187 
188 void QgsRasterTransparencyWidget::on_pbnAddValuesFromDisplay_clicked()
189 {
190  if ( mMapCanvas && mPixelSelectorTool )
191  {
192  mMapCanvas->setMapTool( mPixelSelectorTool );
193  }
194 }
195 
196 void QgsRasterTransparencyWidget::on_pbnAddValuesManually_clicked()
197 {
198  QgsRasterRenderer* renderer = mRasterLayer->renderer();
199  if ( !renderer )
200  {
201  return;
202  }
203 
204  tableTransparency->insertRow( tableTransparency->rowCount() );
205 
206  int n = renderer->usesBands().size();
207  if ( n == 1 ) n++;
208 
209  for ( int i = 0; i < n; i++ )
210  {
211  setTransparencyCell( tableTransparency->rowCount() - 1, i, std::numeric_limits<double>::quiet_NaN() );
212  }
213 
214  setTransparencyCell( tableTransparency->rowCount() - 1, n, 100 );
215 
216  tableTransparency->resizeColumnsToContents();
217  tableTransparency->resizeRowsToContents();
218 }
219 
220 void QgsRasterTransparencyWidget::on_pbnDefaultValues_clicked()
221 {
222  QgsRasterRenderer* r = mRasterLayer->renderer();
223  if ( !r )
224  {
225  return;
226  }
227 
228  int nBands = r->usesBands().size();
229 
230  setupTransparencyTable( nBands );
231 
232  tableTransparency->resizeColumnsToContents(); // works only with values
233  tableTransparency->resizeRowsToContents();
234 
235 }
236 
237 void QgsRasterTransparencyWidget::on_pbnExportTransparentPixelValues_clicked()
238 {
239  QSettings myQSettings;
240  QString myLastDir = myQSettings.value( "lastRasterFileFilterDir", QDir::homePath() ).toString();
241  QString myFileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), myLastDir, tr( "Textfile" ) + " (*.txt)" );
242  if ( !myFileName.isEmpty() )
243  {
244  if ( !myFileName.endsWith( ".txt", Qt::CaseInsensitive ) )
245  {
246  myFileName = myFileName + ".txt";
247  }
248 
249  QFile myOutputFile( myFileName );
250  if ( myOutputFile.open( QFile::WriteOnly ) )
251  {
252  QTextStream myOutputStream( &myOutputFile );
253  myOutputStream << "# " << tr( "QGIS Generated Transparent Pixel Value Export File" ) << '\n';
254  if ( rasterIsMultiBandColor() )
255  {
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++ )
258  {
259  myOutputStream << '\n' << QString::number( transparencyCellValue( myTableRunner, 0 ) ) << "\t"
260  << QString::number( transparencyCellValue( myTableRunner, 1 ) ) << "\t"
261  << QString::number( transparencyCellValue( myTableRunner, 2 ) ) << "\t"
262  << QString::number( transparencyCellValue( myTableRunner, 3 ) );
263  }
264  }
265  else
266  {
267  myOutputStream << "#\n#\n# " << tr( "Value" ) << "\t" << tr( "Percent Transparent" );
268 
269  for ( int myTableRunner = 0; myTableRunner < tableTransparency->rowCount(); myTableRunner++ )
270  {
271  myOutputStream << '\n' << QString::number( transparencyCellValue( myTableRunner, 0 ) ) << "\t"
272  << QString::number( transparencyCellValue( myTableRunner, 1 ) ) << "\t"
273  << QString::number( transparencyCellValue( myTableRunner, 2 ) );
274  }
275  }
276  }
277  else
278  {
279  QMessageBox::warning( this, tr( "Write access denied" ), tr( "Write access denied. Adjust the file permissions and try again.\n\n" ) );
280  }
281  }
282 }
283 
284 void QgsRasterTransparencyWidget::on_pbnImportTransparentPixelValues_clicked()
285 {
286  int myLineCounter = 0;
287  bool myImportError = false;
288  QString myBadLines;
289  QSettings myQSettings;
290  QString myLastDir = myQSettings.value( "lastRasterFileFilterDir", QDir::homePath() ).toString();
291  QString myFileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), myLastDir, tr( "Textfile" ) + " (*.txt)" );
292  QFile myInputFile( myFileName );
293  if ( myInputFile.open( QFile::ReadOnly ) )
294  {
295  QTextStream myInputStream( &myInputFile );
296  QString myInputLine;
297  if ( rasterIsMultiBandColor() )
298  {
299  for ( int myTableRunner = tableTransparency->rowCount() - 1; myTableRunner >= 0; myTableRunner-- )
300  {
301  tableTransparency->removeRow( myTableRunner );
302  }
303 
304  while ( !myInputStream.atEnd() )
305  {
306  myLineCounter++;
307  myInputLine = myInputStream.readLine();
308  if ( !myInputLine.isEmpty() )
309  {
310  if ( !myInputLine.simplified().startsWith( '#' ) )
311  {
312  QStringList myTokens = myInputLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
313  if ( myTokens.count() != 4 )
314  {
315  myImportError = true;
316  myBadLines = myBadLines + QString::number( myLineCounter ) + ":\t[" + myInputLine + "]\n";
317  }
318  else
319  {
320  tableTransparency->insertRow( tableTransparency->rowCount() );
321  for ( int col = 0; col < 4; col++ )
322  {
323  setTransparencyCell( tableTransparency->rowCount() - 1, col, myTokens[col].toDouble() );
324  }
325  }
326  }
327  }
328  }
329  }
330  else
331  {
332  for ( int myTableRunner = tableTransparency->rowCount() - 1; myTableRunner >= 0; myTableRunner-- )
333  {
334  tableTransparency->removeRow( myTableRunner );
335  }
336 
337  while ( !myInputStream.atEnd() )
338  {
339  myLineCounter++;
340  myInputLine = myInputStream.readLine();
341  if ( !myInputLine.isEmpty() )
342  {
343  if ( !myInputLine.simplified().startsWith( '#' ) )
344  {
345  QStringList myTokens = myInputLine.split( QRegExp( "\\s+" ), QString::SkipEmptyParts );
346  if ( myTokens.count() != 3 && myTokens.count() != 2 ) // 2 for QGIS < 1.9 compatibility
347  {
348  myImportError = true;
349  myBadLines = myBadLines + QString::number( myLineCounter ) + ":\t[" + myInputLine + "]\n";
350  }
351  else
352  {
353  if ( myTokens.count() == 2 )
354  {
355  myTokens.insert( 1, myTokens[0] ); // add 'to' value, QGIS < 1.9 compatibility
356  }
357  tableTransparency->insertRow( tableTransparency->rowCount() );
358  for ( int col = 0; col < 3; col++ )
359  {
360  setTransparencyCell( tableTransparency->rowCount() - 1, col, myTokens[col].toDouble() );
361  }
362  }
363  }
364  }
365  }
366  }
367 
368  if ( myImportError )
369  {
370  QMessageBox::warning( this, tr( "Import Error" ), tr( "The following lines contained errors\n\n%1" ).arg( myBadLines ) );
371  }
372  }
373  else if ( !myFileName.isEmpty() )
374  {
375  QMessageBox::warning( this, tr( "Read access denied" ), tr( "Read access denied. Adjust the file permissions and try again.\n\n" ) );
376  }
377  tableTransparency->resizeColumnsToContents();
378  tableTransparency->resizeRowsToContents();
379  emit widgetChanged();
380 }
381 
382 void QgsRasterTransparencyWidget::on_pbnRemoveSelectedRow_clicked()
383 {
384  if ( 0 < tableTransparency->rowCount() )
385  {
386  tableTransparency->removeRow( tableTransparency->currentRow() );
387  }
388  emit widgetChanged();
389 }
390 
391 bool QgsRasterTransparencyWidget::rasterIsMultiBandColor()
392 {
393  return mRasterLayer && nullptr != dynamic_cast<QgsMultiBandColorRenderer*>( mRasterLayer->renderer() );
394 }
395 
397 {
398  QgsRasterRenderer* rasterRenderer = mRasterLayer->renderer();
399  if ( rasterRenderer )
400  {
401  rasterRenderer->setAlphaBand( cboxTransparencyBand->itemData( cboxTransparencyBand->currentIndex() ).toInt() );
402 
403  //Walk through each row in table and test value. If not valid set to 0.0 and continue building transparency list
404  QgsRasterTransparency* rasterTransparency = new QgsRasterTransparency();
405  if ( tableTransparency->columnCount() == 4 )
406  {
408  QList<QgsRasterTransparency::TransparentThreeValuePixel> myTransparentThreeValuePixelList;
409  for ( int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
410  {
411  myTransparentPixel.red = transparencyCellValue( myListRunner, 0 );
412  myTransparentPixel.green = transparencyCellValue( myListRunner, 1 );
413  myTransparentPixel.blue = transparencyCellValue( myListRunner, 2 );
414  myTransparentPixel.percentTransparent = transparencyCellValue( myListRunner, 3 );
415  myTransparentThreeValuePixelList.append( myTransparentPixel );
416  }
417  rasterTransparency->setTransparentThreeValuePixelList( myTransparentThreeValuePixelList );
418  }
419  else if ( tableTransparency->columnCount() == 3 )
420  {
422  QList<QgsRasterTransparency::TransparentSingleValuePixel> myTransparentSingleValuePixelList;
423  for ( int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
424  {
425  myTransparentPixel.min = transparencyCellValue( myListRunner, 0 );
426  myTransparentPixel.max = transparencyCellValue( myListRunner, 1 );
427  myTransparentPixel.percentTransparent = transparencyCellValue( myListRunner, 2 );
428 
429  myTransparentSingleValuePixelList.append( myTransparentPixel );
430  }
431  rasterTransparency->setTransparentSingleValuePixelList( myTransparentSingleValuePixelList );
432  }
433 
434  rasterRenderer->setRasterTransparency( rasterTransparency );
435 
436  //set global transparency
437  rasterRenderer->setOpacity(( 255 - sliderTransparency->value() ) / 255.0 );
438  }
439 }
440 
441 void QgsRasterTransparencyWidget::pixelSelected( const QgsPoint & canvasPoint )
442 {
443  QgsRasterRenderer* renderer = mRasterLayer->renderer();
444  if ( !renderer )
445  {
446  return;
447  }
448 
449  //Get the pixel values and add a new entry to the transparency table
450  if ( mMapCanvas && mPixelSelectorTool )
451  {
452  mMapCanvas->unsetMapTool( mPixelSelectorTool );
453 
454  const QgsMapSettings& ms = mMapCanvas->mapSettings();
455  QgsPoint myPoint = ms.mapToLayerCoordinates( mRasterLayer, canvasPoint );
456 
457  QgsRectangle myExtent = ms.mapToLayerCoordinates( mRasterLayer, mMapCanvas->extent() );
458  double mapUnitsPerPixel = mMapCanvas->mapUnitsPerPixel();
459  int myWidth = mMapCanvas->extent().width() / mapUnitsPerPixel;
460  int myHeight = mMapCanvas->extent().height() / mapUnitsPerPixel;
461 
462  QMap<int, QVariant> myPixelMap = mRasterLayer->dataProvider()->identify( myPoint, QgsRaster::IdentifyFormatValue, myExtent, myWidth, myHeight ).results();
463 
464  QList<int> bands = renderer->usesBands();
465 
466  QList<double> values;
467  for ( int i = 0; i < bands.size(); ++i )
468  {
469  int bandNo = bands.value( i );
470  if ( myPixelMap.count( bandNo ) == 1 )
471  {
472  if ( myPixelMap.value( bandNo ).isNull() )
473  {
474  return; // Don't add nodata, transparent anyway
475  }
476  double value = myPixelMap.value( bandNo ).toDouble();
477  QgsDebugMsg( QString( "value = %1" ).arg( value, 0, 'g', 17 ) );
478  values.append( value );
479  }
480  }
481  if ( bands.size() == 1 )
482  {
483  // Set 'to'
484  values.insert( 1, values.value( 0 ) );
485  }
486  tableTransparency->insertRow( tableTransparency->rowCount() );
487  for ( int i = 0; i < values.size(); i++ )
488  {
489  setTransparencyCell( tableTransparency->rowCount() - 1, i, values.value( i ) );
490  }
491  setTransparencyCell( tableTransparency->rowCount() - 1, tableTransparency->columnCount() - 1, 100 );
492  }
493 
494  tableTransparency->resizeColumnsToContents();
495  tableTransparency->resizeRowsToContents();
496 }
497 
498 void QgsRasterTransparencyWidget::populateTransparencyTable( QgsRasterRenderer *renderer )
499 {
500  if ( !mRasterLayer )
501  {
502  return;
503  }
504 
505  if ( !renderer )
506  {
507  return;
508  }
509 
510  int nBands = renderer->usesBands().size();
511  setupTransparencyTable( nBands );
512 
513  const QgsRasterTransparency* rasterTransparency = renderer->rasterTransparency();
514  if ( !rasterTransparency )
515  {
516  return;
517  }
518 
519  if ( nBands == 1 )
520  {
522  for ( int i = 0; i < pixelList.size(); ++i )
523  {
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 );
528  // break synchronization only if values differ
529  if ( pixelList[i].min != pixelList[i].max )
530  {
531  setTransparencyToEdited( i );
532  }
533  }
534  }
535  else if ( nBands == 3 )
536  {
538  for ( int i = 0; i < pixelList.size(); ++i )
539  {
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 );
545  }
546  }
547 
548  tableTransparency->resizeColumnsToContents();
549  tableTransparency->resizeRowsToContents();
550 
551 }
552 
553 void QgsRasterTransparencyWidget::setupTransparencyTable( int nBands )
554 {
555  tableTransparency->clear();
556  tableTransparency->setColumnCount( 0 );
557  tableTransparency->setRowCount( 0 );
558  mTransparencyToEdited.clear();
559 
560  if ( nBands == 3 )
561  {
562  tableTransparency->setColumnCount( 4 );
563  tableTransparency->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "Red" ) ) );
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" ) ) );
567  }
568  else //1 band
569  {
570  tableTransparency->setColumnCount( 3 );
571 // Is it important to distinguish the header? It becomes difficult with range.
572 #if 0
573  if ( QgsRasterLayer::PalettedColor != mRasterLayer->drawingStyle() &&
574  QgsRasterLayer::PalettedSingleBandGray != mRasterLayer->drawingStyle() &&
575  QgsRasterLayer::PalettedSingleBandPseudoColor != mRasterLayer->drawingStyle() &&
576  QgsRasterLayer::PalettedMultiBandColor != mRasterLayer->drawingStyle() )
577  {
578  tableTransparency->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "Gray" ) ) );
579  }
580  else
581  {
582  tableTransparency->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "Indexed Value" ) ) );
583  }
584 #endif
585  tableTransparency->setHorizontalHeaderItem( 0, new QTableWidgetItem( tr( "From" ) ) );
586  tableTransparency->setHorizontalHeaderItem( 1, new QTableWidgetItem( tr( "To" ) ) );
587  tableTransparency->setHorizontalHeaderItem( 2, new QTableWidgetItem( tr( "Percent Transparent" ) ) );
588  }
589 }
590 
591 void QgsRasterTransparencyWidget::setTransparencyCell( int row, int column, double value )
592 {
593  QgsDebugMsg( QString( "value = %1" ).arg( value, 0, 'g', 17 ) );
594  QgsRasterDataProvider* provider = mRasterLayer->dataProvider();
595  if ( !provider ) return;
596 
597  QgsRasterRenderer* renderer = mRasterLayer->renderer();
598  if ( !renderer ) return;
599  int nBands = renderer->usesBands().size();
600 
601  QLineEdit *lineEdit = new QLineEdit();
602  lineEdit->setFrame( false ); // frame looks bad in table
603  // Without margins row selection is not displayed (important for delete row)
604  lineEdit->setContentsMargins( 1, 1, 1, 1 );
605 
606  if ( column == tableTransparency->columnCount() - 1 )
607  {
608  // transparency
609  // Who needs transparency as floating point?
610  lineEdit->setValidator( new QIntValidator( nullptr ) );
611  lineEdit->setText( QString::number( static_cast<int>( value ) ) );
612  }
613  else
614  {
615  // value
616  QString valueString;
617  switch ( provider->srcDataType( 1 ) )
618  {
619  case QGis::Float32:
620  case QGis::Float64:
621  lineEdit->setValidator( new QDoubleValidator( nullptr ) );
622  if ( !qIsNaN( value ) )
623  {
624  valueString = QgsRasterBlock::printValue( value );
625  }
626  break;
627  default:
628  lineEdit->setValidator( new QIntValidator( nullptr ) );
629  if ( !qIsNaN( value ) )
630  {
631  valueString = QString::number( static_cast<int>( value ) );
632  }
633  break;
634  }
635  lineEdit->setText( valueString );
636  connect( lineEdit, SIGNAL( textEdited( QString ) ), this, SIGNAL( widgetChanged() ) );
637  }
638  tableTransparency->setCellWidget( row, column, lineEdit );
639  adjustTransparencyCellWidth( row, column );
640 
641  if ( nBands == 1 && ( column == 0 || column == 1 ) )
642  {
643  connect( lineEdit, SIGNAL( textEdited( const QString & ) ), this, SLOT( transparencyCellTextEdited( const QString & ) ) );
644  }
645  tableTransparency->resizeColumnsToContents();
646  emit widgetChanged();
647 }
648 
649 void QgsRasterTransparencyWidget::adjustTransparencyCellWidth( int row, int column )
650 {
651  QLineEdit *lineEdit = dynamic_cast<QLineEdit *>( tableTransparency->cellWidget( row, column ) );
652  if ( !lineEdit ) return;
653 
654  int width = qMax( lineEdit->fontMetrics().width( lineEdit->text() ) + 10, 100 );
655  width = qMax( width, tableTransparency->columnWidth( column ) );
656 
657  lineEdit->setFixedWidth( width );
658 }
659 
660 void QgsRasterTransparencyWidget::setTransparencyToEdited( int row )
661 {
662  if ( row >= mTransparencyToEdited.size() )
663  {
664  mTransparencyToEdited.resize( row + 1 );
665  }
666  mTransparencyToEdited[row] = true;
667 }
668 
669 double QgsRasterTransparencyWidget::transparencyCellValue( int row, int column )
670 {
671  QLineEdit *lineEdit = dynamic_cast<QLineEdit *>( tableTransparency->cellWidget( row, column ) );
672  if ( !lineEdit || lineEdit->text().isEmpty() )
673  {
674  std::numeric_limits<double>::quiet_NaN();
675  }
676  return lineEdit->text().toDouble();
677 
678 }
void unsetMapTool(QgsMapTool *mapTool)
Unset the current map tool or last non zoom tool.
A panel widget that can be shown in the map style dock.
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QString & append(QChar ch)
void setupUi(QWidget *widget)
static QString printValue(double value)
Print double value with all necessary significant digits.
void setFixedWidth(int w)
QgsRasterTransparencyWidget(QgsRasterLayer *layer, QgsMapCanvas *canvas, QWidget *parent=0)
Widget to control a layers transparency and related options.
void apply()
Apply any changes on the widget to the set layer.
QString readLine(qint64 maxlen)
void setText(const QString &)
virtual QList< int > usesBands() const
Returns a list of band numbers used by the renderer.
double opacity() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
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
QObject * sender() const
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)
Definition: qgis.h:141
QgsRasterRenderer * renderer() const
QString homePath()
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
const QgsRasterTransparency * rasterTransparency() const
int size() const
T value(int i) const
T value(int i) const
The QgsMapSettings class contains configuration for rendering of the map.
int width() const
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.
void clear()
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)
void syncToLayer()
Sync the widget state to the layer set for the widget.
double mapUnitsPerPixel() const
Returns the mapUnitsPerPixel (map units per pixel) for the canvas.
Sixty four bit floating point (double)
Definition: qgis.h:142
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
Definition: qgis.h:150
void resize(int size)
bool atEnd() const
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 width() const
Width of the rectangle.
Definition: qgsrectangle.h:207
bool isEmpty() const
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.
Definition: qgis.h:148
void setAlphaBand(int band)
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
A class to represent a point.
Definition: qgspoint.h:117
void widgetChanged()
Emitted when the widget state changes.
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.
void setFrame(bool)
QFontMetrics fontMetrics() const
QgsPoint mapToLayerCoordinates(QgsMapLayer *theLayer, QgsPoint point) const
transform point coordinates from output CRS to layer&#39;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)
int size() const
void setRasterTransparency(QgsRasterTransparency *t)
A map tool that simply emits a point when clicking on the map.
QString toString() const
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.
Definition: qgsrectangle.h:212
void setContentsMargins(int left, int top, int right, int bottom)
const T value(const Key &key) const
Base class for raster data providers.