filters

msodimport.cc

00001 /*
00002     Copyright (C) 2000, S.R.Haque <shaheedhaque@hotmail.com>.
00003     This file is part of the KDE project
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 
00020 DESCRIPTION
00021 */
00022 
00023 #include <kdebug.h>
00024 #include <ktempfile.h>
00025 #include <kmimetype.h>
00026 #include <kgenericfactory.h>
00027 #include <KoFilterChain.h>
00028 #include <qfile.h>
00029 #include <msodimport.h>
00030 #include <qpointarray.h>
00031 
00032 typedef KGenericFactory<MSODImport, KoFilter> MSODImportFactory;
00033 K_EXPORT_COMPONENT_FACTORY( libmsodimport, MSODImportFactory( "kofficefilters" ) )
00034 
00035 const int MSODImport::s_area = 30505;
00036 
00037 MSODImport::MSODImport(
00038     KoFilter *,
00039     const char *,
00040     const QStringList&) :
00041         KoEmbeddingFilter(), Msod(100)
00042 {
00043 }
00044 
00045 MSODImport::~MSODImport()
00046 {
00047 }
00048 
00049 KoFilter::ConversionStatus MSODImport::convert( const QCString& from, const QCString& to )
00050 {
00051     if (to != "application/x-karbon" || from != "image/x-msod")
00052         return KoFilter::NotImplemented;
00053 
00054     // Get configuration data: the shape id, and any delay stream that we were given.
00055     unsigned shapeId;
00056     emit commSignalShapeID( shapeId );
00057     const char *delayStream = 0L;
00058     emit commSignalDelayStream( delayStream );
00059     kdDebug( s_area ) << "##################################################################" << endl;
00060     kdDebug( s_area ) << "shape id: " << shapeId << endl;
00061     kdDebug( s_area ) << "delay stream: " << delayStream << endl;
00062     kdDebug( s_area ) << "##################################################################" << endl;
00063 /*
00064     QString config = ""; // ###### FIXME: We aren't able to pass config data right now
00065     QStringList args = QStringList::split(";", config);
00066     unsigned i;
00067 
00068     kdDebug(s_area) << "MSODImport::filter: config: " << config << endl;
00069     for (i = 0; i < args.count(); i++)
00070     {
00071         if (args[i].startsWith("shape-id="))
00072         {
00073             shapeId = args[i].mid(9).toUInt();
00074         }
00075         else
00076         if (args[i].startsWith("delay-stream="))
00077         {
00078             delayStream = (const char *)args[i].mid(13).toULong();
00079         }
00080         else
00081         {
00082             kdError(s_area) << "Invalid argument: " << args[i] << endl;
00083             return KoFilter::StupidError;
00084         }
00085     }
00086 */
00087     // doc header
00088     m_text = "";
00089     m_text += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00090     m_text += "<!DOCTYPE DOC>\n";
00091     m_text += "<DOC mime=\"application/x-karbon\" syntaxVersion=\"0.1\" editor=\"WMF import filter\">\n";
00092     m_text += "  <LAYER name=\"Layer\" visible=\"1\">\n";
00093 
00094     if (!parse(shapeId, m_chain->inputFile(), delayStream))
00095         return KoFilter::WrongFormat;
00096 
00097     // close doc
00098     m_text += "  </LAYER>\n";
00099     m_text += "</DOC>\n";
00100 
00101     emit sigProgress(100);
00102 
00103     KoStoreDevice* dev = m_chain->storageFile( "root", KoStore::Write );
00104     if (!dev)
00105     {
00106         kdError(s_area) << "Cannot open output file" << endl;
00107         return KoFilter::StorageCreationError;
00108     }
00109     QCString cstring ( m_text.utf8() );
00110     dev->writeBlock(cstring.data(), cstring.size()-1);
00111 
00112     return KoFilter::OK;
00113 }
00114 
00115 void MSODImport::gotEllipse(
00116     const DrawContext &/*dc*/,
00117     QString /*type*/,
00118     QPoint /*topLeft*/,
00119     QSize /*halfAxes*/,
00120     unsigned /*startAngle*/,
00121     unsigned /*stopAngle*/)
00122 {
00123 // ### TODO
00124 #if 0
00125     m_text += "<ellipse angle1=\"" + QString::number(startAngle) +
00126                 "\" angle2=\"" + QString::number(stopAngle) +
00127                 "\" x=\"" + QString::number(topLeft.x()) +
00128                 "\" y=\"" + QString::number(topLeft.y()) +
00129                 "\" kind=\"" + type +
00130                 "\" rx=\"" + QString::number(halfAxes.width()) +
00131                 "\" ry=\"" + QString::number(halfAxes.height()) +
00132                 "\">\n";
00133     m_text += " <gobject fillcolor=\"#" + QString::number(dc.m_brushColour, 16) +
00134                 "\" fillstyle=\"" + QString::number(1 /*m_winding*/) +
00135                 "\" linewidth=\"" + QString::number(dc.m_penWidth) +
00136                 "\" strokecolor=\"#" + QString::number(dc.m_penColour, 16) +
00137                 "\" strokestyle=\"" + QString::number(dc.m_penStyle) +
00138                 "\">\n";
00139     m_text += "  <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n";
00140     m_text += " </gobject>\n";
00141     m_text += "</ellipse>\n";
00142 #endif
00143 }
00144 
00145 static void toRGB(int c, double &r, double &g, double &b)
00146 {
00147     r = (c >> 16) / 255.0;
00148     g = ((c >> 8) & 0xFF) / 255.0;
00149     b = (c & 0xFF) / 255.0;
00150 }
00151 
00152 void MSODImport::gotPicture(
00153     unsigned key,
00154     QString extension,
00155     unsigned length,
00156     const char *data)
00157 {
00158 // ### TODO
00159 #if 0
00160     kdDebug() << "##########################################MSODImport::gotPicture" << endl;
00161     kdDebug() << "MSODImport::gotPicture -- " << extension << endl;
00162     if ((extension == "wmf") ||
00163         (extension == "emf") ||
00164         (extension == "pict"))
00165     {
00166         int partRef = internalPartReference( QString::number( key ) );
00167 
00168         if (partRef == -1)
00169         {
00170             m_embeddeeData = data;
00171             m_embeddeeLength = length;
00172 
00173             QString srcMime( KoEmbeddingFilter::mimeTypeByExtension( extension ) );
00174             if ( srcMime == KMimeType::defaultMimeType() )
00175                 kdWarning( s_area ) << "Couldn't determine the mimetype from the extension" << endl;
00176 
00177             QCString destMime; // intentionally empty, the filter manager will do the rest
00178             KoFilter::ConversionStatus status;
00179             partRef = embedPart( srcMime.latin1(), destMime, status, QString::number( key ) );
00180 
00181             m_embeddeeData = 0;
00182             m_embeddeeLength = 0;
00183 
00184             if ( status != KoFilter::OK ) {
00185                 kdWarning(s_area) << "Couldn't convert the image!" << endl;
00186                 return;
00187             }
00188         }
00189         m_text += "<object url=\"" + QString::number( partRef ) + "\" mime=\"";
00190         m_text += internalPartMimeType( QString::number( key ) );
00191         m_text += "\" x=\"0\" y=\"0\" width=\"100\" height=\"200\"/>\n";
00192     }
00193     else
00194     {
00195         // We could not import it as a part. Try as an image.
00196         KTempFile tempFile( QString::null, '.' + extension );
00197         tempFile.file()->writeBlock( data, length );
00198         tempFile.close();
00199 
00200         m_text += "<pixmap src=\"" + tempFile.name() + "\">\n"
00201                     " <gobject fillstyle=\"0\" linewidth=\"1\" strokecolor=\"#000000\" strokestyle=\"1\">\n"
00202                     "  <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n"
00203                     " </gobject>\n"
00204                     "</pixmap>\n";
00205 
00206         // Note that we cannot delete the file...
00207     }
00208 #endif
00209 }
00210 
00211 void MSODImport::gotPolygon(
00212     const DrawContext &dc,
00213     const QPointArray &points)
00214 {
00215     kdDebug(s_area) << "MSODImport::gotPolygon" << endl;
00216     kdDebug(s_area) << QString::number(dc.m_penWidth, 16) << endl;
00217     kdDebug(s_area) << dc.m_penStyle << endl;
00218     m_text += "<COMPOSITE>\n";
00219     if( dc.m_penWidth > 0 )
00220     {
00221         m_text += "<STROKE lineWidth=\"1\">\n";// + QString::number(dc.m_penWidth, 16) + "\">\n";
00222         double r, g, b;
00223         toRGB(dc.m_penColour, r, g, b);
00224         m_text += "<COLOR v1=\"" + QString::number(r) + "\" v2=\"" + QString::number(g) + "\"  v3=\"" + QString::number(b) + "\" opacity=\"1\" colorSpace=\"0\"  />\n";
00225     m_text += "</STROKE>\n";
00226     }
00227     else
00228         m_text += "<STROKE lineWidth=\"1\" />\n";
00229     m_text += "<FILL fillRule=\"" + QString::number(dc.m_winding) + "\">\n";
00230     double r, g, b;
00231     toRGB(dc.m_brushColour, r, g, b);
00232     m_text += "<COLOR v1=\"" + QString::number(r) + "\" v2=\"" + QString::number(g) + "\"  v3=\"" + QString::number(b) + "\" opacity=\"1\" colorSpace=\"0\"  />\n";
00233     m_text += "</FILL>\n";
00234 
00235     m_text += "<PATH isClosed=\"1\" >\n";
00236     pointArray(points);
00237     m_text += "</PATH>\n";
00238     m_text += "</COMPOSITE>\n";
00239 }
00240 
00241 
00242 void MSODImport::gotPolyline(
00243     const DrawContext &dc,
00244     const QPointArray &points)
00245 {
00246     kdDebug(s_area) << "MSODImport::gotPolyline" << endl;
00247     return; // ### TODO
00248     m_text += "<COMPOSITE>\n";
00249     m_text += "<STROKE lineWidth=\"" + QString::number(dc.m_penWidth) + "\">\n";
00250     m_text += "</STROKE>\n";
00251     m_text += "<PATH isClosed=\"1\" >\n";
00252     pointArray(points);
00253     m_text += "</PATH>\n";
00254     m_text += "</COMPOSITE>\n";
00255 }
00256 
00257 void MSODImport::gotRectangle(
00258     const DrawContext &dc,
00259     const QPointArray &points)
00260 {
00261 // ### TODO
00262 #if 0
00263     QRect bounds = points.boundingRect();
00264 
00265     m_text += "<rectangle width=\"" + QString::number(bounds.width()) +
00266                 "\" x=\"" + QString::number(bounds.x()) +
00267                 "\" y=\"" + QString::number(bounds.y()) +
00268                 "\" height=\"" + QString::number(bounds.height()) +
00269                 "\" rounding=\"0\">\n";
00270     m_text += "<polyline arrow1=\"0\" arrow2=\"0\">\n";
00271     pointArray(points);
00272     m_text += " <gobject fillcolor=\"#" + QString::number(dc.m_brushColour, 16) +
00273                 "\" fillstyle=\"" + QString::number(1 /*m_winding*/) +
00274                 "\" linewidth=\"" + QString::number(dc.m_penWidth) +
00275                 "\" strokecolor=\"#" + QString::number(dc.m_penColour, 16) +
00276                 "\" strokestyle=\"" + QString::number(dc.m_penStyle) +
00277                 "\">\n";
00278     m_text += "  <matrix dx=\"0\" dy=\"0\" m21=\"0\" m22=\"1\" m11=\"1\" m12=\"0\"/>\n";
00279     m_text += " </gobject>\n";
00280     m_text += "</polyline>\n";
00281     m_text += "</rectangle>\n";
00282 #endif
00283 }
00284 
00285 void MSODImport::savePartContents( QIODevice* file )
00286 {
00287     if ( m_embeddeeData != 0 && m_embeddeeLength != 0 )
00288         file->writeBlock( m_embeddeeData, m_embeddeeLength );
00289 }
00290 
00291 void MSODImport::pointArray(
00292     const QPointArray &points)
00293 {
00294 
00295     m_text += "<MOVE x=\"" + QString::number(points.point(0).x()) +
00296                 "\" y=\"" + QString::number(points.point(0).y()) +
00297                 "\" />\n";
00298     kdDebug(s_area) << "\n<MOVE x=\"" + QString::number(points.point(0).x()) +
00299                             "\" y=\"" + QString::number(points.point(0).y()) +
00300                                         "\" />" << endl;
00301     for (unsigned int i = 1; i < points.count(); i++)
00302     {
00303         m_text += "<LINE x=\"" + QString::number(points.point(i).x()) +
00304                     "\" y=\"" + QString::number(points.point(i).y()) +
00305                     "\" />\n";
00306         kdDebug(s_area) << "<LINE x=\"" + QString::number(points.point(i).x()) +
00307                             "\" y=\"" + QString::number(points.point(i).y()) +
00308                                             "\" />" << endl;
00309     }
00310 
00311 }
00312 
00313 #include <msodimport.moc>
KDE Home | KDE Accessibility Home | Description of Access Keys