F:/KPlato/koffice/libs/kformula/MatrixRowElement.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003    Copyright (C) 2001 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00004    Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net>
00005    Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020    Boston, MA 02110-1301, USA.
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() )   // coming from the parent go to the very right entry
00068         m_matrixEntryElements.last()->moveLeft( cursor, this );
00069     else                            // coming from a child go to the parent
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 );   // just forward the call to MatrixElement   
00084 }
00085 
00086 void MatrixRowElement::moveDown( FormulaCursor* cursor, BasicElement* from )
00087 {
00088     parentElement()->moveDown( cursor, from ); // just forward the call to MatrixElement
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     // calculate the tab positions
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         // There's always a first line. No formulas without lines.
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 /*    MatrixEntryElement* e = static_cast<MatrixEntryElement*>(newChildren.takeAt(0));
00227     e->setParent(this);
00228     m_matrixEntryElements.insert( cursor->getPos(), e );
00229 
00230     if (direction == beforeCursor) {
00231         e->moveLeft(cursor, this);
00232     }
00233     else {
00234         e->moveRight(cursor, this);
00235     }
00236     cursor->setSelecting(false);*/
00237     //formula()->changed();
00238 }
00239 
00240 void MatrixRowElement::remove( FormulaCursor* cursor,
00241                                QList<BasicElement*>& removedChildren,
00242                                Direction direction )
00243 {
00244 /*    if ( m_matrixEntryElements.count() == 1 ) { //&& ( cursor->getPos() == 0 ) ) {
00245         getParent()->selectChild(cursor, this);
00246         getParent()->remove(cursor, removedChildren, direction);
00247     }
00248     else {
00249         MatrixEntryElement* e = m_matrixEntryElements.takeAt( cursor->getPos() );
00250         removedChildren.append( e );
00251         //formula()->elementRemoval( e );
00252         //cursor->setTo( this, denominatorPos );
00253         //formula()->changed();
00254     }*/
00255 }
00256 /*
00257 void MatrixRowElement::normalize( FormulaCursor* cursor, Direction direction )
00258 {
00259     int pos = cursor->getPos();
00260     if ( ( cursor->getElement() == this ) &&
00261          ( pos > -1 ) && ( pos <= m_matrixEntryElements.count() ) ) {
00262         switch ( direction ) {
00263         case beforeCursor:
00264             if ( pos > 0 ) {
00265                 m_matrixEntryElements.at( pos-1 )->moveLeft( cursor, this );
00266                 break;
00267             }
00268             // no break! intended!
00269         case afterCursor:
00270             if ( pos < m_matrixEntryElements.count() ) {
00271                 m_matrixEntryElements.at( pos )->moveRight( cursor, this );
00272             }
00273             else {
00274                 m_matrixEntryElements.at( pos-1 )->moveLeft( cursor, this );
00275             }
00276             break;
00277         }
00278     }
00279     else {
00280         BasicElement::normalize( cursor, direction );
00281     }
00282 }*/
00283 /*
00284 SequenceElement* MatrixRowElement::getMainChild()
00285 {
00286     return m_matrixEntryElements.at( 0 );
00287 }
00288 */
00289 void MatrixRowElement::selectChild(FormulaCursor* cursor, BasicElement* child)
00290 {
00291 /*    int pos = m_matrixEntryElements.indexOf( dynamic_cast<MatrixEntryElement*>( child ) );
00292     if ( pos > -1 ) {
00293         cursor->setTo( this, pos );
00294         //content.at( pos )->moveRight( cursor, this );
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 } // namespace KFormula

Généré le Wed Nov 22 23:41:01 2006 pour KPlato par  doxygen 1.5.1-p1