File: SubSupElement.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 (274 lines) | stat: -rw-r--r-- 8,268 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
/* This file is part of the KDE project
   Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net>
                 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 "SubSupElement.h"

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

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

#include <QPainter>

SubSupElement::SubSupElement( BasicElement* parent, ElementType elementType )
    : FixedElement( parent )
{
    m_baseElement = new RowElement( this );
    if (elementType != SupScript) {
        m_subScript = new RowElement( this );
    } else {
        m_subScript = 0;
    }
    if (elementType != SubScript) {
        m_superScript = new RowElement( this );
    } else {
        m_superScript = 0;
    }
    m_elementType = elementType;
}

SubSupElement::~SubSupElement()
{
    delete m_baseElement;
    if (m_subScript) {
        delete m_subScript;
    }
    if (m_superScript) {
        delete m_superScript;
    }
}

void SubSupElement::paint( QPainter& painter, AttributeManager* am )
{ 
    Q_UNUSED(painter)
    Q_UNUSED(am)
    /*do nothing as this element has no visual representation*/
}

void SubSupElement::layout( const AttributeManager* am )
{
    // Get the minimum amount of shifting
    qreal subscriptshift   = am->doubleOf( "subscriptshift", this ); 
    qreal superscriptshift = am->doubleOf( "superscriptshift", this );
    qreal halfthinSpace   = 0;

    if(m_elementType == SubSupScript) {
        //Add half a thin space between both sup and superscript, so there is a minimum
        //of a whole thin space between them.
        halfthinSpace = am->layoutSpacing( this )/2.0;
    }

    // The yOffset is the amount the base element is moved down to make
    // room for the superscript
    qreal yOffset = 0;
    if(m_superScript) {
        yOffset = m_superScript->height() - m_baseElement->height()/2 + halfthinSpace;
        yOffset = qMax( yOffset, superscriptshift );
    }
    qreal largestWidth = 0;
    if(m_subScript) {
        largestWidth = m_subScript->width();
    }
    if(m_superScript) {
        largestWidth = qMax( largestWidth, m_superScript->width());
        m_superScript->setOrigin( QPointF( m_baseElement->width(), 0) );
    }

    setWidth( m_baseElement->width() + largestWidth );
    setBaseLine( yOffset + m_baseElement->baseLine() );
    m_baseElement->setOrigin( QPointF( 0, yOffset ) );


    if(m_subScript) {
        qreal yPos = yOffset + qMax( m_baseElement->height()/2 + halfthinSpace, 
                                     m_baseElement->height() - m_subScript->baseLine() 
                                     + subscriptshift );
        m_subScript->setOrigin( QPointF( m_baseElement->width(), yPos ) );
	setHeight( yPos + m_subScript->height() );
    } else {
        setHeight( yOffset + m_baseElement->height() );
    }
}

const QList<BasicElement*> SubSupElement::childElements() const
{
    QList<BasicElement*> tmp;
    tmp << m_baseElement;
    if (m_subScript) {
        tmp << m_subScript;
    }
    if (m_superScript) {
        tmp << m_superScript;
    }
    return tmp;
}

bool SubSupElement::replaceChild ( BasicElement* oldelement, BasicElement* newelement )
{
    //TODO: investigate, if we really need this check
    if (newelement->elementType() == Row) {
        RowElement* newrow = static_cast<RowElement*>(newelement);
        if (oldelement == m_baseElement) {
            m_baseElement = newrow;
            return true;
        } else if (oldelement == m_subScript ) {
            m_subScript = newrow;
            return true;
        } else if (oldelement == m_superScript ) {
            m_superScript = newrow;
            return true;
        }
    }
    return false;
}

QString SubSupElement::attributesDefaultValue( const QString& attribute ) const
{
    Q_UNUSED( attribute )
    return QString();
}

ElementType SubSupElement::elementType() const
{
    return m_elementType;
}

bool SubSupElement::readMathMLContent( const KoXmlElement& parent )
{
    KoXmlElement tmp;
    int counter = 0;
    forEachElement( tmp, parent ) {
        switch (counter) {
        case 0:
            loadElement(tmp, &m_baseElement);
            break;
        case 1:
            if (m_elementType == SupScript) {
                loadElement(tmp, &m_superScript);
            }
            else {
                // Valid for both Subscript and Subsupscript
                loadElement(tmp, &m_subScript);
            }
            break;
        case 2:
            if (m_elementType==SubSupScript) {
                loadElement(tmp, &m_superScript);
            }
            else {
                debugFormula  << "Too many arguments to "
                              << ElementFactory::elementName(m_elementType);
            }
            break;
        default:
            debugFormula  << "Too many arguments to "
                          << ElementFactory::elementName(m_elementType);
        }
        counter++;
    }

    // Check for too few arguments.
    if ((counter < 3 && m_elementType == SubSupScript) || (counter < 2)) {
        debugFormula << "Not enough arguments to "<< ElementFactory::elementName(m_elementType);
        return false;
    }

    return true;
}

void SubSupElement::writeMathMLContent( KoXmlWriter* writer, const QString& ns ) const
{
    // just save the children in the right order
    m_baseElement->writeMathML( writer, ns );

    if (m_elementType != SupScript)
        m_subScript->writeMathML( writer, ns );

    if (m_elementType != SubScript )
        m_superScript->writeMathML( writer, ns );
}


int SubSupElement::endPosition() const
{
    return (m_elementType==SubSupScript ? 5 : 3);
}


bool SubSupElement::setCursorTo(FormulaCursor& cursor, QPointF point)
{
    if (cursor.isSelecting()) {
        return false;
    }
    if (m_subScript && m_subScript->boundingRect().contains(point)) {
        return m_subScript->setCursorTo(cursor,point-m_subScript->origin());
    } else if (m_superScript && m_superScript->boundingRect().contains(point)) {
        return m_superScript->setCursorTo(cursor,point-m_superScript->origin());
    } else {
        return m_baseElement->setCursorTo(cursor,point-m_baseElement->origin());
    }
    return false;
}


bool SubSupElement::moveCursor ( FormulaCursor& newcursor, FormulaCursor& oldcursor )
{
    // 0^1_2
    int childpos=newcursor.position()/2;

    switch( newcursor.direction()) {
    case MoveUp:
    case MoveDown:
        if (m_elementType==SubScript) {
            return moveHorSituation(newcursor,oldcursor,1,0);
        } else if (m_elementType==SupScript) {
            return moveHorSituation(newcursor,oldcursor,0,1);
        } else {
            switch (childpos) {
            case 1:
            case 2:
                return moveVertSituation(newcursor,oldcursor,1,2);
            case 0:
                if (newcursor.direction()==MoveDown) {
                    return moveHorSituation(newcursor,oldcursor,1,0);
                } else {
                    return moveHorSituation(newcursor,oldcursor,0,2);
                }
            }
        }
        break;
    case MoveLeft:
    case MoveRight:
        switch (childpos) {
        case 0:
            return moveHorSituation(newcursor,oldcursor,0,1);
        case 1:
            return moveHorSituation(newcursor,oldcursor,0,1);
        case 2:
            return moveHorSituation(newcursor,oldcursor,0,2);
        }
        break;
    default:
        break;
    }
    return false;
}