PyCanvas.cxx

Go to the documentation of this file.
00001 
00013 // for dll interface warning
00014 #ifdef _MSC_VER
00015 #include "msdevstudio/MSconfig.h"
00016 #endif
00017 
00018 #include <iostream>
00019 #include "PyApp.h"
00020 #include "QtCut.h"
00021 
00022 #include "PyCanvas.h"
00023 
00024 #include "controllers/DisplayController.h"
00025 #include "controllers/FunctionController.h"
00026 #include "datasrcs/NTuple.h"
00027 #include "pattern/FactoryException.h"
00028 #include "plotters/Cut1DPlotter.h"
00029 
00030 #include "qt/QtView.h"
00031 #include "qt/CanvasView.h"
00032 #include "qt/CanvasViewProxy.h"
00033 #include "qt/CanvasWindow.h"
00034 #include "transforms/BinaryTransform.h"
00035 
00036 #include "axes/Range.h"
00037 
00038 #include <stdexcept>
00039 
00040 using std::runtime_error;
00041 using std::string;
00042 
00043 using namespace hippodraw;
00044 
00045 PyCanvas::PyCanvas ( CanvasWindow * window ) 
00046   : m_has_gui ( true )
00047 {
00048   m_canvas = window;
00049   CanvasView * view = m_canvas -> getCanvasView ();
00050 
00051   m_canvas_proxy = new CanvasViewProxy ( view );
00052 }
00053 
00057 PyCanvas::PyCanvas ( )
00058   : m_canvas (0),
00059     m_canvas_proxy (0),
00060     m_has_gui ( false )
00061 {
00062   PyApp::lock();
00063 
00064   m_canvas = new CanvasWindow ();
00065   CanvasView * view = m_canvas -> getCanvasView ();
00066   m_canvas_proxy = new CanvasViewProxy ( view );
00067 
00068   PyApp::unlock ();
00069 }
00070 
00071 void
00072 PyCanvas::
00073 check () const
00074 {
00075   if ( m_canvas == 0 ) {
00076     string what ( "Can not use this method as there is no window\n"
00077                   "              associated with the canvas." );
00078     throw  std::runtime_error ( what );
00079   }
00080 }
00081 
00082 void
00083 PyCanvas::
00084 show ()
00085 {
00086   check ();
00087   PyApp::lock();
00088   m_canvas -> show ();
00089   PyApp::unlock ();
00090 }
00091 
00092 void
00093 PyCanvas::
00094 close ()
00095 {
00096   PyApp::lock();
00097   m_canvas -> closeNoPrompt ();
00098   m_canvas = 0;
00099   PyApp::unlock ();
00100 }
00101 
00104 void
00105 PyCanvas::
00106 addDisplay ( QtDisplay * display_wrap )
00107 {
00108   if ( m_has_gui ) {
00109     check();
00110     PlotterBase * plotter = display_wrap->display();
00111     m_canvas_proxy -> addDisplay ( plotter );
00112   }
00113   else {
00114     m_displays.push_back ( display_wrap );
00115   }
00116   //  bool yes = 
00117   PyApp::hasPendingEvents ();
00118  }
00119 
00120 void PyCanvas::saveAs ( const std::string & filename )
00121 {
00122   if ( m_has_gui ) {
00123     check();
00124     PyApp::lock();
00125     m_canvas->saveAs ( filename );
00126     PyApp::unlock ();
00127   }
00128   else {
00129     vector < PlotterBase * > plotters;
00130     unsigned int size = m_displays.size ();
00131 
00132     for ( unsigned int i = 0; i < size; i++ ) {
00133       QtDisplay * display = m_displays [i];
00134       PlotterBase * plotter = display -> display ();
00135       plotters.push_back ( plotter );
00136     }
00137     CanvasView::saveAs ( plotters, filename );
00138   }
00139 
00140 }
00141 
00142 QtDisplay * PyCanvas::getDisplay ()
00143 {
00144   check();
00145 
00146   PyApp::lock();
00147   QtDisplay * display = 0;
00148    PlotterBase * plotter = m_canvas->selectedPlotter();
00149    if (plotter != 0) {
00150       display = new QtDisplay( m_canvas->selectedPlotter() );
00151    }
00152    PyApp::unlock ();
00153 
00154    return display;
00155 }
00156 
00160 const std::vector<QtDisplay *> & PyCanvas::getDisplays() const {
00161 
00162   check();
00163   PyApp::lock();
00164    m_displays.clear();
00165 
00166    // Replicate some of the logic in PlotTable::initialize() to create
00167    // a vector of QtDisplay pointers.
00168  
00169    const std::vector< const ViewBase * > & views = m_canvas->views();
00170    std::vector< const ViewBase * > :: const_iterator viewIt = views.begin();
00171    while ( viewIt != views.end() ) {
00172       const ViewBase * view = *viewIt++;
00173       PlotterBase * plotter = view->getPlotter();
00174       m_displays.push_back( new QtDisplay(plotter) );
00175    }
00176    PyApp::unlock ();
00177    return m_displays;
00178 }
00179 
00180 QtCut * 
00181 PyCanvas::
00182 getCut ()
00183 {
00184   check();
00185   QtCut * qtcut = 0;
00186   PlotterBase * plotter = m_canvas->selectedPlotter();
00187 
00188   if ( plotter != 0 ) {
00189      CutPlotter * cut_plotter = dynamic_cast < CutPlotter * > ( plotter );
00190 
00191      if ( cut_plotter != 0 ) {
00192        qtcut = new QtCut ( cut_plotter );
00193      }
00194   }
00195 
00196   return qtcut;
00197 }
00198 
00199 void PyCanvas::selectAllDisplays ( bool flag )
00200 {
00201   check();
00202    PyApp::lock();
00203    m_canvas -> setAllSelected ( flag );
00204    PyApp::unlock ();
00205 }
00206 
00207 void PyCanvas::selectDisplay ( QtDisplay * display ) {
00208   check();
00209    PyApp::lock();
00210    QtView * selectedView = findSelectedView ( display );
00211    if ( selectedView ) {
00212       m_canvas->setSelected( selectedView );
00213    }
00214    PyApp::unlock ();
00215 }
00216 
00217 void
00218 PyCanvas::
00219 print ( const std::string & filename )
00220 {
00221   check();
00222   PyApp::lock();
00223   m_canvas -> print ( filename );
00224   PyApp::unlock ();
00225 }
00226 
00230 void PyCanvas::saveAsImage( QtDisplay * display, const std::string &filename )
00231 {
00232 //   check();
00233    // Ensure that a suffix is provided...
00234    std::string::size_type i = filename.find_last_of( '.' );
00235    if ( i == std::string::npos ) {
00236       const std::string 
00237          what ( "PyCanvas::saveAsImage: filename suffix missing." );
00238       PyApp::unlock ();
00239       throw runtime_error( what );
00240    }
00241 
00242    QtView * selectedView = findSelectedView( display );
00243    if ( selectedView ) {
00244       std::string file = filename;
00245       PlotterBase * plotter = selectedView->getPlotter();
00246       m_canvas_proxy -> saveAsImage ( plotter, filename );
00247    }
00248 }
00249 
00250 void PyCanvas::saveSelectedImages( const std::string &filename )
00251 {
00252   check();
00253    PyApp::lock();
00254    // Ensure that a suffix is provided...
00255    std::string::size_type i = filename.find_last_of( '.' );
00256    if ( i == std::string::npos ) {
00257       const std::string 
00258          what ( "PyCanvas::saveSelectedImages: filename suffix missing." );
00259       PyApp::unlock ();
00260       throw runtime_error( what );
00261    }
00262    m_canvas->fileSaveSelectedImages ( filename );
00263    PyApp::unlock ();
00264 }
00265 
00266 QtView * PyCanvas::findSelectedView ( QtDisplay * display )
00267 {
00268   check();
00269    PlotterBase * myPlotter = display->display();
00270 
00271    return m_canvas -> getViewFor ( myPlotter );
00272 }   
00273 
00274 void PyCanvas::removeDisplay ( QtDisplay * display )
00275 {
00276   check();
00277    PlotterBase * plotter = display->display();
00278    m_canvas->removeDisplay ( plotter );
00279 }
00280 
00281 void
00282 PyCanvas::
00283 addTextRep ( QtDisplay * display, const std::string & type )
00284 {
00285   check();
00286   PyApp::lock();
00287 
00288   try {
00289     PlotterBase * plotter = display -> display ();
00290     if ( type == "Function Parameters" ||
00291          type == "Chi-squared" ) {
00292       FunctionController * controller = FunctionController::instance ();
00293       if ( controller -> hasFunction ( plotter, 0 ) ) {
00294         m_canvas -> addFuncDisplay ( plotter, type );
00295       }
00296     }
00297     else {
00298       plotter -> setActivePlot ( 0, false );
00299       const std::string null ("");
00300       m_canvas -> addTextDisplay ( plotter, type, null );
00301       plotter -> setActivePlot ( -1, true );
00302     }
00303 
00304   }
00305   catch ( const FactoryException & e ) {
00306     PyApp::unlock ();
00307     throw e;
00308   }
00309 
00310   PyApp::unlock ();
00311 }
00312 
00313 const std::vector < std::string > &
00314 PyCanvas::getTextRepTypes () const
00315 {
00316   check();
00317   DisplayController * controller = DisplayController:: instance ();
00318 
00319   return controller -> getTextTypes ();
00320 }
00321 
00322 void PyCanvas::addText( QtDisplay * display, 
00323                         const std::string &text )
00324 {
00325   check();
00326    PyApp::lock();
00327 
00328    PlotterBase * plotter = display->display();
00329 
00330    // Only one DataRep can be active in order to add a "Text From Box"
00331    // textrep.
00332    plotter->setActivePlot(0, false);
00333 
00334    m_canvas->addTextDisplay( plotter, "Text From Box", text );
00335 
00336    // Reset all plotters active.
00337    plotter->setActivePlot(-1, true);
00338 
00339    PyApp::unlock ();
00340 }
00341 
00342 void PyCanvas::addTextAt ( QtDisplay * display, const std::string &text,
00343                            double xrel, double yrel )
00344 {
00345   check();
00346 
00347    PyApp::lock();
00348    PlotterBase * plotter = display->display();
00349 
00350    // Only one DataRep can be active in order to add a "Text From Box"
00351    // textrep.
00352    plotter->setActivePlot(0, false);
00353    m_canvas->addTextDisplayAt ( plotter, "Text From Box", 
00354                                 text, xrel, yrel );
00355 
00356    // Reset all plotters active.
00357    plotter->setActivePlot(-1, true);
00358    plotter -> update ();
00359    PyApp::unlock ();
00360 }
00361 
00362 void PyCanvas::addTextAtAbs ( QtDisplay * display, const std::string &text,
00363                               double xabs, double yabs )
00364 {
00365   check();
00366 
00367    std::cout << "xabs_bef = " << xabs << "yabs_bef = " << yabs << std::endl;
00368    PyApp::lock();
00369    PlotterBase * plotter = display->display();
00370    TransformBase * transform = plotter->getTransform();
00371    BinaryTransform * tf
00372           = dynamic_cast < BinaryTransform * > ( transform );
00373    
00374    tf->transform(xabs,yabs);
00375 
00376    QtView * view = m_canvas -> getViewFor ( plotter );
00377    //xabs = (view -> toViewX(xabs))/1000.;
00378    //yabs = (view -> toViewY(yabs))/1000.;
00379    //xabs = view -> userToDrawX(xabs);
00380    //yabs = view -> userToDrawY(yabs);
00381    const Range & rx = plotter -> getDataRange(Axes::X);
00382    const Range & ry = plotter -> getDataRange(Axes::Y);
00383    double xmax = rx.high();
00384    double xmin = rx.low();
00385    double ymax = ry.high();
00386    double ymin = ry.low();
00387    std::cout << "xmax = " << xmax << "  xmin = " << xmin <<std::endl;
00388    std::cout << "ymax = " << ymax << "  ymin = " << ymin <<std::endl;
00389    tf->transform(xmax,xmin);
00390    tf->transform(ymax,ymin);
00391    double xref = (xabs-xmin)/(xmax-xmin);
00392    double yref = 1.-(yabs-ymin)/(ymax-ymin);
00393    std::cout << "xabs = " << xabs << "yabs = " << yabs << std::endl;
00394    std::cout << "xmax = " << xmax << "  xmin = " << xmin <<std::endl;
00395    std::cout << "ymax = " << ymax << "  ymin = " << ymin <<std::endl;
00396    std::cout << "xref = " << xref << "yref = " << yref << std::endl;
00397    QRect rect = view->boundingRect();
00398    //std::cout << rect.width() << '\t' << rect.height() << std::endl;
00399    //xabs = xabs/rect.width();
00400    //yabs = yabs/rect.height();
00401    // Only one DataRep can be active in order to add a "Text From Box"
00402    // textrep.
00403    plotter->setActivePlot(0, false);
00404    m_canvas->addTextDisplayAt ( plotter, "Text From Box", 
00405                                 text, xref, yref );
00406 
00407    // Reset all plotters active.
00408    plotter->setActivePlot(-1, true);
00409    plotter -> update ();
00410    PyApp::unlock ();
00411 }
00412 
00413 const std::vector<double> & PyCanvas::mouseData() 
00414 {
00415   check();
00416    return m_canvas->mouseEventData();
00417 }
00418 
00419 void
00420 PyCanvas::
00421 setPlotMatrix ( unsigned int columns, unsigned int rows )
00422 {
00423   PyApp::lock();
00424   check();
00425 
00426   m_canvas -> setPlotMatrix ( columns, rows );
00427   PyApp::unlock ();
00428 }
00429 
00430 void
00431 PyCanvas::
00432 swapOrientation ()
00433 {
00434   check ();
00435   m_canvas_proxy -> swapOrientation ();
00436 }
00437 
00441 void
00442 PyCanvas::
00443 clear ()
00444 {
00445   check();
00446 
00447   m_canvas_proxy -> clear ();
00448 }
00449 
00450 int
00451 PyCanvas::
00452 getHeight ( QtDisplay * display ) const
00453 {
00454   check();
00455   int height = 0;
00456   const PlotterBase * plotter = display -> display ();
00457   const QtView * view = m_canvas -> getViewFor ( plotter );
00458   if ( view != 0 ) {
00459     height = view -> height ();
00460   }
00461 
00462   return height;
00463 }
00464 int
00465 PyCanvas::
00466 getWidth ( QtDisplay * display ) const
00467 {
00468   check();
00469   int width = 0;
00470   const PlotterBase * plotter = display -> display ();
00471   const QtView * view = m_canvas -> getViewFor ( plotter );
00472   if ( view != 0 ) {
00473     width = view -> width ();
00474   }
00475 
00476   return width;
00477 }
00478 
00479 void
00480 PyCanvas::
00481 setHeight ( QtDisplay * display, double h )
00482 {
00483   check();
00484   PyApp::lock();
00485   const PlotterBase * plotter = display -> display ();
00486   QtView * view = m_canvas -> getViewFor ( plotter );
00487   if ( view != 0 ) {
00488     Rect rect = view -> getDrawRect ();
00489     view -> setDrawRect ( rect.getX(), rect.getY(),
00490                           rect.getWidth(), h );
00491   }
00492   PyApp::unlock ();
00493 }
00494 
00495 void
00496 PyCanvas::
00497 setWidth ( QtDisplay * display, double w )
00498 {
00499   check();
00500   PyApp::lock();
00501   const PlotterBase * plotter = display -> display ();
00502   QtView * view = m_canvas -> getViewFor ( plotter );
00503   if ( view != 0 ) {
00504     Rect rect = view -> getDrawRect ();
00505     view -> setDrawRect ( rect.getX(), rect.getY(),
00506                           w, rect.getHeight () );
00507 
00508   }
00509   PyApp::unlock ();
00510 }
00511 
00512 int
00513 PyCanvas::
00514 getX ( QtDisplay * display ) const
00515 {
00516   check();
00517   int x = 0;
00518   const PlotterBase * plotter = display -> display ();
00519   QtView * view = m_canvas -> getViewFor ( plotter );
00520   if ( view != 0 ) {
00521     x = static_cast < int > ( view -> x () );
00522   }
00523   return x;
00524 }
00525 
00526 int
00527 PyCanvas::
00528 getY ( QtDisplay * display ) const
00529 {
00530   check();
00531   int y = 0;
00532   const PlotterBase * plotter = display -> display ();
00533   QtView * view = m_canvas -> getViewFor ( plotter );
00534   if ( view != 0 ) {
00535     y = static_cast < int > ( view -> y () );
00536   }
00537   return y;
00538 }
00539 
00540 void
00541 PyCanvas::
00542 setX ( QtDisplay * display, double value )
00543 {
00544   check();
00545   PyApp::lock();
00546   const PlotterBase * plotter = display -> display ();
00547   QtView * view = m_canvas -> getViewFor ( plotter );
00548   if ( view != 0 ) {
00549     view -> setX ( static_cast < int > ( value ) );
00550   }
00551   PyApp::unlock ();
00552 }
00553 
00554 void
00555 PyCanvas::
00556 setY ( QtDisplay * display, double value )
00557 {
00558   check();
00559   PyApp::lock();
00560   const PlotterBase * plotter = display -> display ();
00561   QtView * view = m_canvas -> getViewFor ( plotter );
00562   if ( view != 0 ) {
00563     view -> setY ( static_cast < int > ( value ) );
00564   }
00565   PyApp::unlock ();
00566 }
00567 
00568 NTuple *
00569 PyCanvas::
00570 getSelPickTable ()
00571 {
00572   check();
00573   PyApp::lock();
00574   NTuple * nt = m_canvas->getPickTable();
00575   PyApp::unlock();
00576   return nt;
00577 }
00578 
00579 NTuple *
00580 PyCanvas::
00581 getPickTable ( QtDisplay * display )
00582 {
00583   check();
00584   PyApp::lock();
00585   const PlotterBase * plotter = display->display ();
00586   NTuple * nt = m_canvas->getPickTable( plotter );
00587   PyApp::unlock();
00588 
00589   return nt;
00590 }

Generated for HippoDraw Class Library by doxygen