lib

kformulamimesource.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
3 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <iostream>
22
23#include <tqpopupmenu.h>
24#include <tqbuffer.h>
25#include <tqcolor.h>
26#include <tqimage.h>
27#include <tqpainter.h>
28#include <tqpixmap.h>
29
30#include <kcommand.h>
31
32#include "contextstyle.h"
33#include "formulacursor.h"
34#include "formulaelement.h"
35#include "kformuladocument.h"
36#include "kformulamimesource.h"
37
38KFORMULA_NAMESPACE_BEGIN
39using namespace std;
40
41
42MimeSource::MimeSource(Document* doc, const TQDomDocument& formula)
43 : formulaDocument( doc ), document(formula)
44{
45 // The query for text/plain comes very often. So make sure
46 // it's fast.
47
48 rootElement = new FormulaElement(this);
49 FormulaCursor cursor(rootElement);
50
51 TQPtrList<BasicElement> list;
52 list.setAutoDelete(true);
53 if ( cursor.buildElementsFromDom( document.documentElement(), list ) ) {
54 cursor.insert(list);
55 latexString = rootElement->toLatex().utf8();
56 if (latexString.size() > 0) {
57 latexString.truncate(latexString.size()-1);
58 }
59 }
60}
61
62MimeSource::~MimeSource()
63{
64 delete rootElement;
65}
66
67const char * MimeSource::selectionMimeType()
68{
69 return "application/x-kformula";
70}
71
72const char* MimeSource::format( int n ) const
73{
74 switch (n) {
75 case 0:
76 return selectionMimeType();
77 case 1:
78 return "image/ppm";
79 case 2:
80 return "text/plain";
81 case 3:
82 return "text/x-tex";
83 }
84 return NULL;
85}
86
87bool MimeSource::provides( const char * format) const
88{
89//This is not completed
90 if(TQString(format)==selectionMimeType())
91 return true;
92 else if(TQString(format)=="image/ppm")
93 return true;
94 else if(TQString(format)=="text/plain")
95 return true;
96 else if(TQString(format)=="text/x-tex")
97 return true;
98 else
99 return false;
100}
101
102TQByteArray MimeSource::encodedData ( const char *format ) const
103{
104 TQString fmt=format; //case sensitive?
105
106 if ((fmt=="text/plain") || (fmt=="text/x-tex"))
107 return latexString;
108
109 if (fmt==selectionMimeType()) {
110 TQByteArray d=document.toCString();
111 d.truncate(d.size()-1);
112 return d;
113 }
114
115 if (fmt=="image/ppm") {
116
117 //cerr << "asking image" << endl;
118 ContextStyle& context = formulaDocument->getContextStyle( false );
119 //context.setResolution(5, 5);
120
121 rootElement->calcSizes(context);
122 TQRect rect(rootElement->getX(), rootElement->getY(),
123 rootElement->getWidth(), rootElement->getHeight());
124
125 TQPixmap pm( context.layoutUnitToPixelX( rootElement->getWidth() ),
126 context.layoutUnitToPixelY( rootElement->getHeight() ) );
127 pm.fill();
128 TQPainter paint(&pm);
129 rootElement->draw(paint, rect, context);
130 paint.end();
131
132 TQByteArray d;
133 TQBuffer buff(d);
134 buff.open(IO_WriteOnly);
135 TQImageIO io(&buff,"PPM");
136 TQImage ima=pm.convertToImage();
137 ima.detach();
138 io.setImage(ima);
139 if(!io.write())
140 return TQByteArray();
141
142 buff.close();
143 return d;
144 }
145
146 return TQByteArray();
147}
148
149const SymbolTable& MimeSource::getSymbolTable() const
150{
151 return formulaDocument->getContextStyle( false ).symbolTable();
152}
153
154KFORMULA_NAMESPACE_END
A document that can contain a lot of formulas (container).
The selection.
The main element.
int layoutUnitToPixelX(int lupix) const
The "layout unit pixel" -> "[zoomed] view pixel" conversions.
The symbol table.
Definition symboltable.h:77