00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include <qfontmetrics.h>
00022
#include <qpainter.h>
00023
00024
#include <kdebug.h>
00025
#include <kprinter.h>
00026
00027
#include "contextstyle.h"
00028
#include "elementvisitor.h"
00029
#include "spaceelement.h"
00030
00031
00032 KFORMULA_NAMESPACE_BEGIN
00033
00034
00035 SpaceElement::SpaceElement( SpaceWidth space,
bool tab,
BasicElement* parent )
00036 :
BasicElement( parent ), spaceWidth( space ), m_tab( tab )
00037 {
00038 }
00039
00040
00041 SpaceElement::SpaceElement(
const SpaceElement& other )
00042 :
BasicElement( other ), spaceWidth( other.spaceWidth )
00043 {
00044 }
00045
00046
00047 bool SpaceElement::accept(
ElementVisitor* visitor )
00048 {
00049
return visitor->
visit(
this );
00050 }
00051
00052
00053 void SpaceElement::calcSizes(
const ContextStyle& style,
00054 ContextStyle::TextStyle tstyle,
00055 ContextStyle::IndexStyle )
00056 {
00057 luPt mySize = style.getAdjustedSize( tstyle );
00058
00059
QFont font = style.getDefaultFont();
00060 font.setPointSize( mySize );
00061
00062
QFontMetrics fm( font );
00063
QChar ch =
'x';
00064
LuPixelRect bound = fm.boundingRect( ch );
00065
00066 setWidth( style.getSpace( tstyle, spaceWidth ) );
00067 setHeight( bound.height() );
00068 setBaseline( -bound.top() );
00069
00070
00071
if ( m_tab ) {
00072 getParent()->
registerTab(
this );
00073 }
00074 }
00075
00076 void SpaceElement::draw(
QPainter& painter,
const LuPixelRect& ,
00077
const ContextStyle& style,
00078 ContextStyle::TextStyle ,
00079 ContextStyle::IndexStyle ,
00080
const LuPixelPoint& parentOrigin )
00081 {
00082
LuPixelPoint myPos(parentOrigin.x()+getX(), parentOrigin.y()+getY());
00083
00084
00085
00086
00087
if ( style.edit() ) {
00088 painter.setPen( style.getEmptyColor() );
00089 painter.drawLine( style.layoutUnitToPixelX( myPos.x() ),
00090 style.layoutUnitToPixelY( myPos.y()+getHeight() ),
00091 style.layoutUnitToPixelX( myPos.x()+getWidth()-1 ),
00092 style.layoutUnitToPixelY( myPos.y()+getHeight() ) );
00093 painter.drawLine( style.layoutUnitToPixelX( myPos.x() ),
00094 style.layoutUnitToPixelY( myPos.y()+getHeight() ),
00095 style.layoutUnitToPixelX( myPos.x() ),
00096 style.layoutUnitToPixelY( myPos.y()+getHeight()-getHeight()/5 ) );
00097 painter.drawLine( style.layoutUnitToPixelX( myPos.x()+getWidth()-1 ),
00098 style.layoutUnitToPixelY( myPos.y()+getHeight() ),
00099 style.layoutUnitToPixelX( myPos.x()+getWidth()-1 ),
00100 style.layoutUnitToPixelY( myPos.y()+getHeight()-getHeight()/5 ) );
00101 }
00102 }
00103
00104
00105 void SpaceElement::writeDom(
QDomElement element)
00106 {
00107 BasicElement::writeDom(element);
00108
switch ( spaceWidth ) {
00109
case NEGTHIN:
00110 element.setAttribute(
"WIDTH",
"negthin" );
00111
break;
00112
case THIN:
00113 element.setAttribute(
"WIDTH",
"thin" );
00114
break;
00115
case MEDIUM:
00116 element.setAttribute(
"WIDTH",
"medium" );
00117
break;
00118
case THICK:
00119 element.setAttribute(
"WIDTH",
"thick" );
00120
break;
00121
case QUAD:
00122 element.setAttribute(
"WIDTH",
"quad" );
00123
break;
00124 }
00125
if ( m_tab ) {
00126 element.setAttribute(
"TAB",
"true" );
00127 }
00128 }
00129
00130 bool SpaceElement::readAttributesFromDom(
QDomElement element )
00131 {
00132
if ( !BasicElement::readAttributesFromDom( element ) ) {
00133
return false;
00134 }
00135
QString widthStr = element.attribute(
"WIDTH" );
00136
if( !widthStr.isNull() ) {
00137
if ( widthStr.lower() ==
"quad" ) {
00138 spaceWidth = QUAD;
00139 }
00140
else if ( widthStr.lower() ==
"thick" ) {
00141 spaceWidth = THICK;
00142 }
00143
else if ( widthStr.lower() ==
"medium" ) {
00144 spaceWidth = MEDIUM;
00145 }
00146
else if ( widthStr.lower() ==
"negthin" ) {
00147 spaceWidth = NEGTHIN;
00148 }
00149
else {
00150 spaceWidth = THIN;
00151 }
00152 }
00153
else {
00154
return false;
00155 }
00156
QString tabStr = element.attribute(
"TAB" );
00157 m_tab = !tabStr.isNull();
00158
return true;
00159 }
00160
00161 bool SpaceElement::readContentFromDom(
QDomNode& node)
00162 {
00163
return BasicElement::readContentFromDom( node );
00164 }
00165
00166 void SpaceElement::writeMathML(
QDomDocument doc,
QDomNode parent )
00167 {
00168
00169
QDomElement de = doc.createElement(
"mspace" );
00170
QString width;
00171
00172
switch ( spaceWidth ) {
00173
case NEGTHIN:
00174 width =
"-3/18em";
00175
break;
00176
case THIN:
00177 width =
"thinmathspace";
00178
break;
00179
case MEDIUM:
00180 width =
"mediummathspace";
00181
break;
00182
case THICK:
00183 width =
"thickmathspace";
00184
break;
00185
case QUAD:
00186 width =
"veryverythickmathspace";
00187
break;
00188 }
00189
00190 de.setAttribute(
"width", width );
00191
00192 parent.appendChild( de );
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 }
00214
00215 QString SpaceElement::toLatex()
00216 {
00217
switch ( spaceWidth ) {
00218
case NEGTHIN:
return "\\!";
00219
case THIN:
return "\\,";
00220
case MEDIUM:
return "\\>";
00221
case THICK:
return "\\;";
00222
case QUAD:
return "\\quad ";
00223 }
00224
return "";
00225 }
00226
00227 KFORMULA_NAMESPACE_END