lib

formulaelement.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#include <tqpainter.h>
23
24#include <kdebug.h>
25
26#include "contextstyle.h"
27#include "formulacursor.h"
28#include "formulaelement.h"
29#include "kformulacontainer.h"
30#include "kformuladocument.h"
31
32KFORMULA_NAMESPACE_BEGIN
33
35 : document( container ), baseSize( 20 ), ownBaseSize( false )
36{
37}
38
39
40void FormulaElement::setBaseSize( int size )
41{
42 if ( size > 0 ) {
43 baseSize = size;
44 ownBaseSize = true;
45 }
46 else {
47 ownBaseSize = false;
48 }
49 document->baseSizeChanged( size, ownBaseSize );
50}
51
52
56BasicElement* FormulaElement::goToPos( FormulaCursor* cursor, const LuPixelPoint& point )
57{
58 bool handled = false;
59 BasicElement* element = inherited::goToPos(cursor, handled, point, LuPixelPoint());
60 if (element == 0) {
61 //if ((point.x() > getWidth()) || (point.y() > getHeight())) {
62 cursor->setTo(this, countChildren());
63 //}
64 return this;
65 }
66 return element;
67}
68
69void FormulaElement::elementRemoval(BasicElement* child)
70{
71 document->elementRemoval(child);
72}
73
75{
76 document->changed();
77}
78
80{
81 document->cursorHasMoved( cursor );
82}
83
84void FormulaElement::moveOutLeft( FormulaCursor* cursor )
85{
86 document->moveOutLeft( cursor );
87}
88
89void FormulaElement::moveOutRight( FormulaCursor* cursor )
90{
91 document->moveOutRight( cursor );
92}
93
94void FormulaElement::moveOutBelow( FormulaCursor* cursor )
95{
96 document->moveOutBelow( cursor );
97}
98
99void FormulaElement::moveOutAbove( FormulaCursor* cursor )
100{
101 document->moveOutAbove( cursor );
102}
103
104void FormulaElement::tell( const TQString& msg )
105{
106 document->tell( msg );
107}
108
110{
111 document->removeFormula( cursor );
112}
113
114void FormulaElement::insertFormula( FormulaCursor* cursor )
115{
116 document->insertFormula( cursor );
117}
118
121 ContextStyle::IndexStyle istyle,
122 StyleAttributes& style )
123{
124 inherited::calcSizes( context, tstyle, istyle, style );
125}
126
127
128void FormulaElement::draw( TQPainter& painter, const LuPixelRect& r,
129 const ContextStyle& context,
131 ContextStyle::IndexStyle istyle,
132 StyleAttributes& style,
133 const LuPixelPoint& parentOrigin )
134{
135 inherited::draw( painter, r, context, tstyle, istyle, style, parentOrigin );
136}
137
138
143{
144 //kdDebug( DEBUGID ) << "FormulaElement::calcSizes" << endl;
145 if ( ownBaseSize ) {
146 context.setSizeFactor( static_cast<double>( getBaseSize() )/context.baseSize() );
147 }
148 else {
149 context.setSizeFactor( 1 );
150 }
151 StyleAttributes style;
152 calcSizes( context, context.getBaseTextStyle(), ContextStyle::normal, style );
153}
154
158void FormulaElement::draw( TQPainter& painter, const LuPixelRect& r,
159 ContextStyle& context )
160{
161 //kdDebug( DEBUGID ) << "FormulaElement::draw" << endl;
162 if ( ownBaseSize ) {
163 context.setSizeFactor( static_cast<double>( getBaseSize() )/context.baseSize() );
164 }
165 else {
166 context.setSizeFactor( 1 );
167 }
168 StyleAttributes style;
169 draw( painter, r, context, context.getBaseTextStyle(),
170 ContextStyle::normal, style, LuPixelPoint() );
171}
172
173KCommand* FormulaElement::buildCommand( Container* container, Request* request )
174{
175 switch ( *request ) {
176 case req_compactExpression:
177 return 0;
178 default:
179 break;
180 }
181 return inherited::buildCommand( container, request );
182}
183
185{
186 return document->getSymbolTable();
187}
188
189
190TQDomElement FormulaElement::emptyFormulaElement( TQDomDocument& doc )
191{
192 TQDomElement element = doc.createElement( getTagName() );
193 /*
194 element.setAttribute( "VERSION", "6" );
195 if ( ownBaseSize ) {
196 element.setAttribute( "BASESIZE", baseSize );
197 }
198 */
199 return element;
200}
201
202KCommand* FormulaElement::input( Container* container, TQKeyEvent* event )
203{
204 TQChar ch = event->text().at( 0 );
205 if ( !ch.isPrint() ) {
206 int action = event->key();
207 //int state = event->state();
208 //MoveFlag flag = movementFlag(state);
209
210 switch ( action ) {
211 case TQt::Key_Return:
212 case TQt::Key_Enter: {
213 FormulaCursor* cursor = container->activeCursor();
214 insertFormula( cursor );
215 return 0;
216 }
217 }
218 }
219 return inherited::input( container, event );
220}
221
225void FormulaElement::writeDom(TQDomElement element)
226{
227 inherited::writeDom(element);
228 element.setAttribute( "VERSION", "6" );
229 if ( ownBaseSize ) {
230 element.setAttribute( "BASESIZE", baseSize );
231 }
232}
233
239{
240 if (!inherited::readAttributesFromDom(element)) {
241 return false;
242 }
243 int version = -1;
244 TQString versionStr = element.attribute( "VERSION" );
245 if ( !versionStr.isNull() ) {
246 version = versionStr.toInt();
247 }
248 if ( version > -1 ) {
249 // Version 6 added the MultilineElement (TabMarker)
250 // Version 5 added under- and overlines
251 if ( version < 4 ) {
252 convertNames( element );
253 }
254 }
255 TQString baseSizeStr = element.attribute( "BASESIZE" );
256 if ( !baseSizeStr.isNull() ) {
257 ownBaseSize = true;
258 baseSize = baseSizeStr.toInt();
259 }
260 else {
261 ownBaseSize = false;
262 }
263 return true;
264}
265
272{
274}
275
276void FormulaElement::convertNames( TQDomNode node )
277{
278 if ( node.isElement() && ( node.nodeName().upper() == "TEXT" ) ) {
279 TQDomNamedNodeMap attr = node.attributes();
280 TQDomAttr ch = attr.namedItem( "CHAR" ).toAttr();
281 if ( ch.value() == "\\" ) {
282 TQDomNode sequence = node.parentNode();
283 TQDomDocument doc = sequence.ownerDocument();
284 TQDomElement nameseq = doc.createElement( "NAMESEQUENCE" );
285 sequence.replaceChild( nameseq, node );
286
287 bool inName = true;
288 while ( inName ) {
289 inName = false;
290 TQDomNode n = nameseq.nextSibling();
291 if ( n.isElement() && ( n.nodeName().upper() == "TEXT" ) ) {
292 attr = n.attributes();
293 ch = attr.namedItem( "CHAR" ).toAttr();
294 if ( ch.value().at( 0 ).isLetter() ) {
295 nameseq.appendChild( sequence.removeChild( n ) );
296 inName = true;
297 }
298 }
299 }
300 }
301 }
302 if ( node.hasChildNodes() ) {
303 TQDomNode n = node.firstChild();
304 while ( !n.isNull() ) {
305 convertNames( n );
306 n = n.nextSibling();
307 }
308 }
309}
310
312{
313 return inherited::toLatex(); //Consider $$ sorround
314}
315
316void FormulaElement::writeMathML( TQDomDocument& doc, TQDomNode& parent, bool oasisFormat ) const
317{
318 TQDomElement de;
319 if ( !oasisFormat ) {
320 de = doc.createElementNS( "http://www.w3.org/1998/Math/MathML",
321 "math" );
322 parent.appendChild( de );
323 }
324 else {
325 TQDomElement element = doc.createElement( "math:semantics" );
326 de = doc.createElement( "math:mrow" );
327 parent.appendChild( element );
328 element.appendChild( de );
329 }
330 for ( uint i = 0; i < countChildren(); ++i ) {
331 const BasicElement* e = getChild( i );
332 e->writeMathML( doc, de, oasisFormat );
333 }
334}
335
336KFORMULA_NAMESPACE_END
virtual void writeMathML(TQDomDocument &doc, TQDomNode &parent, bool oasisFormat=false) const
Same as above, just MathML.
The document.
FormulaCursor * activeCursor()
Contains all the style information for the formela.
TextStyle
Textstyles like in TeX.
The selection.
void setTo(BasicElement *element, uint cursor, int mark=-1)
Sets the cursor to a new position.
The interface the elements expect from its document.
virtual void calcSizes(const ContextStyle &context, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle, StyleAttributes &style)
Calculates our width and height and our children's parentPosition.
BasicElement * goToPos(FormulaCursor *, const LuPixelPoint &point)
Returns the element the point is in.
void elementRemoval(BasicElement *child)
Gets called just before the child is removed from the element tree.
virtual KCommand * buildCommand(Container *, Request *)
This is called by the container to get a command depending on the current cursor position (this is ho...
void tell(const TQString &msg)
Tell the user something has happened.
void cursorHasMoved(FormulaCursor *)
Gets called when a request has the side effect of moving the cursor.
FormulaElement(FormulaDocument *container)
The container this FormulaElement belongs to must not be 0, except you really know what you are doing...
virtual TQString toLatex()
virtual void changed()
Gets called whenever something changes and we need to recalc.
virtual bool readContentFromDom(TQDomNode &node)
Reads our content from the node.
void draw(TQPainter &painter, const LuPixelRect &r, ContextStyle &context)
Draws the whole thing.
const SymbolTable & getSymbolTable() const
virtual bool readAttributesFromDom(TQDomElement element)
Reads our attributes from the element.
virtual void writeDom(TQDomElement element)
Appends our attributes to the dom element.
virtual void writeMathML(TQDomDocument &doc, TQDomNode &parent, bool oasisFormat=false) const
Same as above, just MathML.
virtual KCommand * input(Container *container, TQKeyEvent *event)
Parses the input.
TQDomElement emptyFormulaElement(TQDomDocument &doc)
For copy&paste we need to create an empty XML element.
void removeFormula(FormulaCursor *)
Gets called when the formula wants to vanish.
virtual TQString getTagName() const
Returns the tag name of this element type.
virtual KCommand * input(Container *container, TQChar ch)
Parses the input.
uint countChildren() const
virtual TQString toLatex()
virtual KCommand * buildCommand(Container *, Request *)
This is called by the container to get a command depending on the current cursor position (this is ho...
virtual bool readAttributesFromDom(TQDomElement element)
Reads our attributes from the element.
virtual BasicElement * getChild(FormulaCursor *, Direction=beforeCursor)
Returns the child at the cursor.
virtual void calcSizes(const ContextStyle &cstyle, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle, StyleAttributes &style)
Calculates our width and height and our children's parentPosition.
virtual void writeDom(TQDomElement element)
Appends our attributes to the dom element.
virtual bool readContentFromDom(TQDomNode &node)
Reads our content from the node.
virtual BasicElement * goToPos(FormulaCursor *, bool &handled, const LuPixelPoint &point, const LuPixelPoint &parentOrigin)
Sets the cursor and returns the element the point is in.
The symbol table.
Definition symboltable.h:77