00001 // 00002 // C++ Implementation: histoview 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 <math.h> 00013 #include <iostream> 00014 #include <iomanip> 00015 #include <QPainter> 00016 #include "histogram.h" 00017 #include "histoview.h" 00018 #include "colortable.h" 00019 00020 using namespace std; 00021 00022 /* ------------------------------------------------------------------------------------ 00023 'HistoView' constructor. 00024 00025 Set the default size and background. Mark histogram as not set 00026 00027 Arguments: 00028 parent: who wanted me to exist. You take responsibility for killing me. 00029 00030 Written by Nicholas Phillips, UMCP, 6 August 2008. 00031 ------------------------------------------------------------------------------------ */ 00032 HistoView::HistoView(QWidget *parent) : QWidget(parent) 00033 { 00034 histo = 0; // So no segfaults while we wait for some data to paint 00035 ct = 0; 00036 00037 h = 128; 00038 w = 256; 00039 w1= 64; 00040 /* 00041 h = 320; 00042 w = 480; 00043 w1= 80; 00044 */ 00045 setBackgroundRole(QPalette::Base); 00046 setAutoFillBackground(true); 00047 } 00048 00049 00050 /* ------------------------------------------------------------------------------------ 00051 'set' 00052 What histogram to display. It is assumed the passed Histogram exists as 00053 long as QPaintEvents may arrive. 00054 00055 Arguments: 00056 h Pointer to the histogram to use 00057 Returned: 00058 00059 Written by Nicholas Phillips, UMCP, 6 August 2008. 00060 ------------------------------------------------------------------------------------ */ 00061 void HistoView::set(const ColorTable *newct) 00062 { 00063 ct = newct; 00064 update(); 00065 return; 00066 } 00067 /* ------------------------------------------------------------------------------------ 00068 'set' 00069 What histogram to display. It is assumed the passed Histogram exists as 00070 long as QPaintEvents may arrive. 00071 00072 Arguments: 00073 h Pointer to the histogram to use 00074 Returned: 00075 00076 Written by Nicholas Phillips, UMCP, 6 August 2008. 00077 ------------------------------------------------------------------------------------ */ 00078 void HistoView::set(const Histogram *h) 00079 { 00080 histo = h; 00081 setCenterZoom(0.5,1); 00082 return; 00083 } 00084 /* ------------------------------------------------------------------------------------ 00085 'setCenterZoom' 00086 The new values are assumed to be within range. 00087 The hilighted region is taken to span [c-z/2,c+z/2] 00088 00089 Arguments: 00090 c_ New Center value 00091 z_ New Zoom value 00092 Returned: 00093 Nothing 00094 Written by Nicholas Phillips, UMCP, 6 August 2008. 00095 ------------------------------------------------------------------------------------ */ 00096 void HistoView::setCenterZoom( float c_, float z_) 00097 { 00098 c = c_; 00099 z = z_; 00100 update(); 00101 return; 00102 } 00103 00104 /* ------------------------------------------------------------------------------------ 00105 'paintEvent' 00106 Paint the actual histogram, hilighting the selected range as set by the 00107 most recent call of setCenterZoom. 00108 00109 Assumes the histogram returns a value between 0 and 1 00110 00111 Arguments: 00112 <unused QPaintEvent> 00113 Returned: 00114 Nothing. 00115 00116 Written by Nicholas Phillips, UMCP, 6 August 2008. 00117 ------------------------------------------------------------------------------------ */ 00118 void HistoView::paintEvent(QPaintEvent *) 00119 { 00120 if( ! histo ) return; 00121 00122 float a = c - (w1/(w-1.) + 0.5)*z; 00123 float b = z/(w-1.); 00124 00125 QPainter painter(this); 00126 painter.setPen(QPen(QColor("black"))); 00127 painter.drawLine(w1-1, 0, w1-1, h); 00128 painter.drawLine(w+w1-1, 0, w+w1-1, h); 00129 00130 QColor color; 00131 float x0,x1,r,cv; 00132 for(int p = 0; p < w+2*w1-1; p ++) { 00133 //cout << setw(5) << p; 00134 x0 = a + b*p; 00135 x1 = a + b*(p+1); 00136 00137 if( (x0 <= 0) || (x1 >= 1) ) 00138 r=0; 00139 else 00140 r = (*histo)(x0,x1); 00141 00142 //cout << setw(3) << ((p < w1) || (p >= w+w1)); 00143 00144 if( (p < w1) || (p >= w+w1) ) 00145 painter.setPen(QPen(QColor("black"))); 00146 else { 00147 if( ct != NULL ) { 00148 cv = ((float)(p-w1))/(w-1); 00149 painter.setPen(QPen((*ct)(cv))); 00150 } 00151 else { 00152 color.setHsvF(((float)(p-w1))/(1.5*w),1,1); 00153 //cout << setw(10) << ((float)(p-w1))/(w-1); 00154 painter.setPen(QPen(color)); 00155 } 00156 } 00157 //cout << endl; 00158 painter.drawLine(p,(int)(h*(1-r)),p,h); 00159 } 00160 return; 00161 }