Qdriver.cpp

00001 #include "QDisplayDialog.h"
00002 #include "Qdriver.h"
00003 #include <QPicture>
00004 #include <QPainter>
00005 #include <QPainterPath>
00006 #include <QPen>
00007 #include <QColor>
00008 #include <iostream>
00009 #include <math.h>
00010 
00011 
00012 #define QDRIVER_MAXLOGICAL_COORDINATE 10000
00013 
00014 //
00015 // Locally defined utility routines
00016 //
00017 QColor getColorRef(int iColor)
00018 {
00019 
00020     switch(iColor)
00021     {
00022     case UCdriver::BLACK         : return QColor(0,0,0);
00023     case UCdriver::DARK_GREY     : return QColor(128,128,128);
00024     case UCdriver::LIGHT_GREY    : return QColor(192,192,192);
00025     case UCdriver::BLUE          : return QColor(0,0,255);
00026     case UCdriver::LIGHT_BLUE    : return QColor(130,130,255);
00027     case UCdriver::GREEN         : return QColor(0,255,0);
00028     case UCdriver::LIGHT_GREEN   : return QColor(192,192,192);
00029     case UCdriver::CYAN          : return QColor(0,255,255);
00030     case UCdriver::LIGHT_CYAN    : return QColor(128,255,255);
00031     case UCdriver::RED           : return QColor(255,0,0);
00032     case UCdriver::MAGENTA       : return QColor(255,0,255);
00033     case UCdriver::LIGHT_MAGENTA : return QColor(255,128,255);
00034     case UCdriver::ORANGE        : return QColor(255,128,64);
00035     case UCdriver::YELLOW        : return QColor(255,255,0);
00036     case UCdriver::WHITE         : return QColor(255,255,255);
00037     }
00038     return QColor(0,0,0);
00039 }
00040 
00041 Qdriver::Qdriver()
00042 {
00043     graphicsMetaData     = new QPicture();
00044     qDisplayDialog       = new QDisplayDialog();
00045     qPainter             = new QPainter();
00046     qPen                 = new QPen();
00047     qFont                = new QFont("Helvetica");
00048     qBrush               = new QBrush();
00049     qFontMetrics         = 0;
00050     setMaxLogicalCoordinate(QDRIVER_MAXLOGICAL_COORDINATE);
00051     qPainter->begin(graphicsMetaData);   
00052     qPainter->setRenderHint(QPainter::Antialiasing, true);
00053     qPainter->setClipping(true);
00054     qPainter->setClipRegion(QRegion(0,0,maxLogicalCoordinate,maxLogicalCoordinate),Qt::ReplaceClip);
00055 //
00056 //  Pen style defaults
00057 //
00058     qPen->setJoinStyle(Qt::RoundJoin);
00059     setDriverTypeName("QDriver");
00060 }
00061 
00062 Qdriver::~Qdriver()
00063 {
00064     if(qDisplayDialog   != 0) delete qDisplayDialog;
00065     if(qPen             != 0) delete qPen;
00066     if(qPainter         != 0) delete qPainter;
00067     if(graphicsMetaData != 0) delete graphicsMetaData;
00068     if(qFont            != 0) delete qFont;
00069     if(qBrush           != 0) delete qBrush;
00070     if(qFontMetrics     != 0) delete qFontMetrics;
00071 }
00072 
00073 
00074 void Qdriver::setMaxLogicalCoordinate(long maxCoord)
00075 {
00076     maxLogicalCoordinate = maxCoord;
00077 }
00078 
00079 //
00080 // User_pattern ignored for now
00081 //
00082 void Qdriver::setPenDashAndColor(int dash_pattern, unsigned user_pattern, int color, double *rgb)
00083 {
00084 
00085     if(color != UCdriver::USER_RGB)
00086     {qPen->setColor(getColorRef(color));}
00087     else
00088     {qPen->setColor(QColor(long(rgb[0]),long(rgb[1]),long(rgb[2])));}
00089     
00090     switch(dash_pattern)
00091     {
00092     case UCdriver::SOLID           : qPen->setStyle(Qt::SolidLine);       break;
00093     case UCdriver::DASH            : qPen->setStyle(Qt::DashLine);        break;
00094     case UCdriver::DOTS            : qPen->setStyle(Qt::DotLine);         break;
00095     case UCdriver::DASH_DOT        : qPen->setStyle(Qt::DashDotLine);     break;
00096     case UCdriver::DASH_DOUBLE_DOT : qPen->setStyle(Qt::DashDotDotLine);  break;
00097     case UCdriver::DOUBLE_DASH     : qPen->setStyle(Qt::DashLine);        break;
00098     case UCdriver::USER_DASH       : qPen->setStyle(Qt::SolidLine);       break;
00099     default : qPen->setStyle(Qt::SolidLine); 
00100     }
00101 
00102     //
00103     // Scale the dash pattern
00104     //
00105     
00106     QVector<double> qVector(qPen->dashPattern());
00107     for(int i = 0; i < qVector.size(); i++)
00108     {
00109     qVector[i] *= 3.0;
00110     }
00111     
00112     qPen->setDashPattern(qVector);
00113     qPainter->setPen(*qPen);
00114 }
00115     
00116 void Qdriver::setBrushColor(int color, double *rgb)
00117 {
00118 
00119     if(color != UCdriver::USER_RGB)
00120     {qBrush->setColor(getColorRef(color));}
00121     else
00122     {qBrush->setColor(QColor(long(rgb[0]),long(rgb[1]),long(rgb[2])));}
00123 }
00124 
00125 void Qdriver::setFont(char* font, double size)
00126 {
00127     double defaultTextScaling = 0.02;
00128  
00129     if(qFontMetrics != 0) delete qFontMetrics;
00130  
00131     if(font != 0)
00132     {
00133       if(qFont != 0) delete qFont;
00134       qFont      =   new QFont(font);
00135     }
00136  
00137     if(fabs(size) != 0.0) 
00138     {
00139  qFont->setPointSize((int)(maxLogicalCoordinate*size));
00140     } 
00141     else 
00142     {
00143  qFont->setPointSize((int)(maxLogicalCoordinate*defaultTextScaling));
00144     }
00145  
00146      qFont->setStyleStrategy(QFont::ForceOutline);
00147      qFont->setStyleHint(QFont::Helvetica);
00148      qFontMetrics =  new QFontMetrics(*qFont);
00149  
00150      qPainter->setFont(*qFont);  
00151 }
00152     
00153 void Qdriver::setImageOutputQuality(int quality)
00154 {
00155     qDisplayDialog->setImageOutputQuality(quality);
00156 }
00157     
00158 void Qdriver::lines(double *x, double *y, long npoints, int dash_pattern,
00159              unsigned user_pattern, double width, int color, double *rgb)
00160 {
00161     setPenDashAndColor(dash_pattern,user_pattern,color,rgb);
00162     qPen->setWidth((int)(maxLogicalCoordinate*width));
00163     
00164 
00165     QVector<QPointF> pointPairs(2*npoints);
00166     for(int i = 0; i < npoints-1; i++)
00167     {
00168         pointPairs[2*i].setX(x[i]*maxLogicalCoordinate);
00169         pointPairs[2*i].setY((1.0-y[i])*maxLogicalCoordinate);
00170         pointPairs[2*i+1].setX(x[i+1]*maxLogicalCoordinate);
00171         pointPairs[2*i+1].setY((1.0-y[i+1])*maxLogicalCoordinate);
00172     }
00173 
00174     qPainter->drawLines(pointPairs);
00175 }
00176  
00177 void Qdriver::line(double x1, double y1, double x2, double y2, int dash_pattern, 
00178              unsigned user_pattern, double width, int color, double *rgb)
00179 {
00180     setPenDashAndColor(dash_pattern,user_pattern,color,rgb);
00181     qPen->setWidth((int)(maxLogicalCoordinate*width));
00182     
00183     qPainter->drawLine(QLineF(maxLogicalCoordinate*x1,maxLogicalCoordinate*(1.0-y1),
00184     maxLogicalCoordinate*x2,maxLogicalCoordinate*(1.0-y2)));
00185 } 
00186 
00187 void Qdriver::point(double x, double y, char p, char *font, double size,
00188              int color, double *rgb)
00189 {
00190     points(&x, &y, 1, p, font,size, color, rgb);
00191 }
00192 
00193 void Qdriver::points(double *X, double *Y, long np, char p, char *font,
00194               double size, int color, double *rgb)
00195 {
00196      setFont(font,size);
00197  
00198      setBrushColor(color,rgb);
00199      qBrush->setStyle(Qt::SolidPattern);
00200      qPainter->setPen(Qt::NoPen);
00201      qPainter->setBrush(*qBrush);
00202      
00203      QPainterPath path;
00204 
00205      char s[] = {'A','\0'};
00206      s[0] = p;
00207      
00208      QRect qBoundRect =  qFontMetrics->boundingRect(s);  
00209      int xShift  = (int)(((1.0)*qBoundRect.width())/2.0); 
00210      int yShift  = (int)(((-1.0)*qBoundRect.height())/2.0);
00211      
00212      for(long i = 0; i < np; i++)
00213      {
00214      path.addText(QPointF(X[i]*maxLogicalCoordinate - xShift,maxLogicalCoordinate*(1.0-Y[i]) - yShift), *qFont, s);
00215      }
00216      qPainter->drawPath(path); 
00217 }
00218 
00219 void Qdriver::text(double x, double y, char *s, char *font, double size,
00220             double rotation, double horiz_just, double vert_just,
00221             int color, double *rgb)
00222 {
00223      setFont(font,size);
00224     
00225      setBrushColor(color,rgb);
00226      qBrush->setStyle(Qt::SolidPattern);
00227      qPainter->setPen(Qt::NoPen);
00228      qPainter->setBrush(*qBrush);
00229      
00230      QRect qBoundRect =  qFontMetrics->boundingRect(s);  
00231      int xShift  = (int)(((horiz_just + 1.0)*qBoundRect.width())/2.0); 
00232      int yShift  = (int)(((vert_just  - 1.0)*qBoundRect.height())/2.0);
00233 
00234      qPainter->save();
00235      qPainter->translate(QPointF(x*maxLogicalCoordinate - xShift ,maxLogicalCoordinate*(1.0-y) - yShift));
00236      qPainter->rotate(rotation);
00237      QPainterPath path;
00238      path.addText(QPointF(0,0), *qFont, s);
00239      qPainter->drawPath(path);
00240      qPainter->restore();
00241    
00242 }
00243 void Qdriver::region(double *X, double *Y, long npoints, double *RGB)
00244 {
00245     QPointF* points = new QPointF[npoints];
00246     for(long i = 0; i < npoints; i++)
00247     {
00248       points[i].setX(X[i]*maxLogicalCoordinate);
00249       points[i].setY(maxLogicalCoordinate*(1.0-Y[i]));
00250     }
00251     qBrush->setColor(QColor((int)RGB[0],(int)RGB[1],(int)RGB[2]));
00252     qBrush->setStyle(Qt::SolidPattern);
00253     qPainter->setBrush(*qBrush);
00254     qPainter->drawPolygon(points, npoints);
00255 }
00256 
00257 void  Qdriver::frame()
00258 {
00259      qPainter->end();  
00260      qDisplayDialog->setGraphicsMetaData(graphicsMetaData,maxLogicalCoordinate); 
00261      qDisplayDialog->exec(); 
00262      qPainter->begin(graphicsMetaData);                 
00263 }
00264 
00265 
00266 
00267      /* 
00268       * drawing using the drawText routine ...
00269       * 
00270  qPainter->save();
00271  qPainter->translate(QPointF(x*maxLogicalCoordinate,maxLogicalCoordinate*(1.0-y)));
00272  qPainter->rotate(rotation);
00273  qPainter->drawText(QPointF(0,0),s);
00274      qPainter->restore();
00275      */

Generated on Thu Jan 29 19:34:56 2009 for Qdriver by  doxygen 1.5.1-p1