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
00024 #include "qpainter.h"
00025 #if QT_VERSION < 0x040000
00026 #else
00027 #include <QtGui/QPaintEngine>
00028 #endif
00029
00030 #include <cassert>
00031 #include <iostream>
00032
00033 using std::vector;
00034
00035 using namespace hippodraw;
00036
00037 QtView::QtView ( PlotterBase * plotter )
00038 : QtViewImp ( plotter ),
00039 #if QT_VERSION < 0x040000
00040 QCanvasRectangle ( 0 ),
00041 #else
00042 Q3CanvasRectangle ( 0 ),
00043 #endif
00044 m_filled ( false ),
00045 m_crosshairs ( false )
00046 {
00047 assert ( plotter != 0 );
00048 m_plotter -> addObserver ( this );
00049 setEnabled ( false );
00050
00051 }
00052
00053 QtView::
00054 ~QtView ()
00055 {
00056 if ( m_plotter != 0 ) {
00057 m_plotter -> removeObserver ( this );
00058 }
00059
00060
00061 }
00062
00065 void
00066 QtView::
00067 drawWithPixmap ( QPainter & painter )
00068
00069 {
00070
00071 const Rect rectOrg = getDrawRect ();
00072
00073 int x = static_cast < int > ( rectOrg.getX () );
00074 int y = static_cast < int > ( rectOrg.getY () );
00075
00076
00077
00078
00079 const Rect rect = getDrawRect ();
00080 int width = static_cast < int > ( rect.getWidth () );
00081 int height = static_cast < int > ( rect.getHeight () );
00082
00083 if ( m_filled == false ||
00084 isEnabled () == true ) {
00085 QPainter offscrPainter;
00086 ensureOffScrSize ( width, height );
00087 m_pixmap.fill ( );
00088 offscrPainter.begin ( & m_pixmap );
00089 offscrPainter.translate ( -x, -y );
00090 m_painter = & offscrPainter;
00091
00092 drawSelf ();
00093
00094 offscrPainter.end();
00095 m_filled = true;
00096 setEnabled ( false );
00097 }
00098
00099 m_painter = & painter;
00100 painter.drawPixmap ( x, y, m_pixmap,
00101 0, 0, width, height );
00102 }
00103
00104 void
00105 QtView::
00106 setShowCrossHairs ( bool yes )
00107 {
00108 m_crosshairs = yes;
00109 }
00110
00116 void QtView::draw ( QPainter & painter )
00117
00118 {
00119 if ( m_plotter == 0 ) return;
00120
00121 m_is_drawing = true;
00122 const QColor color ( "black" );
00123 painter.setPen ( color );
00124
00125 QPaintDevice * dev = painter.device ();
00126 #if QT_VERSION < 0x040000
00127 if ( m_plotter -> wantsPixmap () &&
00128 dev -> isExtDev () == false ) {
00129 drawWithPixmap ( painter );
00130 }
00131 #else
00132 QPaintEngine * pe = dev -> paintEngine ();
00133 QPaintEngine::Type type = pe-> type ();
00134 if ( m_plotter -> wantsPixmap () &&
00135 type != QPaintEngine::PostScript ) {
00136 drawWithPixmap ( painter );
00137 }
00138 #endif
00139 else {
00140 m_painter = & painter;
00141 drawSelf ();
00142 }
00143
00144 if ( m_crosshairs ) {
00145 m_plotter -> drawCrossHairs ( this );
00146 }
00147
00148 if ( isSelected () )
00149 {
00150 DrawBorder * border = DrawBorder::instance();
00151 border->setView (this);
00152 border->draw();
00153 }
00154 m_is_drawing = false;
00155 }
00156
00158 void QtView::drawShape ( QPainter & painter )
00159 {
00160 draw ( painter );
00161 }
00162
00163 Rect QtView::getDrawRect () const
00164 {
00165 return Rect ( x(), y(), width(), height() );
00166 }
00167
00168 void
00169 QtView::
00170 update ( const Observable * display )
00171 {
00172 if ( m_is_drawing == true ) return;
00173
00174 if ( display != 0 ) {
00175 m_filled = false;
00176 }
00177
00178
00179 updateDrawRect();
00180
00181 #if QT_VERSION < 0x040000
00182 QCanvasItem::update ();
00183 #else
00184 Q3CanvasItem::update ();
00185 #endif
00186 }
00187
00188 void QtView::updateDrawRect ()
00189 {
00190 double aspect = getAspectRatio();
00191 Rect drawrect = getDrawRect();
00192 if ( aspect > 0 ) {
00193 setDrawRect ( drawrect.getX(), drawrect.getY(),
00194 drawrect.getHeight()*aspect, drawrect.getHeight() );
00195 } else {
00196 setDrawRect ( drawrect );
00197 }
00198 }
00199
00200 void QtView::setDrawRect ( const QRect & rect )
00201 {
00202 setDrawRect ( rect.x (), rect.y (), rect.width (), rect. height () );
00203 }
00204
00205 void QtView::setDrawRect ( Rect & rect )
00206 {
00207 setDrawRect ( rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight() );
00208 }
00209
00210
00211
00212 void QtView::setDrawRect ( float x, float y, float w, float h )
00213 {
00214 setX ( x );
00215 setY ( y );
00216
00217 int iw = static_cast< int > ( w );
00218 int ih = static_cast< int > ( h );
00219 setSize ( iw, ih );
00220
00221 prepareMarginRect ();
00222 float new_width = m_margin_rect.getX () + m_margin_rect.getWidth() + 20.;
00223 iw = static_cast < int > ( new_width );
00224
00225 setSize ( iw, ih );
00226
00227 m_filled = false;
00228 }
00229
00230 int QtView::toViewX ( double datX ) const
00231 {
00232 if (m_plotter -> isReverse())
00233 return static_cast< int > ( x() + userToInvertedMarginX ( datX ) );
00234 else
00235 return static_cast< int > ( x() + userToMarginX (datX ) );
00236 }
00237
00238 int QtView::toViewY ( double datY ) const
00239 {
00240 return static_cast< int > ( y() + userToInvertedMarginY ( datY ) );
00241 }
00242
00243 void
00244 QtView::
00245 fillPickedPoint ( double dx, double dy,
00246 std::vector < double > & picked ) const
00247 {
00248 if ( m_plotter != 0 ) {
00249 double xx;
00250 if (m_plotter -> isReverse())
00251 xx = marginToInvertedUserX ( dx - x() );
00252 else
00253 xx = marginToUserX ( dx - x() );
00254 double yy = marginToInvertedUserY ( dy - y() );
00255
00256 m_plotter -> fillPickedPointFrom ( xx, yy, picked );
00257 }
00258 }
00259
00260 int QtView::toCanvasX ( double dx ) const
00261 {
00262 return static_cast < int > ( x() + dx );
00263 }
00264
00265 int QtView::toCanvasY ( double dy ) const
00266 {
00267 return static_cast < int > ( y() + dy );
00268 }
00269
00270 void
00271 QtView::
00272 #if QT_VERSION < 0x040000
00273 transformAndFill ( QPointArray & array,
00274 #else
00275 transformAndFill ( QPolygon & array,
00276 #endif
00277 const std::vector< double > & x,
00278 const std::vector< double > & y,
00279 int (QtView::* xfunc) ( double ) const,
00280 int (QtView::* yfunc) ( double ) const )
00281 {
00282 unsigned int size = x.size();
00283 assert ( size == y.size() );
00284
00285 for ( unsigned int i = 0; i < size; i++ ) {
00286 int ix = (this->*xfunc) ( x[i] );
00287 int iy = (this->*yfunc) ( y[i] );
00288 array.setPoint ( i , ix, iy );
00289 }
00290
00291 }
00292
00295 void QtView::drawViewMethod ( const std::vector< double > & x,
00296 const std::vector< double > & y,
00297 int,
00298 int )
00299 {
00300 unsigned int size = x.size();
00301 assert ( size == y.size() );
00302
00303 #if QT_VERSION < 0x040000
00304 QPointArray array ( size );
00305 #else
00306 QPolygon array ( size );
00307 #endif
00308 transformAndFill ( array, x, y,
00309 &QtView::toCanvasX, &QtView::toCanvasY );
00310
00311 m_painter->drawLineSegments ( array );
00312
00313 }
00314
00317 void QtView::drawMethod ( const std::vector < double > & x,
00318 const std::vector < double > & y,
00319 int,
00320 int )
00321 {
00322
00323 unsigned int size = x.size();
00324 assert ( size == y.size() );
00325
00326 #if QT_VERSION < 0x040000
00327 QPointArray array ( size );
00328 #else
00329 QPolygon array ( size );
00330 #endif
00331
00332 transformAndFill ( array, x, y,
00333 &QtView::toViewX, &QtView::toViewY );
00334
00335 m_painter->drawPolyline ( array, 0, -1 );
00336 }
00337
00338 void
00339 QtView::
00340 ensureOffScrSize ( int osw, int osh )
00341 {
00342 if ( osw > m_pixmap.width() || osh > m_pixmap.height() ) {
00343 m_pixmap.resize(QMAX(osw,m_pixmap.width()),
00344 QMAX(osh,m_pixmap.height()));
00345 }
00346 else if ( m_pixmap.width() == 0 || m_pixmap.height() == 0 ) {
00347 m_pixmap.resize( QMAX( m_pixmap.width(), 1),
00348 QMAX( m_pixmap.height(), 1 ) );
00349 }
00350 }
00351
00352 void
00353 QtView::
00354 setPageWidth ( int upage_w )
00355 {
00356 m_upage_w = upage_w;
00357 }
00358
00359
00360 bool
00361 QtView::
00362 isTextView() const
00363 {
00364 return m_plotter->isTextPlotter();
00365 }