~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kformula/flake/SubSupElement.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of the KDE project
2
 
   Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net>
3
 
                 2009 Jeremias Epperlein <jeeree@web.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 "SubSupElement.h"
22
 
#include "FormulaCursor.h"
23
 
#include "AttributeManager.h"
24
 
#include <KoXmlWriter.h>
25
 
#include <KoXmlReader.h>
26
 
#include <QPainter>
27
 
#include <kdebug.h>
28
 
 
29
 
SubSupElement::SubSupElement( BasicElement* parent, ElementType elementType ) : FixedElement( parent )
30
 
{
31
 
    m_baseElement = new RowElement( this );
32
 
    if (elementType!=SupScript) {
33
 
        m_subScript = new RowElement( this );
34
 
    } else {
35
 
        m_subScript = 0;
36
 
    }
37
 
    if (elementType!=SubScript) {
38
 
        m_superScript = new RowElement( this );
39
 
    } else {
40
 
        m_superScript = 0;
41
 
    }
42
 
    m_elementType = elementType;
43
 
}
44
 
 
45
 
SubSupElement::~SubSupElement()
46
 
{
47
 
    delete m_baseElement;
48
 
    if (m_subScript) {
49
 
        delete m_subScript;
50
 
    }
51
 
    if (m_superScript) {
52
 
        delete m_superScript;
53
 
    }
54
 
}
55
 
 
56
 
void SubSupElement::paint( QPainter& painter, AttributeManager* am )
57
 
58
 
    Q_UNUSED(painter)
59
 
    Q_UNUSED(am)
60
 
    /*do nothing as this element has no visual representation*/
61
 
}
62
 
 
63
 
void SubSupElement::layout( const AttributeManager* am )
64
 
{
65
 
    // Get the minimum amount of shifting
66
 
    double subscriptshift   = am->doubleOf( "subscriptshift", this ); 
67
 
    double superscriptshift = am->doubleOf( "superscriptshift", this );
68
 
    double halfthinSpace   = 0;
69
 
 
70
 
    if(m_elementType == SubSupScript) {
71
 
        //Add half a thin space between both sup and superscript, so there is a minimum
72
 
        //of a whole thin space between them.
73
 
        halfthinSpace = am->layoutSpacing( this )/2.0;
74
 
    }
75
 
 
76
 
    // The yOffset is the amount the base element is moved down to make
77
 
    // room for the superscript
78
 
    double yOffset = 0;
79
 
    if(m_superScript) {
80
 
        yOffset = m_superScript->height() - m_baseElement->height()/2 + halfthinSpace;
81
 
        yOffset = qMax( yOffset, superscriptshift );
82
 
    }
83
 
    double largestWidth = 0;
84
 
    if(m_subScript) {
85
 
        largestWidth = m_subScript->width();
86
 
    }
87
 
    if(m_superScript) {
88
 
        largestWidth = qMax( largestWidth, m_superScript->width());
89
 
        m_superScript->setOrigin( QPointF( m_baseElement->width(), 0) );
90
 
    }
91
 
 
92
 
    setWidth( m_baseElement->width() + largestWidth );
93
 
    setBaseLine( yOffset + m_baseElement->baseLine() );
94
 
    m_baseElement->setOrigin( QPointF( 0, yOffset ) );
95
 
 
96
 
 
97
 
    if(m_subScript) {
98
 
        double yPos = yOffset +
99
 
                qMax( m_baseElement->height()/2 + halfthinSpace, 
100
 
                      m_baseElement->height() - m_subScript->baseLine() 
101
 
                          + subscriptshift );
102
 
        m_subScript->setOrigin( QPointF( m_baseElement->width(), yPos ) );
103
 
        setHeight( yPos + m_subScript->height() );
104
 
    } else {
105
 
        setHeight( yOffset + m_baseElement->height() );
106
 
    }
107
 
}
108
 
 
109
 
const QList<BasicElement*> SubSupElement::childElements() const
110
 
{
111
 
    QList<BasicElement*> tmp;
112
 
    tmp << m_baseElement;
113
 
    if (m_superScript) {
114
 
        tmp << m_superScript;
115
 
    } if (m_subScript) {
116
 
        tmp << m_subScript;
117
 
    }
118
 
    return tmp;
119
 
}
120
 
 
121
 
QString SubSupElement::attributesDefaultValue( const QString& attribute ) const
122
 
{
123
 
    Q_UNUSED( attribute )
124
 
    return QString();
125
 
}
126
 
 
127
 
ElementType SubSupElement::elementType() const
128
 
{
129
 
    return m_elementType;
130
 
}
131
 
 
132
 
bool SubSupElement::readMathMLContent( const KoXmlElement& parent )
133
 
{
134
 
    KoXmlElement tmp;
135
 
    int counter=0;
136
 
    forEachElement( tmp, parent ) {
137
 
        if (counter==0) {
138
 
            loadElement(tmp,&m_baseElement);
139
 
        } else if (counter==1 && m_elementType != SupScript) {
140
 
            loadElement(tmp,&m_subScript);
141
 
        } else if ((counter==2 && m_elementType==SubSupScript) || (counter==1 && m_elementType==SupScript)) {
142
 
            loadElement(tmp,&m_superScript);
143
 
        } else if ((counter==3 && m_elementType==SubSupScript) || (counter==2)) {
144
 
            kDebug(39001) << "Too many arguments to " << ElementFactory::elementName(m_elementType);
145
 
        }
146
 
        counter++;
147
 
    }
148
 
    if ((counter<3 && m_elementType==SubSupScript) || (counter<2)) {
149
 
        kDebug(39001) << "Not enough arguments to "<< ElementFactory::elementName(m_elementType);
150
 
        return false;
151
 
    }
152
 
    return true;
153
 
}
154
 
 
155
 
void SubSupElement::writeMathMLContent( KoXmlWriter* writer ) const
156
 
{
157
 
    // just save the children in the right order
158
 
    m_baseElement->writeMathML( writer );
159
 
 
160
 
    if( m_elementType!= SupScript)
161
 
        m_subScript->writeMathML( writer );
162
 
 
163
 
    if( m_elementType!= SubScript )
164
 
        m_superScript->writeMathML( writer );
165
 
}
166
 
 
167
 
 
168
 
int SubSupElement::endPosition() const
169
 
{
170
 
    return (m_elementType==SubSupScript ? 5 : 3);
171
 
}
172
 
 
173
 
 
174
 
bool SubSupElement::setCursorTo(FormulaCursor& cursor, QPointF point)
175
 
{
176
 
    if (cursor.isSelecting()) {
177
 
        return false;
178
 
    }
179
 
    if (m_subScript && m_subScript->boundingRect().contains(point)) {
180
 
        return m_subScript->setCursorTo(cursor,point-m_subScript->origin());
181
 
    } else if (m_superScript && m_superScript->boundingRect().contains(point)) {
182
 
        return m_superScript->setCursorTo(cursor,point-m_superScript->origin());
183
 
    } else {
184
 
        return m_baseElement->setCursorTo(cursor,point-m_baseElement->origin());
185
 
    }
186
 
    return false;
187
 
}
188
 
 
189
 
 
190
 
bool SubSupElement::moveCursor ( FormulaCursor& newcursor, FormulaCursor& oldcursor )
191
 
{
192
 
    // 0^1_2
193
 
    int childpos=newcursor.position()/2;
194
 
 
195
 
    switch( newcursor.direction()) {
196
 
    case MoveUp:
197
 
    case MoveDown:
198
 
        if (m_elementType==SubScript) {
199
 
            return moveHorSituation(newcursor,oldcursor,1,0);
200
 
        } else if (m_elementType==SupScript) {
201
 
            return moveHorSituation(newcursor,oldcursor,0,1);
202
 
        } else {
203
 
            switch (childpos) {
204
 
            case 1:
205
 
            case 2:
206
 
                return moveVertSituation(newcursor,oldcursor,1,2);
207
 
            case 0:
208
 
                if (newcursor.direction()==MoveDown) {
209
 
                    return moveHorSituation(newcursor,oldcursor,1,0);
210
 
                } else {
211
 
                    return moveHorSituation(newcursor,oldcursor,0,2);
212
 
                }
213
 
            }
214
 
        }
215
 
        break;
216
 
    case MoveLeft:
217
 
    case MoveRight:
218
 
        switch (childpos) {
219
 
        case 0:
220
 
            return moveHorSituation(newcursor,oldcursor,0,1);
221
 
        case 1:
222
 
            return moveHorSituation(newcursor,oldcursor,0,1);
223
 
        case 2:
224
 
            return moveHorSituation(newcursor,oldcursor,0,2);
225
 
        }
226
 
        break;
227
 
    default:
228
 
        break;
229
 
    }
230
 
    return false;
231
 
}