QtView.cxx
Go to the documentation of this file.
1 
12 // inconsistent dll linkage
13 #ifdef _MSC_VER
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "QtView.h"
18 
19 #include "DrawBorder.h"
20 
21 #include "plotters/PlotterBase.h"
22 
23 
24 #include "qpainter.h"
25 #if QT_VERSION < 0x040000
26 #else
27 #include <QtGui/QPaintEngine>
28 #endif
29 
30 #include <cassert>
31 #include <iostream>
32 
33 using std::vector;
34 
35 using namespace hippodraw;
36 
38  : QtViewImp ( plotter ),
39 #if QT_VERSION < 0x040000
40  QCanvasRectangle ( 0 ),
41 #else
42  Q3CanvasRectangle ( 0 ),
43 #endif
44  m_filled ( false ),
45  m_crosshairs ( false )
46 {
47  assert ( plotter != 0 );
48  m_plotter -> addObserver ( this );
49  setEnabled ( false );
50 
51 }
52 
55 {
56  if ( m_plotter != 0 ) {
57  m_plotter -> removeObserver ( this );
58  }
59  // for SIP only
60  // delete m_plotter;
61 }
62 
65 void
67 drawWithPixmap ( QPainter & painter )
68 
69 {
70  // Before drawing set the aspect ratio.
71  const Rect rectOrg = getDrawRect ();
72 
73  int x = static_cast < int > ( rectOrg.getX () );
74  int y = static_cast < int > ( rectOrg.getY () );
75 
76 
77 
78  // Now work with the new rectangle
79  const Rect rect = getDrawRect ();
80  int width = static_cast < int > ( rect.getWidth () );
81  int height = static_cast < int > ( rect.getHeight () );
82 
83  if ( m_filled == false ||
84  isEnabled () == true ) {
85  QPainter offscrPainter;
86  ensureOffScrSize ( width, height );
87  m_pixmap.fill ( ); // with white
88  offscrPainter.begin ( & m_pixmap );
89  offscrPainter.translate ( -x, -y );
90  m_painter = & offscrPainter;
91 
92  drawSelf ();
93 
94  offscrPainter.end();
95  m_filled = true;
96  setEnabled ( false );
97  }
98 
99  m_painter = & painter;
100  painter.drawPixmap ( x, y, m_pixmap,
101  0, 0, width, height );
102 }
103 
104 void
105 QtView::
107 {
108  m_crosshairs = yes;
109 }
110 
116 void QtView::draw ( QPainter & painter )
117 
118 {
119  if ( m_plotter == 0 ) return;
120 
121  m_is_drawing = true;
122  const QColor color ( "black" );
123  painter.setPen ( color );
124 
125  QPaintDevice * dev = painter.device ();
126 #if QT_VERSION < 0x040000
127  if ( m_plotter -> wantsPixmap () &&
128  dev -> isExtDev () == false ) { // not a printer
129  drawWithPixmap ( painter );
130  }
131 #else
132  QPaintEngine * pe = dev -> paintEngine ();
133  QPaintEngine::Type type = pe-> type ();
134  if ( m_plotter -> wantsPixmap () &&
135  type != QPaintEngine::PostScript ) {
136  drawWithPixmap ( painter );
137  }
138 #endif
139  else {
140  m_painter = & painter;
141  drawSelf ();
142  }
143 
144  if ( m_crosshairs ) {
145  m_plotter -> drawCrossHairs ( this );
146  }
147 
148  if ( isSelected () )
149  {
150  DrawBorder * border = DrawBorder::instance();
151  border->setView (this);
152  border->draw();
153  }
154  m_is_drawing = false;
155 }
156 
158 void QtView::drawShape ( QPainter & painter )
159 {
160  draw ( painter );
161 }
162 
164 {
165  return Rect ( x(), y(), width(), height() );
166 }
167 
168 void
169 QtView::
170 update ( const Observable * display )
171 {
172  if ( m_is_drawing == true ) return;
173 
174  if ( display != 0 ) { // from Observable object
175  m_filled = false;
176  }
177 
178  // Update the size of the draw rectangle first. ( For aspect ratio. )
179  updateDrawRect();
180 
181 #if QT_VERSION < 0x040000
183 #else
184  Q3CanvasItem::update ();
185 #endif
186 }
187 
189 {
190  double aspect = getAspectRatio();
191  Rect drawrect = getDrawRect();
192  if ( aspect > 0 ) {
193  setDrawRect ( drawrect.getX(), drawrect.getY(),
194  drawrect.getHeight()*aspect, drawrect.getHeight() );
195  } else {
196  setDrawRect ( drawrect );
197  }
198 }
199 
200 void QtView::setDrawRect ( const QRect & rect )
201 {
202  setDrawRect ( rect.x (), rect.y (), rect.width (), rect. height () );
203 }
204 
205 void QtView::setDrawRect ( Rect & rect )
206 {
207  setDrawRect ( rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight() );
208 }
209 
210 
211 
212 void QtView::setDrawRect ( float x, float y, float w, float h )
213 {
214  setX ( x );
215  setY ( y );
216 
217  int iw = static_cast< int > ( w );
218  int ih = static_cast< int > ( h );
219  setSize ( iw, ih );
220 
222  float new_width = m_margin_rect.getX () + m_margin_rect.getWidth() + 20.;
223  iw = static_cast < int > ( new_width );
224 
225  setSize ( iw, ih );
226 
227  m_filled = false;
228 }
229 
230 int QtView::toViewX ( double datX ) const
231 {
232  if (m_plotter -> isReverse())
233  return static_cast< int > ( x() + userToInvertedMarginX ( datX ) );
234  else
235  return static_cast< int > ( x() + userToMarginX (datX ) );
236 }
237 
238 int QtView::toViewY ( double datY ) const
239 {
240  return static_cast< int > ( y() + userToInvertedMarginY ( datY ) );
241 }
242 
243 void
244 QtView::
245 fillPickedPoint ( double dx, double dy,
246  std::vector < double > & picked ) const
247 {
248  if ( m_plotter != 0 ) {
249  double xx;
250  if (m_plotter -> isReverse())
251  xx = marginToInvertedUserX ( dx - x() );
252  else
253  xx = marginToUserX ( dx - x() );
254  double yy = marginToInvertedUserY ( dy - y() );
255 
256  m_plotter -> fillPickedPointFrom ( xx, yy, picked );
257  }
258 }
259 
260 int QtView::toCanvasX ( double dx ) const
261 {
262  return static_cast < int > ( x() + dx );
263 }
264 
265 int QtView::toCanvasY ( double dy ) const
266 {
267  return static_cast < int > ( y() + dy );
268 }
269 
270 void
271 QtView::
272 #if QT_VERSION < 0x040000
273 transformAndFill ( QPointArray & array,
274 #else
275 transformAndFill ( QPolygon & array,
276 #endif
277  const std::vector< double > & x,
278  const std::vector< double > & y,
279  int (QtView::* xfunc) ( double ) const,
280  int (QtView::* yfunc) ( double ) const )
281 {
282  unsigned int size = x.size();
283  assert ( size == y.size() );
284 
285  for ( unsigned int i = 0; i < size; i++ ) {
286  int ix = (this->*xfunc) ( x[i] );
287  int iy = (this->*yfunc) ( y[i] );
288  array.setPoint ( i , ix, iy );
289  }
290 
291 }
292 
295 void QtView::drawViewMethod ( const std::vector< double > & x,
296  const std::vector< double > & y,
297  int, // style,
298  int ) //color )
299 {
300  unsigned int size = x.size();
301  assert ( size == y.size() );
302 
303 #if QT_VERSION < 0x040000
304  QPointArray array ( size );
305 #else
306  QPolygon array ( size );
307 #endif
308  transformAndFill ( array, x, y,
310 
311  m_painter->drawLineSegments ( array );
312 
313 }
314 
317 void QtView::drawMethod ( const std::vector < double > & x,
318  const std::vector < double > & y,
319  int, // style,
320  int ) //color )
321 {
322 
323  unsigned int size = x.size();
324  assert ( size == y.size() );
325 
326 #if QT_VERSION < 0x040000
327  QPointArray array ( size );
328 #else
329  QPolygon array ( size );
330 #endif
331 
332  transformAndFill ( array, x, y,
334 
335  m_painter->drawPolyline ( array, 0, -1 );
336 }
337 
338 void
339 QtView::
340 ensureOffScrSize ( int osw, int osh )
341 {
342  if ( osw > m_pixmap.width() || osh > m_pixmap.height() ) {
343  m_pixmap.resize(QMAX(osw,m_pixmap.width()),
344  QMAX(osh,m_pixmap.height()));
345  }
346  else if ( m_pixmap.width() == 0 || m_pixmap.height() == 0 ) {
347  m_pixmap.resize( QMAX( m_pixmap.width(), 1),
348  QMAX( m_pixmap.height(), 1 ) );
349  }
350 }
351 
352 void
353 QtView::
354 setPageWidth ( int upage_w )
355 {
356  m_upage_w = upage_w;
357 }
358 
359 
360 bool
361 QtView::
362 isTextView() const
363 {
364  return m_plotter->isTextPlotter();
365 }
virtual void setPageWidth(int upage_w)
Set the page width.
Definition: QtView.cxx:354
Part of an implementation of the Observable-Observer pattern based on the example in the GOF Patterns...
Definition: Observable.h:39
unsigned int i
width() const
fill(const QColor &fillColor=Qt::white)
virtual void drawSelf()
Draws itself.
Definition: ViewBase.cxx:60
double getHeight() const
A shortcut to get size.height.
Definition: Rectangle.cxx:113
drawPolyline(const QPointArray &a, int index=0, int npoints=-1)
static DrawBorder * instance()
Returns the pointer to the singleton instance.
Definition: DrawBorder.cxx:39
void draw()
Does the actual drawing in the view.
Definition: DrawBorder.cxx:57
void updateDrawRect()
Updates the drawing rectangle in the paint device space.
Definition: QtView.cxx:188
QPainter * m_painter
The current Qt QPainter object.
Definition: QtViewImp.h:76
void prepareMarginRect()
Prepares the margin rectangle.
Definition: DataView.cxx:62
setPen(const QPen &pen)
virtual void update(const Observable *)
Responds to Observable update message.
Definition: QtView.cxx:170
virtual double getAspectRatio() const
Get the aspect ratio.
Definition: ViewBase.cxx:88
x() const
y() const
bool isTextView() const
Definition: QtView.cxx:362
void drawWithPixmap(QPainter &painter)
Does the drawing with QPixmap cache instead of directly to paint device.
Definition: QtView.cxx:67
virtual float userToMarginX(double x) const
Converts the user X coordinate into the margin X coordinate.
Definition: DataView.cxx:122
void drawViewMethod(const std::vector< double > &x, const std::vector< double > &y, int opt1, int opt2)
Draws in the view using the specified method.
Definition: QtView.cxx:295
void ensureOffScrSize(int osw, int osh)
Used by the double buffering done in method draw.
Definition: QtView.cxx:340
void setView(ViewBase *view)
Sets the View object.
Definition: DrawBorder.cxx:47
else
Definition: CanvasView.cxx:879
virtual void setShowCrossHairs(bool flag)
Sets display status of cross-hairs.
Definition: QtView.cxx:106
void transformAndFill(QPointArray &array, const std::vector< double > &x, const std::vector< double > &y, int(QtView::*xfunc)(double) const, int(QtView::*yfunc)(double) const )
Fills the array with QPoint objects whose coordinates are in the QPaintDevice space from the vectors ...
Definition: QtView.cxx:273
DrawBorder class interface.
setSize(int width, int height)
QtView(PlotterBase *plotter)
The constructor.
Definition: QtView.cxx:37
double getWidth() const
A shortcut to get size.width.
Definition: Rectangle.cxx:108
bool m_filled
A flag indicating if the buffer pixmap has been filled.
Definition: QtView.h:58
return yes
Definition: CanvasView.cxx:883
QPixmap m_pixmap
A QPixmap used for buffer the plot.
Definition: QtView.h:51
The class of derived from ViewBase for drawing to the screen and PostScript generation using Qt toolk...
Definition: QtViewImp.h:47
The base class for the PlotterBase hierarchy.
Definition: PlotterBase.h:55
virtual ~QtView()
The required virtual destructor.
Definition: QtView.cxx:54
PlotterBase * m_plotter
The plotter object used by this view.
Definition: ViewBase.h:69
drawPixmap(int x, int y, const QPixmap &pixmap, int sx=0, int sy=0, int sw=-1, int sh=-1)
PyArray_TYPES type(numeric::array arr)
Definition: num_util.cpp:249
device() const
resize(int w, int h)
virtual void drawShape(QPainter &)
Overrides pure virtual function of QCanvasPolygonalItem.
Definition: QtView.cxx:158
intp size(numeric::array arr)
Definition: num_util.cpp:296
virtual float userToInvertedMarginX(double x) const
Converts the user X coordinate into the inverted X coordinate.
Definition: DataView.cxx:133
virtual bool isTextPlotter() const
Return true if it&#39;s a text plotter.
Class representing a rectangle.
Definition: Rectangle.h:34
hippodraw::QtView class interface
height() const
The class of derived from ViewBase and QCanvasRectangle for drawing on a QCanvas. ...
Definition: QtView.h:41
virtual Rect getDrawRect() const
Overrides virtual function of ViewBase.
Definition: QtView.cxx:163
A singleton class to draw the border with knobies around the selected display.
Definition: DrawBorder.h:23
drawLineSegments(const QPointArray &a, int index=0, int nlines=-1)
width() const
int toCanvasY(double dy) const
Converts coordinate from view space to canvas space.
Definition: QtView.cxx:265
virtual void draw(QPainter &)
Re-Implements the inherited function from QCanvasPoygonalItem.
Definition: QtView.cxx:116
Rect m_margin_rect
The rectangle area in which the projected points are plotted.
Definition: DataView.h:48
if(ok==false) return
int toCanvasX(double dx) const
Converts coordinate from view space to canvas space.
Definition: QtView.cxx:260
virtual void fillPickedPoint(double x, double y, std::vector< double > &picked) const
Fills the picked point vector.
Definition: QtView.cxx:245
int toViewY(double datY) const
Converts the data space coordinate Y to the view space coordinate Y.
Definition: QtView.cxx:238
double getX() const
A shortcut to get origin.X.
Definition: Rectangle.h:154
void setDrawRect(const QRect &rect)
Sets the drawing rectangle in the paint device space.
Definition: QtView.cxx:200
translate(double dx, double dy)
virtual float userToInvertedMarginY(double y) const
Converts the user Y coordinate into the top-left based margin Y coordinate.
Definition: DataView.cxx:154
void drawMethod(const std::vector< double > &x, const std::vector< double > &y, int opt1, int opt2)
Draws in the view using the specified method.
Definition: QtView.cxx:317
virtual double marginToInvertedUserX(double x) const
Converts from view coordinate to data conordiate, take into account the inversion of x axes...
Definition: DataView.cxx:187
int m_upage_w
Page width.
Definition: QtView.h:54
begin(const QPaintDevice *pd, bool unclipped=FALSE)
int toViewX(double datX) const
Converts the data space coordinate X to the view space coordinate X.
Definition: QtView.cxx:230
virtual double marginToUserX(double x) const
Converts from view coordinate to data coordinate, without taking into account transforms or scaling...
Definition: DataView.cxx:176
bool m_crosshairs
A flag indicating if cross-hairs should be drawn.
Definition: QtView.h:62
virtual double marginToInvertedUserY(double y) const
Converts from inverted view coordinate to data coordinate, without taking into account transforms or ...
Definition: DataView.cxx:209
bool m_is_drawing
A flag set true when drawing has stared.
Definition: QtView.h:66
Type
Axes constants.
Definition: AxesType.h:31
double getY() const
A shortcut to get origin.Y.
Definition: Rectangle.h:162
hippodraw::PlotterBase class interface.

Generated for HippoDraw Class Library by doxygen