lib

basicelement.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 BASICELEMENT_H
00022 #define BASICELEMENT_H
00023 
00024 // Qt Include
00025 #include <qdom.h>
00026 #include <qptrlist.h>
00027 #include <qstring.h>
00028 
00029 // KDE Include
00030 
00031 // Formula include
00032 #include "contextstyle.h"
00033 #include "kformuladefs.h"
00034 
00035 class QKeyEvent;
00036 
00037 class KCommand;
00038 
00039 KFORMULA_NAMESPACE_BEGIN
00040 
00041 class ComplexElement;
00042 class Container;
00043 class ElementType;
00044 class ElementVisitor;
00045 class FontCommand;
00046 class FormulaCursor;
00047 class FormulaElement;
00048 class SequenceElement;
00049 class StyleElement;
00050 
00051 
00075 class BasicElement
00076 {
00077     friend class SequenceElement;
00078     friend class SequenceParser;
00079 
00080     BasicElement& operator= ( const BasicElement& ) { return *this; }
00081 public:
00082 
00083     /*
00084      * Each element might contain children. Each child needs
00085      * its own unique number. It is not guaranteed, however,
00086      * that the number stays the same all the time.
00087      * (The SequenceElement's children are simply counted.)
00088      */
00089 
00090     BasicElement(BasicElement* parent = 0);
00091     virtual ~BasicElement();
00092 
00093     // deep copy
00094     BasicElement( const BasicElement& );
00095 
00096     virtual BasicElement* clone() = 0;
00097 
00101     virtual bool accept( ElementVisitor* ) = 0;
00102 
00107     virtual bool readOnly( const BasicElement* child ) const;
00108 
00112     virtual FormulaElement* formula();
00113 
00117     virtual const FormulaElement* formula() const { return parent->formula(); }
00118 
00124     virtual QChar getCharacter() const { return QChar::null; }
00125 
00130     virtual TokenType getTokenType() const { return ELEMENT; }
00131 
00135     virtual bool isInvisible() const { return false; }
00136 
00143     virtual BasicElement* goToPos( FormulaCursor*, bool& handled,
00144                                    const LuPixelPoint& point, const LuPixelPoint& parentOrigin );
00145 
00149     LuPixelPoint widgetPos();
00150 
00151 
00152     // drawing
00153     //
00154     // Drawing depends on a context which knows the required properties like
00155     // fonts, spaces and such.
00156     // It is essential to calculate elements size with the same context
00157     // before you draw.
00158 
00163     virtual void calcSizes( const ContextStyle& context,
00164                             ContextStyle::TextStyle tstyle,
00165                             ContextStyle::IndexStyle istyle,
00166                             StyleAttributes& style ) = 0;
00167 
00173     virtual void draw( QPainter& painter, const LuPixelRect& r,
00174                        const ContextStyle& context,
00175                        ContextStyle::TextStyle tstyle,
00176                        ContextStyle::IndexStyle istyle,
00177                        StyleAttributes& style,
00178                        const LuPixelPoint& parentOrigin ) = 0;
00179 
00180 
00184     virtual void dispatchFontCommand( FontCommand* /*cmd*/ ) {}
00185 
00186     // navigation
00187     //
00188     // The elements are responsible to handle cursor movement themselves.
00189     // To do this they need to know the direction the cursor moves and
00190     // the element it comes from.
00191     //
00192     // The cursor might be in normal or in selection mode.
00193 
00199     virtual void moveLeft(FormulaCursor* cursor, BasicElement* from);
00200 
00206     virtual void moveRight(FormulaCursor* cursor, BasicElement* from);
00207 
00213     virtual void moveUp(FormulaCursor*, BasicElement*) {}
00214 
00220     virtual void moveDown(FormulaCursor*, BasicElement* ) {}
00221 
00226     virtual void moveHome(FormulaCursor*) {}
00227 
00232     virtual void moveEnd(FormulaCursor*) {}
00233 
00238     virtual void goInside(FormulaCursor* cursor);
00239 
00244     virtual void entered( SequenceElement* /*child*/ );
00245 
00246     // children
00247 
00255     //virtual void removeChild(FormulaCursor* cursor, BasicElement* child) {}
00256 
00257 
00258     // main child
00259     //
00260     // If an element has children one has to become the main one.
00261 
00262     virtual SequenceElement* getMainChild() { return 0; }
00263     //virtual void setMainChild(SequenceElement*) {}
00264 
00265 
00266     // editing
00267     //
00268     // Insert and remove children.
00269 
00276     virtual void insert(FormulaCursor*, QPtrList<BasicElement>&, Direction) {}
00277 
00282     virtual void remove(FormulaCursor*, QPtrList<BasicElement>&, Direction) {}
00283 
00288     virtual void normalize(FormulaCursor*, Direction);
00289 
00290 
00296     virtual bool isSenseless() { return false; }
00297 
00301     virtual BasicElement* getChild(FormulaCursor*, Direction = beforeCursor) { return 0; }
00302 
00303 
00308     virtual void selectChild(FormulaCursor*, BasicElement*) {}
00309 
00310 
00315     virtual void childWillVanish(FormulaCursor*, BasicElement*) {}
00316 
00317 
00321     virtual void registerTab( BasicElement* /*tab*/ ) {}
00322 
00323 
00332     virtual KCommand* buildCommand( Container*, Request* ) { return 0; }
00333 
00339     virtual KCommand* input( Container*, QKeyEvent* ) { return 0; }
00340 
00341     // basic support
00342 
00343     const BasicElement* getParent() const { return parent; }
00344     BasicElement* getParent() { return parent; }
00345     void setParent(BasicElement* p) { parent = p; }
00346 
00347     luPixel getX() const { return m_x; }
00348     luPixel getY() const { return m_y; }
00349 
00350     void setX( luPixel x ) { m_x = x; }
00351     void setY( luPixel y ) { m_y = y; }
00352 
00353     //QSize getSize() { return size; }
00354 
00355     luPixel getWidth() const { return m_width; }
00356     luPixel getHeight() const { return m_height; }
00357 
00358     void setWidth( luPixel width )   { m_width = width; }
00359     void setHeight( luPixel height ) { m_height = height; }
00360 
00361     luPixel getBaseline() const { return m_baseline; }
00362     void setBaseline( luPixel line ) { m_baseline = line; }
00363 
00364     luPixel axis( const ContextStyle& style,
00365                   ContextStyle::TextStyle tstyle,
00366                   double factor ) const {
00367         return getBaseline() - style.axisHeight( tstyle, factor ); }
00368 
00374     QDomElement getElementDom( QDomDocument& doc);
00375 
00380     virtual void writeMathML( QDomDocument& doc, QDomNode& parent, bool oasisFormat = false ) const ;
00381 
00386     bool buildFromDom(QDomElement element);
00387 
00393     int buildFromMathMLDom( QDomElement element );
00394 
00395     // debug
00396     static int getEvilDestructionCount() { return evilDestructionCount; }
00397 
00402     ElementType* getElementType() const { return elementType; }
00403 
00407     virtual void setElementType(ElementType* t) { elementType = t; }
00408 
00409     virtual void setStyle(StyleElement*) {}
00410 
00411     virtual QString getElementName() const { return ""; };
00412 protected:
00413 
00414     //Save/load support
00415 
00419     virtual QString getTagName() const { return "BASIC"; }
00420 
00424     virtual void writeDom(QDomElement element);
00425 
00426     virtual void writeMathMLAttributes( QDomElement& /*element*/ ) const {};
00427     virtual void writeMathMLContent( QDomDocument& /*doc*/, 
00428                                      QDomElement& /*element*/,
00429                                      bool /*oasisFormat*/ ) const {};
00430 
00435     virtual bool readAttributesFromDom(QDomElement element);
00436 
00442     virtual bool readContentFromDom(QDomNode& node);
00443 
00450     bool buildChild( SequenceElement* child, QDomNode node, QString name );
00451 
00452 
00457     virtual bool readAttributesFromMathMLDom(const QDomElement& element);
00458 
00465     virtual int readContentFromMathMLDom(QDomNode& node);
00466 
00467 
00472     virtual QString toLatex();
00473 
00474     virtual QString formulaString() { return ""; }
00475 
00486     double getSize( const QString& str, SizeType* st );
00487 
00488     SizeType getSpace( const QString& str );
00489 
00490 private:
00491 
00495     double str2size( const QString& str, SizeType* st, uint index, SizeType type );
00496 
00502     BasicElement* parent;
00503 
00507     //QSize size;
00508     luPixel m_width;
00509     luPixel m_height;
00510 
00514     //KoPoint position;
00515     luPixel m_x;
00516     luPixel m_y;
00517 
00526     luPixel m_baseline;
00527 
00534     //luPixel m_axis;
00535 
00540     ElementType* elementType;
00541 
00542     // debug
00543     static int evilDestructionCount;
00544 };
00545 
00546 KFORMULA_NAMESPACE_END
00547 
00548 #endif // BASICELEMENT_H
KDE Home | KDE Accessibility Home | Description of Access Keys