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 }
void clear()
Removes all items from the CanvasWindow.
Definition: PyCanvas.cxx:431
unsigned int i
void removeDisplay(PlotterBase *plotter)
Removes the plotter and its view from the canvas.
void selectDisplay(QtDisplay *display)
Select a specific display.
Definition: PyCanvas.cxx:207
hippodraw::CanvasWindow class interface.
static bool hasPendingEvents()
Returns true if the application object has pending events.
Definition: PyApp.cxx:370
A singleton class that is the interface between GUI and the displays.
double high() const
Returns the maximum of the range object.
Definition: Range.cxx:100
Qt Displays wraps a derived class of Cut1DPlotter.
Definition: QtCut.h:42
void addText(QtDisplay *display, const std::string &text)
Add a BoxTextRep.
Definition: PyCanvas.cxx:322
double getHeight() const
A shortcut to get size.height.
Definition: Rectangle.cxx:113
A transform that transforms coordinates from one coordinate system to another.
Definition: TransformBase.h:35
A Proxy for the CanvasView class.
hippodraw::QtCut class interface
This class implements additional FigureEditor functionality particular to QtView objects.
Definition: CanvasView.h:96
virtual void transform(double &x, double &y) const =0
Transform the coordinates on the X and Y axes.
bool m_has_gui
Set to true if this canvas as associated GUI CanvasWindow.
Definition: PyCanvas.h:61
static FunctionController * instance()
Returns the pointer to the singleton instance.
int getY(QtDisplay *display) const
Returns the view&#39;s Y coordinate for the display.
Definition: PyCanvas.cxx:516
const std::vector< double > & mouseData()
Retrieve a tuple of (x, y, z) points from the next mouse event.
Definition: PyCanvas.cxx:401
static void lock()
Obtains a lock on the application&#39;s mutex.
Definition: PyApp.cxx:331
PlotterBase * selectedPlotter()
Returns the selected plotter.
void saveAs(const std::string &filename)
Saves the document to the specified file.
hippodraw::FunctionController class interface
A singleton class is the interface between an application and the list of FunctionRep objects contain...
int getHeight(QtDisplay *display) const
Returns the view&#39;s height for the display.
Definition: PyCanvas.cxx:440
void setHeight(QtDisplay *, double h)
Sets the height of the view for the display.
Definition: PyCanvas.cxx:469
NTuple * getPickTable() const
Gets the pick table the selected plotter.
hippodraw::CanvasView class interface
void addTextDisplay(PlotterBase *plotter, const std::string &type)
Adds a textual display to the canvas of type type.
CanvasWindow * m_canvas
The actual canvas window in the application thread.
Definition: PyCanvas.h:51
void close()
Closes the canvas window.
Definition: PyCanvas.cxx:94
double getWidth() const
A shortcut to get size.width.
Definition: Rectangle.cxx:108
void swapOrientation()
Swaps the orientation from portrait to landscape and vice verse.
Definition: PyCanvas.cxx:420
void addTextAt(QtDisplay *display, const std::string &text, double xrel, double yrel)
Add a BoxTextRep at a specific location in the selected display item&#39;s canvas coordinate system...
Definition: PyCanvas.cxx:342
void saveSelectedImages(const std::string &filename)
Save the selected images as an image file.
Definition: PyCanvas.cxx:250
void addTextRep(QtDisplay *display, const std::string &type)
Adds a textual data representation to display or type type.
Definition: PyCanvas.cxx:283
const std::vector< QtDisplay * > & getDisplays() const
Returns all displays on the canvas.
Definition: PyCanvas.cxx:160
hippodraw::NTuple class interface.
The base class for the PlotterBase hierarchy.
Definition: PlotterBase.h:55
void setY(QtDisplay *display, double value)
Sets the view&#39;s Y coordinate for the display.
Definition: PyCanvas.cxx:544
PlotterBase * getPlotter() const
Returns the plotter used by this view.
Definition: ViewBase.cxx:50
std::pair< double, double > addTextDisplayAt(PlotterBase *plotter, const std::string &type, const std::string &text, double xrel, double yrel)
Adds a text display at the relative position (xrel, yrel) in the selected item&#39;s coordinates.
PyArray_TYPES type(numeric::array arr)
Definition: num_util.cpp:249
CanvasViewProxy * m_canvas_proxy
The proxy for the CanvasView object.
Definition: PyCanvas.h:57
virtual void setActivePlot(int index, bool redraw)
Sets the active plot.
hippodraw::BinaryTransform class interface
hippodraw::PyApp class interface.
void saveAs(const std::string &filename) const
Saves all the QtView canvas item to a XML file.
A PlotterBase derived class that serves a base class for cut plotters.
Definition: CutPlotter.h:43
void check() const
Checks if the CanvasWindow has been closed and throws a runtime_error if it has been.
Definition: PyCanvas.cxx:73
void setX(QtDisplay *display, double value)
Sets the view&#39;s X coordinate for the display.
Definition: PyCanvas.cxx:530
intp size(numeric::array arr)
Definition: num_util.cpp:296
const std::vector< const ViewBase * > & views()
Returns the list of ViewBase objects on the canvas.
void setSelected(QtView *view)
Select a specific view in the window.
void addDisplay(QtDisplay *display_wrap)
Adds the display to the canvas.
Definition: PyCanvas.cxx:106
hippodraw::CanvasViewProxy class interface.
Class representing a rectangle.
Definition: Rectangle.h:34
hippodraw::QtView class interface
FactoryException class interface.
The class of derived from ViewBase and QCanvasRectangle for drawing on a QCanvas. ...
Definition: QtView.h:41
void saveAsImage(QtDisplay *display, const std::string &filename)
Save the display as an image file.
Definition: PyCanvas.cxx:230
static DisplayController * instance()
Returns the pointer to the singleton instance.
void setWidth(QtDisplay *, double w)
Sets the width of the view for the display.
Definition: PyCanvas.cxx:485
const std::vector< std::string > & getTextRepTypes() const
Returns the types of textual data representations available.
Definition: PyCanvas.cxx:314
void saveAs(const std::string &filename)
Saves the document to the specified file.
Definition: PyCanvas.cxx:120
PlotterBase * display()
Returns the wrapped display object.
Definition: QtDisplay.cxx:724
A DataSource class implemented with std::vector&lt;double&gt; to store the column data. ...
Definition: NTuple.h:33
void addTextAtAbs(QtDisplay *display, const std::string &text, double xabs, double yabs)
Add a BoxTextRep at a specific location in the selected display item&#39;s canvas coordinate system...
Definition: PyCanvas.cxx:362
The abstract base class for views.
Definition: ViewBase.h:62
void removeDisplay(QtDisplay *display)
Remove a display.
Definition: PyCanvas.cxx:274
void setPlotMatrix(unsigned int columns, unsigned int rows)
Sets the number for columns and rows of plots on each page.
Definition: PyCanvas.cxx:409
Qt Displays wraps a derived class of PlotterBase.
Definition: QtDisplay.h:71
int getWidth(QtDisplay *display) const
Returns the view&#39;s width for the display.
Definition: PyCanvas.cxx:454
void selectAllDisplays(bool flag=true)
Select or un-select all the displays on the canvas.
Definition: PyCanvas.cxx:199
double getX() const
A shortcut to get origin.X.
Definition: Rectangle.h:154
A transform that transforms coordinates from one 2D coordinate system to another. ...
hippodraw::Range class interface
std::vector< QtDisplay * > m_displays
Pointers to QtDisplay objects on the current canvas.
Definition: PyCanvas.h:64
static void unlock()
Releases the lock on the application&#39;s mutex.
Definition: PyApp.cxx:357
virtual TransformBase * getTransform() const
Returns the transform object used by the plotter.
NTuple * getPickTable(QtDisplay *)
Gets the PickTable for the display.
Definition: PyCanvas.cxx:569
virtual void fileSaveSelectedImages()
Saves the selected ViewBase object as an image file.
double low() const
Returns the minimum of the range object.
Definition: Range.cxx:87
PyCanvas()
The default constructor.
Definition: PyCanvas.cxx:57
const std::vector< double > & mouseEventData()
Retrieve a vector (x, y, z) points from the next mouse event.
Expresses a range of values.
Definition: Range.h:33
Cut1DPlotter class interface.
An exception class that is thrown when the factory fails to find the request class by its name...
QtDisplay * getDisplay()
Returns the selected display on the canvas.
Definition: PyCanvas.cxx:142
NTuple * getSelPickTable()
Gets the PickTable of selected display as a NTuple.
Definition: PyCanvas.cxx:558
QtView * findSelectedView(QtDisplay *display)
Return the QtView associated with the given QtDisplay.
Definition: PyCanvas.cxx:266
A concrete window class that contains the canvas and responds to menu item and tool bar events from t...
Definition: CanvasWindow.h:106
DisplayController class interface declaration.
int getX(QtDisplay *display) const
Returns the view&#39;s X coordinate for the display.
Definition: PyCanvas.cxx:502
void show()
Displays the canvas window on the screen.
Definition: PyCanvas.cxx:84
hippodraw::PyCanvas class interface.
double getY() const
A shortcut to get origin.Y.
Definition: Rectangle.h:162
QtCut * getCut()
Returns a QtCut object or null pointer if its not a QtCut object.
Definition: PyCanvas.cxx:182
void print(const std::string &filename)
Prints the canvas to a PostScript file.
Definition: PyCanvas.cxx:219

Generated for HippoDraw Class Library by doxygen