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>
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.
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.
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.
21
#include <qfontmetrics.h>
27
#include "contextstyle.h"
28
#include "elementvisitor.h"
29
#include "spaceelement.h"
32
KFORMULA_NAMESPACE_BEGIN
35
SpaceElement::SpaceElement( SpaceWidth space, bool tab, BasicElement* parent )
36
: BasicElement( parent ), spaceWidth( space ), m_tab( tab )
41
SpaceElement::SpaceElement( const SpaceElement& other )
42
: BasicElement( other ), spaceWidth( other.spaceWidth )
47
bool SpaceElement::accept( ElementVisitor* visitor )
49
return visitor->visit( this );
53
void SpaceElement::calcSizes( const ContextStyle& style,
54
ContextStyle::TextStyle tstyle,
55
ContextStyle::IndexStyle /*istyle*/ )
57
luPt mySize = style.getAdjustedSize( tstyle );
59
QFont font = style.getDefaultFont();
60
font.setPointSize( mySize );
62
QFontMetrics fm( font );
64
LuPixelRect bound = fm.boundingRect( ch );
66
setWidth( style.getSpace( tstyle, spaceWidth ) );
67
setHeight( bound.height() );
68
setBaseline( -bound.top() );
69
//setMidline( getBaseline() - fm.strikeOutPos() );
72
getParent()->registerTab( this );
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 )
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 ) )
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 ) );
105
void SpaceElement::writeDom(QDomElement element)
107
BasicElement::writeDom(element);
108
switch ( spaceWidth ) {
110
element.setAttribute( "WIDTH", "negthin" );
113
element.setAttribute( "WIDTH", "thin" );
116
element.setAttribute( "WIDTH", "medium" );
119
element.setAttribute( "WIDTH", "thick" );
122
element.setAttribute( "WIDTH", "quad" );
126
element.setAttribute( "TAB", "true" );
130
bool SpaceElement::readAttributesFromDom( QDomElement element )
132
if ( !BasicElement::readAttributesFromDom( element ) ) {
135
QString widthStr = element.attribute( "WIDTH" );
136
if( !widthStr.isNull() ) {
137
if ( widthStr.lower() == "quad" ) {
140
else if ( widthStr.lower() == "thick" ) {
143
else if ( widthStr.lower() == "medium" ) {
146
else if ( widthStr.lower() == "negthin" ) {
147
spaceWidth = NEGTHIN;
156
QString tabStr = element.attribute( "TAB" );
157
m_tab = !tabStr.isNull();
161
bool SpaceElement::readContentFromDom(QDomNode& node)
163
return BasicElement::readContentFromDom( node );
166
void SpaceElement::writeMathML( QDomDocument doc, QDomNode parent )
169
QDomElement de = doc.createElement( "mspace" );
172
switch ( spaceWidth ) {
177
width = "thinmathspace";
180
width = "mediummathspace";
183
width = "thickmathspace";
186
width = "veryverythickmathspace"; // double 'very' is appropriate.
190
de.setAttribute( "width", width );
192
parent.appendChild( de );
195
/* // worked, but I redecided.
196
switch ( spaceWidth )
199
return doc.createEntityReference( "NegativeThinSpace" );
201
return doc.createEntityReference( "ThinSpace" );
203
return doc.createEntityReference( "MediumSpace" );
205
return doc.createEntityReference( "ThickSpace" );
207
//return doc.createEntityReference( "Space" ); // misused &Space;???
208
QDomElement de = doc.createElement( "mspace" );
209
de.setAttribute( "width", "veryverythickmathspace" );
215
QString SpaceElement::toLatex()
217
switch ( spaceWidth ) {
218
case NEGTHIN: return "\\!";
219
case THIN: return "\\,";
220
case MEDIUM: return "\\>";
221
case THICK: return "\\;";
222
case QUAD: return "\\quad ";
227
KFORMULA_NAMESPACE_END