00001
00012
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016
00017 #include "QtViewWidget.h"
00018
00019 #include "DrawBorder.h"
00020 #include "qwt_paint_buffer.h"
00021
00022 #include "plotters/PlotterBase.h"
00023 #include "qpainter.h"
00024
00025 #if QT_VERSION < 0x040000
00026 #else
00027
00028 #include <QtGui/QMouseEvent>
00029 #include <QtGui/QPaintEvent>
00030 #endif
00031
00032
00033 #include <cassert>
00034
00035 using std::vector;
00036
00037 using namespace hippodraw;
00038
00039 QtViewWidget::
00040 QtViewWidget ( QWidget * parent, const char * name, Qt::WFlags wflags )
00041 : QtViewImp ( 0 ),
00042 QWidget ( parent, name, wflags )
00043 {
00044 setPaletteBackgroundColor ( QColor ( "white" ) );
00045 setDoubleBuffering(0);
00046 }
00047
00048 QtViewWidget::~QtViewWidget ()
00049 {
00050 }
00051
00052
00053 QSize
00054 QtViewWidget::
00055 sizeHint () const
00056 {
00057 return size();
00058 }
00059
00060 void
00061 QtViewWidget::
00062 setPlotter ( PlotterBase * plotter )
00063 {
00064 ViewBase::setPlotter ( plotter );
00065 plotter -> addObserver ( this );
00066 }
00067
00068 void QtViewWidget::draw ( QPainter & painter )
00069 {
00070 m_painter = & painter;
00071
00072 drawSelf ();
00073 }
00074
00075 void
00076 QtViewWidget::
00077 paintEvent ( QPaintEvent * ev )
00078 {
00079
00080 if ( m_plotter == 0 ) return;
00081
00082 prepareMarginRect ();
00083
00084 if (double_buffer == 1)
00085 {
00086
00087 const QRect &ur = ev->rect();
00088 if ( ur.isValid() )
00089 {
00090
00091 QwtPaintBuffer paintBuffer(this, ur);
00092 m_painter = paintBuffer.painter();
00093 drawSelf ();
00094 }
00095 }
00096 else
00097 {
00098 QPainter painter ( this );
00099 m_painter = & painter;
00100 drawSelf ();
00101 }
00102 }
00103
00104 Rect
00105 QtViewWidget::
00106 getDrawRect () const
00107 {
00108 const QRect & rect = geometry ();
00109 return Rect ( 0.0, 0.0, rect.width(), rect.height() );
00110 }
00111
00112 void QtViewWidget:: update ( const Observable * display )
00113 {
00114 if ( display == 0 ) return;
00115
00116
00117 if ( m_plotter != 0 ) {
00118 double ratio = m_plotter -> getAspectRatio ();
00119 if ( ratio != 0. ) {
00120 double w = width ();
00121 double h = height ();
00122 double diff = ratio - w / h;
00123 if ( diff > 0.00001 ) {
00124 int new_h = height();
00125 int new_w = static_cast < int > ( w * ratio );
00126 setFixedSize ( new_w, new_h );
00127 prepareMarginRect ();
00128 }
00129 }
00130 }
00131 QtViewImp::update ( display );
00132
00133 QWidget::update ();
00134 }
00135
00136 void QtViewWidget::setDrawRect ( const QRect & rect )
00137 {
00138 setDrawRect ( rect.x (), rect.y (), rect.width (), rect. height () );
00139 }
00140
00141 void QtViewWidget::setDrawRect ( float x, float y, float w, float h )
00142 {
00143 int ix = static_cast < int > ( x );
00144 int iy = static_cast < int > ( y );
00145 int iw = static_cast < int > ( w );
00146 int ih = static_cast < int > ( h );
00147
00148 setGeometry ( ix, iy, iw, ih );
00149 }
00150
00151 void
00152 QtViewWidget::
00153 setGeometry ( int x, int y, int w, int h )
00154 {
00155 QWidget::setGeometry ( x, y, w, h );
00156 if ( m_plotter != 0 ) {
00157 prepareMarginRect ();
00158 }
00159 }
00160
00161 void
00162 QtViewWidget::
00163 setGeometry ( const QRect & rect )
00164 {
00165 setGeometry ( rect.x(), rect.y(), rect.width(), rect.height() );
00166 }
00167
00168
00169 void
00170 QtViewWidget::
00171 resizeEvent ( QResizeEvent * event )
00172 {
00173 QWidget::resizeEvent ( event );
00174 if ( m_plotter != 0 ) {
00175 prepareMarginRect ();
00176 }
00177 }
00178
00179 int QtViewWidget::toViewX ( double datX ) const
00180 {
00181 return static_cast< int > ( userToMarginX ( datX ) );
00182 }
00183
00184 int QtViewWidget::toViewY ( double datY ) const
00185 {
00186 return static_cast< int > ( userToInvertedMarginY ( datY ) );
00187 }
00188
00189 void
00190 QtViewWidget::
00191 fillPickedPoint ( double x, double y,
00192 std::vector < double > & picked ) const
00193 {
00194 double xx = marginToUserX ( x );
00195 double yy = marginToInvertedUserY ( y );
00196
00197 m_plotter -> fillPickedPointFrom ( xx, yy, picked );
00198 }
00199
00200 int QtViewWidget::toCanvasX ( double dx ) const
00201 {
00202 return static_cast < int > ( dx );
00203 }
00204
00205 int QtViewWidget::toCanvasY ( double dy ) const
00206 {
00207 return static_cast < int > ( dy );
00208 }
00209
00210 void
00211 QtViewWidget::
00212 #if QT_VERSION < 0x040000
00213 transformAndFill ( QPointArray & array,
00214 #else
00215 transformAndFill ( QPolygon & array,
00216 #endif
00217 const std::vector< double > & x,
00218 const std::vector< double > & y,
00219 int (QtViewWidget::* xfunc) ( double ) const,
00220 int (QtViewWidget::* yfunc) ( double ) const )
00221 {
00222
00223 unsigned int size = x.size();
00224 assert ( size == y.size() );
00225
00226 for ( unsigned int i = 0; i < size; i++ ) {
00227 int ix = (this->*xfunc) ( x[i] );
00228 int iy = (this->*yfunc) ( y[i] );
00229 array.setPoint ( i , ix, iy );
00230 }
00231
00232 }
00233
00236 void
00237 QtViewWidget::
00238 drawViewMethod ( const std::vector< double > & x,
00239 const std::vector< double > & y,
00240 int,
00241 int )
00242 {
00243 unsigned int size = x.size();
00244 assert ( size == y.size() );
00245
00246 #if QT_VERSION < 0x040000
00247 QPointArray array ( size );
00248 #else
00249 QPolygon array ( size );
00250 #endif
00251 transformAndFill ( array, x, y,
00252 &QtViewWidget::toCanvasX, &QtViewWidget::toCanvasY );
00253
00254 m_painter->drawLineSegments ( array );
00255 }
00256
00259 void
00260 QtViewWidget::
00261 drawMethod ( const std::vector < double > & x,
00262 const std::vector < double > & y,
00263 int,
00264 int )
00265 {
00266
00267 unsigned int size = x.size();
00268 assert ( size == y.size() );
00269
00270 #if QT_VERSION < 0x040000
00271 QPointArray array ( size );
00272 #else
00273 QPolygon array ( size );
00274 #endif
00275 transformAndFill ( array, x, y,
00276 &QtViewWidget::toViewX, &QtViewWidget::toViewY );
00277
00278 m_painter->drawPolyline ( array, 0, -1 );
00279 }
00280
00281 void
00282 QtViewWidget::
00283 setDoubleBuffering( unsigned dblbuf)
00284 {
00285 double_buffer = dblbuf;
00286 }
00287
00288 void
00289 QtViewWidget::
00290 mousePressEvent ( QMouseEvent * e )
00291 {
00292 if ( e -> state () == Qt::ControlButton ) {
00293 m_plotter -> toggleActivePlot ();
00294 e -> accept ();
00295 }
00296 }