summaryrefslogtreecommitdiffstats
path: root/lib/kformula/basicelement.h
blob: 025dfbc6d640433ac60eab5080dc55eb405df9d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* This file is part of the KDE project
   Copyright (C) 2001 Andrea Rizzi <[email protected]>
	              Ulrich Kuettler <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef BASICELEMENT_H
#define BASICELEMENT_H

// TQt Include
#include <tqdom.h>
#include <tqptrlist.h>
#include <tqstring.h>

// KDE Include

// Formula include
#include "contextstyle.h"
#include "kformuladefs.h"

class TQKeyEvent;

class KCommand;

KFORMULA_NAMESPACE_BEGIN

class ComplexElement;
class Container;
class ElementType;
class ElementVisitor;
class FontCommand;
class FormulaCursor;
class FormulaElement;
class SequenceElement;
class StyleElement;


/**
 * Basis of every formula element. An element is used basically by
 * other elements and by the @ref FormulaCursor .
 *
 * Each element knows its size (a rect that includes all tqchildren)
 * and how to draw itself. See @ref calcSizes and @ref draw .
 *
 * An element might contain valid cursor position. If the cursor
 * enters the element it must find the next valid position
 * depending on the direction in that the cursor moves and the
 * element it comes from. There might also be some flags inside the
 * cursor that tell how it wants to move. See @ref moveLeft ,
 * @ref moveRight , @ref moveUp , @ref moveDown .
 *
 * To build a tree an element must own tqchildren. If there are tqchildren
 * there must be a main child. This is the child that might be used to
 * replace the element. See @ref getMainChild().
 *
 * If there can be tqchildren you might want to @ref insert and @ref remove
 * them. After a removal the element might be senseless.
 * (See @ref isSenseless )
 * If it is it must be removed.
 */
class BasicElement
{
    friend class SequenceElement;
    friend class SequenceParser;

    BasicElement& operator= ( const BasicElement& ) { return *this; }
public:

    /*
     * Each element might contain tqchildren. Each child needs
     * its own unique number. It is not guaranteed, however,
     * that the number stays the same all the time.
     * (The SequenceElement's tqchildren are simply counted.)
     */

    BasicElement(BasicElement* tqparent = 0);
    virtual ~BasicElement();

    // deep copy
    BasicElement( const BasicElement& );

    virtual BasicElement* clone() = 0;

    /**
     * Visit this element. An implementation of the visitor pattern.
     */
    virtual bool accept( ElementVisitor* ) = 0;

    /**
     * @returns whether the child should be read-only. The idea is
     * that a read-only tqparent has read-only tqchildren.
     */
    virtual bool readOnly( const BasicElement* child ) const;

    /**
     * Provide fast access to the rootElement for each child.
     */
    virtual FormulaElement* formula();

    /**
     * Provide fast access to the rootElement for each child.
     */
    virtual const FormulaElement* formula() const { return tqparent->formula(); }

    /**
     * @returns the character that represents this element. Used for
     * parsing a sequence.
     * This is guaranteed to be TQChar::null for all non-text elements.
     */
    virtual TQChar getCharacter() const { return TQChar::null; }

    /**
     * @returns the type of this element. Used for
     * parsing a sequence.
     */
    virtual TokenType getTokenType() const { return ELEMENT; }

    /**
     * @returns true if we don't want to see the element.
     */
    virtual bool isInvisible() const { return false; }

    /**
     * Sets the cursor and returns the element the point is in.
     * The handled flag shows whether the cursor has been set.
     * This is needed because only the innermost matching element
     * is allowed to set the cursor.
     */
    virtual BasicElement* goToPos( FormulaCursor*, bool& handled,
                                   const LuPixelPoint& point, const LuPixelPoint& parentOrigin );

    /**
     * Returns our position inside the widget.
     */
    LuPixelPoint widgetPos();


    // drawing
    //
    // Drawing depends on a context which knows the required properties like
    // fonts, spaces and such.
    // It is essential to calculate elements size with the same context
    // before you draw.

    /**
     * Calculates our width and height and
     * our tqchildren's parentPosition.
     */
    virtual void calcSizes( const ContextStyle& context,
							ContextStyle::TextStyle tstyle,
							ContextStyle::IndexStyle istyle,
							StyleAttributes& style ) = 0;

    /**
     * Draws the whole element including its tqchildren.
     * The `parentOrigin' is the point this element's tqparent starts.
     * We can use our parentPosition to get our own origin then.
     */
    virtual void draw( TQPainter& painter, const LuPixelRect& r,
                       const ContextStyle& context,
                       ContextStyle::TextStyle tstyle,
                       ContextStyle::IndexStyle istyle,
					   StyleAttributes& style,
                       const LuPixelPoint& parentOrigin ) = 0;


    /**
     * Dispatch this FontCommand to all our TextElement tqchildren.
     */
    virtual void dispatchFontCommand( FontCommand* /*cmd*/ ) {}

    // navigation
    //
    // The elements are responsible to handle cursor movement themselves.
    // To do this they need to know the direction the cursor moves and
    // the element it comes from.
    //
    // The cursor might be in normal or in selection mode.

    /**
     * Enters this element while moving to the left starting inside
     * the element `from'. Searches for a cursor position inside
     * this element or to the left of it.
     */
    virtual void moveLeft(FormulaCursor* cursor, BasicElement* from);

    /**
     * Enters this element while moving to the right starting inside
     * the element `from'. Searches for a cursor position inside
     * this element or to the right of it.
     */
    virtual void moveRight(FormulaCursor* cursor, BasicElement* from);

    /**
     * Enters this element while moving up starting inside
     * the element `from'. Searches for a cursor position inside
     * this element or above it.
     */
    virtual void moveUp(FormulaCursor*, BasicElement*) {}

    /**
     * Enters this element while moving down starting inside
     * the element `from'. Searches for a cursor position inside
     * this element or below it.
     */
    virtual void moveDown(FormulaCursor*, BasicElement* ) {}

    /**
     * Moves the cursor to the first position in this sequence.
     * (That is before the first child.)
     */
    virtual void moveHome(FormulaCursor*) {}

    /**
     * Moves the cursor to the last position in this sequence.
     * (That is behind the last child.)
     */
    virtual void moveEnd(FormulaCursor*) {}

    /**
     * Sets the cursor inside this element to its start position.
     * For most elements that is the main child.
     */
    virtual void goInside(FormulaCursor* cursor);

    /**
     * The cursor has entered one of our child sequences.
     * This is a good point to tell the user where he is.
     */
    virtual void entered( SequenceElement* /*child*/ );

    // tqchildren

    /**
     * Removes the child. If this was the main child this element might
     * request its own removal.
     * The cursor is the one that caused the removal. It has to be moved
     * to the place any user expects the cursor after that particular
     * element has been removed.
     */
    //virtual void removeChild(FormulaCursor* cursor, BasicElement* child) {}


    // main child
    //
    // If an element has tqchildren one has to become the main one.

    virtual SequenceElement* getMainChild() { return 0; }
    //virtual void setMainChild(SequenceElement*) {}


    // editing
    //
    // Insert and remove tqchildren.

    /**
     * Inserts all new tqchildren at the cursor position. Places the
     * cursor according to the direction.
     *
     * The list will be emptied but stays the property of the caller.
     */
    virtual void insert(FormulaCursor*, TQPtrList<BasicElement>&, Direction) {}

    /**
     * Removes all selected tqchildren and returns them. Places the
     * cursor to where the tqchildren have been.
     */
    virtual void remove(FormulaCursor*, TQPtrList<BasicElement>&, Direction) {}

    /**
     * Moves the cursor to a normal place where new elements
     * might be inserted.
     */
    virtual void normalize(FormulaCursor*, Direction);


    /**
     * Returns wether the element has no more useful
     * tqchildren (except its main child) and should therefore
     * be replaced by its main child's content.
     */
    virtual bool isSenseless() { return false; }

    /**
     * Returns the child at the cursor.
     */
    virtual BasicElement* getChild(FormulaCursor*, Direction = beforeCursor) { return 0; }


    /**
     * Sets the cursor to select the child. The mark is placed before,
     * the position behind it.
     */
    virtual void selectChild(FormulaCursor*, BasicElement*) {}


    /**
     * Moves the cursor away from the given child. The cursor is
     * guaranteed to be inside this element.
     */
    virtual void childWillVanish(FormulaCursor*, BasicElement*) {}


    /**
     * Callback for the tabs among our tqchildren. Needed for tqalignment.
     */
    virtual void registerTab( BasicElement* /*tab*/ ) {}


    /**
     * This is called by the container to get a command depending on
     * the current cursor position (this is how the element gets chosen)
     * and the request.
     *
     * @returns the command that performs the requested action with
     * the containers active cursor.
     */
    virtual KCommand* buildCommand( Container*, Request* ) { return 0; }

    /**
     * Parses the input. It's the container which does create
     * new elements because it owns the undo stack. But only the
     * sequence knows what chars are allowed.
     */
    virtual KCommand* input( Container*, TQKeyEvent* ) { return 0; }

    // basic support

    const BasicElement* getParent() const { return tqparent; }
    BasicElement* getParent() { return tqparent; }
    void setParent(BasicElement* p) { tqparent = p; }

    luPixel getX() const { return m_x; }
    luPixel getY() const { return m_y; }

    void setX( luPixel x ) { m_x = x; }
    void setY( luPixel y ) { m_y = y; }

    //TQSize getSize() { return size; }

    luPixel getWidth() const { return m_width; }
    luPixel getHeight() const { return m_height; }

    void setWidth( luPixel width )   { m_width = width; }
    void setHeight( luPixel height ) { m_height = height; }

    luPixel getBaseline() const { return m_baseline; }
    void setBaseline( luPixel line ) { m_baseline = line; }

    luPixel axis( const ContextStyle& style,
                  ContextStyle::TextStyle tstyle,
                  double factor ) const {
        return getBaseline() - style.axisHeight( tstyle, factor ); }

    /**
     * @return a TQDomElement that contain as DomChildren the
     * tqchildren, and as attribute the attribute of this
     * element.
     */
    TQDomElement getElementDom( TQDomDocument& doc);

    /**
     * Same as above, just MathML.
     * It shouldn't be redefined but for exceptional cases, use the general writeMathML* API instead
     */
    virtual void writeMathML( TQDomDocument& doc, TQDomNode& tqparent, bool oasisFormat = false ) const ;

    /**
     * Set this element attribute, build tqchildren and
     * call their buildFromDom.
     */
    bool buildFromDom(TQDomElement element);

    /**
     * Set this element attribute, build tqchildren and call 
     * their builFromMathMLDom.
     * Returns the number of nodes processed or -1 if it failed.
     */
	int buildFromMathMLDom( TQDomElement element );

    // debug
    static int getEvilDestructionCount() { return evilDestructionCount; }

    /**
     * @returns our type. This is an object from our tqparent's syntax tree
     * or 0 if there was a very bad parsing error.
     */
    ElementType* getElementType() const { return elementType; }

    /**
     * Sets a new type. This is done during parsing.
     */
    virtual void setElementType(ElementType* t) { elementType = t; }

	virtual void setStyle(StyleElement*) {}

    virtual TQString getElementName() const { return ""; };
protected:

    //Save/load support

    /**
     * Returns the tag name of this element type.
     */
    virtual TQString getTagName() const { return "BASIC"; }

    /**
     * Appends our attributes to the dom element.
     */
    virtual void writeDom(TQDomElement element);

    virtual void writeMathMLAttributes( TQDomElement& /*element*/ ) const {};
    virtual void writeMathMLContent( TQDomDocument& /*doc*/, 
                                     TQDomElement& /*element*/,
                                     bool /*oasisFormat*/ ) const {};

    /**
     * Reads our attributes from the element.
     * Returns false if it failed.
     */
    virtual bool readAttributesFromDom(TQDomElement element);

    /**
     * Reads our content from the node. Sets the node to the next node
     * that needs to be read.
     * Returns false if it failed.
     */
    virtual bool readContentFromDom(TQDomNode& node);

    /**
     * Returns if the SequenceElement could be constructed from the nodes first child.
     * The node name must match the given name.
     *
     * This is a service for all subclasses that contain tqchildren.
     */
    bool buildChild( SequenceElement* child, TQDomNode node, TQString name );


    /**
     * Reads our attributes from the MathML element.
     * Returns false if it failed.
     */
	virtual bool readAttributesFromMathMLDom(const TQDomElement& element);

    /**
     * Reads our content from the MathML node. Sets the node to the next node
     * that needs to be read. It is sometimes needed to read more than one node
     * (e. g. for fence operators).
     * Returns the number of nodes processed or -1 if it failed.
     */
	virtual int readContentFromMathMLDom(TQDomNode& node);


    /**
     * @returns the latex representation of the element and
     * of the element's tqchildren
     */
    virtual TQString toLatex();

    virtual TQString formulaString() { return ""; }

    /**
     * Utility function that sets the size type and returns the size value from
     * a MathML attribute string with unit as defined in Section 2.4.4.2
     *
     * @returns the size value
     *
     * @param str the attribute string.
     * @param st size type container. It will be properly assigned to its size
     * type or NoSize if str is invalid
     */
    double getSize( const TQString& str, SizeType* st );

    SizeType getSpace( const TQString& str );

private:

    /**
     * Used internally by getSize()
     */
    double str2size( const TQString& str, SizeType* st, uint index, SizeType type );

    /**
     * Our tqparent.
     * The tqparent might not be null except for the FormulaElement
     * that is the top of the element tree.
     */
    BasicElement* tqparent;

    /**
     * This elements size.
     */
    //TQSize size;
    luPixel m_width;
    luPixel m_height;

    /**
     * Our position relative to our tqparent.
     */
    //KoPoint position;
    luPixel m_x;
    luPixel m_y;

    /**
     * The position of our base line from
     * the upper border. A sequence aligns its elements
     * along this line.
     *
     * There are elements (like matrix) that don't have a base line. It is
     * -1 in this case. The tqalignment is done using the middle line.
     */
    luPixel m_baseline;

    /**
     * The position of our middle line from
     * the upper border. The strike out position.
     *
     * This will have to go. (?)
     */
    //luPixel m_axis;

    /**
     * The token that describes our type. Please note that we don't
     * own it.
     */
    ElementType* elementType;

    // debug
    static int evilDestructionCount;
};

KFORMULA_NAMESPACE_END

#endif // BASICELEMENT_H