lib
kformulamimesource.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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, const QDomDocument& formula)
00043 : formulaDocument( doc ), document(formula)
00044 {
00045
00046
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
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;
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
00118 ContextStyle& context = formulaDocument->getContextStyle( false );
00119
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
|