filters

wmfimportparser.cc

00001 /* This file is part of the KDE project
00002  * Copyright (c) 2003 thierry lorthiois (lorthioist@wanadoo.fr)
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License version 2 as published by the Free Software Foundation.
00007  *
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Library General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Library General Public License
00014  * along with this library; see the file COPYING.LIB.  If not, write to
00015  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include <kdebug.h>
00020 #include <shapes/vellipse.h>
00021 #include <shapes/vrectangle.h>
00022 #include <shapes/vpolygon.h>
00023 #include <core/vstroke.h>
00024 #include <core/vfill.h>
00025 
00026 #include "wmfimportparser.h"
00027 
00028 /*
00029 bug : see motar.wmf
00030 */
00031 
00032 WMFImportParser::WMFImportParser() : KoWmfRead() {
00033 }
00034 
00035 
00036 bool WMFImportParser::play( VDocument& doc )
00037 {
00038     mDoc = &doc;
00039     mScaleX = mScaleY = 1;
00040     
00041     // Play the wmf file
00042     return KoWmfRead::play( );
00043 }
00044 
00045 
00046 //-----------------------------------------------------------------------------
00047 // Virtual Painter
00048 
00049 bool WMFImportParser::begin() { 
00050     QRect bounding = boundingRect();
00051     
00052     mBackgroundMode = Qt::TransparentMode;
00053     mCurrentOrg.setX( bounding.left() );
00054     mCurrentOrg.setY( bounding.top() );
00055     
00056     if ( isStandard() ) {
00057         mDoc->setUnit( KoUnit::U_PT );
00058         mDoc->setWidth( bounding.width() );  
00059         mDoc->setHeight( bounding.height() ); 
00060     }
00061     else {
00062         // Placeable Wmf store the boundingRect() in pixel and the default DPI
00063         // The placeable format doesn't have informations on witch Unit to use
00064         // so we choose millimeters by default
00065         mDoc->setUnit( KoUnit::U_MM );
00066         mDoc->setWidth( INCH_TO_POINT( (double)bounding.width() / defaultDpi() ) );  
00067         mDoc->setHeight( INCH_TO_POINT( (double)bounding.height() / defaultDpi() ) ); 
00068     }
00069     if ( (bounding.width() != 0) && (bounding.height() != 0) ) {
00070         mScaleX = mDoc->width() / (double)bounding.width();
00071         mScaleY = mDoc->height() / (double)bounding.height();
00072     }    
00073     return true;
00074 }
00075 
00076 
00077 bool WMFImportParser::end() {
00078     return true;
00079 }
00080 
00081 
00082 void WMFImportParser::save() {
00083 }
00084 
00085 
00086 void WMFImportParser::restore() {
00087 }
00088 
00089 
00090 void WMFImportParser::setFont( const QFont & ) {
00091 }
00092 
00093 
00094 void WMFImportParser::setPen( const QPen &pen ) {
00095     mPen = pen;
00096 }
00097 
00098 
00099 const QPen &WMFImportParser::pen() const {
00100     return mPen;
00101 }
00102 
00103 
00104 void WMFImportParser::setBrush( const QBrush &brush ) {
00105     mBrush = brush;
00106 }
00107 
00108 
00109 void WMFImportParser::setBackgroundColor( const QColor &c ) {
00110     mBackgroundColor = c;
00111 }
00112 
00113 
00114 void WMFImportParser::setBackgroundMode( Qt::BGMode mode ) {
00115     mBackgroundMode = mode;
00116 }
00117 
00118 
00119 void WMFImportParser::setRasterOp( Qt::RasterOp  ) {
00120 }
00121 
00122 
00123 void WMFImportParser::setWindowOrg( int left, int top ) {
00124     mCurrentOrg.setX( left );
00125     mCurrentOrg.setY( top );
00126 }
00127 
00128 
00129 void WMFImportParser::setWindowExt( int width, int height ) {    
00130     // the wmf file can change width/height during the drawing
00131     if ( (width != 0) && (height != 0) ) {  
00132         mScaleX = mDoc->width() / (double)width;
00133         mScaleY = mDoc->height() / (double)height;
00134     }
00135 }
00136 
00137 
00138 void WMFImportParser::setWorldMatrix( const QWMatrix &, bool  ) {
00139 }
00140 
00141 
00142 void WMFImportParser::setClipRegion( const QRegion & ) {
00143 }
00144 
00145 
00146 QRegion WMFImportParser::clipRegion() {
00147     return mClippingRegion;
00148 }
00149 
00150 
00151 void WMFImportParser::moveTo( int left, int top ) {
00152     mCurrentPoint.setX( left );
00153     mCurrentPoint.setY( top );
00154 }
00155 
00156 
00157 void WMFImportParser::lineTo( int left, int top ) {    
00158     VPath *line = new VPath( mDoc );
00159     line->moveTo( KoPoint( coordX(mCurrentPoint.x()), coordY(mCurrentPoint.y()) ) );
00160     line->lineTo( KoPoint( coordX(left), coordY(top) ) );
00161     appendPen( *line );
00162     
00163     mDoc->append( line );
00164     mCurrentPoint.setX( left );
00165     mCurrentPoint.setY( top );
00166 }
00167 
00168 
00169 void WMFImportParser::drawRect( int left, int top, int width, int height ) {
00170     VRectangle *rectangle;
00171     
00172     rectangle = new VRectangle( mDoc, KoPoint( coordX(left), coordY(top) ), scaleW(width), scaleH(height), 0 );
00173     appendPen( *rectangle );
00174     appendBrush( *rectangle );
00175 
00176     mDoc->append( rectangle );
00177 }
00178 
00179 
00180 void WMFImportParser::drawRoundRect( int left, int top, int width, int height, int roudw, int  ) {
00181     VRectangle *rectangle;
00182     
00183     // TODO : round rectangle
00184     rectangle = new VRectangle( mDoc, KoPoint( coordX(left), coordY(top) ), scaleW(width), scaleH(height), roudw );
00185     appendPen( *rectangle );
00186     appendBrush( *rectangle );
00187 
00188     mDoc->append( rectangle );
00189 }
00190 
00191 
00192 void WMFImportParser::drawEllipse( int left, int top, int width, int height ) {
00193     VEllipse *ellipse;
00194      
00195     ellipse = new VEllipse( mDoc, KoPoint( coordX(left), coordY(top+height) ), scaleW(width), scaleH(height) );
00196     appendPen( *ellipse );
00197     appendBrush( *ellipse );
00198 
00199     mDoc->append( ellipse );
00200 }
00201 
00202 
00203 void WMFImportParser::drawArc( int x, int y, int w, int h, int aStart, int aLen ) {
00204     double start = (aStart * 180) / 2880.0;
00205     double end = (aLen * 180) / 2880.0;
00206     end += start;
00207     VEllipse::VEllipseType type = VEllipse::arc;
00208     
00209     VEllipse *arc = new VEllipse( mDoc, KoPoint( coordX(x), coordY(y+h) ), scaleW(w), scaleH(h), type, start, end );    
00210     appendPen( *arc );
00211     
00212     mDoc->append( arc );
00213 }
00214 
00215 
00216 void WMFImportParser::drawPie( int x, int y, int w, int h, int aStart, int aLen ) {
00217     double start = (aStart * 180) / 2880.0;
00218     double end = (aLen * 180) / 2880.0;
00219     end += start;
00220     VEllipse::VEllipseType type = VEllipse::cut;
00221     
00222     VEllipse *arc = new VEllipse( mDoc, KoPoint( coordX(x), coordY(y+h) ), scaleW(w), scaleH(h), type, start, end );    
00223     appendPen( *arc );
00224     appendBrush( *arc );
00225     
00226     mDoc->append( arc );
00227 }
00228 
00229 
00230 void WMFImportParser::drawChord( int x, int y, int w, int h, int aStart, int aLen ) {
00231     double start = (aStart * 180) / 2880.0;
00232     double end = (aLen * 180) / 2880.0;
00233     end += start;
00234     VEllipse::VEllipseType type = VEllipse::section;
00235     
00236     VEllipse *arc = new VEllipse( mDoc, KoPoint( coordX(x), coordY(y+h) ), scaleW(w), scaleH(h), type, start, end );    
00237     appendPen( *arc );
00238     appendBrush( *arc );
00239     
00240     mDoc->append( arc );
00241 }
00242 
00243 
00244 void WMFImportParser::drawPolyline( const QPointArray &pa ) {
00245     VPath *polyline = new VPath( mDoc );
00246     appendPen( *polyline );
00247     appendPoints( *polyline, pa );
00248     
00249     mDoc->append( polyline );
00250 }
00251 
00252 
00253 void WMFImportParser::drawPolygon( const QPointArray &pa, bool ) {
00254     VPath *polygon = new VPath( mDoc );
00255     appendPen( *polygon );
00256     appendBrush( *polygon );
00257     appendPoints( *polygon, pa );
00258     
00259     polygon->close();
00260     mDoc->append( polygon );
00261 }
00262 
00263 
00264 void WMFImportParser::drawPolyPolygon( QPtrList<QPointArray>& listPa, bool ) {
00265     VPath *path = new VPath( mDoc );
00266     
00267     if ( listPa.count() > 0 ) {
00268         appendPen( *path );
00269         appendBrush( *path );
00270         appendPoints( *path, *listPa.first() );
00271         path->close();
00272 
00273         while ( listPa.next() ) {
00274             VPath *newPath = new VPath( mDoc );
00275             appendPoints( *newPath, *listPa.current() );
00276             newPath->close();
00277             path->combine( *newPath ); 
00278         }
00279 
00280         mDoc->append( path );
00281     }
00282 }
00283 
00284 
00285 void WMFImportParser::drawImage( int , int , const QImage &, int , int , int , int  ) {}
00286 
00287 
00288 void WMFImportParser::drawText( int , int , int , int , int , const QString& , double ) {}
00289 
00290 
00291 //-----------------------------------------------------------------------------
00292 // Utilities
00293 
00294 void WMFImportParser::appendPen( VObject& obj )
00295 {
00296     VStroke stroke( mDoc );
00297     stroke.setLineCap( VStroke::capRound );
00298     
00299     if ( mPen.style() == Qt::NoPen ) {
00300         stroke.setType( VStroke::none );
00301     }
00302     else {
00303         QValueList<float> dashes;
00304         stroke.setType( VStroke::solid );
00305         switch ( mPen.style() ) {
00306             case Qt::DashLine :
00307             stroke.dashPattern().setArray( dashes << MM_TO_POINT(3) << MM_TO_POINT(2) );
00308             break;
00309             case Qt::DotLine :
00310             stroke.dashPattern().setArray( dashes << MM_TO_POINT(1) << MM_TO_POINT(1) );
00311             break;
00312             case Qt::DashDotLine :
00313             stroke.dashPattern().setArray( dashes << MM_TO_POINT(3) << MM_TO_POINT(1) << MM_TO_POINT(1) << MM_TO_POINT(1) );
00314             break;
00315             case Qt::DashDotDotLine :
00316             stroke.dashPattern().setArray( dashes << MM_TO_POINT(3) << MM_TO_POINT(1) << MM_TO_POINT(1) << MM_TO_POINT(1) << MM_TO_POINT(1) << MM_TO_POINT(1) );
00317             break;
00318             default:
00319             break;
00320         }
00321     }
00322     stroke.setColor( mPen.color() );
00323     double width = mPen.width() * mScaleX;
00324     stroke.setLineWidth( ((width < 0.99) ? 1 : width) );
00325     obj.setStroke( stroke );    
00326 }
00327 
00328 
00329 void WMFImportParser::appendBrush( VObject& obj )
00330 {
00331     VFill fill( mBackgroundColor );
00332     fill.setColor( mBrush.color() );
00333         
00334     switch ( mBrush.style() ) {
00335         case Qt::NoBrush :
00336         fill.setType( VFill::none );
00337         break;
00338         case Qt::SolidPattern :
00339         fill.setType( VFill::solid );
00340         break;
00341         case Qt::CustomPattern :
00342         // TODO: bitmap pattern brush
00343         fill.setType( VFill::solid );
00344         //fill.pattern().
00345         break;
00346         default :
00347         // TODO: pattern brush
00348         if ( mBackgroundMode == Qt::OpaqueMode ) {
00349             fill.setColor( mBackgroundColor );
00350             fill.setType( VFill::solid );
00351         }
00352         else {
00353             fill.setType( VFill::none );
00354         }
00355         break;
00356     }
00357     obj.setFill( fill );
00358 }
00359 
00360 
00361 void WMFImportParser::appendPoints(VPath &path, const QPointArray& pa)
00362 {
00363     // list of point array
00364     if ( pa.size() > 0 ) {
00365         path.moveTo( KoPoint( coordX(pa.point(0).x()), coordY(pa.point(0).y()) ) );
00366     }
00367     for ( uint i=1 ; i < pa.size() ; i++ ) {
00368         path.lineTo( KoPoint( coordX(pa.point(i).x()), coordY(pa.point(i).y()) ) );
00369     }
00370 }
00371 
KDE Home | KDE Accessibility Home | Description of Access Keys