00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "MatrixRowElement.h"
00024 #include "MatrixEntryElement.h"
00025 #include "FormulaCursor.h"
00026 #include <KoXmlWriter.h>
00027
00028 #include <klocale.h>
00029
00030 #include <QPainter>
00031 #include <QList>
00032
00033 namespace KFormula {
00034
00035 MatrixRowElement::MatrixRowElement( BasicElement* parent ) : BasicElement( parent )
00036 {
00037 m_matrixEntryElements.append( new MatrixEntryElement( this ) );
00038 }
00039
00040 MatrixRowElement::~MatrixRowElement()
00041 {
00042 }
00043
00044 int MatrixRowElement::positionOfEntry( BasicElement* entry ) const
00045 {
00046 for( int i = 0; i < m_matrixEntryElements.count(); i++ )
00047 if( m_matrixEntryElements[ i ] == entry )
00048 return i;
00049 }
00050
00051 MatrixEntryElement* MatrixRowElement::entryAt( int pos )
00052 {
00053 return m_matrixEntryElements[ pos ];
00054 }
00055
00056 const QList<BasicElement*> MatrixRowElement::childElements()
00057 {
00058 QList<BasicElement*> tmp;
00059 foreach( MatrixEntryElement* element, m_matrixEntryElements )
00060 tmp.append( element );
00061
00062 return tmp;
00063 }
00064
00065 void MatrixRowElement::moveLeft( FormulaCursor* cursor, BasicElement* from )
00066 {
00067 if( from == parentElement() )
00068 m_matrixEntryElements.last()->moveLeft( cursor, this );
00069 else
00070 parentElement()->moveLeft( cursor, from );
00071 }
00072
00073 void MatrixRowElement::moveRight( FormulaCursor* cursor, BasicElement* from )
00074 {
00075 if( from == parentElement() )
00076 m_matrixEntryElements.first()->moveRight( cursor, this );
00077 else
00078 parentElement()->moveRight( cursor, this );
00079 }
00080
00081 void MatrixRowElement::moveUp( FormulaCursor* cursor, BasicElement* from )
00082 {
00083 parentElement()->moveUp( cursor, from );
00084 }
00085
00086 void MatrixRowElement::moveDown( FormulaCursor* cursor, BasicElement* from )
00087 {
00088 parentElement()->moveDown( cursor, from );
00089 }
00090
00091 void MatrixRowElement::readMathML( const QDomElement& element )
00092 {
00093 readMathMLAttributes( element );
00094
00095 MatrixEntryElement* tmpEntry = 0;
00096 QDomElement tmp = element.firstChildElement();
00097 while( !tmp.isNull() )
00098 {
00099 tmpEntry = new MatrixEntryElement( this );
00100 m_matrixEntryElements << tmpEntry;
00101 tmpEntry->readMathML( tmp );
00102 tmp = tmp.nextSiblingElement();
00103 }
00104 }
00105
00106 void MatrixRowElement::writeMathML( KoXmlWriter* writer, bool oasisFormat )
00107 {
00108 writer->startElement( oasisFormat ? "math:mtr" : "mtr" );
00109 writeMathMLAttributes( writer );
00110
00111 foreach( MatrixEntryElement* tmpEntry, m_matrixEntryElements )
00112 tmpEntry->writeMathML( writer, oasisFormat );
00113
00114 writer->endElement();
00115 }
00116
00117
00118
00119
00120 void MatrixRowElement::goInside( FormulaCursor* cursor )
00121 {
00122 m_matrixEntryElements.at( 0 )->goInside( cursor );
00123 }
00124
00125
00126
00127
00128 void MatrixRowElement::calcSizes( const ContextStyle& context,
00129 ContextStyle::TextStyle tstyle,
00130 ContextStyle::IndexStyle istyle,
00131 StyleAttributes& style )
00132 {
00133 luPt mySize = context.getAdjustedSize( tstyle );
00134 QFont font = context.getDefaultFont();
00135 font.setPointSizeF( context.layoutUnitPtToPt( mySize ) );
00136 QFontMetrics fm( font );
00137 luPixel leading = context.ptToLayoutUnitPt( fm.leading() );
00138 luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle ) );
00139
00140 int count = m_matrixEntryElements.count();
00141 luPixel height = -leading;
00142 luPixel width = 0;
00143 int tabCount = 0;
00144 for ( int i = 0; i < count; ++i ) {
00145 MatrixEntryElement* line = m_matrixEntryElements[i];
00146 line->calcSizes( context, tstyle, istyle, style );
00147 tabCount = qMax( tabCount, line->tabCount() );
00148
00149 height += leading;
00150 line->setX( 0 );
00151 line->setY( height );
00152 height += line->getHeight() + distY;
00153 width = qMax( line->getWidth(), width );
00154 }
00155
00156
00157 for ( int t = 0; t < tabCount; ++t ) {
00158 luPixel pos = 0;
00159 for ( int i = 0; i < count; ++i ) {
00160 MatrixEntryElement* line = m_matrixEntryElements[i];
00161 if ( t < line->tabCount() ) {
00162 pos = qMax( pos, line->tab( t )->getX() );
00163 }
00164 else {
00165 pos = qMax( pos, line->getWidth() );
00166 }
00167 }
00168 for ( int i = 0; i < count; ++i ) {
00169 MatrixEntryElement* line = m_matrixEntryElements[i];
00170 if ( t < line->tabCount() ) {
00171 line->moveTabTo( t, pos );
00172 width = qMax( width, line->getWidth() );
00173 }
00174 }
00175 }
00176
00177 setHeight( height );
00178 setWidth( width );
00179 if ( count == 1 ) {
00180 setBaseline( m_matrixEntryElements.at( 0 )->getBaseline() );
00181 }
00182 else {
00183
00184 setBaseline( height/2 + context.axisHeight( tstyle ) );
00185 }
00186 }
00187
00188 void MatrixRowElement::draw( QPainter& painter, const LuPixelRect& r,
00189 const ContextStyle& context,
00190 ContextStyle::TextStyle tstyle,
00191 ContextStyle::IndexStyle istyle,
00192 StyleAttributes& style,
00193 const LuPixelPoint& parentOrigin )
00194 {
00195 LuPixelPoint myPos( parentOrigin.x() + getX(), parentOrigin.y() + getY() );
00196 int count = m_matrixEntryElements.count();
00197
00198 if ( context.edit() ) {
00199 int tabCount = 0;
00200 painter.setPen( context.getHelpColor() );
00201 for ( int i = 0; i < count; ++i ) {
00202 MatrixEntryElement* line = m_matrixEntryElements[i];
00203 if ( tabCount < line->tabCount() ) {
00204 for ( int t = tabCount; t < line->tabCount(); ++t ) {
00205 BasicElement* marker = line->tab( t );
00206 painter.drawLine( context.layoutUnitToPixelX( myPos.x()+marker->getX() ),
00207 context.layoutUnitToPixelY( myPos.y() ),
00208 context.layoutUnitToPixelX( myPos.x()+marker->getX() ),
00209 context.layoutUnitToPixelY( myPos.y()+getHeight() ) );
00210 }
00211 tabCount = line->tabCount();
00212 }
00213 }
00214 }
00215
00216 for ( int i = 0; i < count; ++i ) {
00217 MatrixEntryElement* line = m_matrixEntryElements[i];
00218 line->draw( painter, r, context, tstyle, istyle, style, myPos );
00219 }
00220 }
00221
00222 void MatrixRowElement::insert( FormulaCursor* cursor,
00223 QList<BasicElement*>& newChildren,
00224 Direction direction )
00225 {
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 }
00239
00240 void MatrixRowElement::remove( FormulaCursor* cursor,
00241 QList<BasicElement*>& removedChildren,
00242 Direction direction )
00243 {
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 void MatrixRowElement::selectChild(FormulaCursor* cursor, BasicElement* child)
00290 {
00291
00292
00293
00294
00295
00296 }
00297
00298
00302 void MatrixRowElement::writeDom(QDomElement element)
00303 {
00304 BasicElement::writeDom(element);
00305
00306 int lineCount = m_matrixEntryElements.count();
00307 element.setAttribute( "LINES", lineCount );
00308
00309 QDomDocument doc = element.ownerDocument();
00310 for ( int i = 0; i < lineCount; ++i ) {
00311 QDomElement tmp = m_matrixEntryElements.at( i )->getElementDom(doc);
00312 element.appendChild(tmp);
00313 }
00314 }
00315
00316
00317
00318
00319
00320
00325 bool MatrixRowElement::readAttributesFromDom(QDomElement element)
00326 {
00327 if (!BasicElement::readAttributesFromDom(element)) {
00328 return false;
00329 }
00330 int lineCount = 0;
00331 QString lineCountStr = element.attribute("LINES");
00332 if(!lineCountStr.isNull()) {
00333 lineCount = lineCountStr.toInt();
00334 }
00335 if (lineCount == 0) {
00336 kWarning( DEBUGID ) << "lineCount <= 0 in MultilineElement." << endl;
00337 return false;
00338 }
00339
00340 m_matrixEntryElements.clear();
00341 for ( int i = 0; i < lineCount; ++i ) {
00342 MatrixEntryElement* element = new MatrixEntryElement(this);
00343 m_matrixEntryElements.append(element);
00344 }
00345 return true;
00346 }
00347
00353 bool MatrixRowElement::readContentFromDom(QDomNode& node)
00354 {
00355 if (!BasicElement::readContentFromDom(node)) {
00356 return false;
00357 }
00358
00359 int lineCount = m_matrixEntryElements.count();
00360 int i = 0;
00361 while ( !node.isNull() && i < lineCount ) {
00362 if ( node.isElement() ) {
00363 SequenceElement* element = m_matrixEntryElements.at( i );
00364 QDomElement e = node.toElement();
00365 if ( !element->buildFromDom( e ) ) {
00366 return false;
00367 }
00368 ++i;
00369 }
00370 node = node.nextSibling();
00371 }
00372 return true;
00373 }
00374
00375 }