lib

basicelement.h
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#ifndef BASICELEMENT_H
22#define BASICELEMENT_H
23
24// TQt Include
25#include <tqdom.h>
26#include <tqptrlist.h>
27#include <tqstring.h>
28
29// KDE Include
30
31// Formula include
32#include "contextstyle.h"
33#include "kformuladefs.h"
34
35class TQKeyEvent;
36
37class KCommand;
38
39KFORMULA_NAMESPACE_BEGIN
40
41class ComplexElement;
42class Container;
43class ElementType;
44class ElementVisitor;
45class FontCommand;
46class FormulaCursor;
47class FormulaElement;
48class SequenceElement;
49class StyleElement;
50
51
75class BasicElement
76{
77 friend class SequenceElement;
78 friend class SequenceParser;
79
80 BasicElement& operator= ( const BasicElement& ) { return *this; }
81public:
82
83 /*
84 * Each element might contain children. Each child needs
85 * its own unique number. It is not guaranteed, however,
86 * that the number stays the same all the time.
87 * (The SequenceElement's children are simply counted.)
88 */
89
90 BasicElement(BasicElement* parent = 0);
91 virtual ~BasicElement();
92
93 // deep copy
94 BasicElement( const BasicElement& );
95
96 virtual BasicElement* clone() = 0;
97
101 virtual bool accept( ElementVisitor* ) = 0;
102
107 virtual bool readOnly( const BasicElement* child ) const;
108
112 virtual FormulaElement* formula();
113
117 virtual const FormulaElement* formula() const { return parent->formula(); }
118
124 virtual TQChar getCharacter() const { return TQChar::null; }
125
130 virtual TokenType getTokenType() const { return ELEMENT; }
131
135 virtual bool isInvisible() const { return false; }
136
143 virtual BasicElement* goToPos( FormulaCursor*, bool& handled,
144 const LuPixelPoint& point, const LuPixelPoint& parentOrigin );
145
149 LuPixelPoint widgetPos();
150
151
152 // drawing
153 //
154 // Drawing depends on a context which knows the required properties like
155 // fonts, spaces and such.
156 // It is essential to calculate elements size with the same context
157 // before you draw.
158
163 virtual void calcSizes( const ContextStyle& context,
165 ContextStyle::IndexStyle istyle,
166 StyleAttributes& style ) = 0;
167
173 virtual void draw( TQPainter& painter, const LuPixelRect& r,
174 const ContextStyle& context,
176 ContextStyle::IndexStyle istyle,
177 StyleAttributes& style,
178 const LuPixelPoint& parentOrigin ) = 0;
179
180
184 virtual void dispatchFontCommand( FontCommand* /*cmd*/ ) {}
185
186 // navigation
187 //
188 // The elements are responsible to handle cursor movement themselves.
189 // To do this they need to know the direction the cursor moves and
190 // the element it comes from.
191 //
192 // The cursor might be in normal or in selection mode.
193
199 virtual void moveLeft(FormulaCursor* cursor, BasicElement* from);
200
206 virtual void moveRight(FormulaCursor* cursor, BasicElement* from);
207
213 virtual void moveUp(FormulaCursor*, BasicElement*) {}
214
220 virtual void moveDown(FormulaCursor*, BasicElement* ) {}
221
226 virtual void moveHome(FormulaCursor*) {}
227
232 virtual void moveEnd(FormulaCursor*) {}
233
238 virtual void goInside(FormulaCursor* cursor);
239
244 virtual void entered( SequenceElement* /*child*/ );
245
246 // children
247
255 //virtual void removeChild(FormulaCursor* cursor, BasicElement* child) {}
256
257
258 // main child
259 //
260 // If an element has children one has to become the main one.
261
262 virtual SequenceElement* getMainChild() { return 0; }
263 //virtual void setMainChild(SequenceElement*) {}
264
265
266 // editing
267 //
268 // Insert and remove children.
269
276 virtual void insert(FormulaCursor*, TQPtrList<BasicElement>&, Direction) {}
277
282 virtual void remove(FormulaCursor*, TQPtrList<BasicElement>&, Direction) {}
283
288 virtual void normalize(FormulaCursor*, Direction);
289
290
296 virtual bool isSenseless() { return false; }
297
301 virtual BasicElement* getChild(FormulaCursor*, Direction = beforeCursor) { return 0; }
302
303
308 virtual void selectChild(FormulaCursor*, BasicElement*) {}
309
310
315 virtual void childWillVanish(FormulaCursor*, BasicElement*) {}
316
317
321 virtual void registerTab( BasicElement* /*tab*/ ) {}
322
323
332 virtual KCommand* buildCommand( Container*, Request* ) { return 0; }
333
339 virtual KCommand* input( Container*, TQKeyEvent* ) { return 0; }
340
341 // basic support
342
343 const BasicElement* getParent() const { return parent; }
344 BasicElement* getParent() { return parent; }
345 void setParent(BasicElement* p) { parent = p; }
346
347 luPixel getX() const { return m_x; }
348 luPixel getY() const { return m_y; }
349
350 void setX( luPixel x ) { m_x = x; }
351 void setY( luPixel y ) { m_y = y; }
352
353 //TQSize getSize() { return size; }
354
355 luPixel getWidth() const { return m_width; }
356 luPixel getHeight() const { return m_height; }
357
358 void setWidth( luPixel width ) { m_width = width; }
359 void setHeight( luPixel height ) { m_height = height; }
360
361 luPixel getBaseline() const { return m_baseline; }
362 void setBaseline( luPixel line ) { m_baseline = line; }
363
364 luPixel axis( const ContextStyle& style,
366 double factor ) const {
367 return getBaseline() - style.axisHeight( tstyle, factor ); }
368
374 TQDomElement getElementDom( TQDomDocument& doc);
375
380 virtual void writeMathML( TQDomDocument& doc, TQDomNode& parent, bool oasisFormat = false ) const ;
381
386 bool buildFromDom(TQDomElement element);
387
393 int buildFromMathMLDom( TQDomElement element );
394
395 // debug
396 static int getEvilDestructionCount() { return evilDestructionCount; }
397
402 ElementType* getElementType() const { return elementType; }
403
407 virtual void setElementType(ElementType* t) { elementType = t; }
408
409 virtual void setStyle(StyleElement*) {}
410
411 virtual TQString getElementName() const { return ""; };
412protected:
413
414 //Save/load support
415
419 virtual TQString getTagName() const { return "BASIC"; }
420
424 virtual void writeDom(TQDomElement element);
425
426 virtual void writeMathMLAttributes( TQDomElement& /*element*/ ) const {};
427 virtual void writeMathMLContent( TQDomDocument& /*doc*/,
428 TQDomElement& /*element*/,
429 bool /*oasisFormat*/ ) const {};
430
435 virtual bool readAttributesFromDom(TQDomElement element);
436
442 virtual bool readContentFromDom(TQDomNode& node);
443
450 bool buildChild( SequenceElement* child, TQDomNode node, TQString name );
451
452
457 virtual bool readAttributesFromMathMLDom(const TQDomElement& element);
458
465 virtual int readContentFromMathMLDom(TQDomNode& node);
466
467
472 virtual TQString toLatex();
473
474 virtual TQString formulaString() { return ""; }
475
486 double getSize( const TQString& str, SizeType* st );
487
488 SizeType getSpace( const TQString& str );
489
490private:
491
495 double str2size( const TQString& str, SizeType* st, uint index, SizeType type );
496
502 BasicElement* parent;
503
507 //TQSize size;
508 luPixel m_width;
509 luPixel m_height;
510
514 //KoPoint position;
515 luPixel m_x;
516 luPixel m_y;
517
526 luPixel m_baseline;
527
534 //luPixel m_axis;
535
540 ElementType* elementType;
541
542 // debug
543 static int evilDestructionCount;
544};
545
546KFORMULA_NAMESPACE_END
547
548#endif // BASICELEMENT_H
Basis of every formula element.
LuPixelPoint widgetPos()
Returns our position inside the widget.
virtual FormulaElement * formula()
Provide fast access to the rootElement for each child.
virtual void remove(FormulaCursor *, TQPtrList< BasicElement > &, Direction)
Removes all selected children and returns them.
bool buildChild(SequenceElement *child, TQDomNode node, TQString name)
Returns if the SequenceElement could be constructed from the nodes first child.
virtual KCommand * input(Container *, TQKeyEvent *)
Parses the input.
virtual bool readAttributesFromMathMLDom(const TQDomElement &element)
Reads our attributes from the MathML element.
virtual bool readContentFromDom(TQDomNode &node)
Reads our content from the node.
virtual bool isInvisible() const
virtual void entered(SequenceElement *)
The cursor has entered one of our child sequences.
virtual void moveHome(FormulaCursor *)
Enters this element while moving to the left starting inside the element `from'.
virtual int readContentFromMathMLDom(TQDomNode &node)
Reads our content from the MathML node.
virtual BasicElement * getChild(FormulaCursor *, Direction=beforeCursor)
Returns the child at the cursor.
int buildFromMathMLDom(TQDomElement element)
Set this element attribute, build children and call their builFromMathMLDom.
virtual bool readOnly(const BasicElement *child) const
virtual TQString getTagName() const
Returns the tag name of this element type.
virtual TQString toLatex()
virtual void selectChild(FormulaCursor *, BasicElement *)
Sets the cursor to select the child.
virtual void goInside(FormulaCursor *cursor)
Sets the cursor inside this element to its start position.
bool buildFromDom(TQDomElement element)
Set this element attribute, build children and call their buildFromDom.
virtual void calcSizes(const ContextStyle &context, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle, StyleAttributes &style)=0
Calculates our width and height and our children's parentPosition.
virtual void registerTab(BasicElement *)
Callback for the tabs among our children.
virtual const FormulaElement * formula() const
Provide fast access to the rootElement for each child.
ElementType * getElementType() const
virtual void normalize(FormulaCursor *, Direction)
Moves the cursor to a normal place where new elements might be inserted.
virtual TQChar getCharacter() const
TQDomElement getElementDom(TQDomDocument &doc)
virtual SequenceElement * getMainChild()
Removes the child.
virtual void setElementType(ElementType *t)
Sets a new type.
virtual TokenType getTokenType() const
virtual bool readAttributesFromDom(TQDomElement element)
Reads our attributes from the element.
virtual BasicElement * goToPos(FormulaCursor *, bool &handled, const LuPixelPoint &point, const LuPixelPoint &parentOrigin)
Sets the cursor and returns the element the point is in.
double getSize(const TQString &str, SizeType *st)
Utility function that sets the size type and returns the size value from a MathML attribute string wi...
virtual bool isSenseless()
Returns wether the element has no more useful children (except its main child) and should therefore b...
virtual void dispatchFontCommand(FontCommand *)
Draws the whole element including its children.
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 void writeDom(TQDomElement element)
Appends our attributes to the dom element.
virtual bool accept(ElementVisitor *)=0
Visit this element.
virtual void insert(FormulaCursor *, TQPtrList< BasicElement > &, Direction)
Inserts all new children at the cursor position.
virtual void childWillVanish(FormulaCursor *, BasicElement *)
Moves the cursor away from the given child.
virtual void moveEnd(FormulaCursor *)
Moves the cursor to the last position in this sequence.
virtual void writeMathML(TQDomDocument &doc, TQDomNode &parent, bool oasisFormat=false) const
Same as above, just MathML.
The document.
Contains all the style information for the formela.
TextStyle
Textstyles like in TeX.
Basis of all types.
Definition elementtype.h:52
Base for all font commands that act on the current selection.
The selection.
The main element.
virtual FormulaElement * formula()
Provide fast access to the rootElement for each child.
The element that contains a number of children.