src/MainWindow.cpp

Go to the documentation of this file.
00001 #include "compiler_options.h"
00002 #include "MainWindow.h"
00003 #include "common.h"
00004 #include <QtGui/QPainter>
00005 #include <QtGui/QGradient>
00006 #include <QtGui/QRadioButton>
00007 
00008 
00009 MainWindow::MainWindow( QWidget* parent /* = 0 */, Qt::WindowFlags flags /* = 0 */) : QMainWindow(parent, flags)
00010 {
00011         setupUi(this);
00012 
00013         isFileOpen = false;   // used to switch gui-elements on/off
00014 
00015         glWidgets[0] = 0; // Widget to share
00016 
00017         // add, layout and configure glWidgets
00018         for ( int i=0; i<NUMBER_OF_GL_WIDGETS; i++ )
00019         {
00020                 glWidgets[i] = new OpenGLWidget( 0, glWidgets[0] );
00021         }
00022 
00023   //m_histogram = new Histogram(this);
00024   //m_histogram->Clear();
00025   m_volume = new Volume();
00026 
00027   //m_renderer = new GradientRenderer(this);
00028   m_editor   = new GradientEditor(this, Qt::Tool);
00029   m_editor->setFixedSize(514,300);
00030 
00031   //m_editor->setRenderer(m_renderer);
00032   //m_editor->setHistogram(m_histogram);
00033 
00034         gridLayout->addWidget ( glWidgets[ X_AXIS ] );
00035         gridLayout1->addWidget( glWidgets[ Y_AXIS ] );
00036         gridLayout2->addWidget( glWidgets[ Z_AXIS ] );
00037         gridLayout3->addWidget( glWidgets[ OPENGL_3D_VIEW ] );
00038 
00039   //hboxLayout1->addWidget( m_editor );
00040   //hboxLayout2->addWidget( m_renderer );
00041   //hboxLayout3->addWidget( m_histogram );
00042 
00043         glWidgets[ X_AXIS ]->m_Axis = X_AXIS;
00044         glWidgets[ Y_AXIS ]->m_Axis = Y_AXIS;
00045         glWidgets[ Z_AXIS ]->m_Axis = Z_AXIS;
00046   glWidgets[ OPENGL_3D_VIEW ]->m_Axis = NO_RENDER_3D_VIEW;
00047 
00048         configureSlider( m_verticalSlider_x );
00049         configureSlider( m_verticalSlider_y );
00050         configureSlider( m_verticalSlider_z );
00051         //configureSlider( m_verticalSlider_stepSize );
00052 
00053   configureStepSizeSlider();
00054   
00055         // Connect GUI Elements with methods
00056         connect( m_actionOpen,   SIGNAL( triggered() ), this, SLOT( openFile()  ));
00057         connect( m_actionClose,  SIGNAL( triggered() ), this, SLOT( closeFile() ));
00058         connect( m_actionQuit,   SIGNAL( triggered() ), this, SLOT( closeProgram() ));
00059 
00060   connect( m_button_tf, SIGNAL(clicked()), m_editor, SLOT( showTransferFunction() ));
00061 
00062   // TF Load/Save/Preset connections
00063   connect( m_tfPreset0, SIGNAL (triggered() ), this, SLOT( setDefault0() ));
00064   connect( m_tfPreset1, SIGNAL (triggered() ), this, SLOT( setDefault1() ));
00065   connect( m_tfPreset2, SIGNAL (triggered() ), this, SLOT( setDefault2() ));
00066   connect( m_tfPreset3, SIGNAL (triggered() ), this, SLOT( setDefault3() ));
00067 
00068   connect( m_actionSaveTF, SIGNAL( triggered() ), this, SLOT( saveTransferFunction() ));
00069   connect( m_actionLoadTF, SIGNAL( triggered() ), this, SLOT( loadTransferFunction() ));
00070 
00071   connect( m_radioButton_comp,   SIGNAL( clicked() ), this, SLOT ( setComp()   ));
00072   connect( m_radioButton_mip,    SIGNAL( clicked() ), this, SLOT ( setMIP()    ));
00073   connect( m_radioButton_avg,    SIGNAL( clicked() ), this, SLOT ( setAvg()    ));
00074   connect( m_radioButton_avgTF,  SIGNAL( clicked() ), this, SLOT ( setAvgTF()  ));
00075   connect( m_radioButton_fh,     SIGNAL( clicked() ), this, SLOT ( setFH()     ));
00076   connect( m_radioButton_compPh, SIGNAL( clicked() ), this, SLOT ( setCompPh() ));
00077 
00078   connect( m_slider_ka, SIGNAL (valueChanged (int)), this, SLOT(setAmbient (int) ));
00079   connect( m_slider_kd, SIGNAL (valueChanged (int)), this, SLOT(setDiffuse (int) ));
00080   connect( m_slider_ks, SIGNAL (valueChanged (int)), this, SLOT(setSpecular (int) ));
00081   connect( m_slider_kg, SIGNAL (valueChanged (int)), this, SLOT(setGloss (int) ));
00082 
00083   configureLightSlider(m_slider_ka);
00084   configureLightSlider(m_slider_kd);
00085   configureLightSlider(m_slider_ks);
00086   configureGlossLightSlider();
00087 
00088   // set composition button checked
00089   m_radioButton_comp->click();
00090 
00091         connect( m_verticalSlider_x, SIGNAL( valueChanged( int )), this, SLOT(setXAxisValue( int )) );
00092         connect( m_verticalSlider_y, SIGNAL( valueChanged( int )), this, SLOT(setYAxisValue( int )) );
00093         connect( m_verticalSlider_z, SIGNAL( valueChanged( int )), this, SLOT(setZAxisValue( int )) );
00094         connect( m_verticalSlider_stepSize, SIGNAL( valueChanged( int )), this, SLOT(setStepSize( int )) );
00095 
00096   // gradient changes - create new 1d-texture in glWidgets[0]
00097   connect(m_editor,     SIGNAL( gradientStopsChanged(    const QGradientStops &) ),
00098           glWidgets[ OPENGL_3D_VIEW ], SLOT(   GenerateGradientTexture( const QGradientStops &) ));
00099 
00100   //connect(m_editor,   SIGNAL( gradientStopsChanged(const QGradientStops &) ),
00101   //        m_renderer, SLOT(   setGradientStops(    const QGradientStops &) ));
00102 
00103   connect(this, SIGNAL( updateHistogram(const int*) ), m_editor, SLOT( showHistogram( const int*) ));
00104   connect(this, SIGNAL( releaseHistogram()          ), m_editor, SLOT( clearHistogram()           ));
00105 
00106   QTimer::singleShot( 50, this, SLOT( setDefault0() ));
00107 }
00108 
00109 
00110 MainWindow::~MainWindow()
00111 {
00112   // no need to delete child widgets, QT does it all for us
00113 }
00114 
00115 void MainWindow::resizeEvent(QResizeEvent* re)
00116 {
00117   if(re->size() != re->oldSize())
00118   {
00119     printf("RESIZE\n");
00120 
00121     QRect rect3d = m_openGL3DView->geometry();
00122 
00123     // Space left to resize window
00124     int margin = 10;
00125     int space;
00126     int hspace = re->size().width() - rect3d.x() - rect3d.width();
00127     int vspace = re->size().height() - rect3d.y() - rect3d.height();
00128 
00129     // width & height must me equal
00130     if(hspace >= vspace)
00131     {
00132       space = vspace - margin;
00133     }
00134     else
00135     {
00136       space = hspace - margin;
00137     }
00138 
00139     // resize symmetrical
00140     m_openGL3DView->setGeometry(rect3d.x(), rect3d.y(), rect3d.width() + space, rect3d.height() + space);
00141 
00142   }
00143 }
00144 
00145 
00146 void MainWindow::configureSlider( QSlider *slider )
00147 {
00148         slider->setRange( 0, SLIDER_RANGE );
00149         slider->setSingleStep( 1 );
00150         slider->setPageStep( SLIDER_RANGE / 64 );
00151         slider->setTickInterval( SLIDER_RANGE / 4 );
00152         slider->setTickPosition( QSlider::TicksLeft );
00153 //  slider->setValue( SLIDER_RANGE / 2 );
00154         slider->setEnabled( isFileOpen );
00155 }
00156 
00157 void MainWindow::configureLightSlider(QSlider *slider)
00158 {
00159   int sliderRange = LIGHT_SLIDER_RANGE;
00160 
00161   slider->setRange(0, sliderRange);
00162   slider->setSingleStep(1);
00163   slider->setPageStep(sliderRange/10);
00164   slider->setTickInterval(sliderRange/10);
00165   slider->setTickPosition(QSlider::TicksLeft);
00166   slider->setEnabled(isFileOpen);
00167 }
00168 
00169 
00170 void MainWindow::configureGlossLightSlider()
00171 {
00172   int sliderRange = LIGHT_SLIDER_RANGE;
00173 
00174   m_slider_kg->setRange(1, sliderRange-1);
00175   m_slider_kg->setSingleStep(1);
00176   m_slider_kg->setPageStep(sliderRange/10);
00177   m_slider_kg->setTickInterval(sliderRange/10);
00178   m_slider_kg->setTickPosition(QSlider::TicksLeft);
00179   m_slider_kg->setEnabled(isFileOpen);
00180 }
00181 
00182 
00183 void MainWindow::configureStepSizeSlider()
00184 {
00185   int sliderRange = STEP_SLIDER_RANGE;
00186 
00187         m_verticalSlider_stepSize->setRange( 1, sliderRange );
00188         m_verticalSlider_stepSize->setSingleStep( 1 );
00189         m_verticalSlider_stepSize->setPageStep( sliderRange / 64 );
00190         m_verticalSlider_stepSize->setTickInterval( sliderRange / 4 );
00191         m_verticalSlider_stepSize->setTickPosition( QSlider::TicksLeft );
00192 //      m_verticalSlider_stepSize->setValue( sliderRange / 2 );
00193         m_verticalSlider_stepSize->setEnabled( isFileOpen );
00194 }
00195 
00196 
00197 void MainWindow::activateSliders()
00198 {
00199   m_verticalSlider_x->setEnabled( isFileOpen );
00200   m_verticalSlider_y->setEnabled( isFileOpen );
00201   m_verticalSlider_z->setEnabled( isFileOpen );
00202 
00203   m_slider_ka->setEnabled( isFileOpen );
00204   m_slider_kd->setEnabled( isFileOpen );
00205   m_slider_ks->setEnabled( isFileOpen );
00206   m_slider_kg->setEnabled( isFileOpen );
00207 
00208   m_slider_ka->setValue (LIGHT_SLIDER_RANGE / 2);
00209   m_slider_kd->setValue (LIGHT_SLIDER_RANGE / 2);
00210   m_slider_ks->setValue (LIGHT_SLIDER_RANGE / 2);
00211   m_slider_kg->setValue (LIGHT_SLIDER_RANGE / 2);
00212 
00213   m_verticalSlider_stepSize->setEnabled( isFileOpen );
00214 
00215   m_verticalSlider_x->setValue( SLIDER_RANGE / 2);
00216   m_verticalSlider_y->setValue( SLIDER_RANGE / 2);
00217   m_verticalSlider_z->setValue( SLIDER_RANGE / 2);
00218   m_verticalSlider_stepSize->setValue( STEP_SLIDER_RANGE / 2);
00219 }
00220 
00221 
00222 void MainWindow::setAxisValue( int axis, int value )
00223 {
00224   //std::cout << "- new " << axis << "-axis value: " << value << std::endl;
00225   glWidgets[ axis ]->m_AxisValue = value;
00226   glWidgets[ axis ]->updateGL();
00227 }
00228 
00229 void MainWindow::setStepSize( int value )
00230 {
00231   //std::cout << "- new stepsize-value: " << value << std::endl;
00232 
00233   //float fStepSize = 0.0;
00234   //m_verticalSlider_stepSize->getRange( 0, SLIDER_RANGE );
00235   //fStepSize = (float)value; // / SLIDER_RANGE;
00236   //std::cout << "- stepsize-value: " << fStepSize << std::endl;
00237 
00238   glWidgets[ OPENGL_3D_VIEW ]->m_rayStepSize = (float)value; // / SLIDER_RANGE;
00239   glWidgets[ OPENGL_3D_VIEW ]->updateGL();
00240 }
00241 
00242 
00243 
00244 // --- private slots ---
00245 
00246 void MainWindow::setXAxisValue( int xVal ) { setAxisValue( X_AXIS, xVal ); }
00247 void MainWindow::setYAxisValue( int yVal ) { setAxisValue( Y_AXIS, yVal ); }
00248 void MainWindow::setZAxisValue( int zVal ) { setAxisValue( Z_AXIS, zVal ); }
00249 
00250 
00251 void MainWindow::setAmbient(int val)
00252 { 
00253   glWidgets[ OPENGL_3D_VIEW ]->setLightValue(ambient, val/(float) LIGHT_SLIDER_RANGE); 
00254 }
00255 
00256 void MainWindow::setDiffuse(int val)
00257 {
00258   glWidgets[ OPENGL_3D_VIEW ]->setLightValue(diffuse, val/(float) LIGHT_SLIDER_RANGE); 
00259 }
00260 
00261 void MainWindow::setSpecular(int val)
00262 {
00263   glWidgets[ OPENGL_3D_VIEW ]->setLightValue(specular, val/(float) LIGHT_SLIDER_RANGE); 
00264 }
00265 
00266 
00267 void MainWindow::setGloss(int val)
00268 {
00269   //printf("Gloss: %d\n",  val);
00270   glWidgets[ OPENGL_3D_VIEW ]->setLightValue(gloss, (float) val); 
00271 }
00272 
00273 
00274 
00275 void MainWindow::openFile()
00276 {
00277   if ( isFileOpen ) closeFile();
00278 
00279   std::cout << "- opening file..." << std::endl;
00280         QString fileName = QFileDialog::getOpenFileName( this, tr("Load Volume Data"), ".", tr("Data Sets (*.dat)") );
00281 
00282         if( !fileName.isNull() )
00283         {
00284                 std::string strFileName = std::string( fileName.toLatin1() );
00285     m_volume = new Volume();
00286 
00287     if( m_volume != NULL )
00288       m_volume->load( strFileName );
00289       emit updateHistogram(m_volume->getDensityDistribution());
00290 
00291     if ( glWidgets[0]->create3DTexture( m_volume ) )
00292       std::cout << "3D-Texture-creation successful." << std::endl;
00293       // hint: do NOT delete volume already here (as it is not needed for the planes anymore),
00294       // but it will still be needed later for raycasting?! (at least gv thinks so ;-)
00295 
00296     glWidgets[ OPENGL_3D_VIEW ]->m_Axis = RENDER_3D_VIEW;
00297 
00298   // for histogram
00299 //    transferFunction->createDensityArray ( volume );
00300 
00301     isFileOpen = true;
00302     setDefaultGradient( 0 );
00303     activateSliders();
00304 
00305     // Update TF points to generate textur after loading file
00306     m_editor->pointsUpdated();
00307         }
00308 }
00309 
00310 void MainWindow::closeFile()
00311 {
00312   std::cout << "- closing file..." << std::endl;
00313 
00314   m_volume->release();
00315   emit releaseHistogram();
00316 
00317   for (int i=0; i<NUMBER_OF_GL_WIDGETS; i++ )
00318     glWidgets[i]->release();
00319 
00320   //transferFunction->release();
00321 
00322   isFileOpen = false;
00323 
00324   glWidgets[ OPENGL_3D_VIEW ]->m_Axis = NO_RENDER_3D_VIEW;
00325 
00326   m_verticalSlider_x->setValue( 0 );
00327   m_verticalSlider_y->setValue( 0 );
00328   m_verticalSlider_z->setValue( 0 );
00329 
00330   activateSliders();
00331 
00332   // update gl views
00333   for(int i = 0; i < NUMBER_OF_GL_WIDGETS; i++)
00334   {
00335     glWidgets[i]->update();
00336   }
00337 }
00338 
00339 
00340 void MainWindow::closeProgram()
00341 {
00342         std::cout << "Exiting ..." << std::endl;
00343 
00344   if ( isFileOpen ) closeFile();
00345 }
00346 
00347 //
00348 //void MainWindow::render()
00349 //{
00350 //      std::cout << "Render-button pressed..." << std::endl;
00351 //
00352 //  // set render-flag
00353 //  glWidgets[ OPENGL_3D_VIEW ]->m_Axis = RENDER_3D_VIEW;
00354 //
00355 //  // repaint to render
00356 //  glWidgets[ OPENGL_3D_VIEW ]->updateGL();
00357 //}
00358 
00359 
00360 void MainWindow::setDefaultGradient(int i)
00361 {
00362     QGradientStops stops;
00363     QPolygonF points;
00364     switch (i) {
00365 
00366     case 0:
00367         stops << QGradientStop(0.00, QColor::fromRgba(0xff000000));
00368         stops << QGradientStop(1.00, QColor::fromRgba(0xffffffff));
00369         break;
00370 
00371     case 1:
00372         stops << QGradientStop(0.00, QColor::fromRgba(0));
00373         stops << QGradientStop(0.04, QColor::fromRgba(0xff131360));
00374         stops << QGradientStop(0.08, QColor::fromRgba(0xff202ccc));
00375         stops << QGradientStop(0.42, QColor::fromRgba(0xff93d3f9));
00376         stops << QGradientStop(0.51, QColor::fromRgba(0xffb3e6ff));
00377         stops << QGradientStop(0.73, QColor::fromRgba(0xffffffec));
00378         stops << QGradientStop(0.92, QColor::fromRgba(0xff5353d9));
00379         stops << QGradientStop(0.96, QColor::fromRgba(0xff262666));
00380         stops << QGradientStop(1.00, QColor::fromRgba(0));
00381         break;
00382 
00383     case 2:
00384         stops << QGradientStop(0.00, QColor::fromRgba(0xffffffff));
00385         stops << QGradientStop(0.11, QColor::fromRgba(0xfff9ffa0));
00386         stops << QGradientStop(0.13, QColor::fromRgba(0xfff9ff99));
00387         stops << QGradientStop(0.14, QColor::fromRgba(0xfff3ff86));
00388         stops << QGradientStop(0.49, QColor::fromRgba(0xff93b353));
00389         stops << QGradientStop(0.87, QColor::fromRgba(0xff264619));
00390         stops << QGradientStop(0.96, QColor::fromRgba(0xff0c1306));
00391         stops << QGradientStop(1.00, QColor::fromRgba(0));
00392         break;
00393 
00394     case 3:
00395         stops << QGradientStop(0.00, QColor::fromRgba(0));
00396         stops << QGradientStop(0.10, QColor::fromRgba(0xffe0cc73));
00397         stops << QGradientStop(0.17, QColor::fromRgba(0xffc6a006));
00398         stops << QGradientStop(0.46, QColor::fromRgba(0xff600659));
00399         stops << QGradientStop(0.72, QColor::fromRgba(0xff0680ac));
00400         stops << QGradientStop(0.92, QColor::fromRgba(0xffb9d9e6));
00401         stops << QGradientStop(1.00, QColor::fromRgba(0));
00402         break;
00403 
00404     default:
00405         qWarning("bad default: %d\n", i);
00406         break;
00407     }
00408 
00409     //QPolygonF pts;
00410     //int h_off = m_renderer->width() / 10;
00411     //int v_off = m_renderer->height() / 8;
00412     //pts << QPointF(m_renderer->width() / 2, m_renderer->height() / 2)
00413     //    << QPointF(m_renderer->width() / 2 - h_off, m_renderer->height() / 2 - v_off);
00414 
00415     //TODO: Hover Points in ALPHA not set correctely
00416     m_editor->setGradientStops(stops);
00417     //m_renderer->setGradientStops(stops);
00418     m_editor->pointsUpdated();
00419 }
00420 
00421 void MainWindow::saveTransferFunction()
00422 {
00423   printf("Save TF\n");
00424 
00425   QString fileName = QFileDialog::getSaveFileName( this, tr("Save Transfer Function"), ".", tr("TransferFunction (*.tf)") );
00426 
00427         if( !fileName.isNull() )
00428         {
00429                 std::string strFileName = std::string( fileName.toLatin1() );
00430 
00431     if( m_editor != NULL )
00432       m_editor->saveTransferFunction( strFileName );
00433   }
00434 }
00435 
00436 void MainWindow::loadTransferFunction()
00437 {
00438   printf("Load TF\n");
00439 
00440   QString fileName = QFileDialog::getOpenFileName( this, tr("Load Transfer Function"), ".", tr("TransferFunction (*.tf)") );
00441 
00442         if( !fileName.isNull() )
00443         {
00444                 std::string strFileName = std::string( fileName.toLatin1() );
00445 
00446     if( m_editor != NULL )
00447       m_editor->loadTransferFunction( strFileName );
00448   }
00449 
00450   m_editor->pointsUpdated();
00451   glWidgets[ OPENGL_3D_VIEW ]->updateGL();
00452 }
00453 
00454 void MainWindow::keyPressEvent( QKeyEvent *e )
00455 {
00456   //std::cout << "pressed button: " << e->key() << std::endl;
00457 
00458   /*
00459   w = 87
00460   s = 83
00461   a = 65
00462   d = 68
00463   q = 81
00464   e = 69
00465   */
00466 
00467   switch ( e->key() )
00468   {
00469     case 87: case 83: // w, s
00470     case 65: case 68: // a, d
00471     case 81: case 69: // q, e
00472     case 89: case 77: // y, m
00473       glWidgets[ OPENGL_3D_VIEW ]->keyEvent( e->key() );
00474       break;
00475 
00476     case 79: // o
00477       openFile();
00478       break;
00479 
00480     case 16777216: // ESC
00481       // quit application
00482       break;
00483 
00484     default:
00485       break;
00486   }
00487 }

Generated on Mon Dec 10 18:18:11 2007 for VisLU by  doxygen 1.5.4