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::
106 setShowCrossHairs ( bool yes )
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
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 }

Generated for HippoDraw Class Library by doxygen