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
00024 #if QT_VERSION < 0x040000
00025 #else
00026
00027 #include <QMouseEvent>
00028 #include <QPaintEvent>
00029 #endif
00030
00031 #include "qpainter.h"
00032
00033 #include <cassert>
00034
00035 using std::vector;
00036
00037 QtViewWidget::
00038 QtViewWidget ( QWidget * parent, const char * name, Qt::WFlags wflags )
00039 : QtViewImp ( 0 ),
00040 QWidget ( parent, name, wflags )
00041 {
00042 setGeometry ( 0, 0, 200, 200 );
00043 setPaletteBackgroundColor ( QColor ( "white" ) );
00044 setDoubleBuffering(0);
00045 }
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
00076 void QtViewWidget::paintEvent ( QPaintEvent * ev )
00077 {
00078 if ( m_plotter == 0 ) return;
00079
00080 if (double_buffer == 1)
00081 {
00082
00083 const QRect &ur = ev->rect();
00084 if ( ur.isValid() )
00085 {
00086 QwtPaintBuffer paintBuffer(this, ur);
00087 m_painter = paintBuffer.painter();
00088 drawSelf ();
00089 }
00090 }
00091 else
00092 {
00093 QPainter painter ( this );
00094 m_painter = & painter;
00095 drawSelf ();
00096 }
00097 }
00098
00099
00100 HippoRectangle QtViewWidget::getDrawRect () const
00101 {
00102 const QRect & rect = geometry ();
00103 return HippoRectangle ( 0.0, 0.0, rect.width(), rect.height() );
00104 }
00105
00106
00107 void QtViewWidget:: update ( const Observable * display )
00108 {
00109 if ( display == 0 ) return;
00110
00111 QtViewImp::update ( display );
00112
00113 QWidget::update ();
00114 }
00115
00116 void QtViewWidget::setDrawRect ( const QRect & rect )
00117 {
00118 setDrawRect ( rect.x (), rect.y (), rect.width (), rect. height () );
00119 }
00120
00121 void QtViewWidget::setDrawRect ( float x, float y, float w, float h )
00122 {
00123 int ix = static_cast < int > ( x );
00124 int iy = static_cast < int > ( y );
00125 int iw = static_cast < int > ( w );
00126 int ih = static_cast < int > ( h );
00127
00128 setGeometry ( ix, iy, iw, ih );
00129 }
00130
00131 void QtViewWidget::initPlot ( double aspect )
00132 {
00133
00134 if ( aspect > 0 )
00135 {
00136 setDrawRect ( x(),
00137 y(),
00138 width (),
00139 width () / aspect
00140 );
00141 }
00142
00143 }
00144
00145 void QtViewWidget::endPlot ()
00146 {
00147 }
00148
00149 int QtViewWidget::toViewX ( double datX ) const
00150 {
00151 return static_cast< int > ( m_plotter->userToMarginX ( datX ) );
00152 }
00153
00154 int QtViewWidget::toViewY ( double datY ) const
00155 {
00156 return static_cast< int > ( m_plotter->userToInvertedMarginY ( datY ) );
00157 }
00158
00159 void
00160 QtViewWidget::
00161 fillPickedPoint ( double x, double y,
00162 std::vector < double > & picked ) const
00163 {
00164 m_plotter -> fillPickedPoint ( x, y, picked );
00165 }
00166
00167 int QtViewWidget::toCanvasX ( double dx ) const
00168 {
00169 return static_cast < int > ( dx );
00170 }
00171
00172 int QtViewWidget::toCanvasY ( double dy ) const
00173 {
00174 return static_cast < int > ( dy );
00175 }
00176
00177 void
00178 QtViewWidget::
00179 transformAndFill ( QPointArray & array,
00180 const std::vector< double > & x,
00181 const std::vector< double > & y,
00182 int (QtViewWidget::* xfunc) ( double ) const,
00183 int (QtViewWidget::* yfunc) ( double ) const )
00184 {
00185
00186 unsigned int size = x.size();
00187 assert ( size == y.size() );
00188
00189 for ( unsigned int i = 0; i < size; i++ ) {
00190 int ix = (this->*xfunc) ( x[i] );
00191 int iy = (this->*yfunc) ( y[i] );
00192 array.setPoint ( i , ix, iy );
00193 }
00194
00195 }
00196
00197 void
00198 QtViewWidget::
00199 drawViewMethod ( const std::vector< double > & x,
00200 const std::vector< double > & y,
00201 int style,
00202 int color )
00203 {
00204 unsigned int size = x.size();
00205 assert ( size == y.size() );
00206
00207 QPointArray array ( size );
00208 transformAndFill ( array, x, y,
00209 &QtViewWidget::toCanvasX, &QtViewWidget::toCanvasY );
00210
00211 m_painter->drawLineSegments ( array );
00212 }
00213
00214 void
00215 QtViewWidget::
00216 drawMethod ( const std::vector < double > & x,
00217 const std::vector < double > & y,
00218 int style,
00219 int color )
00220 {
00221
00222 unsigned int size = x.size();
00223 assert ( size == y.size() );
00224
00225 QPointArray array ( size );
00226 transformAndFill ( array, x, y,
00227 &QtViewWidget::toViewX, &QtViewWidget::toViewY );
00228
00229 m_painter->drawPolyline ( array, 0, -1 );
00230 }
00231
00232 void
00233 QtViewWidget::
00234 setDoubleBuffering( unsigned dblbuf)
00235 {
00236 double_buffer = dblbuf;
00237 }
00238
00239 void
00240 QtViewWidget::
00241 mousePressEvent ( QMouseEvent * e )
00242 {
00243 if ( e -> state () == Qt::ControlButton ) {
00244 m_plotter -> toggleActivePlot ();
00245 e -> accept ();
00246 }
00247 }