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 2006 Martin Pfeiffer <hubipete@gmx.net> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "BracketElement.h" 00023 #include <KoXmlWriter.h> 00024 00025 #include <QPainter> 00026 #include <QPen> 00027 00028 #include <kdebug.h> 00029 #include <klocale.h> 00030 00031 #include "fontstyle.h" 00032 #include "FormulaCursor.h" 00033 #include "FormulaElement.h" 00034 #include "SequenceElement.h" 00035 00036 namespace KFormula { 00037 00038 BracketElement::BracketElement( BasicElement* parent ) : BasicElement( parent ), 00039 left( 0 ), 00040 right( 0 ), 00041 leftType( EmptyBracket ), 00042 rightType( EmptyBracket ) 00043 { 00044 } 00045 00046 00047 BracketElement::~BracketElement() 00048 { 00049 delete left; 00050 delete right; 00051 } 00052 00053 00054 const QList<BasicElement*> BracketElement::childElements() 00055 { 00056 return QList<BasicElement*>(); 00057 } 00058 00059 void BracketElement::readMathML( const QDomElement& element ) 00060 { 00061 } 00062 00063 void BracketElement::writeMathML( KoXmlWriter* writer, bool oasisFormat ) 00064 { 00065 writer->startElement( oasisFormat ? "math:mfenced" : "mfenced" ); 00066 writeMathMLAttributes( writer ); 00067 00068 // save child elements that will come 00069 00070 writer->endElement(); 00071 } 00072 00077 void BracketElement::calcSizes(const ContextStyle& style, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle) 00078 { 00079 /* SequenceElement* content = getContent(); 00080 content->calcSizes(style, tstyle, istyle); 00081 00082 //if ( left == 0 ) { 00083 delete left; 00084 delete right; 00085 left = style.fontStyle().createArtwork( leftType ); 00086 right = style.fontStyle().createArtwork( rightType ); 00087 //} 00088 00089 if (content->isTextOnly()) { 00090 left->calcSizes(style, tstyle); 00091 right->calcSizes(style, tstyle); 00092 00093 setBaseline(qMax(content->getBaseline(), 00094 qMax(left->getBaseline(), right->getBaseline()))); 00095 00096 content->setY(getBaseline() - content->getBaseline()); 00097 left ->setY(getBaseline() - left ->getBaseline()); 00098 right ->setY(getBaseline() - right ->getBaseline()); 00099 00100 //setMidline(content->getY() + content->getMidline()); 00101 setHeight(qMax(content->getY() + content->getHeight(), 00102 qMax(left ->getY() + left ->getHeight(), 00103 right->getY() + right->getHeight()))); 00104 } 00105 else { 00106 //kDebug( DEBUGID ) << "BracketElement::calcSizes " << content->axis( style, tstyle ) << " " << content->getHeight() << endl; 00107 luPixel contentHeight = 2 * qMax( content->axis( style, tstyle ), 00108 content->getHeight() - content->axis( style, tstyle ) ); 00109 left->calcSizes( style, tstyle, contentHeight ); 00110 right->calcSizes( style, tstyle, contentHeight ); 00111 00112 // height 00113 setHeight(qMax(contentHeight, 00114 qMax(left->getHeight(), right->getHeight()))); 00115 //setMidline(getHeight() / 2); 00116 00117 content->setY(getHeight() / 2 - content->axis( style, tstyle )); 00118 setBaseline(content->getBaseline() + content->getY()); 00119 00120 if ( left->isNormalChar() ) { 00121 left->setY(getBaseline() - left->getBaseline()); 00122 } 00123 else { 00124 left->setY((getHeight() - left->getHeight())/2); 00125 } 00126 if ( right->isNormalChar() ) { 00127 right->setY(getBaseline() - right->getBaseline()); 00128 } 00129 else { 00130 right->setY((getHeight() - right->getHeight())/2); 00131 } 00132 00133 // kDebug() << "BracketElement::calcSizes" << endl 00134 // << "getHeight(): " << getHeight() << endl 00135 // << "left->getHeight(): " << left->getHeight() << endl 00136 // << "right->getHeight(): " << right->getHeight() << endl 00137 // << "left->getY(): " << left->getY() << endl 00138 // << "right->getY(): " << right->getY() << endl 00139 // << endl; 00140 } 00141 00142 // width 00143 setWidth(left->getWidth() + content->getWidth() + right->getWidth()); 00144 content->setX(left->getWidth()); 00145 right ->setX(left->getWidth()+content->getWidth());*/ 00146 } 00147 00148 00154 void BracketElement::draw( QPainter& painter, const LuPixelRect& r, 00155 const ContextStyle& style, 00156 ContextStyle::TextStyle tstyle, 00157 ContextStyle::IndexStyle istyle, 00158 const LuPixelPoint& parentOrigin ) 00159 { 00160 /* LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() ); 00161 //if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) ) 00162 // return; 00163 00164 SequenceElement* content = getContent(); 00165 content->draw(painter, r, style, tstyle, istyle, myPos); 00166 00167 if (content->isTextOnly()) { 00168 left->draw(painter, r, style, tstyle, myPos); 00169 right->draw(painter, r, style, tstyle, myPos); 00170 } 00171 else { 00172 luPixel contentHeight = 2 * qMax(content->axis( style, tstyle ), 00173 content->getHeight() - content->axis( style, tstyle )); 00174 left->draw(painter, r, style, tstyle, contentHeight, myPos); 00175 right->draw(painter, r, style, tstyle, contentHeight, myPos); 00176 } 00177 */ 00178 // Debug 00179 #if 0 00180 painter.setBrush( Qt::NoBrush ); 00181 painter.setPen( Qt::red ); 00182 painter.drawRect( style.layoutUnitToPixelX( myPos.x()+left->getX() ), 00183 style.layoutUnitToPixelY( myPos.y()+left->getY() ), 00184 style.layoutUnitToPixelX( left->getWidth() ), 00185 style.layoutUnitToPixelY( left->getHeight() ) ); 00186 painter.drawRect( style.layoutUnitToPixelX( myPos.x()+right->getX() ), 00187 style.layoutUnitToPixelY( myPos.y()+right->getY() ), 00188 style.layoutUnitToPixelX( right->getWidth() ), 00189 style.layoutUnitToPixelY( right->getHeight() ) ); 00190 #endif 00191 } 00192 00193 00197 void BracketElement::writeDom(QDomElement element) 00198 { 00199 /* SingleContentElement::writeDom(element); 00200 element.setAttribute("LEFT", leftType); 00201 element.setAttribute("RIGHT", rightType);*/ 00202 } 00203 00208 bool BracketElement::readAttributesFromDom(QDomElement element) 00209 { 00210 if (!BasicElement::readAttributesFromDom(element)) { 00211 return false; 00212 } 00213 QString leftStr = element.attribute("LEFT"); 00214 if(!leftStr.isNull()) { 00215 leftType = static_cast<SymbolType>(leftStr.toInt()); 00216 } 00217 QString rightStr = element.attribute("RIGHT"); 00218 if(!rightStr.isNull()) { 00219 rightType = static_cast<SymbolType>(rightStr.toInt()); 00220 } 00221 return true; 00222 } 00223 00224 00225 00226 } // namespace KFormula