00001
00012
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016
00017 #include "QtView.h"
00018
00019 #include "DrawBorder.h"
00020
00021 #include "plotters/PlotterBase.h"
00022
00023 #include "qpainter.h"
00024
00025 #include <cassert>
00026
00027 using std::vector;
00028
00029 QtView::QtView ( PlotterBase * plotter )
00030 : QtViewImp ( plotter ),
00031 #if QT_VERSION < 0x040000
00032 QCanvasRectangle ( 0 ),
00033 #else
00034 Q3CanvasRectangle ( 0 ),
00035 #endif
00036 m_filled ( false ),
00037 m_crosshairs ( false )
00038 {
00039 assert ( plotter != 0 );
00040
00041 m_plotter -> addObserver ( this );
00042 setEnabled ( false );
00043 }
00044
00045 QtView::
00046 ~QtView ()
00047 {
00048 m_plotter -> removeObserver ( this );
00049
00050 delete m_plotter;
00051 }
00052
00053 void
00054 QtView::
00055 drawWithPixmap ( QPainter & painter )
00056 {
00057
00058 double transform_aspect_ratio = getAspectRatio();
00059 const HippoRectangle rectOrg = getDrawRect ();
00060
00061 int w = static_cast < int > ( rectOrg.getWidth() );
00062 int x = static_cast < int > ( rectOrg.getX () );
00063 int y = static_cast < int > ( rectOrg.getY () );
00064
00065 if( transform_aspect_ratio > 0.0 )
00066 {
00067 double wth = w;
00068 double hgt = wth / transform_aspect_ratio;
00069 setDrawRect( x, y, wth, hgt );
00070 }
00071
00072
00073 const HippoRectangle rect = getDrawRect ();
00074 int width = static_cast < int > ( rect.getWidth () );
00075 int height = static_cast < int > ( rect.getHeight () );
00076
00077 if ( m_filled == false ||
00078 isEnabled () == true ) {
00079 QPainter offscrPainter;
00080 ensureOffScrSize ( width, height );
00081 m_pixmap.fill ( );
00082 offscrPainter.begin ( & m_pixmap );
00083 offscrPainter.translate ( -x, -y );
00084 m_painter = & offscrPainter;
00085 drawSelf ();
00086 offscrPainter.end();
00087 m_filled = true;
00088 setEnabled ( false );
00089 }
00090
00091 m_painter = & painter;
00092 painter.drawPixmap ( x, y, m_pixmap,
00093 0, 0, width, height );
00094 }
00095
00099 void
00100 QtView::
00101 setShowCrossHairs ( bool yes )
00102 {
00103 m_crosshairs = yes;
00104 }
00105
00111 void QtView::draw ( QPainter & painter )
00112 {
00113 m_is_drawing = true;
00114 const QColor color ( "black" );
00115 painter.setPen ( color );
00116
00117 QPaintDevice * dev = painter.device ();
00118 if ( m_plotter -> wantsPixmap () &&
00119 dev -> isExtDev () == false ) {
00120 drawWithPixmap ( painter );
00121 }
00122 else {
00123 m_painter = & painter;
00124 drawSelf ();
00125 }
00126
00127 if ( m_crosshairs ) {
00128 m_plotter -> drawCrossHairs ( this );
00129 }
00130
00131 if ( isSelected () )
00132 {
00133 DrawBorder * border = DrawBorder::instance();
00134 border->setView (this);
00135 border->draw();
00136 }
00137 m_is_drawing = false;
00138 }
00139
00141 void QtView::drawShape ( QPainter & painter )
00142 {
00143 draw ( painter );
00144 }
00145
00146 HippoRectangle QtView::getDrawRect () const
00147 {
00148 return HippoRectangle ( x(), y(), width(), height() );
00149 }
00150
00151 void
00152 QtView::
00153 update ( const Observable * display )
00154 {
00155 if ( m_is_drawing == true ) return;
00156
00157 if ( display != 0 ) {
00158 m_filled = false;
00159 }
00160 #if QT_VERSION < 0x040000
00161 QCanvasItem::update ();
00162 #else
00163 Q3CanvasItem::update ();
00164 #endif
00165 }
00166
00167 void QtView::setDrawRect ( const QRect & rect )
00168 {
00169 setDrawRect ( rect.x (), rect.y (), rect.width (), rect. height () );
00170 }
00171
00172 void QtView::setDrawRect ( float x, float y, float w, float h )
00173 {
00174 setX ( x );
00175 setY ( y );
00176 int iw = static_cast< int > ( w );
00177 int ih = static_cast< int > ( h );
00178
00179 setSize ( iw, ih );
00180 m_filled = false;
00181 }
00182
00183 void QtView::initPlot( double transform_aspect_ratio )
00184 {
00185 const HippoRectangle draw = getDrawRect();
00186 double w = draw.getWidth();
00187 double h = draw.getHeight();
00188 double draw_aspect_ratio = w / h;
00189
00190
00191 if( transform_aspect_ratio > 0.0 )
00192 if( !( 0.999 * transform_aspect_ratio < draw_aspect_ratio ) ||
00193 !( draw_aspect_ratio < 1.001 * transform_aspect_ratio ) )
00194 setDrawRect( x(), y(), width (), width () / transform_aspect_ratio );
00195
00196 }
00197
00198 void QtView::endPlot ()
00199 {
00200 }
00201
00202 int QtView::toViewX ( double datX ) const
00203 {
00204 return static_cast< int > ( x() + m_plotter->userToMarginX ( datX ) );
00205 }
00206
00207 int QtView::toViewY ( double datY ) const
00208 {
00209 return static_cast< int > ( y() +
00210 m_plotter->userToInvertedMarginY ( datY ) );
00211 }
00212
00213 void
00214 QtView::
00215 fillPickedPoint ( double dx, double dy,
00216 std::vector < double > & picked ) const
00217 {
00218 m_plotter -> fillPickedPoint ( dx - x(), dy - y(), picked );
00219 }
00220
00221 int QtView::toCanvasX ( double dx ) const
00222 {
00223 return static_cast < int > ( x() + dx );
00224 }
00225
00226 int QtView::toCanvasY ( double dy ) const
00227 {
00228 return static_cast < int > ( y() + dy );
00229 }
00230
00231 void
00232 QtView::
00233 transformAndFill ( QPointArray & array,
00234 const std::vector< double > & x,
00235 const std::vector< double > & y,
00236 int (QtView::* xfunc) ( double ) const,
00237 int (QtView::* yfunc) ( double ) const )
00238 {
00239 unsigned int size = x.size();
00240 assert ( size == y.size() );
00241
00242 for ( unsigned int i = 0; i < size; i++ ) {
00243 int ix = (this->*xfunc) ( x[i] );
00244 int iy = (this->*yfunc) ( y[i] );
00245 array.setPoint ( i , ix, iy );
00246 }
00247
00248 }
00249
00250 void QtView::drawViewMethod ( const std::vector< double > & x,
00251 const std::vector< double > & y,
00252 int style,
00253 int color )
00254 {
00255 unsigned int size = x.size();
00256 assert ( size == y.size() );
00257
00258 QPointArray array ( size );
00259 transformAndFill ( array, x, y,
00260 &QtView::toCanvasX, &QtView::toCanvasY );
00261
00262 m_painter->drawLineSegments ( array );
00263
00264 }
00265
00266
00267 void QtView::drawMethod ( const std::vector < double > & x,
00268 const std::vector < double > & y,
00269 int style,
00270 int color )
00271 {
00272
00273 unsigned int size = x.size();
00274 assert ( size == y.size() );
00275 QPointArray array ( size );
00276 transformAndFill ( array, x, y,
00277 &QtView::toViewX, &QtView::toViewY );
00278
00279 m_painter->drawPolyline ( array, 0, -1 );
00280 }
00281
00282 void
00283 QtView::
00284 ensureOffScrSize ( int osw, int osh )
00285 {
00286 if ( osw > m_pixmap.width() || osh > m_pixmap.height() ) {
00287 m_pixmap.resize(QMAX(osw,m_pixmap.width()),
00288 QMAX(osh,m_pixmap.height()));
00289 }
00290 else if ( m_pixmap.width() == 0 || m_pixmap.height() == 0 ) {
00291 m_pixmap.resize( QMAX( m_pixmap.width(), 1),
00292 QMAX( m_pixmap.height(), 1 ) );
00293 }
00294 }
00295
00296 void
00297 QtView::
00298 setPageWidth ( int upage_w )
00299 {
00300 m_upage_w = upage_w;
00301 }