C:/Projekte/C++/FlowVIS_107/src/Navigation.cpp

Go to the documentation of this file.
00001 #include "Navigation.h"
00002 
00004 #include <QRect>
00005 #include <QPixmap>
00006 #include <QLinearGradient>
00007 #include <QPainter>
00008 #include <QLabel>
00009 #include <QRect>
00010 #include <QPointF>
00011 #include <QColorDialog>
00012 #include <QFileDialog>
00013 #include <QRadioButton>
00014 #include <QRgb>
00015 #include <QMessageBox>
00016 #include <fstream>
00017 
00018 Navigation::Navigation(OpenGLWidget* ogl, QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/) : QWidget(parent, flags)
00019 {
00020   setupUi(this);
00021   m_parent = parent;
00022   m_ogl = ogl;
00023   m_isFileOpen = false;
00024 
00025   m_pointSize = QSize(10,10);
00026   m_pointBrush = QBrush(QColor(200,200,200,127));
00027 
00028   // nk - Dropdown
00030   connect(m_backgroundCB, SIGNAL(currentIndexChanged(int) ), m_ogl, SLOT(setActiveBackground(int)));
00031 
00032   // nk - PushButton
00034   connect(m_selectArrowColor, SIGNAL(pressed()), m_ogl, SIGNAL(setArrowColor()));
00036   connect(m_selectSLColor, SIGNAL(pressed()), m_ogl, SLOT(selectStreamLineColor()));
00038   connect(m_selectGlyphColor, SIGNAL(pressed()), m_ogl, SLOT(selectStreamGlyphColor()));
00040   connect(m_updateSL, SIGNAL(pressed()), m_ogl, SLOT(updateStreamLines()));
00041 
00044   connect(m_showArrows, SIGNAL(clicked(bool)), m_ogl, SLOT(setActiveArrows(bool)));
00046   connect(m_activateTF, SIGNAL(clicked(bool)), m_ogl, SLOT(setActiveTransferFunction(bool)));
00048   connect(m_activateSL, SIGNAL(clicked(bool)), m_ogl, SLOT(setLinesActive(bool)));
00050   connect(m_showTapering, SIGNAL(clicked(bool)), m_ogl, SLOT(setTaperingActive(bool)));
00052   connect(m_showGlyph, SIGNAL(clicked(bool)), m_ogl, SLOT(setGlyphActive(bool)));
00054   connect(m_scalePoints, SIGNAL(clicked(bool)), m_ogl, SIGNAL(setActivePointScale(bool)));
00055 
00058   connect(m_pointSizeSlider, SIGNAL(valueChanged(int)), m_ogl, SIGNAL(setPointSize(int)));
00060   connect(m_pointResSlider, SIGNAL(valueChanged(int)), m_ogl, SIGNAL(setResolution(int)));
00062   connect(m_dsepSlider, SIGNAL(valueChanged(int)), m_ogl, SLOT(setDSep(int)));
00064   connect(m_dtestSlider, SIGNAL(valueChanged(int)), m_ogl, SLOT(setDTest(int)));
00066   connect(m_glyphDistance, SIGNAL(valueChanged(int)), m_ogl, SLOT(setStreamGlyphDistance(int)));
00068   connect(m_glyphSize, SIGNAL(valueChanged(int)), m_ogl, SLOT(setStreamGlyphSize(int)));
00069 
00070   // nk - radio button
00072   connect(m_euler, SIGNAL(clicked()), m_ogl, SLOT(setEuler()));
00074   connect(m_runge, SIGNAL(clicked()), m_ogl, SLOT(setRungeKutta()));
00075 
00076   // set maximum slider value explicit for dtestSlider & dSepSlider
00077   m_dsepSlider->setMaximum(MAX_SLIDER_VALUE);
00078   m_dtestSlider->setMaximum(MAX_SLIDER_VALUE);
00079 }
00080 
00081 // nk - set gui elements dependent on file status
00082 void Navigation::resetGuiElements(bool filestatus)
00083 {
00084   if(filestatus == false)
00085   {
00086     // nk - does not work
00087     //m_showArrows->setChecked(filestatus);
00088     //m_scalePoints->setChecked(filestatus);
00089     //m_activateTF->setChecked(filestatus);
00090     //m_activateSL->setChecked(filestatus);
00091 
00092     //m_showGlyph->setChecked(filestatus);
00093     //m_showTapering->setChecked(filestatus);
00094   }
00095 
00097   m_selectArrowColor->setEnabled(filestatus);
00098   
00099   // nk - point depending state
00100   m_showArrows->setEnabled(filestatus);
00101   m_scalePoints->setEnabled(filestatus);
00102   m_pointSizeSlider->setEnabled(filestatus);
00103   m_pointResSlider->setEnabled(filestatus);
00104 
00106   m_activateTF->setEnabled(filestatus);
00107   
00109   m_dsepSlider->setEnabled(filestatus);
00110   m_dtestSlider->setEnabled(filestatus);
00111 
00113   m_activateSL->setEnabled(filestatus);
00114   m_showGlyph->setEnabled(filestatus);
00115   m_showTapering->setEnabled(filestatus);
00116 
00118   m_updateSL->setEnabled(filestatus);
00119   m_selectSLColor->setEnabled(filestatus);
00120   m_selectGlyphColor->setEnabled(filestatus);
00121 
00123   m_euler->setEnabled(filestatus);
00124   m_runge->setEnabled(filestatus);
00125 
00127   m_glyphSize->setEnabled(filestatus);
00128   m_glyphDistance->setEnabled(filestatus);
00129 }
00130 
00131 void Navigation::createColorBar(bool isFileOpen, bool update)
00132 {
00134   m_isFileOpen = isFileOpen;
00135 
00137   m_colorBarRect = m_colorBar->frameRect();
00138   QPixmap* pixmap = new QPixmap(m_colorBarRect.width(), m_colorBarRect.height());
00139 
00140   pixmap->fill(Qt::transparent);
00141 
00142   if(isFileOpen)
00143   {
00144     QLinearGradient gradient(0, 0, m_colorBarRect.width(), 0);
00145     gradient.setSpread(QGradient::PadSpread);
00146 
00147     // set only on startup
00148     if(update == false)
00149     {
00150       m_gradientStops.clear();
00151       m_points.clear();
00152 
00154       m_gradientStops.push_back(QGradientStop(0, QColor::fromRgbF(1, 0, 0, 1))); 
00155       m_gradientStops.push_back(QGradientStop(0.5, QColor::fromRgbF(0.0, 1.0, 0.0, 1))); 
00156       m_gradientStops.push_back(QGradientStop(1, QColor::fromRgbF(0.0, 0.0, 1.0, 1))); 
00157 
00158       int xCenter = m_colorBarRect.left() + (m_colorBarRect.width()) / 2;
00159       int yCenter = m_colorBarRect.top() + ( m_colorBarRect.height() / 2);
00160 
00162       m_points.push_back(QPointF(m_colorBarRect.left(), yCenter )); // left
00163       m_points.push_back(QPointF(xCenter, yCenter)); // center
00164       m_points.push_back(QPointF(m_colorBarRect.right(), yCenter)); // right
00165       
00166     }
00167     
00168     for(int i = 0; i < m_gradientStops.size(); i++)
00169     {
00170       gradient.setColorAt(m_gradientStops.at(i).first, m_gradientStops.at(i).second);
00171     }
00172 
00173     QPainter painter(pixmap);
00174     painter.fillRect(0, 0, m_colorBarRect.width(), m_colorBarRect.height(), gradient);
00175 
00176     painter.setBrush(m_pointBrush);
00177     for(int i = 0; i < m_points.size(); i++)
00178     {
00179       painter.drawEllipse(getPointBounds(i));
00180     }
00181     painter.end();
00182 
00183     //pixmap->save("pixmap_test.png");
00184     m_colorBar->setPixmap(*pixmap);
00185 
00187      m_ogl->setColorBarTexture(m_gradientStops);
00188   }
00189   else // if file closed
00190   {
00191     // nk - transparent pixmap on file close && clear m_gradientStops
00192     m_colorBar->setPixmap(*pixmap);
00193     m_gradientStops.clear();
00194     m_points.clear();
00195   }
00196 
00197   if(!m_colorBar->pixmap())
00198     printf("ERROR in createColorBar\n");
00199 }
00200 
00201 QRectF Navigation::getPointBounds(int i)
00202 {
00203   QPointF p = m_points.at(i);
00204   float w = m_pointSize.width();
00205   float h = m_pointSize.height();
00206   float x = p.x() - w / 2;
00207   float y = p.y() - h / 2;
00208 
00209   return QRectF(x, y, w, h);
00210 }
00211 
00212 
00213 Navigation::~Navigation()
00214 {
00215   // do nothing
00216 }
00217 
00218 void Navigation::mousePressEvent(QMouseEvent* e)
00219 {
00221   if(!m_isFileOpen)
00222     return;
00223 
00225   QRect c = m_colorBar->geometry();
00226   float yPos = c.height() / 2;
00227   float xPos = 0.0f;
00228 
00230   if(!(e->x() >= c.left() && e->x() <= c.right() + 1 && e->y() >= c.top() && e->y() <= c.bottom()))
00231   {
00232     return;
00233   }
00234 
00235   if(e->button() == Qt::LeftButton)
00236   {
00238     xPos = e->x() - c.x();
00239     float curPos = xPos / (float) c.width();
00240     // printf("CurPos: %f\n", curPos); // (nk) for control only
00241 
00242     // (nk) add new gradient stop & new cursor position
00243     m_gradientStops.push_back(QGradientStop(curPos, QColor::fromRgbF(1, 1, 1, 1)));
00244     m_points.push_back(QPointF(xPos, yPos));
00245 
00246     createColorBar(m_isFileOpen, true);
00247   }
00248 
00249   if(e->button() == Qt::MidButton)
00250   {
00252     xPos = e->x() - c.x();
00253 
00254     for(int i = 0; i < m_points.size(); i++)
00255     {
00256       QPointF curPoint = QPointF(xPos, yPos);
00257       QRectF bound = getPointBounds(i);
00258       if(bound.contains(curPoint))
00259       {
00261         QColor color = QColorDialog::getColor();
00262 
00264         qreal curStop = m_gradientStops.at(i).first;
00265         m_gradientStops.replace(i, QGradientStop(curStop, color));
00266         createColorBar(m_isFileOpen, true);
00267       }
00268     }
00269   }
00270   
00271   if(e->button() == Qt::RightButton)
00272   {
00274     xPos = e->x() - c.x();
00275 
00276     for(int i = 0; i < m_points.size(); i++)
00277     {
00278       QPointF curPoint = QPointF(xPos, yPos);
00279       QRectF bound = getPointBounds(i);
00280       if(bound.contains(curPoint))
00281       {
00282         m_points.remove(i);
00283         m_gradientStops.remove(i);
00284         createColorBar(m_isFileOpen, true);
00285       }
00286     }
00287   }
00288 }
00289 
00290 void Navigation::loadTF()
00291 {
00292   QString fileName = QFileDialog::getOpenFileName(this, "Load Transfer Function", ".", "TransferFunction (*.tf)");
00293   std::string line; // buffer for read line
00294   qreal px, py; // save points read from file
00295   qreal stop, r, g, b, a; // save stops read from file
00296   QColor color;
00297   
00298   // return from function if no filename specified (e.g. dialog canceled) or no dataset open
00299   if(fileName.isNull() || !m_isFileOpen)
00300   {
00301     std::cerr << "No dataset load or no filename specifed to Load TF" << std::endl;
00302     return;
00303   }
00304   else
00305   {
00307     m_gradientStops.clear();
00308     m_points.clear();
00309 
00310     std::string strFileName = std::string(fileName.toLatin1());
00311 
00312     // Open File
00313     std::ifstream inFile (strFileName.c_str());
00314 
00315     if(!inFile)
00316     {
00317       std::cerr << "Error loading file!" << std::endl;
00318       return;
00319     }
00320 
00321     while(std::getline(inFile, line))
00322     {
00323       // search for string "Point" in line
00324       if(!line.compare("Point")) // line.compare returns zero if string is equal
00325       {
00326         std::getline(inFile, line);
00327         
00328         // read digits from line
00329         if(isdigit(line.at(0)))
00330         {
00331           sscanf_s(line.c_str(), "%lf %lf\n", &px, &py);
00332           m_points.push_back(QPointF(px, py));
00333         }
00334       }
00335       
00337       if(!line.compare("Stop")) // line.compare returns zero if string is equal
00338       {
00339         std::getline(inFile, line);
00340 
00341         // read only digits from line
00342         if(isdigit(line.at(0)))
00343         {
00344           sscanf_s(line.c_str(), "%lf %lf %lf %lf %lf\n", &stop, &r, &g, &b, &a);
00345           color.setRgbF(r, g, b, a);
00346           m_gradientStops.push_back(QGradientStop(stop, color));
00347         }
00348       }
00349     }
00350   }
00351 
00352   this->createColorBar(m_isFileOpen, true);
00353 }
00354 
00355 void Navigation::saveTF()
00356 {
00357   if(m_gradientStops.isEmpty() || m_points.isEmpty())
00358   {
00359     printf("Error saving TF: Load dataset first\n");
00360     return;
00361   }
00362 
00363   QString fileName = QFileDialog::getSaveFileName(this, "Save Transfer Function", ".", "TransferFunction (*.tf)");
00364 
00365   if (!fileName.isNull())
00366   {
00367     std::string strFileName = std::string(fileName.toLatin1());
00368 
00369     FILE *fp = NULL;
00370     fopen_s(&fp, strFileName.c_str(), "w");
00371 
00372     if(!fp)
00373     {
00374       std::cerr << "Error saving file!" << std::endl;
00375       return;
00376     }
00377 
00378     for(int i = 0; i < m_gradientStops.size(); i++)
00379     {
00380       qreal pointX = m_points.at(i).x();
00381       qreal pointY = m_points.at(i).y();
00382 
00383       qreal gradPos = m_gradientStops.at(i).first;
00384       QColor gradCol = m_gradientStops.at(i).second;
00385 
00386       fprintf(fp, "Point\n");
00388       fprintf(fp, "%lf %lf\n", pointX, pointY);
00389 
00390       fprintf(fp, "Stop\n");
00392       fprintf(fp, "%lf %lf %lf %lf %lf\n", gradPos, gradCol.redF(), gradCol.greenF(), gradCol.blueF(), gradCol.alphaF());
00393     }
00394 
00395     fclose(fp);
00396   }
00397 }
00398 
00399 
00400 
00401 
00402 void Navigation::resetTF()
00403 {
00404   printf("Reset TF\n");
00405   // reset transfer function
00406   createColorBar(m_isFileOpen, false);
00407 
00408   this->update();
00409   m_ogl->update();
00410 }
00411 
00412 void Navigation::setBackgroundChooser(int count)
00413 {
00414   // at file close - no scalar channel 
00415   if ( count == 0 )
00416   {
00417     // clear string list
00418     m_bgList.clear();
00419 
00420     // clear backgroundChooser
00421     m_backgroundCB->clear();
00422   }
00423   else
00424   {
00425     QString str = "Background";
00426     QVariant var;
00427 
00428     for(int i = 0; i < count; i++)
00429     {
00430       var = i;
00431 
00432       // m_bgList.push_back( str + var.toString() );
00433       // gv Names just make it nicer
00434       switch ( i )
00435       {
00436         case 0:
00437             m_bgList.push_back( "Pressure" );
00438             break;
00439 
00440         case 1:
00441             m_bgList.push_back( "Vorticity" );
00442             break;
00443 
00444         default:
00445             m_bgList.push_back( str + var.toString() );
00446             break;
00447       }
00448     }
00449 
00450     m_bgList.push_back("None");
00451 
00452     // Add values to BackgroundChooser CB
00453     m_backgroundCB->addItems(m_bgList);
00454 
00456     m_backgroundCB->setCurrentIndex(count);
00457   }
00458 }
00459 

Generated on Mon Jan 21 14:50:12 2008 for VisLU by  doxygen 1.5.4