lib

spaceelement.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003                   Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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 /*istyle*/ )
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     //setMidline( getBaseline() - fm.strikeOutPos() );
00070 
00071     if ( m_tab ) {
00072         getParent()->registerTab( this );
00073     }
00074 }
00075 
00076 void SpaceElement::draw( QPainter& painter, const LuPixelRect& /*r*/,
00077                          const ContextStyle& style,
00078                          ContextStyle::TextStyle /*tstyle*/,
00079                          ContextStyle::IndexStyle /*istyle*/,
00080                          const LuPixelPoint& parentOrigin )
00081 {
00082     LuPixelPoint myPos(parentOrigin.x()+getX(), parentOrigin.y()+getY());
00083     // there is such a thing as negative space!
00084     //if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
00085     //    return;
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, bool oasisFormat )
00167 {
00168 
00169     QDomElement de = doc.createElement( oasisFormat ? "math:mspace" : "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"; // double 'very' is appropriate.
00187         break;
00188     }
00189 
00190     de.setAttribute( "width", width );
00191 
00192     parent.appendChild( de );
00193 
00194 
00195     /* // worked, but I redecided.
00196     switch ( spaceWidth )
00197     {
00198     case NEGTHIN:
00199         return doc.createEntityReference( "NegativeThinSpace" );
00200     case THIN:
00201         return doc.createEntityReference( "ThinSpace" );
00202     case MEDIUM:
00203         return doc.createEntityReference( "MediumSpace" );
00204     case THICK:
00205         return doc.createEntityReference( "ThickSpace" );
00206     case QUAD:
00207         //return doc.createEntityReference( "Space" ); // misused &Space;???
00208         QDomElement de = doc.createElement( "mspace" );
00209         de.setAttribute( "width", "veryverythickmathspace" );
00210         return de;
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
KDE Home | KDE Accessibility Home | Description of Access Keys