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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
/* 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.
*/
#include <tqpainter.h>
#include <kdebug.h>
#include <klocale.h>
#include "elementvisitor.h"
#include "formulaelement.h"
#include "formulacursor.h"
#include "fractionelement.h"
#include "sequenceelement.h"
KFORMULA_NAMESPACE_BEGIN
using namespace std;
FractionElement::FractionElement(BasicElement* tqparent) : BasicElement(tqparent),
m_lineThicknessType( NoSize ),
m_numAlign( NoHorizontalAlign ),
m_denomAlign( NoHorizontalAlign ),
m_customBevelled( false )
{
numerator = new SequenceElement(this);
denominator = new SequenceElement(this);
}
FractionElement::~FractionElement()
{
delete denominator;
delete numerator;
}
FractionElement::FractionElement( const FractionElement& other )
: BasicElement( other ),
m_lineThicknessType( other.m_lineThicknessType ),
m_lineThickness( other.m_lineThickness ),
m_numAlign( other.m_numAlign ),
m_denomAlign( other.m_denomAlign ),
m_customBevelled( other.m_customBevelled ),
m_bevelled( other.m_bevelled )
{
numerator = new SequenceElement( *( other.numerator ) );
denominator = new SequenceElement( *( other.denominator ) );
numerator->setParent( this );
denominator->setParent( this );
}
bool FractionElement::accept( ElementVisitor* visitor )
{
return visitor->visit( this );
}
void FractionElement::entered( SequenceElement* child )
{
if ( child == numerator ) {
formula()->tell( i18n( "Numerator" ) );
}
else {
formula()->tell( i18n( "Denominator" ) );
}
}
BasicElement* FractionElement::goToPos( FormulaCursor* cursor, bool& handled,
const LuPixelPoint& point, const LuPixelPoint& parentOrigin )
{
BasicElement* e = BasicElement::goToPos(cursor, handled, point, parentOrigin);
if (e != 0) {
LuPixelPoint myPos(parentOrigin.x() + getX(),
parentOrigin.y() + getY());
e = numerator->goToPos(cursor, handled, point, myPos);
if (e != 0) {
return e;
}
e = denominator->goToPos(cursor, handled, point, myPos);
if (e != 0) {
return e;
}
luPixel dx = point.x() - myPos.x();
luPixel dy = point.y() - myPos.y();
// the positions after the numerator / denominator
if ((dx > numerator->getX()) &&
(dy < numerator->getHeight())) {
numerator->moveLeft(cursor, this);
handled = true;
return numerator;
}
else if ((dx > denominator->getX()) &&
(dy > denominator->getY())) {
denominator->moveLeft(cursor, this);
handled = true;
return denominator;
}
return this;
}
return 0;
}
/**
* Calculates our width and height and
* our tqchildren's parentPosition.
*/
void FractionElement::calcSizes( const ContextStyle& context,
ContextStyle::TextStyle tstyle,
ContextStyle::IndexStyle istyle,
StyleAttributes& style )
{
ContextStyle::TextStyle i_tstyle = context.convertTextStyleFraction( tstyle );
ContextStyle::IndexStyle u_istyle = context.convertIndexStyleUpper( istyle );
ContextStyle::IndexStyle l_istyle = context.convertIndexStyleLower( istyle );
double factor = style.sizeFactor();
numerator->calcSizes( context, i_tstyle, u_istyle, style );
denominator->calcSizes( context, i_tstyle, l_istyle, style );
luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, factor ) );
double linethickness = lineThickness( context, factor );
setWidth( TQMAX( numerator->getWidth(), denominator->getWidth() ) );
setHeight( numerator->getHeight() + denominator->getHeight() +
2*distY + linethickness );
setBaseline( tqRound( numerator->getHeight() + distY + .5*linethickness
+ context.axisHeight( tstyle, factor ) ) );
numerator->setX( ( getWidth() - numerator->getWidth() ) / 2 );
denominator->setX( ( getWidth() - denominator->getWidth() ) / 2 );
numerator->setY( 0 );
denominator->setY( getHeight() - denominator->getHeight() );
}
/**
* 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.
*/
void FractionElement::draw( TQPainter& painter, const LuPixelRect& r,
const ContextStyle& context,
ContextStyle::TextStyle tstyle,
ContextStyle::IndexStyle istyle,
StyleAttributes& style,
const LuPixelPoint& parentOrigin )
{
LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
//if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
// return;
numerator->draw( painter, r, context,
context.convertTextStyleFraction( tstyle ),
context.convertIndexStyleUpper( istyle ), style, myPos);
if (denominator) { // Can be temporarily 0 see FractionElement::remove
denominator->draw( painter, r, context,
context.convertTextStyleFraction( tstyle ),
context.convertIndexStyleLower( istyle ), style,
myPos);
}
if ( withLine() ) {
// TODO: thickness
double factor = style.sizeFactor();
double linethickness = lineThickness( context, factor );
painter.setPen( TQPen( style.color(),
context.layoutUnitToPixelY( linethickness ) ) );
painter.drawLine( context.layoutUnitToPixelX( myPos.x() ),
context.layoutUnitToPixelY( myPos.y() + axis( context, tstyle, factor ) ),
context.layoutUnitToPixelX( myPos.x() + getWidth() ),
context.layoutUnitToPixelY( myPos.y() + axis( context, tstyle, factor ) ) );
}
}
void FractionElement::dispatchFontCommand( FontCommand* cmd )
{
numerator->dispatchFontCommand( cmd );
denominator->dispatchFontCommand( cmd );
}
/**
* 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.
*/
void FractionElement::moveLeft(FormulaCursor* cursor, BasicElement* from)
{
if (cursor->isSelectionMode()) {
getParent()->moveLeft(cursor, this);
}
else {
bool linear = cursor->getLinearMovement();
if (from == getParent()) {
if (linear) {
denominator->moveLeft(cursor, this);
}
else {
numerator->moveLeft(cursor, this);
}
}
else if (from == denominator) {
numerator->moveLeft(cursor, this);
}
else {
getParent()->moveLeft(cursor, this);
}
}
}
/**
* 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.
*/
void FractionElement::moveRight(FormulaCursor* cursor, BasicElement* from)
{
if (cursor->isSelectionMode()) {
getParent()->moveRight(cursor, this);
}
else {
bool linear = cursor->getLinearMovement();
if (from == getParent()) {
numerator->moveRight(cursor, this);
}
else if (from == numerator) {
if (linear) {
denominator->moveRight(cursor, this);
}
else {
getParent()->moveRight(cursor, this);
}
}
else {
getParent()->moveRight(cursor, this);
}
}
}
/**
* Enters this element while moving up starting inside
* the element `from'. Searches for a cursor position inside
* this element or above it.
*/
void FractionElement::moveUp(FormulaCursor* cursor, BasicElement* from)
{
if (cursor->isSelectionMode()) {
getParent()->moveUp(cursor, this);
}
else {
if (from == getParent()) {
denominator->moveRight(cursor, this);
}
else if (from == denominator) {
numerator->moveRight(cursor, this);
}
else {
getParent()->moveUp(cursor, this);
}
}
}
/**
* Enters this element while moving down starting inside
* the element `from'. Searches for a cursor position inside
* this element or below it.
*/
void FractionElement::moveDown(FormulaCursor* cursor, BasicElement* from)
{
if (cursor->isSelectionMode()) {
getParent()->moveDown(cursor, this);
}
else {
if (from == getParent()) {
numerator->moveRight(cursor, this);
}
else if (from == numerator) {
denominator->moveRight(cursor, this);
}
else {
getParent()->moveDown(cursor, this);
}
}
}
/**
* Reinserts the denominator if it has been removed.
*/
void FractionElement::insert(FormulaCursor* cursor,
TQPtrList<BasicElement>& newChildren,
Direction direction)
{
if (cursor->getPos() == denominatorPos) {
denominator = static_cast<SequenceElement*>(newChildren.take(0));
denominator->setParent(this);
if (direction == beforeCursor) {
denominator->moveLeft(cursor, this);
}
else {
denominator->moveRight(cursor, this);
}
cursor->setSelection(false);
formula()->changed();
}
}
/**
* Removes all selected tqchildren and returns them. Places the
* cursor to where the tqchildren have been.
*
* We remove ourselve if we are requested to remove our numerator.
*
* It is possible to remove the denominator. But after this we
* are senseless and the caller is required to replace us.
*/
void FractionElement::remove(FormulaCursor* cursor,
TQPtrList<BasicElement>& removedChildren,
Direction direction)
{
switch (cursor->getPos()) {
case numeratorPos:
getParent()->selectChild(cursor, this);
getParent()->remove(cursor, removedChildren, direction);
break;
case denominatorPos:
removedChildren.append(denominator);
formula()->elementRemoval(denominator);
denominator = 0;
cursor->setTo(this, denominatorPos);
formula()->changed();
break;
}
}
/**
* Returns wether the element has no more useful
* tqchildren (except its main child) and should therefore
* be replaced by its main child's content.
*/
bool FractionElement::isSenseless()
{
return denominator == 0;
}
// main child
//
// If an element has tqchildren one has to become the main one.
SequenceElement* FractionElement::getMainChild()
{
return numerator;
}
// void FractionElement::setMainChild(SequenceElement* child)
// {
// formula()->elementRemoval(numerator);
// numerator = child;
// numerator->setParent(this);
// formula()->changed();
// }
/**
* Sets the cursor to select the child. The mark is placed before,
* the position behind it.
*/
void FractionElement::selectChild(FormulaCursor* cursor, BasicElement* child)
{
if (child == numerator) {
cursor->setTo(this, numeratorPos);
}
else if (child == denominator) {
cursor->setTo(this, denominatorPos);
}
}
/**
* Appends our attributes to the dom element.
*/
void FractionElement::writeDom(TQDomElement element)
{
BasicElement::writeDom(element);
TQDomDocument doc = element.ownerDocument();
if (!withLine()) element.setAttribute("NOLINE", 1);
TQDomElement num = doc.createElement("NUMERATOR");
num.appendChild(numerator->getElementDom(doc));
element.appendChild(num);
TQDomElement den = doc.createElement("DENOMINATOR");
den.appendChild(denominator->getElementDom(doc));
element.appendChild(den);
}
/**
* Reads our attributes from the element.
* Returns false if it failed.
*/
bool FractionElement::readAttributesFromDom(TQDomElement element)
{
if (!BasicElement::readAttributesFromDom(element)) {
return false;
}
TQString lineStr = element.attribute("NOLINE");
if(!lineStr.isNull()) {
m_lineThicknessType = RelativeSize;
m_lineThickness = lineStr.toInt();
}
return true;
}
/**
* Reads our attributes from the MathML element.
* Returns false if it failed.
*/
bool FractionElement::readAttributesFromMathMLDom(const TQDomElement& element)
{
if ( ! BasicElement::readAttributesFromMathMLDom( element ) ) {
return false;
}
TQString linethicknessStr = element.attribute( "linethickness" ).lower();
if ( ! linethicknessStr.isNull() ) {
if ( linethicknessStr == "thin" ) {
m_lineThicknessType = RelativeSize;
m_lineThickness = 0.5; // ### Arbitrary size
}
else if ( linethicknessStr == "medium" ) {
m_lineThicknessType = RelativeSize;
m_lineThickness = 1.0;
}
else if ( linethicknessStr == "thick" ) {
m_lineThicknessType = RelativeSize;
m_lineThickness = 2.0; // ### Arbitrary size
}
else {
m_lineThickness = getSize( linethicknessStr, &m_lineThicknessType );
}
}
TQString numalignStr = element.attribute( "numalign" ).lower();
if ( ! numalignStr.isNull() ) {
if ( numalignStr == "left" ) {
m_numAlign = LeftHorizontalAlign;
}
else if ( numalignStr == "center" ) {
m_numAlign = CenterHorizontalAlign;
}
else if ( numalignStr == "right" ) {
m_numAlign = RightHorizontalAlign;
}
}
TQString denomalignStr = element.attribute( "denomalign" ).lower();
if ( ! denomalignStr.isNull() ) {
if ( denomalignStr == "left" ) {
m_denomAlign = LeftHorizontalAlign;
}
else if ( denomalignStr == "center" ) {
m_denomAlign = CenterHorizontalAlign;
}
else if ( denomalignStr == "right" ) {
m_denomAlign = RightHorizontalAlign;
}
}
TQString bevelledStr = element.attribute( "bevelled" ).lower();
if ( ! bevelledStr.isNull() ) {
m_customBevelled = true;
if ( bevelledStr == "true" ) {
m_bevelled = true;
}
else {
m_bevelled = false;
}
}
return true;
}
/**
* Reads our content from the node. Sets the node to the next node
* that needs to be read.
* Returns false if it failed.
*/
bool FractionElement::readContentFromDom(TQDomNode& node)
{
if (!BasicElement::readContentFromDom(node)) {
return false;
}
if ( !buildChild( numerator, node, "NUMERATOR" ) ) {
kdWarning( DEBUGID ) << "Empty numerator in FractionElement." << endl;
return false;
}
node = node.nextSibling();
if ( !buildChild( denominator, node, "DENOMINATOR" ) ) {
kdWarning( DEBUGID ) << "Empty denominator in FractionElement." << endl;
return false;
}
node = node.nextSibling();
return true;
}
/**
* Reads our content from the MathML node. Sets the node to the next node
* that needs to be read.
* Returns false if it failed.
*/
int FractionElement::readContentFromMathMLDom(TQDomNode& node)
{
if ( BasicElement::readContentFromMathMLDom( node ) == -1 ) {
return -1;
}
int numeratorNumber = numerator->buildMathMLChild( node );
if ( numeratorNumber == -1 ) {
kdWarning( DEBUGID ) << "Empty numerator in FractionElement." << endl;
return -1;
}
for (int i = 0; i < numeratorNumber; i++ ) {
if ( node.isNull() ) {
return -1;
}
node = node.nextSibling();
}
if ( denominator->buildMathMLChild( node ) == -1 ) {
kdWarning( DEBUGID ) << "Empty denominator in FractionElement." << endl;
return -1;
}
return 1;
}
TQString FractionElement::toLatex()
{
if ( withLine() ) {
return "\\frac{" + numerator->toLatex() +"}{" + denominator->toLatex() + "}";
}
else {
return "{" + numerator->toLatex() + "\\atop " + denominator->toLatex() + "}";
}
}
TQString FractionElement::formulaString()
{
return "(" + numerator->formulaString() + ")/(" + denominator->formulaString() + ")";
}
void FractionElement::writeMathMLAttributes( TQDomElement& element ) const
{
switch ( m_lineThicknessType ) {
case AbsoluteSize:
element.setAttribute( "linethickness", TQString( "%1pt" ).tqarg( m_lineThickness ) );
break;
case RelativeSize:
element.setAttribute( "linethickness", TQString( "%1%" ).tqarg( m_lineThickness * 100.0 ) );
break;
case PixelSize:
element.setAttribute( "linethickness", TQString( "%1px" ).tqarg( m_lineThickness ) );
break;
default:
break;
}
switch ( m_numAlign ) {
case LeftHorizontalAlign:
element.setAttribute( "numalign", "left" );
break;
case CenterHorizontalAlign:
element.setAttribute( "numalign", "center" );
break;
case RightHorizontalAlign:
element.setAttribute( "numalign", "right" );
break;
default:
break;
}
switch ( m_denomAlign ) {
case LeftHorizontalAlign:
element.setAttribute( "denomalign", "left" );
break;
case CenterHorizontalAlign:
element.setAttribute( "denomalign", "center" );
break;
case RightHorizontalAlign:
element.setAttribute( "denomalign", "right" );
break;
default:
break;
}
if ( m_customBevelled ) {
element.setAttribute( "bevelled", m_bevelled ? "true" : "false" );
}
}
void FractionElement::writeMathMLContent( TQDomDocument& doc,
TQDomElement& element,
bool oasisFormat ) const
{
numerator->writeMathML( doc, element, oasisFormat );
denominator->writeMathML( doc, element, oasisFormat );
}
double FractionElement::lineThickness( const ContextStyle& context, double factor )
{
double linethickness = context.getLineWidth( factor );
switch ( m_lineThicknessType ) {
case AbsoluteSize:
linethickness = context.ptToLayoutUnitPixX( m_lineThickness );
break;
case RelativeSize:
linethickness *= m_lineThickness;
break;
case PixelSize:
linethickness = m_lineThickness;
break;
default:
break;
}
return linethickness;
}
KFORMULA_NAMESPACE_END
|