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 
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
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 }
hippodraw::QtViewWidget class interface
Class representing a rectangle.
Definition: Rectangle.h:34
The class of derived from ViewBase for drawing as a Qt Widget.
Definition: QtViewWidget.h:37
virtual float userToInvertedMarginY(double y) const
Converts the user Y coordinate into the top-left based margin Y coordinate.
Definition: DataView.cxx:154
virtual double getAspectRatio() const
Get the aspect ratio.
Definition: ViewBase.cxx:88
Part of an implementation of the Observable-Observer pattern based on the example in the GOF Patterns...
Definition: Observable.h:39
virtual void setGeometry(int x, int y, int w, int h)
Overloads the inherit member function in order to prepare the margin rectangle.
virtual QSize sizeHint() const
Re-implements inherited member to return a reasonable size.
virtual void fillPickedPoint(double x, double y, std::vector< double > &picked) const
Fills the picked point vector.
drawPolyline(const QPointArray &a, int index=0, int npoints=-1)
setPaletteBackgroundColor(const QColor &)
height() const
x() const
y() const
geometry() const
setGeometry(int x, int y, int w, int h)
virtual ~QtViewWidget()
The required virtual destructor.
Paint buffer for Qwt widgets.
width() const
PlotterBase * m_plotter
The plotter object used by this view.
Definition: ViewBase.h:69
rect() const
virtual void mousePressEvent(QMouseEvent *e)
overrides the inherited virtual function.
x() const
y() const
rect() const
height() const
virtual void setPlotter(PlotterBase *plotter)
Sets the PlotterBase object to plotter.
Definition: ViewBase.cxx:45
Namespace for HippoDraw.
Definition: AxesType.cxx:21
int toCanvasX(double dx) const
Converts coordinate from view space to canvas space.
virtual void draw(QPainter &)
Re-implements the inherited function.
virtual void setDoubleBuffering(unsigned dblbuf)
void drawMethod(const std::vector< double > &x, const std::vector< double > &y, int opt1, int opt2)
Draws in the view using the specified method.
virtual double marginToInvertedUserY(double y) const
Converts from inverted view coordinate to data coordinate, without taking into account transforms or ...
Definition: DataView.cxx:209
void setDrawRect(const QRect &rect)
Sets the drawing rectangle in the paint device space.
void transformAndFill(QPointArray &array, const std::vector< double > &x, const std::vector< double > &y, int(QtViewWidget::*xfunc)(double) const, int(QtViewWidget::*yfunc)(double) const)
Fills the array with QPoint objects whose coordinates are in the QPaintDevice space from the vectors ...
int toViewX(double datX) const
Converts the data space coordinate X to the view space coordinate X.
virtual void update(const Observable *display)
Responds to update message from Observable.
Definition: QtViewImp.cxx:113
void prepareMarginRect()
Prepares the margin rectangle.
Definition: DataView.cxx:62
The base class for the PlotterBase hierarchy.
Definition: PlotterBase.h:55
int toCanvasY(double dy) const
Converts coordinate from view space to canvas space.
QtViewWidget(const QtViewWidget &)
The copy constructor.
setFixedSize(const QSize &s)
isValid() const
The class of derived from ViewBase for drawing to the screen and PostScript generation using Qt toolk...
Definition: QtViewImp.h:47
int toViewY(double datY) const
Converts the data space coordinate Y to the view space coordinate Y.
virtual void paintEvent(QPaintEvent *)
Overrides virtual function of QWidget to do drawing.
drawLineSegments(const QPointArray &a, int index=0, int nlines=-1)
width() const
hippodraw::PlotterBase class interface.
QPainter * m_painter
The current Qt QPainter object.
Definition: QtViewImp.h:76
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.
virtual double marginToUserX(double x) const
Converts from view coordinate to data coordinate, without taking into account transforms or scaling...
Definition: DataView.cxx:176
resizeEvent(QResizeEvent *)
size() const
DrawBorder class interface.
event(QEvent *e)
virtual void drawSelf()
Draws itself.
Definition: ViewBase.cxx:60
virtual void resizeEvent(QResizeEvent *event)
Overloads the inherit member function to set the margin rectangle.
virtual Rect getDrawRect() const
Returns the drawing Rectangle in the devices coordinate system.
void setPlotter(PlotterBase *)
Re-implements inherited member to test something for SIP.

Generated for HippoDraw Class Library by doxygen