00001
00002
00003
00004
00005
00006
00007 #include <QGLFormat>
00008 #include <QGLFramebufferObject>
00009 #include <QMoveEvent>
00010
00011 #include "CGLHistogram.h"
00012 #include "CVolumeStatistics.h"
00013 #include "CTransferWdt.h"
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 CGLHistogram::CGLHistogram(QWidget * parent = 0)
00025 :CGLSurface(QGLFormat(QGL::DoubleBuffer | QGL::Rgba),parent)
00026 {
00027 EnableThread(false);
00028 fboObject1 = NULL;
00029 bgColor[0] = 1.0;
00030 bgColor[1] = bgColor[2] = 0.95;
00031 bgColor[3] = 1.0;
00032 ResetView();
00033 Update();
00034 }
00035
00036
00037 CGLHistogram::~CGLHistogram()
00038 {
00039 SAFE_DELETE(fboObject1);
00040 }
00041
00042 void CGLHistogram::DisplayData(float * data,int dataSize)
00043 {
00044 lockGLContext();
00045
00046
00047 if(!fboObject1){
00048 fboObject1 = new QGLFramebufferObject(dataSize,dataSize);
00049 }else{
00050 if(dataSize != fboObject1->size().width()){
00051 delete fboObject1;
00052 fboObject1 = new QGLFramebufferObject(dataSize,dataSize);
00053 }
00054 }
00055
00056 if(fboObject1->bind())
00057 {
00058 glPushAttrib(GL_VIEWPORT_BIT);
00059 glViewport(0,0,dataSize,dataSize);
00060
00061 glClearColor(bgColor[0],bgColor[1],bgColor[2],bgColor[3]);
00062 glClear(GL_COLOR_BUFFER_BIT);
00063
00064 glMatrixMode(GL_PROJECTION);
00065 glLoadIdentity();
00066 glOrtho(0,dataSize,0,dataSize,1,-1);
00067
00068 glMatrixMode(GL_MODELVIEW);
00069 glLoadIdentity();
00070 glShadeModel(GL_FLAT);
00071
00072 glBegin(GL_LINE_STRIP);
00073 for(int i = 0; i < dataSize ; i++)
00074 {
00075 if(data[i] == 0){
00076 glColor4f(0.95,0.95,0.95,1.0);
00077 glVertex3f(i,0,0);
00078 glVertex3f(i,dataSize,0);
00079 }else{
00080 glColor4f(0.5,0.5,0.7,1.0);
00081 glVertex3f(i,0,0);
00082 glVertex3f(i,data[i]*dataSize,0);
00083 glColor4f(0.98,0.98,0.98,1.0);
00084 glVertex3f(i,dataSize,0);
00085 }
00086 }
00087 glEnd();
00088
00089 glPopAttrib();
00090
00091 glBindTexture(GL_TEXTURE_2D,fboObject1->texture());
00092 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
00093
00094 fboObject1->release();
00095
00096
00097 onDraw();
00098 }
00099
00100 unlockGLContext();
00101 Update();
00102 }
00103
00104
00105
00106
00107
00108 void CGLHistogram::onDraw()
00109 {
00110 glClearColor(bgColor[0],bgColor[1],bgColor[2],bgColor[3]);
00111 glClear(GL_COLOR_BUFFER_BIT);
00112
00113 glMatrixMode(GL_PROJECTION);
00114 glLoadIdentity();
00115 glOrtho(-1,1,-1,1,1,-1);
00116
00117 glMatrixMode(GL_MODELVIEW);
00118 glLoadIdentity();
00119 glTranslatef(panX,0,0);
00120 glTranslatef(-1,-1,0);
00121 glScalef(zoomX,zoomY,1);
00122 glTranslatef(1,1,0);
00123
00124 if(fboObject1)
00125 {
00126 if(glIsTexture(fboObject1->texture()))
00127 {
00128 glEnable(GL_TEXTURE_2D);
00129 glBindTexture(GL_TEXTURE_2D,fboObject1->texture());
00130
00131 glBegin(GL_TRIANGLE_FAN);
00132 glTexCoord2d(0,0);
00133 glVertex3f(-1,-1,0);
00134 glTexCoord2d(1,0);
00135 glVertex3f(1,-1,0);
00136 glTexCoord2d(1,1);
00137 glVertex3f(1,1,0);
00138 glTexCoord2d(0,1);
00139 glVertex3f(-1,1,0);
00140 glEnd();
00141
00142 glDisable(GL_TEXTURE_2D);
00143 }
00144 }
00145
00146
00147 this->swapBuffers();
00148 }
00149
00150 void CGLHistogram::mouseMoveEvent(QMouseEvent *event)
00151 {
00152 static bool done = false;
00153
00154 dx = event->pos().x() - oldPosX;
00155 dy = event->pos().y() - oldPosY;
00156
00157 oldPosX = event->pos().x();
00158 oldPosY = event->pos().y();
00159
00160 if(doZoom){
00161 if(zoomX >= 1){
00162
00163 zoomX += dx/200.0;
00164
00165 if(panX < 2 - (2*zoomX))
00166 {
00167 panX -= (dx*2*zoomX)/(200.0);
00168 if(panX > 0){
00169 panX = 0;
00170 }
00171 }
00172
00173 if(zoomX < 1){
00174 zoomX = 1;
00175 }
00176 }
00177
00178 if(zoomY >= 0.1){
00179 zoomY -= dy/200.0;
00180 if(zoomY < 0.1){
00181 zoomY = 0.1;
00182 }
00183 }
00184 }
00185
00186 if(doPan){
00187 if(panX + (dx*zoomX)/(200.0) >= 2 - (2*zoomX)){
00188 panX += (dx*zoomX)/(200.0);
00189 if(panX > 0){
00190 panX = 0;
00191 }
00192
00193 }else{
00194 panX = 2 - (2*zoomX);
00195 }
00196 panY -= (dy*zoomX)/(200.0);
00197 }
00198
00199 Update();
00200 event->accept();
00201 }
00202
00203 void CGLHistogram::mousePressEvent(QMouseEvent *event)
00204 {
00205 oldPosX = event->pos().x();
00206 oldPosY = event->pos().y();
00207
00208 switch(event->button()){
00209 case Qt::LeftButton :
00210 doZoom = true;
00211 break;
00212 case Qt::MidButton :
00213 doPan = true;
00214 break;
00215 case Qt::RightButton :
00216 ResetView();
00217 break;
00218 }
00219
00220 event->accept();
00221 Update();
00222 }
00223
00224 void CGLHistogram::mouseReleaseEvent(QMouseEvent *event)
00225 {
00226 doZoom = false;
00227 doPan = false;
00228 event->accept();
00229 Update();
00230 }
00231
00232
00233
00234
00235
00236 void CGLHistogram::ResetView()
00237 {
00238 zoomX = zoomY = 1.0;
00239 panX = panY = 0;
00240 doZoom = doPan = false;
00241 Update();
00242 }
00243
00244 void CGLHistogram::ResetViewH()
00245 {
00246 zoomX = 1.0;
00247 panX = 1.0;
00248 doZoom = doPan = false;
00249 Update();
00250 }
00251
00252 void CGLHistogram::ResetViewV()
00253 {
00254 zoomY = 1.0;
00255 doZoom = doPan = false;
00256 Update();
00257 }