lib Library API Documentation

kformulamimesource.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003                   Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
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., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <iostream>
00022 
00023 #include <qpopupmenu.h>
00024 #include <qbuffer.h>
00025 #include <qcolor.h>
00026 #include <qimage.h>
00027 #include <qpainter.h>
00028 #include <qpixmap.h>
00029 
00030 #include <kcommand.h>
00031 
00032 #include "contextstyle.h"
00033 #include "formulacursor.h"
00034 #include "formulaelement.h"
00035 #include "kformuladocument.h"
00036 #include "kformulamimesource.h"
00037 
00038 KFORMULA_NAMESPACE_BEGIN
00039 using namespace std;
00040 
00041 
00042 MimeSource::MimeSource(Document* doc, QDomDocument formula)
00043         : formulaDocument( doc ), document(formula)
00044 {
00045     // The query for text/plain comes very often. So make sure
00046     // it's fast.
00047 
00048     rootElement = new FormulaElement(this);
00049     FormulaCursor cursor(rootElement);
00050 
00051     QPtrList<BasicElement> list;
00052     list.setAutoDelete(true);
00053     if ( cursor.buildElementsFromDom( document.documentElement(), list ) ) {
00054         cursor.insert(list);
00055         latexString = rootElement->toLatex().utf8();
00056         if (latexString.size() > 0) {
00057             latexString.truncate(latexString.size()-1);
00058         }
00059     }
00060 }
00061 
00062 MimeSource::~MimeSource()
00063 {
00064     delete rootElement;
00065 }
00066 
00067 const char * MimeSource::selectionMimeType()
00068 {
00069     return "application/x-kformula";
00070 }
00071 
00072 const char* MimeSource::format( int n ) const
00073 {
00074     switch (n) {
00075         case 0:
00076             return selectionMimeType();
00077         case 1:
00078             return "image/ppm";
00079         case 2:
00080             return "text/plain";
00081         case 3:
00082             return "text/x-tex";
00083     }
00084     return NULL;
00085 }
00086 
00087 bool MimeSource::provides( const char * format) const
00088 {
00089 //This is not completed
00090     if(QString(format)==selectionMimeType())
00091         return true;
00092     else if(QString(format)=="image/ppm")
00093         return true;
00094     else if(QString(format)=="text/plain")
00095         return true;
00096     else if(QString(format)=="text/x-tex")
00097         return true;
00098     else
00099         return false;
00100 }
00101 
00102 QByteArray MimeSource::encodedData ( const char *format ) const
00103 {
00104     QString fmt=format;  //case sensitive?
00105 
00106     if ((fmt=="text/plain") || (fmt=="text/x-tex"))
00107         return latexString;
00108 
00109     if (fmt==selectionMimeType()) {
00110     QByteArray d=document.toCString();
00111     d.truncate(d.size()-1);
00112     return d;
00113     }
00114 
00115     if (fmt=="image/ppm") {
00116 
00117     //cerr << "asking image" << endl;
00118         ContextStyle& context = formulaDocument->getContextStyle( false );
00119         //context.setResolution(5, 5);
00120 
00121         rootElement->calcSizes(context);
00122         QRect rect(rootElement->getX(), rootElement->getY(),
00123                    rootElement->getWidth(), rootElement->getHeight());
00124 
00125         QPixmap pm( context.layoutUnitToPixelX( rootElement->getWidth() ),
00126                     context.layoutUnitToPixelY( rootElement->getHeight() ) );
00127     pm.fill();
00128     QPainter paint(&pm);
00129         rootElement->draw(paint, rect, context);
00130     paint.end();
00131 
00132     QByteArray d;
00133     QBuffer buff(d);
00134     buff.open(IO_WriteOnly);
00135     QImageIO io(&buff,"PPM");
00136     QImage ima=pm.convertToImage();
00137     ima.detach();
00138     io.setImage(ima);
00139     if(!io.write())
00140         return QByteArray();
00141 
00142     buff.close();
00143         return d;
00144     }
00145 
00146     return QByteArray();
00147 }
00148 
00149 const SymbolTable& MimeSource::getSymbolTable() const
00150 {
00151     return formulaDocument->getContextStyle( false ).symbolTable();
00152 }
00153 
00154 KFORMULA_NAMESPACE_END
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 13:19:22 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2003