00001
00013
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 ( true )
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
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
00167
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
00233
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
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
00331
00332 plotter->setActivePlot(0, false);
00333
00334 m_canvas->addTextDisplay( plotter, "Text From Box", text );
00335
00336
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
00351
00352 plotter->setActivePlot(0, false);
00353 m_canvas->addTextDisplayAt ( plotter, "Text From Box",
00354 text, xrel, yrel );
00355
00356
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 PyApp::lock();
00368 PlotterBase * plotter = display->display();
00369 TransformBase * transform = plotter->getTransform();
00370 BinaryTransform * tf
00371 = dynamic_cast < BinaryTransform * > ( transform );
00372
00373 tf->transform(xabs,yabs);
00374
00375 QtView * view = m_canvas -> getViewFor ( plotter );
00376
00377
00378
00379
00380 const Range & rx = plotter -> getDataRange(Axes::X);
00381 const Range & ry = plotter -> getDataRange(Axes::Y);
00382 double xmax = rx.high();
00383 double xmin = rx.low();
00384 double ymax = ry.high();
00385 double ymin = ry.low();
00386 tf->transform(xmax,xmin);
00387 tf->transform(ymax,ymin);
00388 double xref = (xabs-xmin)/(xmax-xmin);
00389 double yref = 1.-(yabs-ymin)/(ymax-ymin);
00390 QRect rect = view->boundingRect();
00391 plotter->setActivePlot(0, false);
00392 m_canvas->addTextDisplayAt ( plotter, "Text From Box",
00393 text, xref, yref );
00394
00395
00396 plotter->setActivePlot(-1, true);
00397 plotter -> update ();
00398 PyApp::unlock ();
00399 }
00400
00401 const std::vector<double> & PyCanvas::mouseData()
00402 {
00403 check();
00404 return m_canvas->mouseEventData();
00405 }
00406
00407 void
00408 PyCanvas::
00409 setPlotMatrix ( unsigned int columns, unsigned int rows )
00410 {
00411 PyApp::lock();
00412 check();
00413
00414 m_canvas -> setPlotMatrix ( columns, rows );
00415 PyApp::unlock ();
00416 }
00417
00418 void
00419 PyCanvas::
00420 swapOrientation ()
00421 {
00422 check ();
00423 m_canvas_proxy -> swapOrientation ();
00424 }
00425
00429 void
00430 PyCanvas::
00431 clear ()
00432 {
00433 check();
00434
00435 m_canvas_proxy -> clear ();
00436 }
00437
00438 int
00439 PyCanvas::
00440 getHeight ( QtDisplay * display ) const
00441 {
00442 check();
00443 int height = 0;
00444 const PlotterBase * plotter = display -> display ();
00445 const QtView * view = m_canvas -> getViewFor ( plotter );
00446 if ( view != 0 ) {
00447 height = view -> height ();
00448 }
00449
00450 return height;
00451 }
00452 int
00453 PyCanvas::
00454 getWidth ( QtDisplay * display ) const
00455 {
00456 check();
00457 int width = 0;
00458 const PlotterBase * plotter = display -> display ();
00459 const QtView * view = m_canvas -> getViewFor ( plotter );
00460 if ( view != 0 ) {
00461 width = view -> width ();
00462 }
00463
00464 return width;
00465 }
00466
00467 void
00468 PyCanvas::
00469 setHeight ( QtDisplay * display, double h )
00470 {
00471 check();
00472 PyApp::lock();
00473 const PlotterBase * plotter = display -> display ();
00474 QtView * view = m_canvas -> getViewFor ( plotter );
00475 if ( view != 0 ) {
00476 Rect rect = view -> getDrawRect ();
00477 view -> setDrawRect ( rect.getX(), rect.getY(),
00478 rect.getWidth(), h );
00479 }
00480 PyApp::unlock ();
00481 }
00482
00483 void
00484 PyCanvas::
00485 setWidth ( QtDisplay * display, double w )
00486 {
00487 check();
00488 PyApp::lock();
00489 const PlotterBase * plotter = display -> display ();
00490 QtView * view = m_canvas -> getViewFor ( plotter );
00491 if ( view != 0 ) {
00492 Rect rect = view -> getDrawRect ();
00493 view -> setDrawRect ( rect.getX(), rect.getY(),
00494 w, rect.getHeight () );
00495
00496 }
00497 PyApp::unlock ();
00498 }
00499
00500 int
00501 PyCanvas::
00502 getX ( QtDisplay * display ) const
00503 {
00504 check();
00505 int x = 0;
00506 const PlotterBase * plotter = display -> display ();
00507 QtView * view = m_canvas -> getViewFor ( plotter );
00508 if ( view != 0 ) {
00509 x = static_cast < int > ( view -> x () );
00510 }
00511 return x;
00512 }
00513
00514 int
00515 PyCanvas::
00516 getY ( QtDisplay * display ) const
00517 {
00518 check();
00519 int y = 0;
00520 const PlotterBase * plotter = display -> display ();
00521 QtView * view = m_canvas -> getViewFor ( plotter );
00522 if ( view != 0 ) {
00523 y = static_cast < int > ( view -> y () );
00524 }
00525 return y;
00526 }
00527
00528 void
00529 PyCanvas::
00530 setX ( QtDisplay * display, double value )
00531 {
00532 check();
00533 PyApp::lock();
00534 const PlotterBase * plotter = display -> display ();
00535 QtView * view = m_canvas -> getViewFor ( plotter );
00536 if ( view != 0 ) {
00537 view -> setX ( static_cast < int > ( value ) );
00538 }
00539 PyApp::unlock ();
00540 }
00541
00542 void
00543 PyCanvas::
00544 setY ( QtDisplay * display, double value )
00545 {
00546 check();
00547 PyApp::lock();
00548 const PlotterBase * plotter = display -> display ();
00549 QtView * view = m_canvas -> getViewFor ( plotter );
00550 if ( view != 0 ) {
00551 view -> setY ( static_cast < int > ( value ) );
00552 }
00553 PyApp::unlock ();
00554 }
00555
00556 NTuple *
00557 PyCanvas::
00558 getSelPickTable ()
00559 {
00560 check();
00561 PyApp::lock();
00562 NTuple * nt = m_canvas->getPickTable();
00563 PyApp::unlock();
00564 return nt;
00565 }
00566
00567 NTuple *
00568 PyCanvas::
00569 getPickTable ( QtDisplay * display )
00570 {
00571 check();
00572 PyApp::lock();
00573 const PlotterBase * plotter = display->display ();
00574 NTuple * nt = m_canvas->getPickTable( plotter );
00575 PyApp::unlock();
00576
00577 return nt;
00578 }