Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

PyCanvas.cxx

Go to the documentation of this file.
00001 
00012 // for dll interface warning
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016 
00017 #include "PyCanvas.h"
00018 
00019 #include "QtCut.h"
00020 
00021 #include "controllers/DisplayController.h"
00022 #include "controllers/FunctionController.h"
00023 #include "datasrcs/DataSourceException.h"
00024 #include "pattern/FactoryException.h"
00025 #include "plotters/Cut1DPlotter.h"
00026 
00027 // with Python 2.3, include before Qt headers to avoid conflict with
00028 // symbol `slots'
00029 #include <boost/python.hpp>
00030 
00031 #include "qt/QtView.h"
00032 #include "qt/CanvasWindow.h"
00033 
00034 #include <qapplication.h>
00035 
00036 using std::string;
00037 using namespace boost::python;
00038 
00039 namespace hippodraw {
00040 namespace Python {
00041 
00042 void 
00043 export_Canvas()
00044 {
00046         class_ < PyCanvas >
00047           ( "Canvas",
00048             "The HippoDraw canvas.\n"
00049             "This class provides an interface to the canvas.\n"
00050             "One can take various actions that are also\n"
00051             "available from menu items in the canvas window.",
00052             no_init )
00053 
00054           .def ( "show", &PyCanvas::show,
00055                  "Displays the canvas window on the screen." )
00056 
00057           .def ( "close", &PyCanvas::close,
00058                  "Closes the window." )
00059 
00060            .def ( "addDisplay", &PyCanvas::addDisplay,
00061                   "Adds a display to the canvas.\n"
00062                   "Arguments:\n"
00063                   "\t a display object" )
00064 
00065            .def ( "saveAs", &PyCanvas::saveAs,
00066                   "Save the canvas as a XML file.\n"
00067                   "Argument:\n"
00068                   "\t A string specifying the file name." )
00069 
00070            .def ( "printTo", &PyCanvas::print,
00071                   "Prints the canvas to PostScript file\n"
00072                   "Argument:\n"
00073                   "\t a string specifying the file name" )
00074 
00075            .def ( "getDisplay", &PyCanvas::getDisplay,
00076                   return_value_policy < reference_existing_object > (),
00077                   "Returns the selected Display object.\n"
00078                   "Arguments:\n"
00079                   "\t none\n"
00080                   "Returns:\n"
00081                   "\t The selected Display object or None" )
00082 
00083            .def ( "getDisplays", &PyCanvas::getDisplays,
00084                   return_value_policy < copy_const_reference > (),
00085                   "Returns a tuple of all Display objects on the canvas.\n"
00086                   "Arguments:\n"
00087                   "\t none" )
00088 
00089            .def ( "getCut", &PyCanvas::getCut,
00090                   return_value_policy < reference_existing_object > (),
00091                   "Returns the currently selected Cut object.\n"
00092                   "Aguments:\n"
00093                   "\t none\n"
00094                   "Returns:\n"
00095                   "\t A Cut object or none if selected Display is not a Cut." )
00096 
00097            .def ( "selectAllDisplays", &PyCanvas::selectAllDisplays,
00098                   "Sets all displays to selected state or not.\n"
00099                   "Arugment:\n"
00100                   "\t True or False" )
00101 
00102            .def ( "selectDisplay", &PyCanvas::selectDisplay,
00103                   "Sets a display to selected state.\n"
00104                   "Arugment:\n"
00105                   "\t a display object" )
00106 
00107            .def ( "saveAsImage", &PyCanvas::saveAsImage,
00108                   "Save a display as an image file.\n"
00109                   "The suffix of the file name controls the image type.\n"
00110                   "Arguments:\n"
00111                   "\t a display object\n"
00112                   "\t a string specifying the file name" )
00113 
00114            .def ( "saveSelectedImages", &PyCanvas::saveSelectedImages,
00115                   "Save the selected displays as an image file.\n"
00116                   "The suffix of the file name controls the image type.\n"
00117                   "Arguments:\n"
00118                   "\t a string specifying the file name" )
00119 
00120            .def ( "removeDisplay", &PyCanvas::removeDisplay,
00121                   "Removes the display from the canvas\n"
00122                   "Argument: display object" )
00123 
00124            .def ( "addText", &PyCanvas::addText,
00125                   "Adds text to display.\n"
00126                   "Arguments:\n"
00127                   "\t display object\n"
00128                   "\t text string"  )
00129 
00130            .def ( "addTextAt", &PyCanvas::addTextAt,
00131                   "Adds text to a display at specified position\n"
00132                   "Position is fraction of width and height\n"
00133                   "Arguments:\n"
00134                   "\t display object\n"
00135                   "\t text string\n"
00136                   "\t x position\n"
00137                   "\t y position" )
00138 
00139           .def ( "addTextRep", &PyCanvas::addTextRep,
00140                  "adds textual data representation to display\n"
00141                  "Arguments:\n"
00142                  "\t Display object\n"
00143                  "\t representation type (string)" )
00144 
00145            .def ( "mouseData", &PyCanvas::mouseData,
00146                   return_value_policy < copy_const_reference > (),
00147                   "Returns a list of picked data\n"
00148                   "Arguments:\n"
00149                   "\t none" )
00150 
00151           .def ( "setPlotMatrix", &PyCanvas::setPlotMatrix,
00152                  "Sets the number of columns and "
00153                  "rows of plots for each page.\n"
00154                  "Arguments:\n"
00155                  "\t columns\n"
00156                  "\t rows" )
00157 
00158           .def ( "getTextRepTypes", &PyCanvas::getTextRepTypes,
00159                  return_value_policy < copy_const_reference > (),
00160                  "Returns the types of textual data representations"
00161                  " available\n"
00162                  "Arguments:\n"
00163                  "\t none" )
00164           .def ( "clear", &PyCanvas::clear,
00165                  "Removes all items from the canvas" )
00166 
00167            ;
00168 }
00169 
00170 } // namesapce Python
00171 } // namesapce hippodraw
00172 
00173 PyCanvas::PyCanvas ( CanvasWindow * window ) 
00174 {
00175   m_canvas = window;
00176   m_app = qApp;
00177 }
00178 
00179 PyCanvas::PyCanvas ( )
00180 {
00181   m_app = qApp;
00182   m_app->lock();
00183   m_canvas = new CanvasWindow ();
00184   m_app->unlock();
00185 }
00186 
00187 void
00188 PyCanvas::
00189 show ()
00190 {
00191   m_app -> lock ();
00192   m_canvas -> show ();
00193   m_app -> unlock ();
00194 }
00195 
00196 void
00197 PyCanvas::
00198 close ()
00199 {
00200   m_app -> lock ();
00201   m_canvas -> closeNoPrompt ();
00202   m_app -> unlock ();
00203 }
00204 
00205 void PyCanvas::addDisplay ( QtDisplay * display_wrap )
00206 {
00207 #ifndef _MSC_VER
00208   m_app->lock ();
00209 #endif
00210 
00211   PlotterBase * plotter = display_wrap->display();
00212   m_canvas->addPlotDisplay ( plotter );
00213   
00214 #ifndef _MSC_VER
00215   m_app->unlock ();
00216 #endif
00217 }
00218 
00219 void PyCanvas::saveAs ( const std::string & filename )
00220 {
00221   m_app->lock ();
00222   m_canvas->saveAs ( filename );
00223   m_app->unlock ();
00224 }
00225 
00226 QtDisplay * PyCanvas::getDisplay ()
00227 {
00228   m_app -> lock ();
00229   QtDisplay * display = 0;
00230    PlotterBase * plotter = m_canvas->selectedPlotter();
00231    if (plotter != 0) {
00232       display = new QtDisplay( m_canvas->selectedPlotter() );
00233    }
00234    m_app -> unlock ();
00235 
00236    return display;
00237 }
00238 
00239 const std::vector<QtDisplay *> & PyCanvas::getDisplays() const {
00240 
00241    m_app->lock();    // Locking the app may not be necessary.
00242    m_displays.clear();
00243 
00244 // Replicate some of the logic in PlotTable::initialize() to create
00245 // a vector of QtDisplay pointers.
00246  
00247    const std::vector< const ViewBase * > & views = m_canvas->views();
00248    std::vector< const ViewBase * > :: const_iterator viewIt = views.begin();
00249    while ( viewIt != views.end() ) {
00250       const ViewBase * view = *viewIt++;
00251       PlotterBase * plotter = view->getPlotter();
00252       m_displays.push_back( new QtDisplay(plotter) );
00253    }
00254    m_app->unlock();
00255    return m_displays;
00256 }
00257 
00258 QtCut * 
00259 PyCanvas::
00260 getCut ()
00261 {
00262   QtCut * qtcut = 0;
00263   PlotterBase * plotter = m_canvas->selectedPlotter();
00264 
00265   if ( plotter != 0 ) {
00266      CutPlotter * cut_plotter = dynamic_cast < CutPlotter * > ( plotter );
00267 
00268      if ( cut_plotter != 0 ) {
00269        qtcut = new QtCut ( cut_plotter );
00270      }
00271   }
00272 
00273   return qtcut;
00274 }
00275 
00276 void PyCanvas::selectAllDisplays ( bool flag )
00277 {
00278    m_app->lock();
00279    m_canvas -> setAllSelected ( flag );
00280    m_app->unlock();
00281 }
00282 
00283 void PyCanvas::selectDisplay ( QtDisplay * display ) {
00284    m_app->lock();
00285    QtView * selectedView = findSelectedView ( display );
00286    if ( selectedView ) {
00287       m_canvas->setSelected( selectedView );
00288    }
00289    m_app->unlock();
00290 }
00291 
00292 void
00293 PyCanvas::
00294 print ( const std::string & filename )
00295 {
00296   m_app -> lock ();
00297   m_canvas -> print ( filename );
00298   m_app -> unlock ();
00299 }
00300 
00301 void PyCanvas::saveAsImage( QtDisplay * display, const std::string &filename )
00302 {
00303    m_app->lock();
00304 // Ensure that a suffix is provided...
00305    std::string::size_type i = filename.find_last_of( '.' );
00306    if ( i == std::string::npos ) {
00307       const std::string 
00308          what ( "PyCanvas::saveAsImage: filename suffix missing." );
00309       m_app->unlock();
00310       throw DataSourceException( what );
00311    }
00312 
00313    QtView * selectedView = findSelectedView( display );
00314    if ( selectedView ) {
00315       std::string file = filename;
00316       PlotterBase * plotter = selectedView->getPlotter();
00317       m_canvas->savePlotAsImageImmediate ( plotter, file );
00318    }
00319    m_app->unlock();
00320 }
00321 
00322 void PyCanvas::saveSelectedImages( const std::string &filename )
00323 {
00324    m_app->lock();
00325 // Ensure that a suffix is provided...
00326    std::string::size_type i = filename.find_last_of( '.' );
00327    if ( i == std::string::npos ) {
00328       const std::string 
00329          what ( "PyCanvas::saveSelectedImages: filename suffix missing." );
00330       m_app->unlock();
00331       throw DataSourceException( what );
00332    }
00333    m_canvas->fileSaveSelectedImages ( filename );
00334    m_app->unlock();
00335 }
00336 
00337 QtView * PyCanvas::findSelectedView ( QtDisplay * display )
00338 {
00339    PlotterBase * myPlotter = display->display();
00340 
00341    const std::vector< const ViewBase * > & views = m_canvas->views();
00342    std::vector< const ViewBase * > :: const_iterator viewIt = views.begin();
00343    while ( viewIt != views.end() ) {
00344       const ViewBase * view = *viewIt++;
00345       if ( myPlotter == view->getPlotter() ) {
00346          return m_canvas->getViewFor( myPlotter );
00347       }
00348    }
00349    return 0;
00350 }   
00351 
00352 void PyCanvas::removeDisplay ( QtDisplay * display )
00353 {
00354    PlotterBase * plotter = display->display();
00355    m_canvas->removeDisplay ( plotter );
00356 }
00357 
00358 void
00359 PyCanvas::
00360 addTextRep ( QtDisplay * display, const std::string & type )
00361 {
00362   m_app -> lock ();
00363 
00364   try {
00365     PlotterBase * plotter = display -> display ();
00366     if ( type == "Function Parameters" ||
00367          type == "Chi-squared" ) {
00368       FunctionController * controller = FunctionController::instance ();
00369       if ( controller -> hasFunction ( plotter, 0 ) ) {
00370         m_canvas -> addFuncDisplay ( plotter, type );
00371       }
00372     }
00373     else {
00374       plotter -> setActivePlot ( 0, false );
00375       const std::string null ("");
00376       m_canvas -> addTextDisplay ( plotter, type, null );
00377       plotter -> setActivePlot ( -1, true );
00378     }
00379 
00380   }
00381   catch ( const FactoryException & e ) {
00382     m_app -> unlock ();
00383     throw e;
00384   }
00385 
00386   m_app -> unlock ();
00387 }
00388 
00389 const std::vector < std::string > &
00390 PyCanvas::getTextRepTypes () const
00391 {
00392   DisplayController * controller = DisplayController:: instance ();
00393 
00394   return controller -> getTextTypes ();
00395 }
00396 
00397 void PyCanvas::addText( QtDisplay * display, 
00398                         const std::string &text )
00399 {
00400    m_app->lock();
00401 
00402    PlotterBase * plotter = display->display();
00403 
00404 // Only one datarep can be active in order to add a "Text From Box"
00405 // textrep.
00406    plotter->setActivePlot(0, false);
00407 
00408    m_canvas->addTextDisplay( plotter, "Text From Box", text );
00409 
00410 // Reset all plotters active.
00411    plotter->setActivePlot(-1, true);
00412 
00413    m_app->unlock();
00414 }
00415 
00416 void PyCanvas::addTextAt ( QtDisplay * display, const std::string &text,
00417                            double xrel, double yrel )
00418 {
00419    m_app->lock();
00420 
00421    PlotterBase * plotter = display->display();
00422 
00423 // Only one datarep can be active in order to add a "Text From Box"
00424 // textrep.
00425    plotter->setActivePlot(0, false);
00426    m_canvas->addTextDisplayAt ( plotter, "Text From Box", 
00427                                 text, xrel, yrel );
00428 
00429 // Reset all plotters active.
00430    plotter->setActivePlot(-1, true);
00431 
00432    m_app->unlock();
00433 }
00434 
00435 const std::vector<double> & PyCanvas::mouseData() 
00436 {
00437    return m_canvas->mouseEventData();
00438 }
00439 
00440 void
00441 PyCanvas::
00442 setPlotMatrix ( unsigned int columns, unsigned int rows )
00443 {
00444   m_app -> lock ();
00445   m_canvas -> setPlotMatrix ( columns, rows );
00446   m_app -> unlock ();
00447 }
00448 
00449 /* examples/run_examples.py fails unless lock/unlock is remove under
00450    windows.
00451 */
00452 void
00453 PyCanvas::
00454 clear ()
00455 {
00456 #ifndef _MSC_VER
00457   m_app -> lock ();
00458 #endif
00459 
00460   m_canvas -> clear ();
00461 
00462 #ifndef _MSC_VER
00463   m_app -> unlock ();
00464 #endif
00465 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3