histogramwidget.cpp

Go to the documentation of this file.
00001 //
00002 // C++ Implementation: histogramwidget
00003 //
00004 // Description: 
00005 //
00006 //
00007 // Author: Nicholas Phillips <Nicholas.G.Phillips@nasa.gov>, (C) 2008
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 #include <iostream>
00013 #include <QTimer>
00014 #include "histogramwidget.h"
00015 #include "healpixmap.h"
00016 
00017 using namespace std;
00018 
00019 /* ------------------------------------------------------------------------------------
00020 'HistogramWidget' constructor
00021 
00022         Set defaults and connections
00023 
00024 Written by Nicholas Phillips, UMCP, 6 August 2008.
00025 ------------------------------------------------------------------------------------ */
00026 HistogramWidget::HistogramWidget(QWidget *parent) : QWidget(parent), minz(0.0001)
00027 {
00028 
00029         setupUi(this);
00030 
00031         old_c = c = 0.5;
00032         old_z = z = 1;
00033 
00034         cztimer = new QTimer(this);
00035         connect(cztimer, SIGNAL(timeout()), this, SLOT(setNewCenterZoom()));
00036         
00037         lutimer = new QTimer(this);
00038         connect(lutimer, SIGNAL(timeout()), this, SLOT(setNewLowerUpper()));
00039 
00040         connect(this, SIGNAL(newCenterZoom(float,float)),histoView,SLOT(setCenterZoom(float,float)));
00041 
00042         setNewRange();
00043 }
00044 
00045 
00046 /* ------------------------------------------------------------------------------------
00047 'set' 
00048         Setup the histoView and sliders for the given data vector
00049 Arguments:
00050         x       The data to select a range for
00051 Returned:
00052         Nothing
00053 
00054 Written by Nicholas Phillips, UMCP, 6 August 2008.
00055 ------------------------------------------------------------------------------------ */
00056 void HistogramWidget::set(vector<float> &x)
00057 {       
00058         old_c = c = 0.5;
00059         old_z = z = 1;
00060         
00061         histogram.setup(x);
00062 
00063         minr = histogram.min();
00064         maxr = histogram.max();
00065         histogram.build(x, minr, maxr);
00066         histoView->set(&histogram);
00067         
00068         setComboBoxes();
00069         setNewRange();
00070 
00071         return;
00072 }
00073         
00074 //void HistogramWidget::set(const HealpixMap *map, Field fld)
00075 void HistogramWidget::set(Skymap *map, Field fld)
00076 {
00077         vector<float> x(map->n());
00078         switch( fld ) {
00079                 case I: for(uint i = 0; i < map->n(); i++) x[i]= (*map)[i].T();
00080                         break;
00081                 case Q: for(uint i = 0; i < map->n(); i++) x[i]= (*map)[i].Q();
00082                         break;
00083                 case U: for(uint i = 0; i < map->n(); i++) x[i]= (*map)[i].U();
00084                         break;
00085                 case P: for(uint i = 0; i < map->n(); i++) x[i]= (*map)[i].Pmag();
00086                         break;
00087                 case Nobs: for(uint i = 0; i < map->n(); i++) x[i]= (*map)[i].Nobs();
00088                         break;
00089         }
00090         
00091         histogram.setup(x);
00092 
00093         switch( fld ) {
00094                 case I:
00095                 case Q:
00096                 case U:
00097                         minr = -histogram.amax();
00098                         maxr = +histogram.amax();
00099                         break;
00100                 case P:
00101                 case Nobs:
00102                         minr = 0;
00103                         maxr = histogram.max();
00104                         break;
00105         }
00106         
00107         histogram.build(x, minr, maxr);
00108         histoView->set(&histogram);
00109         
00110         setComboBoxes();
00111         setNewRange();
00112 
00113         return;
00114 }
00115 void HistogramWidget::set(ColorTable *ct)
00116 {
00117         histoView->set(ct);
00118 }
00119 /* ------------------------------------------------------------------------------------
00120 'setComboBoxes' 
00121         Set the fields for the Preset combo boxes based on the
00122         current min and max ranges
00123         x       The data to select a range for
00124 Returned:
00125         Nothing
00126 
00127 Written by Nicholas Phillips, UMCP, 6 August 2008.
00128 ------------------------------------------------------------------------------------ */
00129 void HistogramWidget::setComboBoxes()
00130 {       
00131         centerComboBox->clear();
00132         centerComboBox->addItem("<slider>");
00133         if( (minr < 0) && (maxr > 0) )
00134                 centerComboBox->addItem("Zero");
00135         cmean = (histogram.mean()-minr)/(maxr-minr);
00136         centerComboBox->addItem("Mean");
00137 
00138         zoomComboBox->clear();
00139         zoomComboBox->addItem("<slider>");
00140         zstddev = histogram.stddev()/(maxr-minr);
00141         if( zstddev < 1 )
00142                 zoomComboBox->addItem("1x Std Dev");
00143         if( 2*zstddev < 1 )
00144                 zoomComboBox->addItem("2x Std Dev");
00145         if( 3*zstddev < 1 )
00146                 zoomComboBox->addItem("3x Std Dev");
00147         zoomComboBox->addItem("Full");
00148 
00149         return;
00150 }
00151 
00152 //=====================================================================================
00153 /* ------------------------------------------------------------------------------------
00154 'on_zoomSlider_sliderPressed' 
00155 'on_centerSlider_sliderPressed'
00156 
00157         When either zoom or center sliders are activated, start the associated timer
00158         so we sample them.
00159 
00160         Qt auto-connect has been assumed
00161 Arguments:
00162         None
00163 Returned:
00164         Nothing
00165 
00166 Written by Nicholas Phillips, UMCP, 6 August 2008.
00167 ------------------------------------------------------------------------------------ */
00168 void HistogramWidget::on_zoomSlider_sliderPressed() {
00169         cztimer->start(100);
00170 }
00171 void HistogramWidget::on_centerSlider_sliderPressed() {
00172         cztimer->start(100);
00173 }
00174 /* ------------------------------------------------------------------------------------
00175 'on_zoomSlider_sliderReleased'
00176 'on_centerSlider_sliderReleased'
00177         When either slider is released, stop sampling them and return them to their
00178         midpoints.
00179         
00180         Qt auto-connect has been assumed
00181 Arguments:
00182         None.
00183 Returned:
00184         Nothing
00185 
00186 Written by Nicholas Phillips, UMCP, 6 August 2008.
00187 ------------------------------------------------------------------------------------ */
00188 void HistogramWidget::on_zoomSlider_sliderReleased() {
00189         cztimer->stop();
00190         zoomSlider->setSliderPosition(50);
00191 }
00192 void HistogramWidget::on_centerSlider_sliderReleased() {
00193         cztimer->stop();
00194         centerSlider->setSliderPosition(50);
00195 }
00196 /* ------------------------------------------------------------------------------------
00197 'setNewCenterZoom'
00198         Read the zoom/center sliders and update 'c' and 'z'.
00199         First convert the posistion into a delta. Change c and z
00200         accordingly and check the range. Update the displays.
00201         
00202 Arguments:
00203         None.
00204 Returned:
00205         Nothing.
00206 
00207 Written by Nicholas Phillips, UMCP, 6 August 2008.
00208 ------------------------------------------------------------------------------------ */
00209 void HistogramWidget::setNewCenterZoom()
00210 {
00211         float dz=(zoomSlider->value()-50.)/100;
00212         dz = dz*dz*dz;
00213         z -= dz;
00214         float dc=(centerSlider->value()-50.)/100;
00215         dc = dc*dc*dc;
00216         c += dc;
00217 
00218         if( z < minz) z = minz;
00219         if( z > 1 ) z = 1;
00220 
00221         if( c+z/2 > 1 ) c =  1-z/2;
00222         if( c-z/2 < 0 ) c = z/2;
00223 
00224         setNewRange();
00225 
00226         return;
00227 }
00228 //=====================================================================================
00229 /* ------------------------------------------------------------------------------------
00230 'on_lowerSlider_sliderPressed' 
00231 'on_upperSlider_sliderPressed'
00232 
00233         When either lower or upper sliders are activated, start the associated timer
00234         so we sample them.
00235 
00236         Qt auto-connect has been assumed
00237 Arguments:
00238         None
00239 Returned:
00240         Nothing
00241 
00242 Written by Nicholas Phillips, UMCP, 6 August 2008.
00243 ------------------------------------------------------------------------------------ */
00244 void HistogramWidget::on_lowerSlider_sliderPressed() {
00245         lutimer->start(100);
00246 }
00247 void HistogramWidget::on_upperSlider_sliderPressed() {
00248         lutimer->start(100);
00249 }
00250 /* ------------------------------------------------------------------------------------
00251 'on_lowerSlider_sliderReleased'
00252 'on_upperSlider_sliderReleased'
00253         When either slider is released, stop sampling them and return them to their
00254         midpoints.
00255         
00256         Qt auto-connect has been assumed
00257 Arguments:
00258         None.
00259 Returned:
00260         Nothing
00261 
00262 Written by Nicholas Phillips, UMCP, 6 August 2008.
00263 ------------------------------------------------------------------------------------ */
00264 void HistogramWidget::on_lowerSlider_sliderReleased() {
00265         lutimer->stop();
00266         lowerSlider->setSliderPosition(50);
00267 }
00268 void HistogramWidget::on_upperSlider_sliderReleased() {
00269         lutimer->stop();
00270         upperSlider->setSliderPosition(50);
00271 }
00272 /* ------------------------------------------------------------------------------------
00273 'setNewLowerUpper'
00274         Read the lower/upper sliders and update 'c' and 'z'.
00275         First convert the posistion into a delta. Change c and z
00276         accordingly and check the range. Update the displays.
00277         
00278 Arguments:
00279         None.
00280 Returned:
00281         Nothing.
00282 
00283 Written by Nicholas Phillips, UMCP, 6 August 2008.
00284 ------------------------------------------------------------------------------------ */
00285 void HistogramWidget::setNewLowerUpper()
00286 {
00287         float dl=(lowerSlider->value()-50.)/100;
00288         dl = dl*dl*dl;
00289 
00290         float du=(upperSlider->value()-50.)/100;
00291         du = du*du*du;
00292 
00293         float l = c-z/2;
00294         float u = c+z/2;
00295 
00296         l += dl;
00297         u += du;
00298 
00299         if( l < 0 ) l = 0;
00300         if( u > 1 ) u = 1;
00301 
00302         if( l >= u ) return;
00303 
00304         c = (u+l)/2;
00305         z = u-l;
00306 
00307         setNewRange();
00308 
00309         return;
00310 }
00311 //=====================================================================================
00312 
00313 /* ------------------------------------------------------------------------------------
00314 ''on_zoomComboBox_activated' 
00315         Change c and z based on the preset selected' 
00316         
00317 Arguments:
00318         None
00319 Returned:
00320         Nothing
00321 
00322 Written by Nicholas Phillips, UMCP, 6 August 2008.
00323 ------------------------------------------------------------------------------------ */
00324 void HistogramWidget::on_zoomComboBox_activated(int /*index*/)
00325 {
00326         bool changed=false;
00327         if( zoomComboBox->currentText() == "1x Std Dev" ) {
00328                 old_z = z = zstddev;
00329                 changed = true;
00330         }
00331         else if( zoomComboBox->currentText() == "2x Std Dev" ) {
00332                 old_z = z = 2*zstddev;
00333                 changed = true;
00334         }
00335         else if( zoomComboBox->currentText() == "3x Std Dev" ) {
00336                 old_z = z = 3*zstddev;
00337                 changed = true;
00338         }
00339         else if( zoomComboBox->currentText() == "Full" ) {
00340                 old_z = z = 1;
00341                 old_c = c = 0.5;
00342                 changed = true;
00343         }
00344         if( changed ) {
00345                 if( c+z/2 > 1 ) c =  1-z/2;
00346                 if( c-z/2 < 0 ) c = z/2;
00347                 setNewRange();
00348         }
00349         return;
00350 }
00351 //-------------------------------------------------------------
00352 /* ------------------------------------------------------------------------------------
00353 'on_centerComboBox_activated' 
00354         Change c and z based on the preset selected
00355 
00356 Arguments:
00357         None
00358 Returned:
00359         Nothing
00360 
00361 Written by Nicholas Phillips, UMCP, 6 August 2008.
00362 ------------------------------------------------------------------------------------ */
00363 void HistogramWidget::on_centerComboBox_activated(int /*index*/)
00364 {
00365         bool changed=false;
00366         if( centerComboBox->currentText() == "Zero" ) {
00367                 old_c = c = -minr/(maxr-minr);
00368                 changed = true;
00369         }
00370         else if( centerComboBox->currentText() == "Mean" ) {
00371                 old_c = c = cmean;
00372                 changed = true;
00373         }
00374         if( changed ) {
00375                 if( c + z/2 > 1 ) z = 2-2*c;
00376                 if( c - z/2 < 0 ) z = 2*c;
00377                 setNewRange();
00378         }
00379         return;
00380 }
00381 /* ------------------------------------------------------------------------------------
00382 'setNewRange'
00383         Use the current 'c' and 'z' values to update the dispayed text. Reset any
00384         Comboboxes that need it. emit the new values so the associated histoView
00385         can update.
00386         
00387 Arguments:
00388         None
00389 Returned:
00390         Nothing
00391 
00392 Written by Nicholas Phillips, UMCP, 6 August 2008.
00393 ------------------------------------------------------------------------------------ */
00394 void HistogramWidget::setNewRange()
00395 {
00396         lower = (maxr-minr)*(c - z/2)+minr;
00397         upper = (maxr-minr)*(c + z/2)+minr;
00398         float cv = (maxr-minr)*c+minr;
00399         if( cv < 1e-6*(maxr-minr) ) cv = 0;
00400  
00401         lowLabel->setText(QString("%1").arg(lower,9,'g',3));
00402         centerLabel->setText(QString("%1").arg(cv,9,'g',3));
00403         hiLabel->setText(QString("%1").arg(upper,9,'g',3));
00404 
00405         if( centerComboBox->currentIndex() != 0 && (c != old_c ) )
00406                 centerComboBox->setCurrentIndex(0);
00407         if( zoomComboBox->currentIndex() != 0 && (z != old_z ) )
00408                 zoomComboBox->setCurrentIndex(0);
00409 
00410         old_c = c;
00411         old_z = z;
00412 
00413         emit newCenterZoom(c, z);
00414         emit newRange(lower, upper);
00415 }

Generated on Fri Feb 6 15:32:42 2009 for Skyviewer by  doxygen 1.4.7