PyCanvas.cxx
Go to the documentation of this file.
1 
13 // for dll interface warning
14 #ifdef _MSC_VER
15 #include "msdevstudio/MSconfig.h"
16 #endif
17 
18 #include <iostream>
19 #include "PyApp.h"
20 #include "QtCut.h"
21 
22 #include "PyCanvas.h"
23 
26 #include "datasrcs/NTuple.h"
28 #include "plotters/Cut1DPlotter.h"
29 
30 #include "qt/QtView.h"
31 #include "qt/CanvasView.h"
32 #include "qt/CanvasViewProxy.h"
33 #include "qt/CanvasWindow.h"
35 
36 #include "axes/Range.h"
37 
38 #include <stdexcept>
39 
40 using std::runtime_error;
41 using std::string;
42 
43 using namespace hippodraw;
44 
46  : m_has_gui ( true )
47 {
48  m_canvas = window;
49  CanvasView * view = m_canvas -> getCanvasView ();
50 
51  m_canvas_proxy = new CanvasViewProxy ( view );
52 }
53 
58  : m_canvas (0),
59  m_canvas_proxy (0),
60  m_has_gui ( true )
61 {
62  PyApp::lock();
63 
64  m_canvas = new CanvasWindow ();
65  CanvasView * view = m_canvas -> getCanvasView ();
66  m_canvas_proxy = new CanvasViewProxy ( view );
67 
68  PyApp::unlock ();
69 }
70 
71 void
73 check () const
74 {
75  if ( m_canvas == 0 ) {
76  string what ( "Can not use this method as there is no window\n"
77  " associated with the canvas." );
78  throw std::runtime_error ( what );
79  }
80 }
81 
82 void
84 show ()
85 {
86  check ();
87  PyApp::lock();
88  m_canvas -> show ();
89  PyApp::unlock ();
90 }
91 
92 void
95 {
96  PyApp::lock();
97  m_canvas -> closeNoPrompt ();
98  m_canvas = 0;
99  PyApp::unlock ();
100 }
101 
104 void
106 addDisplay ( QtDisplay * display_wrap )
107 {
108  if ( m_has_gui ) {
109  check();
110  PlotterBase * plotter = display_wrap->display();
111  m_canvas_proxy -> addDisplay ( plotter );
112  }
113  else {
114  m_displays.push_back ( display_wrap );
115  }
116  // bool yes =
118  }
119 
120 void PyCanvas::saveAs ( const std::string & filename )
121 {
122  if ( m_has_gui ) {
123  check();
124  PyApp::lock();
125  m_canvas->saveAs ( filename );
126  PyApp::unlock ();
127  }
128  else {
129  vector < PlotterBase * > plotters;
130  unsigned int size = m_displays.size ();
131 
132  for ( unsigned int i = 0; i < size; i++ ) {
133  QtDisplay * display = m_displays [i];
134  PlotterBase * plotter = display -> display ();
135  plotters.push_back ( plotter );
136  }
137  CanvasView::saveAs ( plotters, filename );
138  }
139 
140 }
141 
143 {
144  check();
145 
146  PyApp::lock();
147  QtDisplay * display = 0;
148  PlotterBase * plotter = m_canvas->selectedPlotter();
149  if (plotter != 0) {
150  display = new QtDisplay( m_canvas->selectedPlotter() );
151  }
152  PyApp::unlock ();
153 
154  return display;
155 }
156 
160 const std::vector<QtDisplay *> & PyCanvas::getDisplays() const {
161 
162  check();
163  PyApp::lock();
164  m_displays.clear();
165 
166  // Replicate some of the logic in PlotTable::initialize() to create
167  // a vector of QtDisplay pointers.
168 
169  const std::vector< const ViewBase * > & views = m_canvas->views();
170  std::vector< const ViewBase * > :: const_iterator viewIt = views.begin();
171  while ( viewIt != views.end() ) {
172  const ViewBase * view = *viewIt++;
173  PlotterBase * plotter = view->getPlotter();
174  m_displays.push_back( new QtDisplay(plotter) );
175  }
176  PyApp::unlock ();
177  return m_displays;
178 }
179 
180 QtCut *
183 {
184  check();
185  QtCut * qtcut = 0;
186  PlotterBase * plotter = m_canvas->selectedPlotter();
187 
188  if ( plotter != 0 ) {
189  CutPlotter * cut_plotter = dynamic_cast < CutPlotter * > ( plotter );
190 
191  if ( cut_plotter != 0 ) {
192  qtcut = new QtCut ( cut_plotter );
193  }
194  }
195 
196  return qtcut;
197 }
198 
199 void PyCanvas::selectAllDisplays ( bool flag )
200 {
201  check();
202  PyApp::lock();
203  m_canvas -> setAllSelected ( flag );
204  PyApp::unlock ();
205 }
206 
207 void PyCanvas::selectDisplay ( QtDisplay * display ) {
208  check();
209  PyApp::lock();
210  QtView * selectedView = findSelectedView ( display );
211  if ( selectedView ) {
212  m_canvas->setSelected( selectedView );
213  }
214  PyApp::unlock ();
215 }
216 
217 void
219 print ( const std::string & filename )
220 {
221  check();
222  PyApp::lock();
223  m_canvas -> print ( filename );
224  PyApp::unlock ();
225 }
226 
230 void PyCanvas::saveAsImage( QtDisplay * display, const std::string &filename )
231 {
232 // check();
233  // Ensure that a suffix is provided...
234  std::string::size_type i = filename.find_last_of( '.' );
235  if ( i == std::string::npos ) {
236  const std::string
237  what ( "PyCanvas::saveAsImage: filename suffix missing." );
238  PyApp::unlock ();
239  throw runtime_error( what );
240  }
241 
242  QtView * selectedView = findSelectedView( display );
243  if ( selectedView ) {
244  std::string file = filename;
245  PlotterBase * plotter = selectedView->getPlotter();
246  m_canvas_proxy -> saveAsImage ( plotter, filename );
247  }
248 }
249 
250 void PyCanvas::saveSelectedImages( const std::string &filename )
251 {
252  check();
253  PyApp::lock();
254  // Ensure that a suffix is provided...
255  std::string::size_type i = filename.find_last_of( '.' );
256  if ( i == std::string::npos ) {
257  const std::string
258  what ( "PyCanvas::saveSelectedImages: filename suffix missing." );
259  PyApp::unlock ();
260  throw runtime_error( what );
261  }
262  m_canvas->fileSaveSelectedImages ( filename );
263  PyApp::unlock ();
264 }
265 
267 {
268  check();
269  PlotterBase * myPlotter = display->display();
270 
271  return m_canvas -> getViewFor ( myPlotter );
272 }
273 
275 {
276  check();
277  PlotterBase * plotter = display->display();
278  m_canvas->removeDisplay ( plotter );
279 }
280 
281 void
283 addTextRep ( QtDisplay * display, const std::string & type )
284 {
285  check();
286  PyApp::lock();
287 
288  try {
289  PlotterBase * plotter = display -> display ();
290  if ( type == "Function Parameters" ||
291  type == "Chi-squared" ) {
293  if ( controller -> hasFunction ( plotter, 0 ) ) {
294  m_canvas -> addFuncDisplay ( plotter, type );
295  }
296  }
297  else {
298  plotter -> setActivePlot ( 0, false );
299  const std::string null ("");
300  m_canvas -> addTextDisplay ( plotter, type, null );
301  plotter -> setActivePlot ( -1, true );
302  }
303 
304  }
305  catch ( const FactoryException & e ) {
306  PyApp::unlock ();
307  throw e;
308  }
309 
310  PyApp::unlock ();
311 }
312 
313 const std::vector < std::string > &
315 {
316  check();
318 
319  return controller -> getTextTypes ();
320 }
321 
322 void PyCanvas::addText( QtDisplay * display,
323  const std::string &text )
324 {
325  check();
326  PyApp::lock();
327 
328  PlotterBase * plotter = display->display();
329 
330  // Only one DataRep can be active in order to add a "Text From Box"
331  // textrep.
332  plotter->setActivePlot(0, false);
333 
334  m_canvas->addTextDisplay( plotter, "Text From Box", text );
335 
336  // Reset all plotters active.
337  plotter->setActivePlot(-1, true);
338 
339  PyApp::unlock ();
340 }
341 
342 void PyCanvas::addTextAt ( QtDisplay * display, const std::string &text,
343  double xrel, double yrel )
344 {
345  check();
346 
347  PyApp::lock();
348  PlotterBase * plotter = display->display();
349 
350  // Only one DataRep can be active in order to add a "Text From Box"
351  // textrep.
352  plotter->setActivePlot(0, false);
353  m_canvas->addTextDisplayAt ( plotter, "Text From Box",
354  text, xrel, yrel );
355 
356  // Reset all plotters active.
357  plotter->setActivePlot(-1, true);
358  plotter -> update ();
359  PyApp::unlock ();
360 }
361 
362 void PyCanvas::addTextAtAbs ( QtDisplay * display, const std::string &text,
363  double xabs, double yabs )
364 {
365  check();
366 
367  PyApp::lock();
368  PlotterBase * plotter = display->display();
369  TransformBase * transform = plotter->getTransform();
370  BinaryTransform * tf
371  = dynamic_cast < BinaryTransform * > ( transform );
372 
373  tf->transform(xabs,yabs);
374 
375  QtView * view = m_canvas -> getViewFor ( plotter );
376  //xabs = (view -> toViewX(xabs))/1000.;
377  //yabs = (view -> toViewY(yabs))/1000.;
378  //xabs = view -> userToDrawX(xabs);
379  //yabs = view -> userToDrawY(yabs);
380  const Range & rx = plotter -> getDataRange(Axes::X);
381  const Range & ry = plotter -> getDataRange(Axes::Y);
382  double xmax = rx.high();
383  double xmin = rx.low();
384  double ymax = ry.high();
385  double ymin = ry.low();
386  tf->transform(xmax,xmin);
387  tf->transform(ymax,ymin);
388  double xref = (xabs-xmin)/(xmax-xmin);
389  double yref = 1.-(yabs-ymin)/(ymax-ymin);
390  QRect rect = view->boundingRect();
391  plotter->setActivePlot(0, false);
392  m_canvas->addTextDisplayAt ( plotter, "Text From Box",
393  text, xref, yref );
394 
395  // Reset all plotters active.
396  plotter->setActivePlot(-1, true);
397  plotter -> update ();
398  PyApp::unlock ();
399 }
400 
401 const std::vector<double> & PyCanvas::mouseData()
402 {
403  check();
404  return m_canvas->mouseEventData();
405 }
406 
407 void
409 setPlotMatrix ( unsigned int columns, unsigned int rows )
410 {
411  PyApp::lock();
412  check();
413 
414  m_canvas -> setPlotMatrix ( columns, rows );
415  PyApp::unlock ();
416 }
417 
418 void
421 {
422  check ();
424 }
425 
429 void
432 {
433  check();
434 
435  m_canvas_proxy -> clear ();
436 }
437 
438 int
440 getHeight ( QtDisplay * display ) const
441 {
442  check();
443  int height = 0;
444  const PlotterBase * plotter = display -> display ();
445  const QtView * view = m_canvas -> getViewFor ( plotter );
446  if ( view != 0 ) {
447  height = view -> height ();
448  }
449 
450  return height;
451 }
452 int
454 getWidth ( QtDisplay * display ) const
455 {
456  check();
457  int width = 0;
458  const PlotterBase * plotter = display -> display ();
459  const QtView * view = m_canvas -> getViewFor ( plotter );
460  if ( view != 0 ) {
461  width = view -> width ();
462  }
463 
464  return width;
465 }
466 
467 void
469 setHeight ( QtDisplay * display, double h )
470 {
471  check();
472  PyApp::lock();
473  const PlotterBase * plotter = display -> display ();
474  QtView * view = m_canvas -> getViewFor ( plotter );
475  if ( view != 0 ) {
476  Rect rect = view -> getDrawRect ();
477  view -> setDrawRect ( rect.getX(), rect.getY(),
478  rect.getWidth(), h );
479  }
480  PyApp::unlock ();
481 }
482 
483 void
485 setWidth ( QtDisplay * display, double w )
486 {
487  check();
488  PyApp::lock();
489  const PlotterBase * plotter = display -> display ();
490  QtView * view = m_canvas -> getViewFor ( plotter );
491  if ( view != 0 ) {
492  Rect rect = view -> getDrawRect ();
493  view -> setDrawRect ( rect.getX(), rect.getY(),
494  w, rect.getHeight () );
495 
496  }
497  PyApp::unlock ();
498 }
499 
500 int
502 getX ( QtDisplay * display ) const
503 {
504  check();
505  int x = 0;
506  const PlotterBase * plotter = display -> display ();
507  QtView * view = m_canvas -> getViewFor ( plotter );
508  if ( view != 0 ) {
509  x = static_cast < int > ( view -> x () );
510  }
511  return x;
512 }
513 
514 int
516 getY ( QtDisplay * display ) const
517 {
518  check();
519  int y = 0;
520  const PlotterBase * plotter = display -> display ();
521  QtView * view = m_canvas -> getViewFor ( plotter );
522  if ( view != 0 ) {
523  y = static_cast < int > ( view -> y () );
524  }
525  return y;
526 }
527 
528 void
530 setX ( QtDisplay * display, double value )
531 {
532  check();
533  PyApp::lock();
534  const PlotterBase * plotter = display -> display ();
535  QtView * view = m_canvas -> getViewFor ( plotter );
536  if ( view != 0 ) {
537  view -> setX ( static_cast < int > ( value ) );
538  }
539  PyApp::unlock ();
540 }
541 
542 void
544 setY ( QtDisplay * display, double value )
545 {
546  check();
547  PyApp::lock();
548  const PlotterBase * plotter = display -> display ();
549  QtView * view = m_canvas -> getViewFor ( plotter );
550  if ( view != 0 ) {
551  view -> setY ( static_cast < int > ( value ) );
552  }
553  PyApp::unlock ();
554 }
555 
556 NTuple *
559 {
560  check();
561  PyApp::lock();
562  NTuple * nt = m_canvas->getPickTable();
563  PyApp::unlock();
564  return nt;
565 }
566 
567 NTuple *
569 getPickTable ( QtDisplay * display )
570 {
571  check();
572  PyApp::lock();
573  const PlotterBase * plotter = display->display ();
574  NTuple * nt = m_canvas->getPickTable( plotter );
575  PyApp::unlock();
576 
577  return nt;
578 }

Generated for HippoDraw Class Library by doxygen