lib

sequenceelement.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003                   Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #ifndef SEQUENCEELEMENT_H
00022 #define SEQUENCEELEMENT_H
00023 
00024 #include <qptrlist.h>
00025 #include <qstring.h>
00026 
00027 #include "basicelement.h"
00028 
00029 class QKeyEvent;
00030 
00031 KFORMULA_NAMESPACE_BEGIN
00032 
00033 class SymbolTable;
00034 class ElementCreationStrategy;
00035 
00040 class SequenceElement : public BasicElement {
00041     SequenceElement& operator=( const SequenceElement& ) { return *this; }
00042 public:
00043 
00044     SequenceElement(BasicElement* parent = 0);
00045     ~SequenceElement();
00046 
00047     SequenceElement( const SequenceElement& );
00048 
00049     virtual SequenceElement* clone() {
00050         return new SequenceElement( *this );
00051     }
00052 
00053     virtual bool accept( ElementVisitor* visitor );
00054 
00058     virtual bool readOnly( const FormulaCursor* ) const;
00059 
00063     virtual bool isTextOnly() const { return textSequence; }
00064 
00071     virtual BasicElement* goToPos( FormulaCursor*, bool& handled,
00072                                    const LuPixelPoint& point,
00073                                    const LuPixelPoint& parentOrigin );
00074 
00075     // drawing
00076     //
00077     // Drawing depends on a context which knows the required properties like
00078     // fonts, spaces and such.
00079     // It is essential to calculate elements size with the same context
00080     // before you draw.
00081 
00085     void setSizeReduction(const ContextStyle& context);
00086 
00091     bool isEmpty();
00092 
00097     virtual void calcSizes( const ContextStyle& cstyle,
00098                             ContextStyle::TextStyle tstyle,
00099                             ContextStyle::IndexStyle istyle,
00100                             StyleAttributes& style );
00101 
00107     virtual void draw( QPainter& painter, const LuPixelRect& r,
00108                        const ContextStyle& context,
00109                        ContextStyle::TextStyle tstyle,
00110                        ContextStyle::IndexStyle istyle,
00111                        StyleAttributes& style,
00112                        const LuPixelPoint& parentOrigin );
00113 
00117     virtual void dispatchFontCommand( FontCommand* cmd );
00118 
00119     virtual void drawEmptyRect( QPainter& painter, const ContextStyle& context,
00120                                 double factor, const LuPixelPoint& upperLeft );
00121 
00122     virtual void calcCursorSize( const ContextStyle& context,
00123                                  FormulaCursor* cursor, bool smallCursor );
00124 
00128     virtual void drawCursor( QPainter& painter, const ContextStyle& context,
00129                              StyleAttributes& style, FormulaCursor* cursor,
00130                              bool smallCursor, bool activeCursor );
00131 
00132     // navigation
00133     //
00134     // The elements are responsible to handle cursor movement themselves.
00135     // To do this they need to know the direction the cursor moves and
00136     // the element it comes from.
00137     //
00138     // The cursor might be in normal or in selection mode.
00139 
00145     virtual void moveLeft(FormulaCursor* cursor, BasicElement* from);
00146 
00152     virtual void moveRight(FormulaCursor* cursor, BasicElement* from);
00153 
00158     virtual void moveWordLeft(FormulaCursor* cursor);
00159 
00164     virtual void moveWordRight(FormulaCursor* cursor);
00165 
00171     virtual void moveUp(FormulaCursor* cursor, BasicElement* from);
00172 
00178     virtual void moveDown(FormulaCursor* cursor, BasicElement* from);
00179 
00184     virtual void moveHome(FormulaCursor* cursor);
00185 
00190     virtual void moveEnd(FormulaCursor* cursor);
00191 
00196     virtual void goInside(FormulaCursor* cursor);
00197 
00202     virtual void goInsideLast(FormulaCursor* cursor);
00203 
00204 
00205     // children
00206 
00214     virtual void insert(FormulaCursor*, QPtrList<BasicElement>&, Direction);
00215 
00220     virtual void remove(FormulaCursor*, QPtrList<BasicElement>&, Direction);
00221 
00226     virtual void normalize(FormulaCursor*, Direction);
00227 
00232     virtual BasicElement* getChild(FormulaCursor*, Direction = beforeCursor);
00233 
00238     virtual void selectChild(FormulaCursor* cursor, BasicElement* child);
00239 
00244     virtual void childWillVanish(FormulaCursor* cursor, BasicElement* child);
00245 
00249     uint countChildren() const { return children.count(); }
00250 
00254     bool isChildNumber( uint pos, BasicElement* child )
00255         { return children.at( pos ) == child; }
00256 
00260     void selectAllChildren(FormulaCursor* cursor);
00261 
00262     bool onlyTextSelected( FormulaCursor* cursor );
00263 
00264 
00273     virtual KCommand* buildCommand( Container*, Request* );
00274 
00275 
00281     virtual KCommand* input( Container* container, QChar ch );
00282     virtual KCommand* input( Container* container, QKeyEvent* event );
00283 
00288     virtual void parse();
00289 
00293     void getChildrenDom( QDomDocument& doc, QDomElement elem, uint from, uint to);
00294 
00298     void getChildrenMathMLDom( QDomDocument& doc, QDomNode& elem, uint from, uint to );
00299 
00305     bool buildChildrenFromDom(QPtrList<BasicElement>& list, QDomNode n);
00306 
00311     virtual QString toLatex();
00312 
00313     virtual QString formulaString();
00314 
00318     BasicElement* getChild(uint i) { return children.at(i); }
00319     const BasicElement* getChild(uint i) const;
00320 
00321     int childPos( BasicElement* child ) { return children.find( child ); }
00322     int childPos( const BasicElement* child ) const;
00323 
00324     class ChildIterator {
00325     public:
00326         ChildIterator( SequenceElement* sequence, int pos=0 )
00327             : m_sequence( sequence ), m_pos( pos ) {}
00328 
00329         typedef BasicElement value_type;
00330         typedef BasicElement* pointer;
00331         typedef BasicElement& reference;
00332 
00333         // we simply expect the compared iterators to belong
00334         // to the same sequence.
00335         bool operator== ( const ChildIterator& it ) const
00336             { return /*m_sequence==it.m_sequence &&*/ m_pos==it.m_pos; }
00337         bool operator!= ( const ChildIterator& it ) const
00338             { return /*m_sequence!=it.m_sequence ||*/ m_pos!=it.m_pos; }
00339 
00340         const BasicElement& operator* () const
00341             { return *m_sequence->getChild( m_pos ); }
00342         BasicElement& operator* ()
00343             { return *m_sequence->getChild( m_pos ); }
00344 
00345         BasicElement* operator->() const
00346             { return m_sequence->getChild( m_pos ); }
00347 
00348         ChildIterator& operator++ ()
00349             { ++m_pos; return *this; }
00350         ChildIterator operator++ ( int )
00351             { ChildIterator it( *this ); ++m_pos; return it; }
00352         ChildIterator& operator-- ()
00353             { --m_pos; return *this; }
00354         ChildIterator operator-- ( int )
00355             { ChildIterator it( *this ); --m_pos; return it; }
00356         ChildIterator& operator+= ( int j )
00357             { m_pos+=j; return *this; }
00358         ChildIterator & operator-= ( int j )
00359             { m_pos-=j; return *this; }
00360 
00361     private:
00362         SequenceElement* m_sequence;
00363         int m_pos;
00364     };
00365 
00366     typedef ChildIterator iterator;
00367 
00368     iterator begin() { return ChildIterator( this, 0 ); }
00369     iterator end() { return ChildIterator( this, countChildren() ); }
00370 
00371     static void setCreationStrategy( ElementCreationStrategy* strategy );
00372 
00373     virtual void setStyle(StyleElement *style);
00374     virtual int buildChildrenFromMathMLDom(QPtrList<BasicElement>& list, QDomNode n);
00375     virtual int readContentFromMathMLDom(QDomNode& node);
00376     int buildMathMLChild( QDomNode node );
00377 
00378 protected:
00379 
00380     //Save/load support
00381 
00385     virtual QString getTagName() const { return "SEQUENCE"; }
00386 
00390     virtual void writeDom(QDomElement element);
00391 
00392     virtual QString getElementName() const { return "mrow"; }
00393     virtual void writeMathMLContent( QDomDocument& doc, 
00394                                      QDomElement& element,
00395                                      bool oasisFormat ) const;
00400     virtual bool readAttributesFromDom(QDomElement element);
00401 
00407     virtual bool readContentFromDom(QDomNode& node);
00408 
00415     virtual void setChildrenPositions();
00416 
00422     virtual BasicElement* createElement(QString type, const QDomElement &element);
00423 
00430     luPixel getChildPosition( const ContextStyle& context, uint child );
00431 
00435     virtual bool isFirstOfToken( BasicElement* child );
00436 
00446     bool insert( uint index, BasicElement *child );
00447 
00448     static ElementCreationStrategy* getCreationStrategy() { return creationStrategy; }
00449 
00453     virtual luPt getSpaceBefore( const ContextStyle&, ContextStyle::TextStyle, double ) { return 0; }
00454     virtual luPt getSpaceAfter( const ContextStyle&, ContextStyle::TextStyle, double ) { return 0; }
00455 
00456     static ElementCreationStrategy* creationStrategy;
00457     
00458 private:
00459 
00463     void removeChild(QPtrList<BasicElement>& removedChildren, int pos);
00464 
00465 
00470     QPtrList<BasicElement> children;
00471 
00475     ElementType* parseTree;
00476 
00480     bool textSequence;
00481 
00482     bool singlePipe; //The key '|' produces one '|' not '| |', '||' produces '| |'
00483 
00484     StyleElement *style;
00485 };
00486 
00487 
00492 class NameSequence : public SequenceElement {
00493     typedef SequenceElement inherited;
00494 public:
00495 
00496     NameSequence( BasicElement* parent = 0 );
00497 
00498     virtual NameSequence* clone() {
00499         return new NameSequence( *this );
00500     }
00501 
00502     virtual bool accept( ElementVisitor* visitor );
00503 
00507     //virtual bool isTextOnly() const { return true; }
00508 
00514     virtual QChar getCharacter() const { return '\\'; }
00515 
00520     virtual TokenType getTokenType() const { return NAME; }
00521 
00525     virtual SequenceElement* getMainChild() { return this; }
00526 
00527     virtual void calcCursorSize( const ContextStyle& context,
00528                                  FormulaCursor* cursor, bool smallCursor );
00529 
00533     virtual void drawCursor( QPainter& painter, const ContextStyle& context,
00534                              StyleAttributes& style, FormulaCursor* cursor, 
00535                              bool smallCursor, bool activeCursor );
00536 
00541     virtual void moveWordLeft(FormulaCursor* cursor);
00542 
00547     virtual void moveWordRight(FormulaCursor* cursor);
00548 
00557     virtual KCommand* buildCommand( Container*, Request* );
00558 
00559 
00565     virtual KCommand* input( Container* container, QChar ch );
00566 
00570     virtual void setElementType( ElementType* t );
00571 
00575     BasicElement* replaceElement( const SymbolTable& table );
00576 
00581     static bool isValidSelection( FormulaCursor* cursor );
00582 
00583 protected:
00584 
00588     virtual QString getTagName() const { return "NAMESEQUENCE"; }
00589 
00590     virtual QString getElementName() const { return "mi"; }
00596     virtual BasicElement* createElement( QString type );
00597 
00602     //virtual void parse();
00603 
00609     virtual bool isFirstOfToken( BasicElement* ) { return false; }
00610 
00611 private:
00612 
00613     KCommand* compactExpressionCmd( Container* container );
00614 
00615     QString buildName();
00616 };
00617 
00618 KFORMULA_NAMESPACE_END
00619 
00620 #endif // SEQUENCEELEMENT_H
KDE Home | KDE Accessibility Home | Description of Access Keys