QtViewWidget.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 "QtViewWidget.h"
18 
19 #include "DrawBorder.h"
20 #include "qwt_paint_buffer.h"
21 
22 #include "plotters/PlotterBase.h"
23 #include "qpainter.h"
24 
25 #if QT_VERSION < 0x040000
26 #else
27 //Added by the Qt porting tool:
28 #include <QtGui/QMouseEvent>
29 #include <QtGui/QPaintEvent>
30 #endif
31 
32 
33 #include <cassert>
34 
35 using std::vector;
36 
37 using namespace hippodraw;
38 
40 QtViewWidget ( QWidget * parent, const char * name, Qt::WFlags wflags )
41  : QtViewImp ( 0 ), // sets m_plotter to 0
42  QWidget ( parent, name, wflags )
43 {
44  setPaletteBackgroundColor ( QColor ( "white" ) );
46 }
47 
49 {
50 }
51 
52 /* virtual */
53 QSize
55 sizeHint () const
56 {
57  return size();
58 }
59 
60 void
62 setPlotter ( PlotterBase * plotter )
63 {
64  ViewBase::setPlotter ( plotter );
65  plotter -> addObserver ( this );
66 }
67 
68 void QtViewWidget::draw ( QPainter & painter )
69 {
70  m_painter = & painter;
71 
72  drawSelf ();
73 }
74 
75 void
78 {
79 
80  if ( m_plotter == 0 ) return;
81 
83 
84  if (double_buffer == 1)
85  {
86  // Use double-buffering
87  const QRect &ur = ev->rect();
88  if ( ur.isValid() )
89  {
90 
91  QwtPaintBuffer paintBuffer(this, ur);
92  m_painter = paintBuffer.painter();
93  drawSelf ();
94  }
95  }
96  else
97  {
98  QPainter painter ( this );
99  m_painter = & painter;
100  drawSelf ();
101  }
102 }
103 
104 Rect
106 getDrawRect () const
107 {
108  const QRect & rect = geometry ();
109  return Rect ( 0.0, 0.0, rect.width(), rect.height() );
110 }
111 
112 void QtViewWidget:: update ( const Observable * display )
113 {
114  if ( display == 0 ) return;
115 
116  // Adjust the width if the aspect ratio needs changing.
117  if ( m_plotter != 0 ) {
118  double ratio = m_plotter -> getAspectRatio ();
119  if ( ratio != 0. ) {
120  double w = width ();
121  double h = height ();
122  double diff = ratio - w / h;
123  if ( diff > 0.00001 ) {
124  int new_h = height();
125  int new_w = static_cast < int > ( w * ratio );
126  setFixedSize ( new_w, new_h );
128  }
129  }
130  }
131  QtViewImp::update ( display );
132 
133  QWidget::update ();
134 }
135 
136 void QtViewWidget::setDrawRect ( const QRect & rect )
137 {
138  setDrawRect ( rect.x (), rect.y (), rect.width (), rect. height () );
139 }
140 
141 void QtViewWidget::setDrawRect ( float x, float y, float w, float h )
142 {
143  int ix = static_cast < int > ( x );
144  int iy = static_cast < int > ( y );
145  int iw = static_cast < int > ( w );
146  int ih = static_cast < int > ( h );
147 
148  setGeometry ( ix, iy, iw, ih );
149 }
150 
151 void
153 setGeometry ( int x, int y, int w, int h )
154 {
155  QWidget::setGeometry ( x, y, w, h );
156  if ( m_plotter != 0 ) {
158  }
159 }
160 
161 void
163 setGeometry ( const QRect & rect )
164 {
165  setGeometry ( rect.x(), rect.y(), rect.width(), rect.height() );
166 }
167 
168 
169 void
172 {
173  QWidget::resizeEvent ( event );
174  if ( m_plotter != 0 ) {
176  }
177 }
178 
179 int QtViewWidget::toViewX ( double datX ) const
180 {
181  return static_cast< int > ( userToMarginX ( datX ) );
182 }
183 
184 int QtViewWidget::toViewY ( double datY ) const
185 {
186  return static_cast< int > ( userToInvertedMarginY ( datY ) );
187 }
188 
189 void
191 fillPickedPoint ( double x, double y,
192  std::vector < double > & picked ) const
193 {
194  double xx = marginToUserX ( x );
195  double yy = marginToInvertedUserY ( y );
196 
197  m_plotter -> fillPickedPointFrom ( xx, yy, picked );
198 }
199 
200 int QtViewWidget::toCanvasX ( double dx ) const
201 {
202  return static_cast < int > ( dx );
203 }
204 
205 int QtViewWidget::toCanvasY ( double dy ) const
206 {
207  return static_cast < int > ( dy );
208 }
209 
210 void
211 QtViewWidget::
212 #if QT_VERSION < 0x040000
213 transformAndFill ( QPointArray & array,
214 #else
215 transformAndFill ( QPolygon & array,
216 #endif
217  const std::vector< double > & x,
218  const std::vector< double > & y,
219  int (QtViewWidget::* xfunc) ( double ) const,
220  int (QtViewWidget::* yfunc) ( double ) const )
221 {
222 
223  unsigned int size = x.size();
224  assert ( size == y.size() );
225 
226  for ( unsigned int i = 0; i < size; i++ ) {
227  int ix = (this->*xfunc) ( x[i] );
228  int iy = (this->*yfunc) ( y[i] );
229  array.setPoint ( i , ix, iy );
230  }
231 
232 }
233 
236 void
238 drawViewMethod ( const std::vector< double > & x,
239  const std::vector< double > & y,
240  int, // style,
241  int ) // color )
242 {
243  unsigned int size = x.size();
244  assert ( size == y.size() );
245 
246 #if QT_VERSION < 0x040000
247  QPointArray array ( size );
248 #else
249  QPolygon array ( size );
250 #endif
251  transformAndFill ( array, x, y,
253 
254  m_painter->drawLineSegments ( array );
255 }
256 
259 void
261 drawMethod ( const std::vector < double > & x,
262  const std::vector < double > & y,
263  int, // style,
264  int ) //color )
265 {
266 
267  unsigned int size = x.size();
268  assert ( size == y.size() );
269 
270 #if QT_VERSION < 0x040000
271  QPointArray array ( size );
272 #else
273  QPolygon array ( size );
274 #endif
275  transformAndFill ( array, x, y,
277 
278  m_painter->drawPolyline ( array, 0, -1 );
279 }
280 
281 void
283 setDoubleBuffering( unsigned dblbuf)
284 {
285  double_buffer = dblbuf;
286 }
287 
288 void
291 {
292  if ( e -> state () == Qt::ControlButton ) {
293  m_plotter -> toggleActivePlot ();
294  e -> accept ();
295  }
296 }

Generated for HippoDraw Class Library by doxygen