~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to lib/kformula/spaceelement.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
 
3
                      Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.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., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
#include <qfontmetrics.h>
 
22
#include <qpainter.h>
 
23
 
 
24
#include <kdebug.h>
 
25
#include <kprinter.h>
 
26
 
 
27
#include "contextstyle.h"
 
28
#include "elementvisitor.h"
 
29
#include "spaceelement.h"
 
30
 
 
31
 
 
32
KFORMULA_NAMESPACE_BEGIN
 
33
 
 
34
 
 
35
SpaceElement::SpaceElement( SpaceWidth space, bool tab, BasicElement* parent )
 
36
    : BasicElement( parent ), spaceWidth( space ), m_tab( tab )
 
37
{
 
38
}
 
39
 
 
40
 
 
41
SpaceElement::SpaceElement( const SpaceElement& other )
 
42
    : BasicElement( other ), spaceWidth( other.spaceWidth )
 
43
{
 
44
}
 
45
 
 
46
 
 
47
bool SpaceElement::accept( ElementVisitor* visitor )
 
48
{
 
49
    return visitor->visit( this );
 
50
}
 
51
 
 
52
 
 
53
void SpaceElement::calcSizes( const ContextStyle& style,
 
54
                              ContextStyle::TextStyle tstyle,
 
55
                              ContextStyle::IndexStyle /*istyle*/ )
 
56
{
 
57
    luPt mySize = style.getAdjustedSize( tstyle );
 
58
 
 
59
    QFont font = style.getDefaultFont();
 
60
    font.setPointSize( mySize );
 
61
 
 
62
    QFontMetrics fm( font );
 
63
    QChar ch = 'x';
 
64
    LuPixelRect bound = fm.boundingRect( ch );
 
65
 
 
66
    setWidth( style.getSpace( tstyle, spaceWidth ) );
 
67
    setHeight( bound.height() );
 
68
    setBaseline( -bound.top() );
 
69
    //setMidline( getBaseline() - fm.strikeOutPos() );
 
70
 
 
71
    if ( m_tab ) {
 
72
        getParent()->registerTab( this );
 
73
    }
 
74
}
 
75
 
 
76
void SpaceElement::draw( QPainter& painter, const LuPixelRect& /*r*/,
 
77
                         const ContextStyle& style,
 
78
                         ContextStyle::TextStyle /*tstyle*/,
 
79
                         ContextStyle::IndexStyle /*istyle*/,
 
80
                         const LuPixelPoint& parentOrigin )
 
81
{
 
82
    LuPixelPoint myPos(parentOrigin.x()+getX(), parentOrigin.y()+getY());
 
83
    // there is such a thing as negative space!
 
84
    //if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
 
85
    //    return;
 
86
 
 
87
    if ( style.edit() ) {
 
88
        painter.setPen( style.getEmptyColor() );
 
89
        painter.drawLine( style.layoutUnitToPixelX( myPos.x() ),
 
90
                          style.layoutUnitToPixelY( myPos.y()+getHeight() ),
 
91
                          style.layoutUnitToPixelX( myPos.x()+getWidth()-1 ),
 
92
                          style.layoutUnitToPixelY( myPos.y()+getHeight() ) );
 
93
        painter.drawLine( style.layoutUnitToPixelX( myPos.x() ),
 
94
                          style.layoutUnitToPixelY( myPos.y()+getHeight() ),
 
95
                          style.layoutUnitToPixelX( myPos.x() ),
 
96
                          style.layoutUnitToPixelY( myPos.y()+getHeight()-getHeight()/5 ) );
 
97
        painter.drawLine( style.layoutUnitToPixelX( myPos.x()+getWidth()-1 ),
 
98
                          style.layoutUnitToPixelY( myPos.y()+getHeight() ),
 
99
                          style.layoutUnitToPixelX( myPos.x()+getWidth()-1 ),
 
100
                          style.layoutUnitToPixelY( myPos.y()+getHeight()-getHeight()/5 ) );
 
101
    }
 
102
}
 
103
 
 
104
 
 
105
void SpaceElement::writeDom(QDomElement element)
 
106
{
 
107
    BasicElement::writeDom(element);
 
108
    switch ( spaceWidth ) {
 
109
    case NEGTHIN:
 
110
        element.setAttribute( "WIDTH", "negthin" );
 
111
        break;
 
112
    case THIN:
 
113
        element.setAttribute( "WIDTH", "thin" );
 
114
        break;
 
115
    case MEDIUM:
 
116
        element.setAttribute( "WIDTH", "medium" );
 
117
        break;
 
118
    case THICK:
 
119
        element.setAttribute( "WIDTH", "thick" );
 
120
        break;
 
121
    case QUAD:
 
122
        element.setAttribute( "WIDTH", "quad" );
 
123
        break;
 
124
    }
 
125
    if ( m_tab ) {
 
126
        element.setAttribute( "TAB", "true" );
 
127
    }
 
128
}
 
129
 
 
130
bool SpaceElement::readAttributesFromDom( QDomElement element )
 
131
{
 
132
    if ( !BasicElement::readAttributesFromDom( element ) ) {
 
133
        return false;
 
134
    }
 
135
    QString widthStr = element.attribute( "WIDTH" );
 
136
    if( !widthStr.isNull() ) {
 
137
        if ( widthStr.lower() == "quad" ) {
 
138
            spaceWidth = QUAD;
 
139
        }
 
140
        else if ( widthStr.lower() == "thick" ) {
 
141
            spaceWidth = THICK;
 
142
        }
 
143
        else if ( widthStr.lower() == "medium" ) {
 
144
            spaceWidth = MEDIUM;
 
145
        }
 
146
        else if ( widthStr.lower() == "negthin" ) {
 
147
            spaceWidth = NEGTHIN;
 
148
        }
 
149
        else {
 
150
            spaceWidth = THIN;
 
151
        }
 
152
    }
 
153
    else {
 
154
        return false;
 
155
    }
 
156
    QString tabStr = element.attribute( "TAB" );
 
157
    m_tab = !tabStr.isNull();
 
158
    return true;
 
159
}
 
160
 
 
161
bool SpaceElement::readContentFromDom(QDomNode& node)
 
162
{
 
163
    return BasicElement::readContentFromDom( node );
 
164
}
 
165
 
 
166
void SpaceElement::writeMathML( QDomDocument doc, QDomNode parent )
 
167
{
 
168
 
 
169
    QDomElement de = doc.createElement( "mspace" );
 
170
    QString width;
 
171
 
 
172
    switch ( spaceWidth ) {
 
173
    case NEGTHIN:
 
174
        width = "-3/18em";
 
175
        break;
 
176
    case THIN:
 
177
        width = "thinmathspace";
 
178
        break;
 
179
    case MEDIUM:
 
180
        width = "mediummathspace";
 
181
        break;
 
182
    case THICK:
 
183
        width = "thickmathspace";
 
184
        break;
 
185
    case QUAD:
 
186
        width = "veryverythickmathspace"; // double 'very' is appropriate.
 
187
        break;
 
188
    }
 
189
 
 
190
    de.setAttribute( "width", width );
 
191
 
 
192
    parent.appendChild( de );
 
193
 
 
194
 
 
195
    /* // worked, but I redecided.
 
196
    switch ( spaceWidth )
 
197
    {
 
198
    case NEGTHIN:
 
199
        return doc.createEntityReference( "NegativeThinSpace" );
 
200
    case THIN:
 
201
        return doc.createEntityReference( "ThinSpace" );
 
202
    case MEDIUM:
 
203
        return doc.createEntityReference( "MediumSpace" );
 
204
    case THICK:
 
205
        return doc.createEntityReference( "ThickSpace" );
 
206
    case QUAD:
 
207
        //return doc.createEntityReference( "Space" ); // misused &Space;???
 
208
        QDomElement de = doc.createElement( "mspace" );
 
209
        de.setAttribute( "width", "veryverythickmathspace" );
 
210
        return de;
 
211
    }*/
 
212
 
 
213
}
 
214
 
 
215
QString SpaceElement::toLatex()
 
216
{
 
217
    switch ( spaceWidth ) {
 
218
    case NEGTHIN: return "\\!";
 
219
    case THIN:    return "\\,";
 
220
    case MEDIUM:  return "\\>";
 
221
    case THICK:   return "\\;";
 
222
    case QUAD:    return "\\quad ";
 
223
    }
 
224
    return "";
 
225
}
 
226
 
 
227
KFORMULA_NAMESPACE_END