File: TokenElement.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 261,632 kB
  • sloc: cpp: 650,836; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 17
file content (367 lines) | stat: -rw-r--r-- 12,073 bytes parent folder | download | duplicates (3)
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
/* This file is part of the KDE project
   Copyright (C) 2006-2007 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
                 2009 Jeremias Epperlein <jeeree@web.de>
   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 "TokenElement.h"

#include "AttributeManager.h"
#include "FormulaCursor.h"
#include "Dictionary.h"
#include "GlyphElement.h"
#include "FormulaDebug.h"

#include <KoXmlWriter.h>
#include <KoXmlReader.h>

#include <QPainter>

TokenElement::TokenElement( BasicElement* parent ) : BasicElement( parent )
{
    m_stretchHorizontally = false;
    m_stretchVertically = false;
}

const QList<BasicElement*> TokenElement::childElements() const
{
    // only return the mglyph elements
    QList<BasicElement*> tmpList;
    foreach( GlyphElement* tmp, m_glyphs )
        tmpList << tmp;

    return tmpList;
}
void TokenElement::paint( QPainter& painter, AttributeManager* am )
{
    // set the painter to background color and paint it
    painter.setPen( am->colorOf( "mathbackground", this ) );
    painter.setBrush( QBrush( painter.pen().color() ) );
    painter.drawRect( QRectF( 0.0, 0.0, width(), height() ) );

    // set the painter to foreground color and paint the text in the content path
    QColor color = am->colorOf( "mathcolor", this );
    if (!color.isValid())
        color = am->colorOf( "color", this );

    painter.translate( m_xoffset, baseLine() );
    if(m_stretchHorizontally || m_stretchVertically)
        painter.scale(width() / m_originalSize.width(), height() / m_originalSize.height());

    painter.setPen( Qt::NoPen );
    painter.setBrush( QBrush( color ) );
    painter.drawPath( m_contentPath );
}

int TokenElement::endPosition() const
{
    return m_rawString.length();
}

void TokenElement::layout( const AttributeManager* am )
{
    m_offsets.erase(m_offsets.begin(),m_offsets.end());
    m_offsets << 0.0;
    // Query the font to use
    m_font = am->font( this );
    QFontMetricsF fm(m_font);

    // save the token in an empty path
    m_contentPath = QPainterPath();

    /* Current bounding box.  Note that the left can be negative, for italics etc */
    QRectF boundingrect;
    if(m_glyphs.isEmpty()) {//optimize for the common case
        boundingrect = renderToPath(m_rawString, m_contentPath);
        for (int j = 0; j < m_rawString.length(); ++j) {
                m_offsets.append(fm.width(m_rawString.left(j+1)));
        }
     } else {
        // replace all the object replacement characters with glyphs
        // We have to keep track of the bounding box at all times
        QString chunk;
        int counter = 0;
        for( int i = 0; i < m_rawString.length(); i++ ) {
            if( m_rawString[ i ] != QChar::ObjectReplacementCharacter )
                chunk.append( m_rawString[ i ] );
            else {
                m_contentPath.moveTo(boundingrect.right(), 0);
                QRectF newbox = renderToPath( chunk, m_contentPath );
                boundingrect.setRight( boundingrect.right() + newbox.right());
                boundingrect.setTop( qMax(boundingrect.top(), newbox.top()));
                boundingrect.setBottom( qMax(boundingrect.bottom(), newbox.bottom()));
                qreal glyphoffset = m_offsets.last();
                for (int j = 0; j < chunk.length(); ++j) {
                    m_offsets << fm.width(chunk.left(j+1)) + glyphoffset;
                }
                m_contentPath.moveTo(boundingrect.right(), 0);
                newbox = m_glyphs[ counter ]->renderToPath( QString(), m_contentPath );
                boundingrect.setRight( boundingrect.right() + newbox.right());
                boundingrect.setTop( qMax(boundingrect.top(), newbox.top()));
                boundingrect.setBottom( qMax(boundingrect.bottom(), newbox.bottom()));
                m_offsets.append(newbox.width() + m_offsets.last());
                counter++;
                chunk.clear();
            }
        }
        if( !chunk.isEmpty() ) {
            m_contentPath.moveTo(boundingrect.right(), 0);
            QRectF newbox = renderToPath( chunk, m_contentPath );
            boundingrect.setRight( boundingrect.right() + newbox.right());
            boundingrect.setTop( qMax(boundingrect.top(), newbox.top()));
            boundingrect.setBottom( qMax(boundingrect.bottom(), newbox.bottom()));
//             qreal glyphoffset = m_offsets.last();
            for (int j = 0; j < chunk.length(); ++j) {
                m_offsets << fm.width(chunk.left(j+1)) + m_offsets.last();
            }
        }
    }
    //FIXME: This is only a temporary solution
    boundingrect=m_contentPath.boundingRect();
    m_offsets.removeLast();
    m_offsets.append(m_contentPath.boundingRect().right());
    //The left side may be negative, because of italised letters etc. we need to adjust for this when painting
    //The qMax is just incase.  The bounding box left should never be >0
    m_xoffset = qMax(-boundingrect.left(), (qreal)0.0);
    // As the text is added to (0,0) the baseline equals the top edge of the
    // elements bounding rect, while translating it down the text's baseline moves too
    setBaseLine( -boundingrect.y() ); // set baseline accordingly
    setWidth( boundingrect.right() + m_xoffset );
    setHeight( boundingrect.height() );
    m_originalSize = QSizeF(width(), height());
}

bool TokenElement::insertChild( int position, BasicElement* child )
{
    Q_UNUSED( position)
    Q_UNUSED( child )
    //if( child && child->elementType() == Glyph ) {
    //m_rawString.insert( QChar( QChar::ObjectReplacementCharacter ) );
    //    m_glyphs.insert();
    //    return false;
    //} else {
    return false;
    //}
}


void TokenElement::insertGlyphs ( int position, QList< GlyphElement* > glyphs )
{
    for (int i=0; i < glyphs.length(); ++i) {
        m_glyphs.insert(position+i,glyphs[i]);
    }
}


bool TokenElement::insertText ( int position, const QString& text )
{
    m_rawString.insert (position,text);
    return true;
}


QList< GlyphElement* > TokenElement::glyphList ( int position, int length )
{
    QList<GlyphElement*> tmp;
    //find out, how many glyphs we have
    int counter=0;
    for (int i=position; i<position+length; ++i) {
        if (m_rawString[ position ] == QChar::ObjectReplacementCharacter) {
            counter++;
        }
    }
    int start=0;
    //find out where we should start removing glyphs
    if (counter>0) {
        for (int i=0; i<position; ++i) {
            if (m_rawString[position] == QChar::ObjectReplacementCharacter) {
                start++;
            }
        }
    }
    for (int i=start; i<start+counter; ++i) {
        tmp.append(m_glyphs.at(i));
    }
    return tmp;
}


int TokenElement::removeText ( int position, int length )
{
    //find out, how many glyphs we have
    int counter=0;
    for (int i=position; i<position+length; ++i) {
        if (m_rawString[ position ] == QChar::ObjectReplacementCharacter) {
            counter++;
        }
    }
    
    int start=0;
    //find out where we should start removing glyphs
    if (counter>0) {
        for (int i=0; i<position; ++i) {
            if (m_rawString[position] == QChar::ObjectReplacementCharacter) {
                start++;
            }
        }
    } 
    for (int i=start; i<start+counter; ++i) {
        m_glyphs.removeAt(i);
    }
    m_rawString.remove(position,length);
    return start;
}

bool TokenElement::setCursorTo(FormulaCursor& cursor, QPointF point) {
    int i = 0;
    cursor.setCurrentElement(this);
    if (cursorOffset(endPosition())<point.x()) {
        cursor.setPosition(endPosition());
        return true;
    }
    //Find the letter we clicked on
    for( i = 1; i < endPosition(); ++i ) {
        if (point.x() < cursorOffset(i)) {
            break;
        }
    }
    
    //Find out, if we should place the cursor before or after the character
    if ((point.x()-cursorOffset(i-1))<(cursorOffset(i)-point.x())) {	
        --i;
    }
    cursor.setPosition(i);
    return true;
}


QLineF TokenElement::cursorLine(int position) const
{
    // inside tokens let the token calculate the cursor x offset
    qreal tmp = cursorOffset( position );
    QPointF top = absoluteBoundingRect().topLeft() + QPointF( tmp, 0 );
    QPointF bottom = top + QPointF( 0.0,height() );
    return QLineF(top,bottom);
}

bool TokenElement::acceptCursor( const FormulaCursor& cursor )
{
    Q_UNUSED( cursor )
    return true;
}

bool TokenElement::moveCursor(FormulaCursor& newcursor, FormulaCursor& oldcursor) {
    Q_UNUSED( oldcursor )
    if ((newcursor.direction()==MoveUp) ||
        (newcursor.direction()==MoveDown) ||
        (newcursor.isHome() && newcursor.direction()==MoveLeft) ||
        (newcursor.isEnd() && newcursor.direction()==MoveRight) ) {
        return false;
    }
    switch( newcursor.direction() ) {
    case MoveLeft:
        newcursor+=-1;
        break;
    case MoveRight:
        newcursor+=1;
        break;
    default:
        break;
    }
    return true;
}


qreal TokenElement::cursorOffset( const int position) const
{
    return m_offsets[position]+m_xoffset;
}

QFont TokenElement::font() const
{
    return m_font;
}


void TokenElement::setText ( const QString& text )
{
    removeText(0,m_rawString.length());
    insertText(0,text);
}

const QString& TokenElement::text()
{
    return m_rawString;
}


bool TokenElement::readMathMLContent( const KoXmlElement& element )
{
    // iterate over all child elements ( possible embedded glyphs ) and put the text
    // content in the m_rawString and mark glyph positions with
    // QChar::ObjectReplacementCharacter
    GlyphElement* tmpGlyph;
    KoXmlNode node = element.firstChild();
    while( !node.isNull() ) {
        if( node.isElement() && node.toElement().tagName() == "mglyph" ) {
            tmpGlyph = new GlyphElement( this );
            m_rawString.append( QChar( QChar::ObjectReplacementCharacter ) );
            tmpGlyph->readMathML( node.toElement() );
            m_glyphs.append(tmpGlyph);
        }
        else if( node.isElement() )
            return false;
        /*
        else if (node.isEntityReference()) {
            Dictionary dict;
            m_rawString.append( dict.mapEntity( node.nodeName() ) );
        }
        */
        else {
            m_rawString.append( node.toText().data() );
        }

        node = node.nextSibling();
    }
    m_rawString = m_rawString.simplified();
    return true;
}

void TokenElement::writeMathMLContent( KoXmlWriter* writer, const QString& ns ) const
{
    // split the m_rawString into text content chunks that are divided by glyphs 
    // which are represented as ObjectReplacementCharacter and write each chunk
    QStringList tmp = m_rawString.split( QChar( QChar::ObjectReplacementCharacter ) );
    for ( int i = 0; i < tmp.count(); i++ ) {
        if( m_rawString.startsWith( QChar( QChar::ObjectReplacementCharacter ) ) ) {
            m_glyphs[ i ]->writeMathML( writer, ns );
            if (i + 1 < tmp.count()) {
                writer->addTextNode( tmp[ i ] );
            }
        }
        else {
            writer->addTextNode( tmp[ i ] );
            if (i + 1 < tmp.count()) {
                m_glyphs[ i ]->writeMathML( writer, ns );
            }
        }
    }
}

const QString TokenElement::writeElementContent() const
{
    return m_rawString;
}