summaryrefslogtreecommitdiffstats
path: root/lib/kformula/operatorelement.cc
blob: 882eed01e50a273705bb7edfa3029707a7193da1 (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
/* This file is part of the KDE project
   Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>

   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 <algorithm>

#include <tqpainter.h>

#include <tdelocale.h>

#include "elementtype.h"
#include "sequenceelement.h"
#include "textelement.h"
#include "fontstyle.h"
#include "operatordictionary.h"
#include "operatorelement.h"
#include "identifierelement.h"
#include "numberelement.h"
#include "kformulacommand.h"
#include "kformulacontainer.h"
#include "kformuladocument.h"
#include "formulaelement.h"
#include "creationstrategy.h"

KFORMULA_NAMESPACE_BEGIN

OperatorElement::OperatorElement( BasicElement* parent ) : TokenElement( parent ),
                                                           m_form( NoForm ),
                                                           m_lspaceType( ThickMathSpace ),
                                                           m_rspaceType( ThickMathSpace ),
                                                           m_maxSizeType( InfinitySize ),
                                                           m_minSizeType( RelativeSize ),
                                                           m_minSize( 1 ),
                                                           m_fence( false ),
                                                           m_separator( false ),
                                                           m_stretchy( false ),
                                                           m_symmetric( true ),
                                                           m_largeOp( false ),
                                                           m_movableLimits( false ),
                                                           m_accent( false ),
                                                           m_customForm( false ),
                                                           m_customFence( false ),
                                                           m_customSeparator( false ),
                                                           m_customLSpace( false ),
                                                           m_customRSpace( false ),
                                                           m_customStretchy( false ),
                                                           m_customSymmetric( false ),
                                                           m_customMaxSize( false ),
                                                           m_customMinSize( false ),
                                                           m_customLargeOp( false ),
                                                           m_customMovableLimits( false ),
                                                           m_customAccent( false )
{
}

void OperatorElement::setForm( FormType type )
{
    if ( ! m_customForm ) { // Set by an attribute has higher priority
        m_form = type;
    }
    
    if ( ! isTextOnly() ) { // Only text content can be dictionary keys
        return;
    }
    TQString text;
    for ( uint i = 0; i < countChildren(); i++ ) {
        text.append( getChild( i )->getCharacter() );
    }
    TQString form;
    switch ( m_form ) {
    case PrefixForm:
        form = "prefix";
        break;
    case InfixForm:
        form = "infix";
        break;
    case PostfixForm:
        form = "postfix";
        break;
    default:
        // Should not happen
        kdWarning( DEBUGID ) << "Invalid `form' attribute value\n";
        return;
    }
    DictionaryKey key = { text.utf8(), form.ascii() };
    const OperatorDictionary* begin = operators;
    const OperatorDictionary* end = operators + OperatorDictionary::size();
    const OperatorDictionary* pos = std::lower_bound( begin, end, key );
    if ( pos != end && pos->key == key ) { // Entry found !
        if ( ! m_customFence ) {
            m_fence = pos->fence;
        }
        if ( ! m_customSeparator ) {
            m_separator = pos->separator;
        }
        if ( ! m_customLSpace ) {
            m_lspace = getSize( pos->lspace, &m_lspaceType );
            if ( m_lspaceType == NoSize ) {
                m_lspaceType = getSpace( pos->lspace );
            }
        }
        if ( ! m_customRSpace ) {
            m_rspace = getSize( pos->rspace, &m_rspaceType );
            if ( m_rspaceType == NoSize ) {
                m_rspaceType = getSpace( pos->rspace );
            }
        }
        if ( ! m_customStretchy ) {
            m_stretchy = pos->stretchy;
        }
        if ( ! m_customSymmetric ) {
            m_symmetric = pos->symmetric;
        }
        if ( ! m_customMaxSize ) {
            if ( qstrcmp( pos->maxsize, "infinity" ) == 0 ) {
                m_maxSizeType = InfinitySize;
            }
            else {
                m_maxSize = getSize( pos->maxsize, &m_maxSizeType );
                if ( m_maxSizeType == NoSize ) {
                    m_maxSizeType = getSpace( pos->maxsize );
                }
            }
        }
        if ( ! m_customMinSize ) {
            m_minSize = getSize( pos->minsize, &m_minSizeType );
            if ( m_minSizeType == NoSize ) {
                m_minSizeType = getSpace( pos->minsize );
            }
        }
        if ( ! m_customLargeOp ) {
            m_largeOp = pos->largeop;
        }
        if ( ! m_customMovableLimits ) {
            m_movableLimits = pos->movablelimits;
        }
        if ( ! m_customAccent ) {
            m_accent = pos->accent;
        }
    }
}

/*
 * Token elements' content has to be of homogeneous type. Every token element
 * must (TODO: check this) appear inside a non-token sequence, and thus, if
 * the command asks for a different content, a new element has to be created in
 * parent sequence.
 */
KCommand* OperatorElement::buildCommand( Container* container, Request* request )
{
    FormulaCursor* cursor = container->activeCursor();
    if ( cursor->isReadOnly() ) {
        formula()->tell( i18n( "write protection" ) );
        return 0;
    }

    if ( *request == req_addOperator ) {
        KFCReplace* command = new KFCReplace( i18n("Add Operator"), container );
        OperatorRequest* opr = static_cast<OperatorRequest*>( request );
        TextElement* element = creationStrategy->createTextElement( opr->ch(), true );
        command->addElement( element );
        return command;
    }

    if ( countChildren() == 0 || cursor->getPos() == countChildren() ) {
        // We are in the last position, so it's easy, call the parent to 
        // create a new child
        SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
        if ( parent ) {
            uint pos = parent->childPos( this );
            cursor->setTo( parent, pos + 1);
            return parent->buildCommand( container, request );
        }
    }
    if ( cursor->getPos() == 0 ) {
        SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
        if ( parent ) {
            uint pos = parent->childPos( this );
            cursor->setTo( parent, pos );
            return parent->buildCommand( container, request );
        }
    }

    // We are in the middle of a token, so:
    // a) Cut from mark to the end
    // b) Create a new token and add an element from key pressed
    // c) Create a new token and add elements cut previously
    // d) Move cursor to parent so that it command execution works fine

    switch( *request ) {
    case req_addTextChar: {
        KFCSplitToken* command = new KFCSplitToken( i18n("Add Text"), container );
        TextCharRequest* tr = static_cast<TextCharRequest*>( request );
        IdentifierElement* id = creationStrategy->createIdentifierElement();
        TextElement* text = creationStrategy->createTextElement( tr->ch() );
        command->addCursor( cursor );
        command->addToken( id );
        command->addContent( id, text );
        SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
        if ( parent ) {
            cursor->setTo( parent, parent->childPos( this ) + 1 );
        }
        return command;
    }

    case req_addText: {
        KFCSplitToken* command = new KFCSplitToken( i18n("Add Text"), container );
        TextRequest* tr = static_cast<TextRequest*>( request );
        IdentifierElement* id = creationStrategy->createIdentifierElement();
        command->addCursor( cursor );
        command->addToken( id );
        for ( uint i = 0; i < tr->text().length(); i++ ) {
            TextElement* text = creationStrategy->createTextElement( tr->text()[i] );
            command->addContent( id, text );
        }
        SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
        if ( parent ) {
            cursor->setTo( parent, parent->childPos( this ) + 1 );
        }
        return command;
    }

    case req_addNumber: {
        KFCSplitToken* command = new KFCSplitToken( i18n("Add Number"), container );
        NumberRequest* nr = static_cast<NumberRequest*>( request );
        NumberElement* num = creationStrategy->createNumberElement();
        TextElement* text = creationStrategy->createTextElement( nr->ch() );
        command->addCursor( cursor );
        command->addToken( num );
        command->addContent( num, text );
        SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
        if ( parent ) {
            cursor->setTo( parent, parent->childPos( this ) + 1 );
        }
        return command;
    }
    case req_addEmptyBox:
    case req_addNameSequence:
    case req_addBracket:
    case req_addSpace:
    case req_addFraction:
    case req_addRoot:
    case req_addSymbol:
    case req_addOneByTwoMatrix:
    case req_addMatrix: {
        uint pos = static_cast<SequenceElement*>(getParent())->childPos( this );
        cursor->setTo( getParent(), pos + 1);
        return getParent()->buildCommand( container, request );
    }
    default:
        return SequenceElement::buildCommand( container, request );
    }
    return 0;
}


bool OperatorElement::readAttributesFromMathMLDom( const TQDomElement &element )
{
    if ( ! BasicElement::readAttributesFromMathMLDom( element ) ) {
        return false;
    }

    TQString formStr = element.attribute( "form" ).stripWhiteSpace().lower();
    if ( ! formStr.isNull() ) {
        m_customForm = true;
        if ( formStr == "prefix" ) {
            m_form = PrefixForm;
        }
        else if ( formStr == "infix" ) {
            m_form = InfixForm;
        }
        else if ( formStr == "postfix" ) {
            m_form = PostfixForm;
        }
        else {
            kdWarning( DEBUGID ) << "Invalid value for attribute `form': " << formStr << endl;
            m_customForm = false;
        }
    }
    TQString fenceStr = element.attribute( "fence" ).stripWhiteSpace().lower();
    if ( ! fenceStr.isNull() ) {
        m_customFence = true;
        if ( fenceStr == "true" ) {
            m_fence = true;
        }
        else if ( fenceStr == "false" ) {
            m_fence = false;
        }
        else {
            kdWarning( DEBUGID ) << "Invalid value for attribute `fence': " << fenceStr << endl;
            m_customFence = false;
        }
    }
    TQString separatorStr = element.attribute( "separator" ).stripWhiteSpace().lower();
    if ( ! separatorStr.isNull() ) {
        m_customSeparator = true;
        if ( separatorStr == "true" ) {
            m_separator = true;
        }
        else if ( separatorStr == "false" ) {
            m_separator = false;
        }
        else {
            kdWarning( DEBUGID ) << "Invalid value for attribute `separator': " << separatorStr << endl;
            m_customSeparator = false;
        }
    }
    TQString lspaceStr = element.attribute( "lspace" ).stripWhiteSpace().lower();
    if ( ! lspaceStr.isNull() ) {
        m_customLSpace = true;
        m_lspace = getSize( lspaceStr, &m_lspaceType );
        if ( m_lspaceType == NoSize ) {
            m_lspaceType = getSpace( lspaceStr );
        }
    }
    TQString rspaceStr = element.attribute( "rspace" ).stripWhiteSpace().lower();
    if ( ! rspaceStr.isNull() ) {
        m_customRSpace = true;
        m_rspace = getSize( rspaceStr, &m_rspaceType );
        if ( m_rspaceType == NoSize ) {
            m_rspaceType = getSpace( rspaceStr );
        }
    }
    TQString stretchyStr = element.attribute( "stretchy" ).stripWhiteSpace().lower();
    if ( ! stretchyStr.isNull() ) {
        m_customStretchy = true;
        if ( stretchyStr == "true" ) {
            m_stretchy = true;
        }
        else if ( stretchyStr == "false" ) {
            m_stretchy = false;
        }
        else {
            kdWarning( DEBUGID ) << "Invalid value for attribute `stretchy': " << stretchyStr << endl;
            m_customStretchy = false;
        }
    }
    TQString symmetricStr = element.attribute( "symmetric" ).stripWhiteSpace().lower();
    if ( ! symmetricStr.isNull() ) {
        m_customSymmetric = true;
        if ( symmetricStr == "true" ) {
            m_symmetric = true;
        }
        else if ( symmetricStr == "false" ) {
            m_symmetric = false;
        }
        else {
            kdWarning( DEBUGID ) << "Invalid value for attribute `symmetric': " << symmetricStr << endl;
            m_customSymmetric = false;
        }
    }
    TQString maxsizeStr = element.attribute( "maxsize" ).stripWhiteSpace().lower();
    if ( ! maxsizeStr.isNull() ) {
        m_customMaxSize = true;
        if ( maxsizeStr == "infinity" ) {
            m_maxSizeType = InfinitySize;
        }
        else {
            m_maxSize = getSize( maxsizeStr, &m_maxSizeType );
            if ( m_maxSizeType == NoSize ) {
                m_maxSizeType = getSpace( maxsizeStr );
            }
        }
    }
    TQString minsizeStr = element.attribute( "minsize" ).stripWhiteSpace().lower();
    if ( ! minsizeStr.isNull() ) {
        m_customMinSize = true;
        m_minSize = getSize( minsizeStr, &m_minSizeType );
        if ( m_minSizeType == NoSize ) {
            m_minSizeType = getSpace( minsizeStr );
        }
    }
    TQString largeopStr = element.attribute( "largeop" ).stripWhiteSpace().lower();
    if ( ! largeopStr.isNull() ) {
        m_customLargeOp = true;
        if ( largeopStr == "true" ) {
            m_largeOp = true;
        }
        else if ( largeopStr == "false" ) {
            m_largeOp = false;
        }
        else {
            kdWarning( DEBUGID ) << "Invalid value for attribute `largeop': " << largeopStr << endl;
            m_customLargeOp = false;
        }
    }
    TQString movablelimitsStr = element.attribute( "movablelimits" ).stripWhiteSpace().lower();
    if ( ! movablelimitsStr.isNull() ) {
        m_customMovableLimits = true;
        if ( movablelimitsStr == "true" ) {
            m_movableLimits = true;
        }
        else if ( movablelimitsStr == "false" ) {
            m_movableLimits = false;
        }
        else {
            kdWarning( DEBUGID ) << "Invalid value for attribute `movablelimits': " << movablelimitsStr << endl;
            m_customMovableLimits = false;
        }
    }
    TQString accentStr = element.attribute( "accent" ).stripWhiteSpace().lower();
    if ( ! accentStr.isNull() ) {
        m_customAccent = true;
        if ( accentStr == "true" ) {
            m_accent = true;
        }
        else if ( accentStr == "false" ) {
            m_accent = false;
        }
        else {
            kdWarning( DEBUGID ) << "Invalid value for attribute `accent': " << accentStr << endl;
            m_customAccent = false;
        }
    }
    return true;
}

void OperatorElement::writeMathMLAttributes( TQDomElement& element ) const
{
    if ( m_customForm ) {
        switch ( m_form ) {
        case PrefixForm:
            element.setAttribute( "form", "prefix" );
            break;
        case InfixForm:
            element.setAttribute( "form", "infix" );
            break;
        case PostfixForm:
            element.setAttribute( "form", "postfix" );
        default:
            break;
        }
    }
    if ( m_customFence ) {
        element.setAttribute( "fence", m_fence ? "true" : "false" );
    }
    if ( m_customSeparator ) {
        element.setAttribute( "separator", m_separator ? "true" : "false" );
    }
    if ( m_customLSpace ) {
        writeSizeAttribute( element, "lspace", m_lspaceType, m_lspace );
    }
    if ( m_customRSpace ) {
        writeSizeAttribute( element, "rspace", m_rspaceType, m_rspace );
    }
    if ( m_customStretchy ) {
        element.setAttribute( "stretchy", m_stretchy ? "true" : "false" );
    }
    if ( m_customSymmetric ) {
        element.setAttribute( "symmetric", m_symmetric ? "true" : "false" );
    }
    if ( m_customMaxSize ) {
        writeSizeAttribute( element, "maxsize", m_maxSizeType, m_maxSize );
    }
    if ( m_customMinSize ) {
        writeSizeAttribute( element, "minsize", m_minSizeType, m_minSize );
    }
    if ( m_customLargeOp ) {
        element.setAttribute( "largeop", m_largeOp ? "true" : "false" );
    }
    if ( m_customMovableLimits ) {
        element.setAttribute( "movablelimits", m_movableLimits ? "true" : "false" );
    }
    if ( m_customAccent ) {
        element.setAttribute( "accent", m_accent ? "true" : "false" );
    }
}

void OperatorElement::writeSizeAttribute( TQDomElement& element, const TQString &attr, SizeType type, double length ) const
{
    switch ( type ) {
    case InfinitySize:
        element.setAttribute( attr, "infinity" );
        break;
    case AbsoluteSize:
        element.setAttribute( attr, TQString( "%1pt" ).arg( length ) );
        break;
    case RelativeSize:
        element.setAttribute( attr, TQString( "%1% " ).arg( length * 100.0 ) );
        break;
    case PixelSize:
        element.setAttribute( attr, TQString( "%1px " ).arg( length ) );
        break;
    case NegativeVeryVeryThinMathSpace:
        element.setAttribute( attr, "negativeveryverythinmathspace" );
        break;
    case NegativeVeryThinMathSpace:
        element.setAttribute( attr, "negativeverythinmathspace" );
        break;
    case NegativeThinMathSpace:
        element.setAttribute( attr, "negativethinmathspace" );
        break;
    case NegativeMediumMathSpace:
        element.setAttribute( attr, "negativemediummathspace" );
        break;
    case NegativeThickMathSpace:
        element.setAttribute( attr, "negativethickmathspace" );
        break;
    case NegativeVeryThickMathSpace:
        element.setAttribute( attr, "negativeverythickmathspace" );
        break;
    case NegativeVeryVeryThickMathSpace:
        element.setAttribute( attr, "negativeveryverythickmathspace" );
        break;
    case VeryVeryThinMathSpace:
        element.setAttribute( attr, "veryverythinmathspace" );
        break;
    case VeryThinMathSpace:
        element.setAttribute( attr, "verythinmathspace" );
        break;
    case ThinMathSpace:
        element.setAttribute( attr, "thinmathspace" );
        break;
    case MediumMathSpace:
        element.setAttribute( attr, "mediummathspace" );
        break;
    case ThickMathSpace:
        element.setAttribute( attr, "thickmathspace" );
        break;
    case VeryThickMathSpace:
        element.setAttribute( attr, "verythickmathspace" );
        break;
    case VeryVeryThickMathSpace:
        element.setAttribute( attr, "veryverythickmathspace" );
        break;
    default:
        break;
    }
}


KFORMULA_NAMESPACE_END