lib

basicelement.cpp
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#include <tqstring.h>
22#include <kdebug.h>
23
24#include "contextstyle.h"
25#include "basicelement.h"
26#include "formulacursor.h"
27#include "formulaelement.h"
28#include "sequenceelement.h"
29
30KFORMULA_NAMESPACE_BEGIN
31using namespace std;
32
33
34int BasicElement::evilDestructionCount = 0;
35
36BasicElement::BasicElement( BasicElement* p )
37 : parent( p ), m_baseline( 0 ), elementType( 0 )
38{
39 setX( 0 );
40 setY( 0 );
41 setWidth( 0 );
42 setHeight( 0 );
43 evilDestructionCount++;
44}
45
46BasicElement::~BasicElement()
47{
48 evilDestructionCount--;
49}
50
51BasicElement::BasicElement( const BasicElement& other )
52 : parent( 0 ),
53 m_baseline( other.m_baseline ),
54 elementType( other.elementType )
55{
56 setX( other.getX() );
57 setY( other.getY() );
58 setWidth( other.getWidth() );
59 setHeight( other.getHeight() );
60 evilDestructionCount++;
61}
62
63
64bool BasicElement::readOnly( const BasicElement* /*child*/ ) const
65{
66 return parent->readOnly( this );
67}
68
69
71{
72 //if ( parent != 0 ) {
73 return parent->formula();
74 //}
75 //return 0;
76}
77
78
82BasicElement* BasicElement::goToPos( FormulaCursor*, bool&,
83 const LuPixelPoint& point, const LuPixelPoint& parentOrigin )
84{
85 luPixel x = point.x() - (parentOrigin.x() + getX());
86 if ((x >= 0) && (x < getWidth())) {
87 luPixel y = point.y() - (parentOrigin.y() + getY());
88 if ((y >= 0) && (y < getHeight())) {
89 return this;
90 }
91 }
92 return 0;
93}
94
99{
100 luPixel x = 0;
101 luPixel y = 0;
102 for (BasicElement* element = this; element != 0; element = element->parent) {
103 x += element->getX();
104 y += element->getY();
105 }
106 return LuPixelPoint(x, y);
107}
108
109
115{
116 BasicElement* mainChild = getMainChild();
117 if (mainChild != 0) {
118 mainChild->goInside(cursor);
119 }
120}
121
122
123void BasicElement::entered( SequenceElement* /*child*/ )
124{
125 formula()->tell( "" );
126}
127
128
134void BasicElement::moveLeft(FormulaCursor* cursor, BasicElement*)
135{
136 getParent()->moveLeft(cursor, this);
137}
138
139
145void BasicElement::moveRight(FormulaCursor* cursor, BasicElement*)
146{
147 getParent()->moveRight(cursor, this);
148}
149
150
155void BasicElement::normalize(FormulaCursor* cursor, Direction direction)
156{
157 BasicElement* element = getMainChild();
158 if (element != 0) {
159 if (direction == beforeCursor) {
160 element->moveLeft(cursor, this);
161 }
162 else {
163 element->moveRight(cursor, this);
164 }
165 }
166}
167
168
169TQDomElement BasicElement::getElementDom( TQDomDocument& doc)
170{
171 TQDomElement de = doc.createElement(getTagName());
172 writeDom(de);
173 return de;
174}
175
176
177void BasicElement::writeMathML( TQDomDocument& doc, TQDomNode& parent, bool oasisFormat ) const
178{
179 TQDomElement de = doc.createElement( oasisFormat ? "math:" + getElementName() : getElementName() );
180 writeMathMLAttributes( de );
181 writeMathMLContent( doc, de, oasisFormat );
182 parent.appendChild( de );
183}
184
185bool BasicElement::buildFromDom(TQDomElement element)
186{
187 if (element.tagName() != getTagName()) {
188 kdWarning( DEBUGID ) << "Wrong tag name " << element.tagName().latin1() << " for " << getTagName().latin1() << ".\n";
189 return false;
190 }
191 if (!readAttributesFromDom(element)) {
192 return false;
193 }
194 TQDomNode node = element.firstChild();
195 return readContentFromDom(node);
196}
197
198int BasicElement::buildFromMathMLDom(TQDomElement element)
199{/*
200 if (element.tagName() != getTagName()) {
201 kdWarning( DEBUGID ) << "Wrong tag name " << element.tagName().latin1() << " for " << getTagName().latin1() << ".\n";
202 return false;
203 }*/
204 if (!readAttributesFromMathMLDom(element)) {
205 return -1;
206 }
207 TQDomNode node = element.firstChild();
208 return readContentFromMathMLDom(node);
209}
210
214void BasicElement::writeDom(TQDomElement)
215{
216}
217
223{
224 return true;
225}
226
233{
234 return true;
235}
236
241bool BasicElement::buildChild( SequenceElement* child, TQDomNode node, TQString name )
242{
243 if (node.isElement()) {
244 TQDomElement e = node.toElement();
245 if (e.tagName().upper() == name) {
246 TQDomNode nodeInner = e.firstChild();
247 if (nodeInner.isElement()) {
248 TQDomElement element = nodeInner.toElement();
249 return child->buildFromDom( element );
250 }
251 }
252 }
253 return false;
254}
255
261{
262 return true;
263}
264
271{
272 return 1;
273}
274
276{
277 return "{}";
278}
279
290double BasicElement::getSize( const TQString& str, SizeType* st )
291{
292 int index = str.find( "%" );
293 if ( index != -1 ) {
294 return str2size( str, st, index, RelativeSize ) / 100.0;
295 }
296 index = str.find( "pt", 0, false );
297 if ( index != -1 ) {
298 return str2size( str, st, index, AbsoluteSize );
299 }
300 index = str.find( "mm", 0, false );
301 if ( index != -1 ) {
302 return str2size( str, st, index, AbsoluteSize ) * 72.0 / 20.54;
303 }
304 index = str.find( "cm", 0, false );
305 if ( index != -1 ) {
306 return str2size( str, st, index, AbsoluteSize ) * 72.0 / 2.54;
307 }
308 index = str.find( "in", 0, false );
309 if ( index != -1 ) {
310 return str2size( str, st, index, AbsoluteSize ) * 72.0;
311 }
312 index = str.find( "em", 0, false );
313 if ( index != -1 ) {
314 return str2size( str, st, index, RelativeSize );
315 }
316 index = str.find( "ex", 0, false );
317 if ( index != -1 ) {
318 return str2size( str, st, index, RelativeSize );
319 }
320 index = str.find( "pc", 0, false );
321 if ( index != -1 ) {
322 return str2size( str, st, index, AbsoluteSize ) * 12.0;
323 }
324 index = str.find( "px", 0, false );
325 if ( index != -1 ) {
326 return str2size( str, st, index, PixelSize );
327 }
328 // If there's no unit, assume 'pt'
329 return str2size( str, st, str.length(),AbsoluteSize );
330}
331
332SizeType BasicElement::getSpace( const TQString& str )
333{
334 if ( str == "negativeveryverythinmathspace" ) {
335 return NegativeVeryVeryThinMathSpace;
336 }
337 if ( str == "negativeverythinmathspace" ) {
338 return NegativeVeryThinMathSpace;
339 }
340 if ( str == "negativethinmathspace" ) {
341 return NegativeThinMathSpace;
342 }
343 if ( str == "negativemediummathspace" ) {
344 return NegativeMediumMathSpace;
345 }
346 if ( str == "negativethickmathspace" ) {
347 return NegativeThickMathSpace;
348 }
349 if ( str == "negativeverythickmathspace" ) {
350 return NegativeVeryThickMathSpace;
351 }
352 if ( str == "negativeveryverythickmathspace" ) {
353 return NegativeVeryVeryThickMathSpace;
354 }
355 if ( str == "veryverythinmathspace" ) {
356 return VeryVeryThinMathSpace;
357 }
358 if ( str == "verythinmathspace" ) {
359 return VeryThinMathSpace;
360 }
361 if ( str == "thinmathspace" ) {
362 return ThinMathSpace;
363 }
364 if ( str == "mediummathspace" ) {
365 return MediumMathSpace;
366 }
367 if ( str == "thickmathspace" ) {
368 return ThickMathSpace;
369 }
370 if ( str == "verythickmathspace" ) {
371 return VeryThickMathSpace;
372 }
373 if ( str == "veryverythickmathspace" ) {
374 return VeryVeryThickMathSpace;
375 }
376 return NoSize;
377}
378
379
383double BasicElement::str2size( const TQString& str, SizeType *st, uint index, SizeType type )
384{
385 TQString num = str.left( index );
386 bool ok;
387 double size = num.toDouble( &ok );
388 if ( ok ) {
389 if ( st ) {
390 *st = type;
391 }
392 return size;
393 }
394 if ( st ) {
395 *st = NoSize;
396 }
397 return -1;
398}
399
400KFORMULA_NAMESPACE_END
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.
bool buildChild(SequenceElement *child, TQDomNode node, TQString name)
Returns if the SequenceElement could be constructed from the nodes first child.
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 void entered(SequenceElement *)
The cursor has entered one of our child sequences.
virtual int readContentFromMathMLDom(TQDomNode &node)
Reads our content from the MathML node.
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 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 normalize(FormulaCursor *, Direction)
Moves the cursor to a normal place where new elements might be inserted.
TQDomElement getElementDom(TQDomDocument &doc)
virtual SequenceElement * getMainChild()
Removes the child.
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 void writeDom(TQDomElement element)
Appends our attributes to the dom element.
virtual void writeMathML(TQDomDocument &doc, TQDomNode &parent, bool oasisFormat=false) const
Same as above, just MathML.
The selection.
The main element.
void tell(const TQString &msg)
Tell the user something has happened.
virtual FormulaElement * formula()
Provide fast access to the rootElement for each child.