lib

sequenceelement.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 SEQUENCEELEMENT_H
22#define SEQUENCEELEMENT_H
23
24#include <tqptrlist.h>
25#include <tqstring.h>
26
27#include "basicelement.h"
28
29class TQKeyEvent;
30
31KFORMULA_NAMESPACE_BEGIN
32
33class SymbolTable;
35
40class SequenceElement : public BasicElement {
41 SequenceElement& operator=( const SequenceElement& ) { return *this; }
42public:
43
44 SequenceElement(BasicElement* parent = 0);
45 ~SequenceElement();
46
47 SequenceElement( const SequenceElement& );
48
49 virtual SequenceElement* clone() {
50 return new SequenceElement( *this );
51 }
52
53 virtual bool accept( ElementVisitor* visitor );
54
58 virtual bool readOnly( const FormulaCursor* ) const;
59
63 virtual bool isTextOnly() const { return textSequence; }
64
71 virtual BasicElement* goToPos( FormulaCursor*, bool& handled,
72 const LuPixelPoint& point,
73 const LuPixelPoint& parentOrigin );
74
75 // drawing
76 //
77 // Drawing depends on a context which knows the required properties like
78 // fonts, spaces and such.
79 // It is essential to calculate elements size with the same context
80 // before you draw.
81
85 void setSizeReduction(const ContextStyle& context);
86
91 bool isEmpty();
92
97 virtual void calcSizes( const ContextStyle& cstyle,
99 ContextStyle::IndexStyle istyle,
100 StyleAttributes& style );
101
107 virtual void draw( TQPainter& painter, const LuPixelRect& r,
108 const ContextStyle& context,
110 ContextStyle::IndexStyle istyle,
111 StyleAttributes& style,
112 const LuPixelPoint& parentOrigin );
113
117 virtual void dispatchFontCommand( FontCommand* cmd );
118
119 virtual void drawEmptyRect( TQPainter& painter, const ContextStyle& context,
120 double factor, const LuPixelPoint& upperLeft );
121
122 virtual void calcCursorSize( const ContextStyle& context,
123 FormulaCursor* cursor, bool smallCursor );
124
128 virtual void drawCursor( TQPainter& painter, const ContextStyle& context,
129 StyleAttributes& style, FormulaCursor* cursor,
130 bool smallCursor, bool activeCursor );
131
132 // navigation
133 //
134 // The elements are responsible to handle cursor movement themselves.
135 // To do this they need to know the direction the cursor moves and
136 // the element it comes from.
137 //
138 // The cursor might be in normal or in selection mode.
139
145 virtual void moveLeft(FormulaCursor* cursor, BasicElement* from);
146
152 virtual void moveRight(FormulaCursor* cursor, BasicElement* from);
153
158 virtual void moveWordLeft(FormulaCursor* cursor);
159
164 virtual void moveWordRight(FormulaCursor* cursor);
165
171 virtual void moveUp(FormulaCursor* cursor, BasicElement* from);
172
178 virtual void moveDown(FormulaCursor* cursor, BasicElement* from);
179
184 virtual void moveHome(FormulaCursor* cursor);
185
190 virtual void moveEnd(FormulaCursor* cursor);
191
196 virtual void goInside(FormulaCursor* cursor);
197
202 virtual void goInsideLast(FormulaCursor* cursor);
203
204
205 // children
206
214 virtual void insert(FormulaCursor*, TQPtrList<BasicElement>&, Direction);
215
220 virtual void remove(FormulaCursor*, TQPtrList<BasicElement>&, Direction);
221
226 virtual void normalize(FormulaCursor*, Direction);
227
232 virtual BasicElement* getChild(FormulaCursor*, Direction = beforeCursor);
233
238 virtual void selectChild(FormulaCursor* cursor, BasicElement* child);
239
244 virtual void childWillVanish(FormulaCursor* cursor, BasicElement* child);
245
249 uint countChildren() const { return children.count(); }
250
254 bool isChildNumber( uint pos, BasicElement* child )
255 { return children.at( pos ) == child; }
256
260 void selectAllChildren(FormulaCursor* cursor);
261
262 bool onlyTextSelected( FormulaCursor* cursor );
263
264
273 virtual KCommand* buildCommand( Container*, Request* );
274
275
281 virtual KCommand* input( Container* container, TQChar ch );
282 virtual KCommand* input( Container* container, TQKeyEvent* event );
283
288 virtual void parse();
289
293 void getChildrenDom( TQDomDocument& doc, TQDomElement elem, uint from, uint to);
294
298 void getChildrenMathMLDom( TQDomDocument& doc, TQDomNode& elem, uint from, uint to );
299
305 bool buildChildrenFromDom(TQPtrList<BasicElement>& list, TQDomNode n);
306
311 virtual TQString toLatex();
312
313 virtual TQString formulaString();
314
318 BasicElement* getChild(uint i) { return children.at(i); }
319 const BasicElement* getChild(uint i) const;
320
321 int childPos( BasicElement* child ) { return children.find( child ); }
322 int childPos( const BasicElement* child ) const;
323
324 class ChildIterator {
325 public:
326 ChildIterator( SequenceElement* sequence, int pos=0 )
327 : m_sequence( sequence ), m_pos( pos ) {}
328
329 typedef BasicElement value_type;
330 typedef BasicElement* pointer;
331 typedef BasicElement& reference;
332
333 // we simply expect the compared iterators to belong
334 // to the same sequence.
335 bool operator== ( const ChildIterator& it ) const
336 { return /*m_sequence==it.m_sequence &&*/ m_pos==it.m_pos; }
337 bool operator!= ( const ChildIterator& it ) const
338 { return /*m_sequence!=it.m_sequence ||*/ m_pos!=it.m_pos; }
339
340 const BasicElement& operator* () const
341 { return *m_sequence->getChild( m_pos ); }
342 BasicElement& operator* ()
343 { return *m_sequence->getChild( m_pos ); }
344
345 BasicElement* operator->() const
346 { return m_sequence->getChild( m_pos ); }
347
348 ChildIterator& operator++ ()
349 { ++m_pos; return *this; }
350 ChildIterator operator++ ( int )
351 { ChildIterator it( *this ); ++m_pos; return it; }
352 ChildIterator& operator-- ()
353 { --m_pos; return *this; }
354 ChildIterator operator-- ( int )
355 { ChildIterator it( *this ); --m_pos; return it; }
356 ChildIterator& operator+= ( int j )
357 { m_pos+=j; return *this; }
358 ChildIterator & operator-= ( int j )
359 { m_pos-=j; return *this; }
360
361 private:
362 SequenceElement* m_sequence;
363 int m_pos;
364 };
365
366 typedef ChildIterator iterator;
367
368 iterator begin() { return ChildIterator( this, 0 ); }
369 iterator end() { return ChildIterator( this, countChildren() ); }
370
371 static void setCreationStrategy( ElementCreationStrategy* strategy );
372
373 virtual void setStyle(StyleElement *style);
374 virtual int buildChildrenFromMathMLDom(TQPtrList<BasicElement>& list, TQDomNode n);
375 virtual int readContentFromMathMLDom(TQDomNode& node);
376 int buildMathMLChild( TQDomNode node );
377
378protected:
379
380 //Save/load support
381
385 virtual TQString getTagName() const { return "SEQUENCE"; }
386
390 virtual void writeDom(TQDomElement element);
391
392 virtual TQString getElementName() const { return "mrow"; }
393 virtual void writeMathMLContent( TQDomDocument& doc,
394 TQDomElement& element,
395 bool oasisFormat ) const;
400 virtual bool readAttributesFromDom(TQDomElement element);
401
407 virtual bool readContentFromDom(TQDomNode& node);
408
415 virtual void setChildrenPositions();
416
422 virtual BasicElement* createElement(TQString type, const TQDomElement &element);
423
430 luPixel getChildPosition( const ContextStyle& context, uint child );
431
435 virtual bool isFirstOfToken( BasicElement* child );
436
446 bool insert( uint index, BasicElement *child );
447
448 static ElementCreationStrategy* getCreationStrategy() { return creationStrategy; }
449
453 virtual luPt getSpaceBefore( const ContextStyle&, ContextStyle::TextStyle, double ) { return 0; }
454 virtual luPt getSpaceAfter( const ContextStyle&, ContextStyle::TextStyle, double ) { return 0; }
455
456 static ElementCreationStrategy* creationStrategy;
457
458private:
459
463 void removeChild(TQPtrList<BasicElement>& removedChildren, int pos);
464
465
470 TQPtrList<BasicElement> children;
471
475 ElementType* parseTree;
476
480 bool textSequence;
481
482 bool singlePipe; //The key '|' produces one '|' not '| |', '||' produces '| |'
483
484 StyleElement *style;
485};
486
487
492class NameSequence : public SequenceElement {
493 typedef SequenceElement inherited;
494public:
495
496 NameSequence( BasicElement* parent = 0 );
497
498 virtual NameSequence* clone() {
499 return new NameSequence( *this );
500 }
501
502 virtual bool accept( ElementVisitor* visitor );
503
507 //virtual bool isTextOnly() const { return true; }
508
514 virtual TQChar getCharacter() const { return '\\'; }
515
520 virtual TokenType getTokenType() const { return NAME; }
521
525 virtual SequenceElement* getMainChild() { return this; }
526
527 virtual void calcCursorSize( const ContextStyle& context,
528 FormulaCursor* cursor, bool smallCursor );
529
533 virtual void drawCursor( TQPainter& painter, const ContextStyle& context,
534 StyleAttributes& style, FormulaCursor* cursor,
535 bool smallCursor, bool activeCursor );
536
541 virtual void moveWordLeft(FormulaCursor* cursor);
542
547 virtual void moveWordRight(FormulaCursor* cursor);
548
557 virtual KCommand* buildCommand( Container*, Request* );
558
559
565 virtual KCommand* input( Container* container, TQChar ch );
566
570 virtual void setElementType( ElementType* t );
571
575 BasicElement* replaceElement( const SymbolTable& table );
576
581 static bool isValidSelection( FormulaCursor* cursor );
582
583protected:
584
588 virtual TQString getTagName() const { return "NAMESEQUENCE"; }
589
590 virtual TQString getElementName() const { return "mi"; }
596 virtual BasicElement* createElement( TQString type );
597
602 //virtual void parse();
603
609 virtual bool isFirstOfToken( BasicElement* ) { return false; }
610
611private:
612
613 KCommand* compactExpressionCmd( Container* container );
614
615 TQString buildName();
616};
617
618KFORMULA_NAMESPACE_END
619
620#endif // SEQUENCEELEMENT_H
Basis of every formula element.
The document.
Contains all the style information for the formela.
TextStyle
Textstyles like in TeX.
An object of this type needs to be known by the SequenceElement.
Basis of all types.
Definition elementtype.h:52
Base for all font commands that act on the current selection.
The selection.
BasicElement * replaceElement(const SymbolTable &table)
virtual void drawCursor(TQPainter &painter, const ContextStyle &context, StyleAttributes &style, FormulaCursor *cursor, bool smallCursor, bool activeCursor)
If the cursor is inside a sequence it needs to be drawn.
static bool isValidSelection(FormulaCursor *cursor)
Tests whether the selected elements can be inserted in a name sequence.
virtual TokenType getTokenType() const
virtual void moveWordRight(FormulaCursor *cursor)
Moves to the end of this word or if we are there already to the end of the next.
virtual SequenceElement * getMainChild()
We are our own main child.
virtual TQChar getCharacter() const
virtual bool isFirstOfToken(BasicElement *)
Parses the sequence and generates a new syntax 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...
virtual BasicElement * createElement(TQString type)
Creates a new element with the given type.
virtual void moveWordLeft(FormulaCursor *cursor)
Moves to the beginning of this word or if we are there already to the beginning of the previous.
virtual void setElementType(ElementType *t)
Sets a new type.
virtual KCommand * input(Container *container, TQChar ch)
Parses the input.
virtual TQString getTagName() const
Returns the tag name of this element type.
virtual bool accept(ElementVisitor *visitor)
Visit this element.
virtual void goInsideLast(FormulaCursor *cursor)
Sets the cursor inside this element to its last position.
virtual KCommand * input(Container *container, TQChar ch)
Parses the input.
uint countChildren() const
virtual TQString toLatex()
void selectAllChildren(FormulaCursor *cursor)
Selects all children.
void getChildrenMathMLDom(TQDomDocument &doc, TQDomNode &elem, uint from, uint to)
Stores the given childrens MathML dom in the element.
virtual void moveWordLeft(FormulaCursor *cursor)
Enters this element while moving to the left starting inside the element `from'.
virtual bool isTextOnly() const
virtual void moveEnd(FormulaCursor *cursor)
Moves the cursor to the last position in this sequence.
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 remove(FormulaCursor *, TQPtrList< BasicElement > &, Direction)
Removes all selected children and returns them.
virtual bool readAttributesFromDom(TQDomElement element)
Reads our attributes from the element.
void getChildrenDom(TQDomDocument &doc, TQDomElement elem, uint from, uint to)
Stores the given childrens dom in the element.
virtual BasicElement * getChild(FormulaCursor *, Direction=beforeCursor)
Returns the child at the cursor.
virtual bool isFirstOfToken(BasicElement *child)
virtual void drawCursor(TQPainter &painter, const ContextStyle &context, StyleAttributes &style, FormulaCursor *cursor, bool smallCursor, bool activeCursor)
If the cursor is inside a sequence it needs to be drawn.
virtual void parse()
Parses the sequence and generates a new syntax tree.
virtual void moveWordRight(FormulaCursor *cursor)
Moves to the end of this word or if we are there already to the end of the next.
luPixel getChildPosition(const ContextStyle &context, uint child)
virtual void goInside(FormulaCursor *cursor)
Sets the cursor inside this element to its start position.
virtual void dispatchFontCommand(FontCommand *cmd)
Draws the whole element including its children.
virtual luPt getSpaceBefore(const ContextStyle &, ContextStyle::TextStyle, double)
Space around sequence.
virtual void childWillVanish(FormulaCursor *cursor, BasicElement *child)
Moves the cursor away from the given child.
virtual void calcSizes(const ContextStyle &cstyle, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle, StyleAttributes &style)
Calculates our width and height and our children's parentPosition.
BasicElement * getChild(uint i)
virtual void writeDom(TQDomElement element)
Appends our attributes to the dom element.
virtual bool accept(ElementVisitor *visitor)
Visit this element.
virtual void selectChild(FormulaCursor *cursor, BasicElement *child)
Sets the cursor to select the child.
bool isChildNumber(uint pos, BasicElement *child)
virtual bool readContentFromDom(TQDomNode &node)
Reads our content from the node.
virtual void setChildrenPositions()
Sets the childrens' positions after their size has been calculated.
virtual bool readOnly(const FormulaCursor *) const
bool buildChildrenFromDom(TQPtrList< BasicElement > &list, TQDomNode n)
Builds elements from the given node and its siblings and puts them into the list.
virtual BasicElement * goToPos(FormulaCursor *, bool &handled, const LuPixelPoint &point, const LuPixelPoint &parentOrigin)
Sets the cursor and returns the element the point is in.
virtual void insert(FormulaCursor *, TQPtrList< BasicElement > &, Direction)
Inserts all new children at the cursor position.
virtual int readContentFromMathMLDom(TQDomNode &node)
Reads our content from the MathML node.
virtual TQString getTagName() const
Returns the tag name of this element type.
virtual BasicElement * createElement(TQString type, const TQDomElement &element)
Creates a new element with the given type.
virtual void normalize(FormulaCursor *, Direction)
Moves the cursor to a normal place where new elements might be inserted.
void setSizeReduction(const ContextStyle &context)
Tells the sequence to have a smaller size than its parant.
virtual void moveHome(FormulaCursor *cursor)
Enters this element while moving up starting inside the element `from'.
The symbol table.
Definition symboltable.h:77