~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/qgsrasterlayerproperties.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
  qgsrasterlayerproperties.cpp  -  description
3
 
  -------------------
4
 
begin                : 1/1/2004
5
 
copyright            : (C) 2004 Tim Sutton
6
 
email                : tim@linfiniti.com
7
 
 ***************************************************************************/
8
 
 
9
 
/***************************************************************************
10
 
 *                                                                         *
11
 
 *   This program is free software; you can redistribute it and/or modify  *
12
 
 *   it under the terms of the GNU General Public License as published by  *
13
 
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 
 *   (at your option) any later version.                                   *
15
 
 *                                                                         *
16
 
 ***************************************************************************/
17
 
 
18
 
 
19
 
#include "qgsrasterlayerproperties.h"
20
 
#include "qgslayerprojectionselector.h"
21
 
#include "qgsproject.h"
22
 
#include <qlabel.h>
23
 
#include <qapplication.h>
24
 
#include <qpixmap.h>
25
 
#include <qimage.h>
26
 
#include <qslider.h>
27
 
#include <qcombobox.h>
28
 
#include <qcheckbox.h>
29
 
#include <qgroupbox.h>
30
 
#include <qstring.h>
31
 
#include <qradiobutton.h>
32
 
#include <qlineedit.h>
33
 
#include <qtable.h>
34
 
#include <qtextedit.h>
35
 
#include <qpainter.h>
36
 
#include <qfont.h>
37
 
#include <qtabwidget.h>
38
 
#include <qwidget.h>
39
 
#include <qlistview.h>
40
 
#include <qlistbox.h>
41
 
#include <qtextbrowser.h>
42
 
#include <qspinbox.h>
43
 
#include <qpointarray.h>
44
 
#include <qrect.h>
45
 
 
46
 
const char * const ident = 
47
 
  "$Id: qgsrasterlayerproperties.cpp,v 1.57.2.9 2005/10/16 23:35:33 timlinux Exp $";
48
 
 
49
 
 
50
 
QgsRasterLayerProperties::QgsRasterLayerProperties(QgsMapLayer *lyr, QWidget *parent, const char *name, bool modal)
51
 
: QgsRasterLayerPropertiesBase(parent, name, modal), 
52
 
  rasterLayer( dynamic_cast<QgsRasterLayer*>(lyr) )
53
 
{
54
 
 
55
 
  // set up the scale based layer visibility stuff....
56
 
  chkUseScaleDependentRendering->setChecked(lyr->scaleBasedVisibility());
57
 
  spinMinimumScale->setValue((int)lyr->minScale());
58
 
  spinMaximumScale->setValue((int)lyr->maxScale());
59
 
 
60
 
  // build GUI components
61
 
 
62
 
  cboColorMap->insertItem(tr("Grayscale"));
63
 
  cboColorMap->insertItem(tr("Pseudocolor"));
64
 
  cboColorMap->insertItem(tr("Freak Out"));
65
 
 
66
 
  //set the std deviations to be plotted combo box
67
 
  cboStdDev->insertItem("0");
68
 
  cboStdDev->insertItem("0.5");
69
 
  cboStdDev->insertItem("0.75");
70
 
  cboStdDev->insertItem("1");
71
 
  cboStdDev->insertItem("1.25");
72
 
  cboStdDev->insertItem("1.5");
73
 
  cboStdDev->insertItem("1.75");
74
 
  cboStdDev->insertItem("2");
75
 
  cboStdDev->insertItem("2.25");
76
 
  cboStdDev->insertItem("2.5");
77
 
  cboStdDev->insertItem("2.75");
78
 
  cboStdDev->insertItem("3");
79
 
 
80
 
  //
81
 
  // Set up the combo boxes that contain band lists using the qstring list generated above
82
 
  //
83
 
 
84
 
  if (rasterLayer->getRasterLayerType()
85
 
          == QgsRasterLayer::PALETTE) //paletted layers have hard coded color entries
86
 
  {
87
 
    cboRed->insertItem("Red");
88
 
    cboGreen->insertItem("Red");
89
 
    cboBlue->insertItem("Red");
90
 
 
91
 
    cboRed->insertItem("Green");
92
 
    cboGreen->insertItem("Green");
93
 
    cboBlue->insertItem("Green");
94
 
 
95
 
    cboRed->insertItem("Blue");
96
 
    cboGreen->insertItem("Blue");
97
 
    cboBlue->insertItem("Blue");
98
 
 
99
 
    cboRed->insertItem(tr("Not Set"));
100
 
    cboGreen->insertItem(tr("Not Set"));
101
 
    cboBlue->insertItem(tr("Not Set"));
102
 
 
103
 
    cboGray->insertItem("Red");
104
 
    cboGray->insertItem("Green");
105
 
    cboGray->insertItem("Blue");
106
 
    cboGray->insertItem(tr("Not Set"));
107
 
 
108
 
    lstHistogramLabels->insertItem(tr("Palette"));
109
 
  }
110
 
  else                   // all other layer types use band name entries only
111
 
  {
112
 
#ifdef QGISDEBUG
113
 
    std::cout << "Populating combos for non paletted layer" << std::endl;
114
 
#endif
115
 
 
116
 
    //
117
 
    // Get a list of band names
118
 
    //
119
 
    QStringList myBandNameQStringList;
120
 
 
121
 
    int myBandCountInt = rasterLayer->getBandCount();
122
 
#ifdef QGISDEBIG
123
 
    std::cout << "Looping though " << myBandCountInt << " image layers to get their names " << std::endl;
124
 
#endif
125
 
    for (int myIteratorInt = 1;
126
 
            myIteratorInt <= myBandCountInt;
127
 
            ++myIteratorInt)
128
 
    {
129
 
      //find out the name of this band
130
 
      QString myRasterBandNameQString = rasterLayer->getRasterBandName(myIteratorInt) ;
131
 
 
132
 
      //
133
 
      //add the band to the histogram tab
134
 
      //
135
 
      QPixmap myPixmap(10,10);
136
 
 
137
 
      if (myBandCountInt==1) //draw single band images with black
138
 
      {
139
 
        myPixmap.fill( Qt::black );
140
 
      }
141
 
      else if (myIteratorInt==1)
142
 
      {
143
 
        myPixmap.fill( Qt::red );
144
 
      }
145
 
      else if (myIteratorInt==2)
146
 
      {
147
 
        myPixmap.fill( Qt::green );
148
 
      }
149
 
      else if (myIteratorInt==3)
150
 
      {
151
 
        myPixmap.fill( Qt::blue );
152
 
      }
153
 
      else if (myIteratorInt==4)
154
 
      {
155
 
        myPixmap.fill( Qt::magenta );
156
 
      }
157
 
      else if (myIteratorInt==5)
158
 
      {
159
 
        myPixmap.fill( Qt::darkRed );
160
 
      }
161
 
      else if (myIteratorInt==6)
162
 
      {
163
 
        myPixmap.fill( Qt::darkGreen );
164
 
      }
165
 
      else if (myIteratorInt==7)
166
 
      {
167
 
        myPixmap.fill( Qt::darkBlue );
168
 
      }
169
 
      else
170
 
      {
171
 
        myPixmap.fill( Qt::gray );
172
 
      }
173
 
      lstHistogramLabels->insertItem(myPixmap,myRasterBandNameQString);
174
 
      //keep a list of band names for later use
175
 
      //! @note band names should not be translated!
176
 
      myBandNameQStringList.append(myRasterBandNameQString);
177
 
    }
178
 
 
179
 
    //select all histogram layers list items by default
180
 
    for (int myIteratorInt = 1;
181
 
          myIteratorInt <= myBandCountInt;
182
 
          ++myIteratorInt)
183
 
    {
184
 
      QListBoxItem *myItem = lstHistogramLabels->item( myIteratorInt-1 );
185
 
      lstHistogramLabels->setSelected( myItem,true);
186
 
    }
187
 
 
188
 
    for (QStringList::Iterator myIterator = myBandNameQStringList.begin(); 
189
 
            myIterator != myBandNameQStringList.end(); 
190
 
            ++myIterator)
191
 
    {
192
 
      QString myQString = *myIterator;
193
 
#ifdef QGISDEBUG
194
 
 
195
 
      std::cout << "Inserting : " << myQString.local8Bit() << std::endl;
196
 
#endif
197
 
 
198
 
      cboGray->insertItem(myQString);
199
 
      cboRed->insertItem(myQString);
200
 
      cboGreen->insertItem(myQString);
201
 
      cboBlue->insertItem(myQString);
202
 
    }
203
 
    cboRed->insertItem(tr("Not Set"));
204
 
    cboGreen->insertItem(tr("Not Set"));
205
 
    cboBlue->insertItem(tr("Not Set"));
206
 
    cboGray->insertItem(tr("Not Set"));
207
 
  }
208
 
 
209
 
  //
210
 
#if defined(WIN32) || defined(Q_OS_MACX)
211
 
  QString PKGDATAPATH = qApp->applicationDirPath() + "/share/qgis";
212
 
#endif
213
 
  QPixmap myPyramidPixmap(QString(PKGDATAPATH) + QString("/images/icons/pyramid.png"));
214
 
  QPixmap myNoPyramidPixmap(QString(PKGDATAPATH) + QString("/images/icons/no_pyramid.png"));
215
 
 
216
 
  RasterPyramidList myPyramidList = rasterLayer->buildRasterPyramidList();
217
 
  RasterPyramidList::iterator myRasterPyramidIterator;
218
 
 
219
 
  for ( myRasterPyramidIterator=myPyramidList.begin();
220
 
          myRasterPyramidIterator != myPyramidList.end();
221
 
          ++myRasterPyramidIterator )
222
 
  {
223
 
    if ((*myRasterPyramidIterator).existsFlag==true)
224
 
    {
225
 
      lbxPyramidResolutions->insertItem(myPyramidPixmap,
226
 
              QString::number((*myRasterPyramidIterator).xDimInt) + QString(" x ") + 
227
 
              QString::number((*myRasterPyramidIterator).yDimInt)); 
228
 
    }
229
 
    else
230
 
    {
231
 
      lbxPyramidResolutions->insertItem(myNoPyramidPixmap,
232
 
              QString::number((*myRasterPyramidIterator).xDimInt) + QString(" x ") + 
233
 
              QString::number((*myRasterPyramidIterator).yDimInt)); 
234
 
    }
235
 
  }
236
 
 
237
 
  if ( rasterLayer->coordinateTransform() )
238
 
  {
239
 
    leSpatialRefSys->setText(rasterLayer->coordinateTransform()->sourceSRS().proj4String());
240
 
  }
241
 
  //draw the histogram
242
 
  //pbnHistRefresh_clicked();
243
 
 
244
 
  // update based on lyr's current state
245
 
  sync();  
246
 
} // QgsRasterLayerProperties ctor
247
 
 
248
 
 
249
 
QgsRasterLayerProperties::~QgsRasterLayerProperties()
250
 
{
251
 
  delete rasterLayer;
252
 
}
253
 
 
254
 
 
255
 
void QgsRasterLayerProperties::apply()
256
 
{
257
 
  // set up the scale based layer visibility stuff....
258
 
  rasterLayer->setScaleBasedVisibility(chkUseScaleDependentRendering->isChecked());
259
 
  rasterLayer->setMinScale(spinMinimumScale->value());
260
 
  rasterLayer->setMaxScale(spinMaximumScale->value());
261
 
 
262
 
  rasterLayer->setTransparency(static_cast < unsigned int >(255 - sliderTransparency->value()));
263
 
  //set the std deviations to be plotted
264
 
  rasterLayer->setStdDevsToPlot(cboStdDev->currentText().toDouble());
265
 
  //set whether the layer histogram should be inverted
266
 
  if (cboxInvertColorMap->isChecked()
267
 
     )
268
 
  {
269
 
    rasterLayer->setInvertHistogramFlag(true);
270
 
  }
271
 
  else
272
 
  {
273
 
    rasterLayer->setInvertHistogramFlag(false);
274
 
  }
275
 
  //now set the color -> band mapping combos to the correct values
276
 
  rasterLayer->setRedBandName(cboRed->currentText());
277
 
  rasterLayer->setGreenBandName(cboGreen->currentText());
278
 
  rasterLayer->setBlueBandName(cboBlue->currentText());
279
 
  rasterLayer->setGrayBandName(cboGray->currentText());
280
 
  //set the appropriate color ramping type
281
 
  if (cboColorMap->currentText() == tr("Pseudocolor"))
282
 
  {
283
 
    rasterLayer->setColorRampingType(QgsRasterLayer::BLUE_GREEN_RED);  
284
 
  }
285
 
  else if (cboColorMap->currentText() == tr("Freak Out"))
286
 
  {
287
 
    rasterLayer->setColorRampingType(QgsRasterLayer::FREAK_OUT);  
288
 
  }
289
 
  //set the appropriate render style
290
 
  if ((grpBoxGrayscale->isEnabled())
291
 
          && (rbtnSingleBand->isChecked()))
292
 
  {
293
 
    //
294
 
    // Grayscale
295
 
    //
296
 
    if (rasterLayer->rasterLayerType == QgsRasterLayer::GRAY_OR_UNDEFINED)
297
 
    {
298
 
 
299
 
      if (cboColorMap->currentText() != tr("Grayscale"))
300
 
      {
301
 
#ifdef QGISDEBUG
302
 
        std::cout << "Setting Raster Drawing Style to :: SINGLE_BAND_PSEUDO_COLOR" << std::endl;
303
 
#endif
304
 
 
305
 
        rasterLayer->setDrawingStyle(QgsRasterLayer::SINGLE_BAND_PSEUDO_COLOR);
306
 
      }
307
 
      else
308
 
      {
309
 
#ifdef QGISDEBUG
310
 
        std::cout << "Setting Raster Drawing Style to :: SINGLE_BAND_GRAY" << std::endl;
311
 
#endif
312
 
 
313
 
        rasterLayer->setDrawingStyle(QgsRasterLayer::SINGLE_BAND_GRAY);
314
 
      }
315
 
    }
316
 
    //
317
 
    // Paletted Image
318
 
    //
319
 
    else if (rasterLayer->rasterLayerType == QgsRasterLayer::PALETTE)
320
 
    {
321
 
      if (cboColorMap->currentText() != tr("Grayscale"))
322
 
      {
323
 
#ifdef QGISDEBUG
324
 
        std::cout << "Setting Raster Drawing Style to :: PALETTED_SINGLE_BAND_PSEUDO_COLOR" << std::endl;
325
 
#endif
326
 
 
327
 
        rasterLayer->setDrawingStyle(QgsRasterLayer::PALETTED_SINGLE_BAND_PSEUDO_COLOR);
328
 
      }
329
 
      else
330
 
      {
331
 
#ifdef QGISDEBUG
332
 
        std::cout << "Setting Raster Drawing Style to :: PALETTED_SINGLE_BAND_GRAY" << std::endl;
333
 
#endif
334
 
#ifdef QGISDEBUG
335
 
 
336
 
        std::cout << "Combo value : " << cboGray->currentText().local8Bit() << " GrayBand Mapping : " << rasterLayer->
337
 
            getGrayBandName().local8Bit() << std::endl;
338
 
#endif
339
 
 
340
 
        rasterLayer->setDrawingStyle(QgsRasterLayer::PALETTED_SINGLE_BAND_GRAY);
341
 
      }
342
 
    }
343
 
    //
344
 
    // Mutltiband
345
 
    //
346
 
    else if (rasterLayer->rasterLayerType == QgsRasterLayer::MULTIBAND)
347
 
    {
348
 
      if (cboColorMap->currentText() != tr("Grayscale"))
349
 
      {
350
 
#ifdef QGISDEBUG
351
 
        std::cout << "Setting Raster Drawing Style to ::MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR " << std::endl;
352
 
#endif
353
 
 
354
 
        rasterLayer->setDrawingStyle(QgsRasterLayer::MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR);
355
 
      }
356
 
      else
357
 
      {
358
 
#ifdef QGISDEBUG
359
 
        std::cout << "Setting Raster Drawing Style to :: MULTI_BAND_SINGLE_BAND_GRAY" << std::endl;
360
 
        std::cout << "Combo value : " << cboGray->currentText().local8Bit() << " GrayBand Mapping : " << rasterLayer->
361
 
            getGrayBandName().local8Bit() << std::endl;
362
 
#endif
363
 
 
364
 
        rasterLayer->setDrawingStyle(QgsRasterLayer::MULTI_BAND_SINGLE_BAND_GRAY);
365
 
 
366
 
      }
367
 
    }
368
 
  }                           //end of grayscale box enabled and rbtnsingleband checked
369
 
  else                          //assume that rbtnThreeBand is checked and render in rgb color
370
 
  {
371
 
    //set the grayscale color table type if the groupbox is enabled
372
 
 
373
 
    if (rasterLayer->rasterLayerType == QgsRasterLayer::PALETTE)
374
 
    {
375
 
#ifdef QGISDEBUG
376
 
      std::cout << "Setting Raster Drawing Style to :: PALETTED_MULTI_BAND_COLOR" << std::endl;
377
 
#endif
378
 
 
379
 
      rasterLayer->setDrawingStyle(QgsRasterLayer::PALETTED_MULTI_BAND_COLOR);
380
 
    }
381
 
    else if (rasterLayer->rasterLayerType == QgsRasterLayer::MULTIBAND)
382
 
    {
383
 
 
384
 
#ifdef QGISDEBUG
385
 
      std::cout << "Setting Raster Drawing Style to :: MULTI_BAND_COLOR" << std::endl;
386
 
#endif
387
 
 
388
 
      rasterLayer->setDrawingStyle(QgsRasterLayer::MULTI_BAND_COLOR);
389
 
    }
390
 
 
391
 
  }
392
 
  
393
 
  rasterLayer->setLayerName(leDisplayName->text());
394
 
  
395
 
  //update the legend pixmap
396
 
  pixmapLegend->setPixmap(rasterLayer->getLegendQPixmap());
397
 
  pixmapLegend->setScaledContents(true);
398
 
  pixmapLegend->repaint(false);
399
 
  rasterLayer->updateItemPixmap();
400
 
  
401
 
  //see if the user would like debug overlays
402
 
  if (cboxShowDebugInfo->isChecked()
403
 
          == true)
404
 
  {
405
 
    rasterLayer->setShowDebugOverlayFlag(true);
406
 
  }
407
 
  else
408
 
  {
409
 
    rasterLayer->setShowDebugOverlayFlag(false);
410
 
  }
411
 
  //
412
 
  // update histogram clipping ranges from the advanced symbology tab
413
 
  //
414
 
  rasterLayer->setMinRedDouble(static_cast < double >(sliderMinRed->value()));
415
 
  rasterLayer->setMaxRedDouble(static_cast < double >(255 - sliderMaxRed->value()));
416
 
  rasterLayer->setMinGreenDouble(static_cast < double >(sliderMinGreen->value()));
417
 
  rasterLayer->setMaxGreenDouble(static_cast < double >(255 - sliderMaxGreen->value()));
418
 
  rasterLayer->setMinBlueDouble(static_cast < double >(sliderMinBlue->value()));
419
 
  rasterLayer->setMaxBlueDouble(static_cast < double >(255 - sliderMaxBlue->value()));
420
 
  rasterLayer->setMinGrayDouble(static_cast < double >(sliderMinGray->value()));
421
 
  rasterLayer->setMaxGrayDouble(static_cast < double >(255 - sliderMaxGray->value()));
422
 
  //get the thumbnail for the layer
423
 
  QPixmap myQPixmap = QPixmap(pixmapThumbnail->width(),pixmapThumbnail->height());
424
 
  rasterLayer->drawThumbnail(&myQPixmap);
425
 
  pixmapThumbnail->setPixmap(myQPixmap);
426
 
  //make sure the layer is redrawn
427
 
  rasterLayer->triggerRepaint();
428
 
}//apply
429
 
 
430
 
void QgsRasterLayerProperties::accept()
431
 
{
432
 
  apply();
433
 
  close();
434
 
}//accept
435
 
void QgsRasterLayerProperties::sliderTransparency_valueChanged(int theValue)
436
 
{
437
 
  //set the transparency percentage label to a suitable value
438
 
  int myInt = static_cast < int >((theValue / 255.0) * 100);  //255.0 to prevent integer division
439
 
  lblTransparencyPercent->setText(QString::number(myInt) + "%");
440
 
}//sliderTransparency_valueChanged
441
 
 
442
 
void QgsRasterLayerProperties::sliderMaxRed_valueChanged(int)
443
 
{
444
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
445
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
446
 
  if ((255 - sliderMaxRed->value()) < sliderMinRed->value())
447
 
  {
448
 
    sliderMinRed->setValue(255 - sliderMaxRed->value());
449
 
  }
450
 
  makeScalePreview("red");
451
 
}
452
 
 
453
 
 
454
 
void QgsRasterLayerProperties::sliderMinRed_valueChanged(int)
455
 
{
456
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
457
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
458
 
  if ((255 - sliderMaxRed->value()) < sliderMinRed->value())
459
 
  {
460
 
    sliderMaxRed->setValue(255 - sliderMinRed->value());
461
 
  }
462
 
  makeScalePreview("red");
463
 
}
464
 
 
465
 
 
466
 
void QgsRasterLayerProperties::sliderMaxBlue_valueChanged(int)
467
 
{
468
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
469
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
470
 
  if ((255 - sliderMaxBlue->value()) < sliderMinBlue->value())
471
 
  {
472
 
    sliderMinBlue->setValue(255 - sliderMaxBlue->value());
473
 
  }
474
 
  makeScalePreview("blue");
475
 
}
476
 
 
477
 
 
478
 
void QgsRasterLayerProperties::sliderMinBlue_valueChanged(int)
479
 
{
480
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
481
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
482
 
  if ((255 - sliderMaxBlue->value()) < sliderMinBlue->value())
483
 
  {
484
 
    sliderMaxBlue->setValue(255 - sliderMinBlue->value());
485
 
  }
486
 
 
487
 
  makeScalePreview("blue");
488
 
}
489
 
 
490
 
 
491
 
void QgsRasterLayerProperties::sliderMaxGreen_valueChanged(int)
492
 
{
493
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
494
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
495
 
  if ((255 - sliderMaxGreen->value()) < sliderMinGreen->value())
496
 
  {
497
 
    sliderMinGreen->setValue(255 - sliderMaxGreen->value());
498
 
  }
499
 
 
500
 
  makeScalePreview("green");
501
 
}
502
 
 
503
 
 
504
 
void QgsRasterLayerProperties::sliderMinGreen_valueChanged(int)
505
 
{
506
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
507
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
508
 
 
509
 
  if ((255 - sliderMaxGreen->value()) < sliderMinGreen->value())
510
 
  {
511
 
    sliderMaxGreen->setValue(255 - sliderMinGreen->value());
512
 
  }
513
 
  makeScalePreview("green");
514
 
}
515
 
 
516
 
 
517
 
void QgsRasterLayerProperties::sliderMaxGray_valueChanged(int)
518
 
{
519
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
520
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
521
 
 
522
 
  if ((255 - sliderMaxGray->value()) < sliderMinGray->value())
523
 
  {
524
 
    sliderMinGray->setValue(255 - sliderMaxGray->value());
525
 
  }
526
 
  makeScalePreview("gray");
527
 
}
528
 
 
529
 
 
530
 
void QgsRasterLayerProperties::sliderMinGray_valueChanged(int)
531
 
{
532
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
533
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
534
 
  if ((255 - sliderMaxGray->value()) < sliderMinGray->value())
535
 
  {
536
 
    sliderMaxGray->setValue(255 - sliderMinGray->value());
537
 
  }
538
 
  makeScalePreview("gray");
539
 
}
540
 
 
541
 
 
542
 
 
543
 
void QgsRasterLayerProperties::makeScalePreview(QString theColor)
544
 
{
545
 
  double myMinDouble = 0;
546
 
  double myMaxDouble = 255;
547
 
  double myRedDouble = 0;
548
 
  double myBlueDouble = 0;
549
 
  double myGreenDouble = 0;
550
 
  unsigned int myTransparencyInt = sliderTransparency->value();
551
 
  //the 255- is used because a vertical qslider has its max value at the bottom and
552
 
  //we want it to appear to the user that the max value is at the top, so we invert its value
553
 
  if (theColor == "red")
554
 
  {
555
 
    myMinDouble = sliderMinRed->value();
556
 
    myMaxDouble = 255 - sliderMaxRed->value();
557
 
    myRedDouble = myMaxDouble;
558
 
  }
559
 
  else if (theColor == "green")
560
 
  {
561
 
    myMinDouble = sliderMinGreen->value();
562
 
    myMaxDouble = 255 - sliderMaxGreen->value();
563
 
  }
564
 
  else if (theColor == "blue")
565
 
  {
566
 
    myMinDouble = sliderMinBlue->value();
567
 
    myMaxDouble = 255 - sliderMaxBlue->value();
568
 
  }
569
 
  else if (theColor == "gray")
570
 
  {
571
 
    myMinDouble = sliderMinGray->value();
572
 
    myMaxDouble = 255 - sliderMaxGray->value();
573
 
  }
574
 
  QImage myQImage = QImage(100, 100, 32); //32bpp
575
 
  double myRangeDouble = myMaxDouble - myMinDouble;
576
 
  double myDecrementDouble = myRangeDouble / 100;
577
 
  //std::cout << "Decrementing " << theColor << " by : " << myDecrementDouble << std::endl;
578
 
  if (myDecrementDouble == 0)
579
 
    return;
580
 
  for (int myRowInt = 99; myRowInt >= 0; myRowInt = myRowInt - 1)
581
 
  {
582
 
    //reset the max value that the scale starts at
583
 
    if (theColor == "red")
584
 
    {
585
 
      myRedDouble = myMaxDouble;
586
 
    }
587
 
    else if (theColor == "green")
588
 
    {
589
 
      myGreenDouble = myMaxDouble;
590
 
    }
591
 
    else if (theColor == "blue")
592
 
    {
593
 
      myBlueDouble = myMaxDouble;
594
 
    }
595
 
    else if (theColor == "gray")
596
 
    {
597
 
      myRedDouble = myMaxDouble;
598
 
      myGreenDouble = myMaxDouble;
599
 
      myBlueDouble = myMaxDouble;
600
 
    }
601
 
    for (int myColInt = 99; myColInt >= 0; myColInt--)
602
 
    {
603
 
      if (theColor == "red")
604
 
      {
605
 
        myRedDouble -= myDecrementDouble;
606
 
      }
607
 
      else if (theColor == "green")
608
 
      {
609
 
        myGreenDouble -= myDecrementDouble;
610
 
      }
611
 
      else if (theColor == "blue")
612
 
      {
613
 
        myBlueDouble -= myDecrementDouble;
614
 
      }
615
 
      else if (theColor == "gray")
616
 
      {
617
 
        myRedDouble -= myDecrementDouble;
618
 
        myGreenDouble -= myDecrementDouble;
619
 
        myBlueDouble -= myDecrementDouble;
620
 
      }
621
 
      //std::cout << "R " << myRedDouble << " G " << myGreenDouble << " B " << myBlueDouble << std::endl;
622
 
      myQImage.setPixel(myColInt, myRowInt,
623
 
              qRgb((unsigned int) myRedDouble, (unsigned int) myGreenDouble, (unsigned int) myBlueDouble));
624
 
    }
625
 
  }
626
 
  // Create a pixmap the same size as the image - to be placed in the pixmalLabel
627
 
  QPixmap *myQPixmap = new QPixmap(100, 100);
628
 
 
629
 
  //
630
 
  // Draw a text alabel onto the pixmap showing the min max value
631
 
  //
632
 
  QPainter myQPainter(myQPixmap);
633
 
  myQPainter.rotate(-45);
634
 
  myQPainter.drawImage(-70, 0, myQImage.scale(140, 140));
635
 
  myQPainter.rotate(45);
636
 
  QFont myQFont("arial", 18, QFont::Bold);
637
 
  myQPainter.setFont(myQFont);
638
 
  myQPainter.setPen(Qt::white);
639
 
  myQPainter.drawText(15, 50, QString::number(static_cast < unsigned int >(myMinDouble)) + " - " + QString::number(static_cast
640
 
              <
641
 
              unsigned int
642
 
              >(myMaxDouble)));
643
 
 
644
 
  //now draw the image into the relevant pixmap label
645
 
  if (theColor == "red")
646
 
  {
647
 
    pixmapScaleRed->setScaledContents(true);
648
 
    pixmapScaleRed->setPixmap(*myQPixmap);
649
 
    pixmapScaleRed->repaint(false);
650
 
  }
651
 
  else if (theColor == "green")
652
 
  {
653
 
    pixmapScaleGreen->setScaledContents(true);
654
 
    pixmapScaleGreen->setPixmap(*myQPixmap);
655
 
    pixmapScaleGreen->repaint(false);
656
 
  }
657
 
  else if (theColor == "blue")
658
 
  {
659
 
    pixmapScaleBlue->setScaledContents(true);
660
 
    pixmapScaleBlue->setPixmap(*myQPixmap);
661
 
    pixmapScaleBlue->repaint(false);
662
 
  }
663
 
  else if (theColor == "gray")
664
 
  {
665
 
    pixmapScaleGray->setScaledContents(true);
666
 
    pixmapScaleGray->setPixmap(*myQPixmap);
667
 
    pixmapScaleGray->repaint(false);
668
 
  }
669
 
}
670
 
 
671
 
 
672
 
void QgsRasterLayerProperties::rbtnSingleBand_toggled(bool)
673
 
{}
674
 
 
675
 
 
676
 
void QgsRasterLayerProperties::rbtnThreeBand_toggled(bool)
677
 
{}
678
 
 
679
 
void QgsRasterLayerProperties::buttonBuildPyramids_clicked()
680
 
{
681
 
 
682
 
  //
683
 
  // Go through the list marking any files that are selected in the listview
684
 
  // as true so that we can generate pyramids for them.
685
 
  //
686
 
  RasterPyramidList myPyramidList = rasterLayer->buildRasterPyramidList();
687
 
  for ( unsigned int myCounterInt = 0; myCounterInt < lbxPyramidResolutions->count(); myCounterInt++ )
688
 
  {
689
 
    QListBoxItem *myItem = lbxPyramidResolutions->item( myCounterInt );
690
 
    if ( myItem->isSelected() )
691
 
    {
692
 
      //mark to be pyramided
693
 
      myPyramidList[myCounterInt].existsFlag=true;
694
 
    }
695
 
  }
696
 
  //
697
 
  // Ask raster layer to build the pyramids
698
 
  //
699
 
  rasterLayer->buildPyramids(myPyramidList,cboResamplingMethod->currentText());  
700
 
  //
701
 
  // repopulate the pyramids list
702
 
  //
703
 
  lbxPyramidResolutions->clear();
704
 
#if defined(WIN32) || defined(Q_OS_MACX)
705
 
  QString PKGDATAPATH = qApp->applicationDirPath() + "/share/qgis";
706
 
#endif
707
 
  QPixmap myPyramidPixmap(QString(PKGDATAPATH) + QString("/images/icons/pyramid.png"));
708
 
  QPixmap myNoPyramidPixmap(QString(PKGDATAPATH) + QString("/images/icons/no_pyramid.png"));
709
 
  RasterPyramidList::iterator myRasterPyramidIterator;
710
 
  for ( myRasterPyramidIterator=myPyramidList.begin();
711
 
          myRasterPyramidIterator != myPyramidList.end();
712
 
          ++myRasterPyramidIterator )
713
 
  {
714
 
    if ((*myRasterPyramidIterator).existsFlag==true)
715
 
    {
716
 
      lbxPyramidResolutions->insertItem(myPyramidPixmap,
717
 
              QString::number((*myRasterPyramidIterator).xDimInt) + QString(" x ") + 
718
 
              QString::number((*myRasterPyramidIterator).yDimInt)); 
719
 
    }
720
 
    else
721
 
    {
722
 
      lbxPyramidResolutions->insertItem(myNoPyramidPixmap,
723
 
              QString::number((*myRasterPyramidIterator).xDimInt) + QString(" x ") + 
724
 
              QString::number((*myRasterPyramidIterator).yDimInt)); 
725
 
    }
726
 
  }
727
 
  //update the legend pixmap
728
 
  pixmapLegend->setPixmap(rasterLayer->getLegendQPixmap());
729
 
  pixmapLegend->setScaledContents(true);
730
 
  pixmapLegend->repaint(false);
731
 
  rasterLayer->updateItemPixmap();
732
 
  //populate the metadata tab's text browser widget with gdal metadata info
733
 
  txtbMetadata->setText(rasterLayer->getMetadata());
734
 
}
735
 
 
736
 
 
737
 
/**
738
 
  @note moved from ctor
739
 
 
740
 
  Previously this dialog was created anew with each right-click pop-up menu
741
 
  invokation.  Changed so that the dialog always exists after first
742
 
  invocation, and is just re-synchronized with its layer's state when
743
 
  re-shown.
744
 
 
745
 
*/
746
 
void QgsRasterLayerProperties::sync()
747
 
{
748
 
  // XXX Remove the advanced symbology widget and debug overlay for 0.1 release
749
 
 
750
 
  tabSymbology->removePage(TabPage);
751
 
  cboxShowDebugInfo->hide();
752
 
 
753
 
  //get the thumbnail for the layer
754
 
  QPixmap myQPixmap = QPixmap(pixmapThumbnail->width(),pixmapThumbnail->height());
755
 
  rasterLayer->drawThumbnail(&myQPixmap);
756
 
  pixmapThumbnail->setPixmap(myQPixmap);
757
 
 
758
 
  //populate the metadata tab's text browser widget with gdal metadata info
759
 
  txtbMetadata->setText(rasterLayer->getMetadata());
760
 
  //tabSymbology->removePage(tabMetadata);
761
 
  //display the raster dimensions and no data value
762
 
  lblColumns->setText(tr("Columns:") + QString::number(rasterLayer->getRasterXDim()));
763
 
  lblRows->setText(tr("Rows:") + QString::number(rasterLayer->getRasterYDim()));
764
 
  lblNoData->setText(tr("No Data:") + QString::number(rasterLayer->getNoDataValue()));
765
 
 
766
 
  //these properties (layername and label) are provided by the qgsmaplayer superclass
767
 
  leLayerSource->setText(rasterLayer->source());
768
 
  leDisplayName->setText(rasterLayer->name());
769
 
 
770
 
  //update the debug checkbox
771
 
  cboxShowDebugInfo->setChecked(rasterLayer->getShowDebugOverlayFlag());
772
 
 
773
 
  //update the legend pixmap on this dialog
774
 
  pixmapLegend->setPixmap(rasterLayer->getLegendQPixmap());
775
 
  pixmapLegend->setScaledContents(true);
776
 
  pixmapLegend->repaint(false);
777
 
 
778
 
  //set the palette pixmap
779
 
  pixmapPalette->setPixmap(rasterLayer->getPaletteAsPixmap());
780
 
  pixmapPalette->setScaledContents(true);
781
 
  pixmapPalette->repaint(false);
782
 
 
783
 
  //set the transparency slider
784
 
  sliderTransparency->setValue(255 - rasterLayer->getTransparency());
785
 
  //update the transparency percentage label
786
 
  sliderTransparency_valueChanged(255 - rasterLayer->getTransparency());
787
 
  //decide whether user can change rgb settings
788
 
 
789
 
  switch (rasterLayer->getDrawingStyle())
790
 
  {
791
 
      case QgsRasterLayer::SINGLE_BAND_GRAY:
792
 
          rbtnThreeBand->setEnabled(false);
793
 
          rbtnSingleBand->setEnabled(true);
794
 
          rbtnSingleBand->toggle();
795
 
          break;
796
 
      case QgsRasterLayer::SINGLE_BAND_PSEUDO_COLOR:
797
 
          rbtnThreeBand->setEnabled(false);
798
 
          rbtnSingleBand->setEnabled(true);
799
 
          rbtnSingleBand->toggle();
800
 
          break;
801
 
      case QgsRasterLayer::PALETTED_SINGLE_BAND_GRAY:
802
 
          rbtnThreeBand->setEnabled(true);
803
 
          rbtnSingleBand->setEnabled(true);
804
 
          rbtnSingleBand->toggle();
805
 
          break;
806
 
      case QgsRasterLayer::PALETTED_SINGLE_BAND_PSEUDO_COLOR:
807
 
          rbtnThreeBand->setEnabled(true);
808
 
          rbtnSingleBand->setEnabled(true);
809
 
          rbtnSingleBand->toggle();
810
 
          break;
811
 
      case QgsRasterLayer::PALETTED_MULTI_BAND_COLOR:
812
 
          rbtnThreeBand->setEnabled(true);
813
 
          rbtnSingleBand->setEnabled(true);
814
 
          rbtnThreeBand->toggle();
815
 
          break;
816
 
      case QgsRasterLayer::MULTI_BAND_SINGLE_BAND_GRAY:
817
 
          rbtnThreeBand->setEnabled(true);
818
 
          rbtnSingleBand->setEnabled(true);
819
 
          rbtnSingleBand->toggle();
820
 
          break;
821
 
      case QgsRasterLayer::MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR:
822
 
          rbtnThreeBand->setEnabled(true);
823
 
          rbtnSingleBand->setEnabled(true);
824
 
          rbtnSingleBand->toggle();
825
 
          break;
826
 
      case QgsRasterLayer::MULTI_BAND_COLOR:
827
 
          rbtnThreeBand->setEnabled(true);
828
 
          rbtnSingleBand->setEnabled(true);
829
 
          rbtnThreeBand->toggle();
830
 
          break;
831
 
      default:
832
 
          break;
833
 
  }
834
 
 
835
 
 
836
 
  if (rasterLayer->getRasterLayerType() == QgsRasterLayer::MULTIBAND)
837
 
  {
838
 
    //multiband images can also be rendered as single band (using only one of the bands)
839
 
    txtSymologyNotes->
840
 
        setText(tr
841
 
                ("<h3>Multiband Image Notes</h3><p>This is a multiband image. You can choose to render it as grayscale or color (RGB). For color images, you can associate bands to colors arbitarily. For example, if you have a seven band landsat image, you may choose to render it as:</p><ul><li>Visible Blue (0.45 to 0.52 microns) - not mapped</li><li>Visible Green (0.52 to 0.60 microns) - not mapped</li></li>Visible Red (0.63 to 0.69 microns) - mapped to red in image</li><li>Near Infrared (0.76 to 0.90 microns) - mapped to green in image</li><li>Mid Infrared (1.55 to 1.75 microns) - not mapped</li><li>Thermal Infrared (10.4 to 12.5 microns) - not mapped</li><li>Mid Infrared (2.08 to 2.35 microns) - mapped to blue in image</li></ul>"));
842
 
  }
843
 
  else if (rasterLayer->getRasterLayerType() == QgsRasterLayer::PALETTE)
844
 
  {
845
 
    //paletted images (e.g. tif) can only be rendered as three band rgb images
846
 
    txtSymologyNotes->
847
 
        setText(tr
848
 
                ("<h3>Paletted Image Notes</h3> <p>This image uses a fixed color palette. You can remap these colors in different combinations e.g.</p><ul><li>Red - blue in image</li><li>Green - blue in image</li><li>Blue - green in image</li></ul>"));
849
 
  }
850
 
  else                        //only grayscale settings allowed
851
 
  {
852
 
    //grayscale images can only be rendered as singleband
853
 
    txtSymologyNotes->
854
 
        setText(tr
855
 
                ("<h3>Grayscale Image Notes</h3> <p>You can remap these grayscale colors to a pseudocolor image using an automatically generated color ramp.</p>"));
856
 
  }
857
 
 
858
 
 
859
 
  //
860
 
  // Populate the various controls on the form
861
 
  //
862
 
  if (rasterLayer->getDrawingStyle() == QgsRasterLayer::SINGLE_BAND_PSEUDO_COLOR ||
863
 
          rasterLayer->getDrawingStyle() == QgsRasterLayer::PALETTED_SINGLE_BAND_PSEUDO_COLOR ||
864
 
          rasterLayer->getDrawingStyle() == QgsRasterLayer::MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR)
865
 
  {
866
 
    if(rasterLayer->getColorRampingType()==QgsRasterLayer::BLUE_GREEN_RED)
867
 
    {
868
 
      cboColorMap->setCurrentText(tr("Pseudocolor"));
869
 
    }
870
 
    else
871
 
    {
872
 
      cboColorMap->setCurrentText(tr("Freak Out"));
873
 
    }
874
 
 
875
 
  }
876
 
  else
877
 
  {
878
 
    cboColorMap->setCurrentText(tr("Grayscale"));
879
 
  }
880
 
  //set whether the layer histogram should be inverted
881
 
  if (rasterLayer->getInvertHistogramFlag())
882
 
  {
883
 
    cboxInvertColorMap->setChecked(true);
884
 
  }
885
 
  else
886
 
  {
887
 
    cboxInvertColorMap->setChecked(false);
888
 
  }
889
 
 
890
 
  double myStdDevsDouble = rasterLayer->getStdDevsToPlot();
891
 
  cboStdDev->setCurrentText(QString::number(myStdDevsDouble));
892
 
 
893
 
  //
894
 
  // Set up the sliders on the advanced symbology tab
895
 
  //
896
 
  sliderMinRed->setValue(static_cast < int >(rasterLayer->getMinRedDouble()));
897
 
  sliderMaxRed->setValue(static_cast < int >(255 - rasterLayer->getMaxRedDouble()));
898
 
  sliderMinGreen->setValue(static_cast < int >(rasterLayer->getMinGreenDouble()));
899
 
  sliderMaxGreen->setValue(static_cast < int >(255 - rasterLayer->getMaxGreenDouble()));
900
 
  sliderMinBlue->setValue(static_cast < int >(rasterLayer->getMinBlueDouble()));
901
 
  sliderMaxBlue->setValue(static_cast < int >(255 - rasterLayer->getMaxBlueDouble()));
902
 
  sliderMinGray->setValue(static_cast < int >(rasterLayer->getMinGrayDouble()));
903
 
  sliderMaxGray->setValue(static_cast < int >(255 - rasterLayer->getMaxGrayDouble()));
904
 
 
905
 
  //now set the combos to the correct values
906
 
  cboRed->setCurrentText(rasterLayer->getRedBandName());
907
 
  cboGreen->setCurrentText(rasterLayer->getGreenBandName());
908
 
  cboBlue->setCurrentText(rasterLayer->getBlueBandName());
909
 
  cboGray->setCurrentText(rasterLayer->getGrayBandName());
910
 
 
911
 
  //
912
 
  // Set up the colour scaling previews
913
 
  //
914
 
  makeScalePreview("red");
915
 
  makeScalePreview("green");
916
 
  makeScalePreview("blue");
917
 
  makeScalePreview("gray");
918
 
 
919
 
 
920
 
} // QgsRasterLayerProperties::sync()
921
 
 
922
 
void QgsRasterLayerProperties::pbnHistRefresh_clicked()
923
 
{
924
 
#ifdef QGISDEBUG
925
 
  std::cout << "QgsRasterLayerProperties::pbnHistRefresh_clicked" << std::endl;
926
 
#endif
927
 
  int myBandCountInt = rasterLayer->getBandCount();
928
 
  //
929
 
  // Find out how many bands are selected and short circuit out clearing the image
930
 
  // if needed
931
 
  int mySelectionCount=0;
932
 
  for (int myIteratorInt = 1;
933
 
          myIteratorInt <= myBandCountInt;
934
 
          ++myIteratorInt)
935
 
  {
936
 
    RasterBandStats myRasterBandStats = rasterLayer->getRasterBandStats(myIteratorInt);
937
 
    QListBoxItem *myItem = lstHistogramLabels->item( myIteratorInt-1 );
938
 
    if ( myItem->isSelected() )
939
 
    {
940
 
      mySelectionCount++;
941
 
    }
942
 
  }
943
 
  if (mySelectionCount==0)
944
 
  {
945
 
    int myImageWidth = pixHistogram->width();
946
 
    int myImageHeight =  pixHistogram->height();
947
 
    QPixmap myPixmap(myImageWidth,myImageHeight);
948
 
    myPixmap.fill(Qt::white);
949
 
    pixHistogram->setPixmap(myPixmap);
950
 
  }
951
 
 
952
 
  // Explanation:
953
 
  // We use the gdal histogram creation routine is called for each selected  
954
 
  // layer. Currently the hist is hardcoded
955
 
  // to create 256 bins. Each bin stores the total number of cells that 
956
 
  // fit into the range defined by that bin.
957
 
  //
958
 
  // The graph routine below determines the greatest number of pixesl in any given 
959
 
  // bin in all selected layers, and the min. It then draws a scaled line between min 
960
 
  // and max - scaled to image height. 1 line drawn per selected band
961
 
  //
962
 
  const int BINCOUNT = spinHistBinCount->value();
963
 
  enum GRAPH_TYPE { BAR_CHART, LINE_CHART } myGraphType;
964
 
  if (radHistTypeBar->isOn()) myGraphType=BAR_CHART; else myGraphType=LINE_CHART;
965
 
  bool myIgnoreOutOfRangeFlag = chkHistIgnoreOutOfRange->isChecked();
966
 
  bool myThoroughBandScanFlag = chkHistAllowApproximation->isChecked();
967
 
 
968
 
  long myCellCount = rasterLayer->getRasterXDim() * rasterLayer->getRasterYDim();
969
 
 
970
 
 
971
 
#ifdef QGISDEBUG
972
 
  std::cout << "Computing histogram minima and maxima" << std::endl;
973
 
#endif
974
 
  //somtimes there are more bins than needed
975
 
  //we find out the last on that actully has data in it
976
 
  //so we can discard the rest adn the x-axis scales correctly
977
 
  int myLastBinWithData=0;
978
 
  //
979
 
  // First scan through to get max and min cell counts from among selected layers' histograms
980
 
  //
981
 
  double myYAxisMax=0;
982
 
  double myYAxisMin=0;
983
 
  int myXAxisMin=0;
984
 
  int myXAxisMax=0;
985
 
  for (int myIteratorInt = 1;
986
 
          myIteratorInt <= myBandCountInt;
987
 
          ++myIteratorInt)
988
 
  {
989
 
    RasterBandStats myRasterBandStats = rasterLayer->getRasterBandStats(myIteratorInt);
990
 
    //calculate the x axis min max
991
 
    if (myRasterBandStats.minValDouble < myXAxisMin || myIteratorInt==1)
992
 
    {
993
 
      myXAxisMin=static_cast < unsigned int >(myRasterBandStats.minValDouble);
994
 
    }
995
 
    if (myRasterBandStats.maxValDouble < myXAxisMax || myIteratorInt==1)
996
 
    {
997
 
      myXAxisMax=static_cast < unsigned int >(myRasterBandStats.maxValDouble);
998
 
    }
999
 
    QListBoxItem *myItem = lstHistogramLabels->item( myIteratorInt-1 );
1000
 
    if ( myItem->isSelected() )
1001
 
    {
1002
 
#ifdef QGISDEBUG
1003
 
      std::cout << "Ensuring hist is populated for this layer" << std::endl;
1004
 
#endif
1005
 
      rasterLayer->populateHistogram(myIteratorInt,BINCOUNT,myIgnoreOutOfRangeFlag,myThoroughBandScanFlag); 
1006
 
 
1007
 
#ifdef QGISDEBUG
1008
 
      std::cout << "...done..." << myRasterBandStats.histogramVector->size() << " bins filled" << std::endl;
1009
 
#endif
1010
 
      for (int myBin = 0; myBin <BINCOUNT; myBin++)
1011
 
      {
1012
 
        int myBinValue = myRasterBandStats.histogramVector->at(myBin);
1013
 
        if (myBinValue > 0 && myBin > myLastBinWithData)
1014
 
        {
1015
 
          myLastBinWithData = myBin;
1016
 
        }
1017
 
#ifdef QGISDEBUG
1018
 
        std::cout << "Testing if " << myBinValue << " is less than " << myYAxisMin  << "or greater then " <<myYAxisMax  <<  std::endl;
1019
 
#endif
1020
 
        if ( myBin==0)
1021
 
        {
1022
 
          myYAxisMin = myBinValue;
1023
 
          myYAxisMax = myBinValue;
1024
 
        }
1025
 
 
1026
 
        if (myBinValue  > myYAxisMax)
1027
 
        {
1028
 
          myYAxisMax = myBinValue;
1029
 
        }
1030
 
        if ( myBinValue < myYAxisMin)
1031
 
        {
1032
 
          myYAxisMin = myBinValue;
1033
 
        }
1034
 
      }
1035
 
    }
1036
 
  }
1037
 
#ifdef QGISDEBUG
1038
 
  std::cout << "max " << myYAxisMax << std::endl;
1039
 
  std::cout << "min " << myYAxisMin << std::endl;
1040
 
#endif
1041
 
 
1042
 
 
1043
 
  //create the image onto which graph and axes will be drawn
1044
 
  int myImageWidth = pixHistogram->width();
1045
 
  int myImageHeight =  pixHistogram->height();
1046
 
  QPixmap myPixmap(myImageWidth,myImageHeight);
1047
 
  myPixmap.fill(Qt::white);
1048
 
  QPainter myPainter(&myPixmap, this);
1049
 
  //determine labels sizes and draw them
1050
 
  QFont myQFont("arial", 8, QFont::Normal);
1051
 
  QFontMetrics myFontMetrics( myQFont );
1052
 
  myPainter.setFont(myQFont);
1053
 
  myPainter.setPen(Qt::black);
1054
 
  QString myYMaxLabel= QString::number(static_cast < unsigned int >(myYAxisMax));
1055
 
  QString myXMinLabel= QString::number(myXAxisMin);
1056
 
  QString myXMaxLabel= QString::number(myXAxisMax);
1057
 
  //calculate the gutters
1058
 
  int myYGutterWidth=0;
1059
 
  if (myFontMetrics.width(myXMinLabel) < myFontMetrics.width(myYMaxLabel))
1060
 
  {
1061
 
    myYGutterWidth = myFontMetrics.width(myYMaxLabel )+2; //add 2 so we can have 1 pix whitespace either side of label
1062
 
  }
1063
 
  else
1064
 
  {
1065
 
    myYGutterWidth = myFontMetrics.width(myXMinLabel )+2; //add 2 so we can have 1 pix whitespace either side of label
1066
 
  }
1067
 
  int myXGutterHeight = myFontMetrics.height()+2;
1068
 
  int myXGutterWidth = myFontMetrics.width(myXMaxLabel)+1;//1 pix whtispace from right edge of image
1069
 
 
1070
 
  //
1071
 
  // Now calculate the graph drawable area after the axis labels have been taken
1072
 
  // into account
1073
 
  //
1074
 
  int myGraphImageWidth =myImageWidth-myYGutterWidth; 
1075
 
  int myGraphImageHeight = myImageHeight-myXGutterHeight; 
1076
 
 
1077
 
  //find out how wide to draw bars when in bar chart mode
1078
 
  int myBarWidth = static_cast<int>((((double)myGraphImageWidth)/((double)BINCOUNT)));
1079
 
 
1080
 
 
1081
 
  //
1082
 
  //now draw actual graphs
1083
 
  //
1084
 
  if (rasterLayer->getRasterLayerType()
1085
 
          == QgsRasterLayer::PALETTE) //paletted layers have hard coded color entries
1086
 
  {
1087
 
    QPointArray myPointArray(myLastBinWithData);
1088
 
    QgsColorTable *myColorTable=rasterLayer->colorTable(1);
1089
 
#ifdef QGISDEBUG
1090
 
    std::cout << "Making paletted image histogram....computing band stats" << std::endl;
1091
 
    std::cout << "myLastBinWithData = " << myLastBinWithData << std::endl;
1092
 
#endif
1093
 
 
1094
 
    RasterBandStats myRasterBandStats = rasterLayer->getRasterBandStats(1);
1095
 
    for (int myBin = 0; myBin < myLastBinWithData; myBin++)
1096
 
    {
1097
 
      double myBinValue = myRasterBandStats.histogramVector->at(myBin);
1098
 
#ifdef QGISDEBUG
1099
 
      std::cout << "myBin = " << myBin << " myBinValue = " << myBinValue << std::endl;
1100
 
#endif
1101
 
      //NOTE: Int division is 0 if the numerator is smaller than the denominator.
1102
 
      //hence the casts
1103
 
      int myX = static_cast<int>((((double)myGraphImageWidth)/((double)myLastBinWithData))*myBin);
1104
 
      //height varies according to freq. and scaled to greatet value in all layers
1105
 
      int myY=0;
1106
 
      if (myYAxisMax!=0)
1107
 
      {  
1108
 
        myY=static_cast<int>(((double)myBinValue/(double)myYAxisMax)*myGraphImageHeight);
1109
 
      }
1110
 
      
1111
 
      //see wehter to draw something each loop or to save up drawing for after iteration
1112
 
      if (myGraphType==BAR_CHART)
1113
 
      {
1114
 
        //determin which color to draw the bar
1115
 
        int c1, c2, c3;
1116
 
        // Take middle of the interval for color
1117
 
        // TODO: this is not precise
1118
 
        double myInterval = (myXAxisMax - myXAxisMin) / myLastBinWithData;
1119
 
        double myMiddle = myXAxisMin + myBin * myInterval + myInterval/2;
1120
 
 
1121
 
#ifdef QGISDEBUG
1122
 
        std::cout << "myMiddle = " << myMiddle << std::endl;
1123
 
#endif
1124
 
        
1125
 
        bool found = myColorTable->color ( myMiddle, &c1, &c2, &c3 );
1126
 
        if ( !found ) {
1127
 
            std::cout << "Color not found" << std::endl;
1128
 
            c1 = c2 = c3 = 180; // grey
1129
 
        }
1130
 
          
1131
 
#ifdef QGISDEBUG
1132
 
        std::cout << "c1 = " << c1 << " c2 = " << c2 << " c3 = " << c3 << std::endl;
1133
 
#endif
1134
 
          
1135
 
        //draw the bar
1136
 
        //QBrush myBrush(QColor(c1,c2,c3));
1137
 
        myPainter.setBrush(QColor(c1,c2,c3));
1138
 
        myPainter.setPen(QColor(c1,c2,c3));
1139
 
#ifdef QGISDEBUG
1140
 
        std::cout << "myX = " << myX << " myY = " << myY << std::endl;
1141
 
        std::cout << "rect: " << myX+myYGutterWidth << ", " << myImageHeight-(myY+myXGutterHeight)
1142
 
                  << ", " << myBarWidth << ", " << myY << std::endl;
1143
 
#endif
1144
 
        myPainter.drawRect(myX+myYGutterWidth,myImageHeight-(myY+myXGutterHeight),myBarWidth,myY);
1145
 
      }
1146
 
      //store this point in our line too
1147
 
      myY = myGraphImageHeight - myY;
1148
 
      myPointArray.setPoint(myBin, myX+myYGutterWidth, myY-myXGutterHeight);
1149
 
    }
1150
 
    //draw a line on the graph along the bar peaks; 
1151
 
    if (myGraphType==LINE_CHART)
1152
 
    {
1153
 
      myPainter.setPen( Qt::black );
1154
 
      myPainter.drawPolyline(myPointArray);
1155
 
    }
1156
 
  }
1157
 
  else
1158
 
  {
1159
 
 
1160
 
    for (int myIteratorInt = 1;
1161
 
            myIteratorInt <= myBandCountInt;
1162
 
            ++myIteratorInt)
1163
 
    {
1164
 
      RasterBandStats myRasterBandStats = rasterLayer->getRasterBandStats(myIteratorInt);
1165
 
      QListBoxItem *myItem = lstHistogramLabels->item( myIteratorInt-1 );
1166
 
      if ( myItem->isSelected() )
1167
 
      {
1168
 
 
1169
 
        QPointArray myPointArray(myLastBinWithData);
1170
 
        for (int myBin = 0; myBin <myLastBinWithData; myBin++)
1171
 
        {
1172
 
          double myBinValue = myRasterBandStats.histogramVector->at(myBin);
1173
 
          //NOTE: Int division is 0 if the numerator is smaller than the denominator.
1174
 
          //hence the casts
1175
 
          int myX = static_cast<int>((((double)myGraphImageWidth)/((double)myLastBinWithData))*myBin);
1176
 
          //height varies according to freq. and scaled to greatet value in all layers
1177
 
          int myY = static_cast<int>(((double)myBinValue/(double)myYAxisMax)*myGraphImageHeight);
1178
 
          //adjust for image origin being top left
1179
 
#ifdef QGISDEBUG
1180
 
          std::cout << "-------------" << std::endl;
1181
 
          std::cout << "int myY = (myBinValue/myCellCount)*myGraphImageHeight" << std::endl;
1182
 
          std::cout << "int myY = (" << myBinValue << "/" << myCellCount << ")*" << myGraphImageHeight << std::endl;
1183
 
          std::cout << "Band " << myIteratorInt << ", bin " << myBin << ", Hist Value : " << myBinValue << ", Scaled Value : " << myY << std::endl;
1184
 
          std::cout << "myY = myGraphImageHeight - myY" << std::endl;
1185
 
          std::cout << "myY = " << myGraphImageHeight << "-" << myY << std::endl;
1186
 
#endif
1187
 
          if (myGraphType==BAR_CHART)
1188
 
          {
1189
 
            //draw the bar
1190
 
            if (myBandCountInt==1) //draw single band images with black
1191
 
            {
1192
 
              myPainter.setPen( Qt::black );
1193
 
            }
1194
 
            else if (myIteratorInt==1)
1195
 
            {
1196
 
              myPainter.setPen( Qt::red );
1197
 
            }
1198
 
            else if (myIteratorInt==2)
1199
 
            {
1200
 
              myPainter.setPen( Qt::green );
1201
 
            }
1202
 
            else if (myIteratorInt==3)
1203
 
            {
1204
 
              myPainter.setPen( Qt::blue );
1205
 
            }
1206
 
            else if (myIteratorInt==4)
1207
 
            {
1208
 
              myPainter.setPen( Qt::magenta );
1209
 
            }
1210
 
            else if (myIteratorInt==5)
1211
 
            {
1212
 
              myPainter.setPen( Qt::darkRed );
1213
 
            }
1214
 
            else if (myIteratorInt==6)
1215
 
            {
1216
 
              myPainter.setPen( Qt::darkGreen );
1217
 
            }
1218
 
            else if (myIteratorInt==7)
1219
 
            {
1220
 
              myPainter.setPen( Qt::darkBlue );
1221
 
            }
1222
 
            else
1223
 
            {
1224
 
              myPainter.setPen( Qt::gray );
1225
 
            }
1226
 
#ifdef QGISDEBUG
1227
 
            //  std::cout << "myPainter.fillRect(QRect(" << myX << "," << myY << "," << myBarWidth << "," <<myY << ") , myBrush );" << std::endl;
1228
 
#endif
1229
 
            myPainter.drawRect(myX+myYGutterWidth,myImageHeight-(myY+myXGutterHeight),myBarWidth,myY);
1230
 
          }
1231
 
          else //line graph
1232
 
          {
1233
 
            myY = myGraphImageHeight - myY;
1234
 
            myPointArray.setPoint(myBin, myX+myYGutterWidth, myY);
1235
 
          }
1236
 
        }
1237
 
        if (myGraphType==LINE_CHART)
1238
 
        {
1239
 
          if (myBandCountInt==1) //draw single band images with black
1240
 
          {
1241
 
            myPainter.setPen( Qt::black );
1242
 
          }
1243
 
          else if (myIteratorInt==1)
1244
 
          {
1245
 
            myPainter.setPen( Qt::red );
1246
 
          }
1247
 
          else if (myIteratorInt==2)
1248
 
          {
1249
 
            myPainter.setPen( Qt::green );
1250
 
          }
1251
 
          else if (myIteratorInt==3)
1252
 
          {
1253
 
            myPainter.setPen( Qt::blue );
1254
 
          }
1255
 
          else if (myIteratorInt==4)
1256
 
          {
1257
 
            myPainter.setPen( Qt::magenta );
1258
 
          }
1259
 
          else if (myIteratorInt==5)
1260
 
          {
1261
 
            myPainter.setPen( Qt::darkRed );
1262
 
          }
1263
 
          else if (myIteratorInt==6)
1264
 
          {
1265
 
            myPainter.setPen( Qt::darkGreen );
1266
 
          }
1267
 
          else if (myIteratorInt==7)
1268
 
          {
1269
 
            myPainter.setPen( Qt::darkBlue );
1270
 
          }
1271
 
          else
1272
 
          {
1273
 
            myPainter.setPen( Qt::gray );
1274
 
          }
1275
 
          myPainter.drawPolyline(myPointArray);
1276
 
        }
1277
 
      }
1278
 
    }
1279
 
  }
1280
 
 
1281
 
  //
1282
 
  // Now draw interval markers on the x axis
1283
 
  //
1284
 
  int myXDivisions = myGraphImageWidth/10;
1285
 
  myPainter.setPen( Qt::gray );
1286
 
  for (int i=0;i<myXDivisions;++i)
1287
 
  {
1288
 
    QPointArray myPointArray(4);
1289
 
    myPointArray.setPoint(0,(i*myXDivisions)+myYGutterWidth , myImageHeight-myXGutterHeight);
1290
 
    myPointArray.setPoint(1,(i*myXDivisions)+myYGutterWidth , myImageHeight-(myXGutterHeight-5));
1291
 
    myPointArray.setPoint(2,(i*myXDivisions)+myYGutterWidth , myImageHeight-myXGutterHeight);
1292
 
    myPointArray.setPoint(3,((i+1)*myXDivisions)+myYGutterWidth , myImageHeight-myXGutterHeight);
1293
 
    myPainter.drawPolyline(myPointArray);
1294
 
  }
1295
 
  //
1296
 
  // Now draw interval markers on the y axis
1297
 
  //
1298
 
  int myYDivisions = myGraphImageHeight/10;
1299
 
  myPainter.setPen( Qt::gray );
1300
 
  for (int i=myYDivisions;i>0;--i)
1301
 
  {
1302
 
 
1303
 
    QPointArray myPointArray(4);
1304
 
    int myYOrigin = myImageHeight-myXGutterHeight;
1305
 
    myPointArray.setPoint(0,myYGutterWidth,myYOrigin-(i*myYDivisions ));
1306
 
    myPointArray.setPoint(1,myYGutterWidth-5,myYOrigin-(i*myYDivisions ));
1307
 
    myPointArray.setPoint(2,myYGutterWidth,myYOrigin-(i*myYDivisions ));
1308
 
    myPointArray.setPoint(3,myYGutterWidth,myYOrigin-((i-1)*myYDivisions ));
1309
 
    myPainter.drawPolyline(myPointArray);
1310
 
  }
1311
 
 
1312
 
  //now draw the axis labels onto the graph
1313
 
  myPainter.drawText(1, 12, myYMaxLabel);
1314
 
  myPainter.drawText(1, myImageHeight-myXGutterHeight, QString::number(static_cast < unsigned int >(myYAxisMin)));
1315
 
  myPainter.drawText(myYGutterWidth,myImageHeight-1 , myXMinLabel);
1316
 
  myPainter.drawText( myImageWidth-myXGutterWidth,myImageHeight-1, myXMaxLabel );
1317
 
 
1318
 
  //
1319
 
  // Finish up
1320
 
  //
1321
 
  myPainter.end();
1322
 
  pixHistogram->setPixmap(myPixmap);
1323
 
}
1324
 
 
1325
 
void QgsRasterLayerProperties::pbnChangeSpatialRefSys_clicked()
1326
 
{
1327
 
    
1328
 
 
1329
 
    QgsLayerProjectionSelector * mySelector = new QgsLayerProjectionSelector(this);
1330
 
    long myDefaultSRS = rasterLayer->coordinateTransform()->sourceSRS().srsid();
1331
 
    if (myDefaultSRS==0)
1332
 
    {
1333
 
      myDefaultSRS=QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectSRSID",GEOSRS_ID);
1334
 
    }
1335
 
    mySelector->setSelectedSRSID(myDefaultSRS);
1336
 
    if(mySelector->exec())
1337
 
    {
1338
 
      rasterLayer->coordinateTransform()->sourceSRS().createFromSrsId(mySelector->getCurrentSRSID());
1339
 
      rasterLayer->coordinateTransform()->initialise();
1340
 
    }
1341
 
    else
1342
 
    {
1343
 
      QApplication::restoreOverrideCursor();
1344
 
    }
1345
 
    delete mySelector;
1346
 
    leSpatialRefSys->setText(rasterLayer->coordinateTransform()->sourceSRS().proj4String());
1347
 
}