kformula/flake

FormulaRenderer.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "FormulaRenderer.h"
00021 #include "AttributeManager.h"
00022 #include "BasicElement.h"
00023 #include <kdebug.h>
00024 
00025 FormulaRenderer::FormulaRenderer()
00026 {
00027     m_dirtyElement = 0;
00028     m_attributeManager = new AttributeManager();
00029 }
00030 
00031 FormulaRenderer::~FormulaRenderer()
00032 {
00033     delete m_attributeManager;
00034 }
00035 
00036 void FormulaRenderer::paintElement( QPainter& p, BasicElement* element, bool hints )
00037 {
00038     p.save();
00039     p.setRenderHint( QPainter::Antialiasing );
00040     p.translate( element->origin() );          // setup painter
00041     if (!hints) {
00042         element->paint( p, m_attributeManager );   // let element paint itself
00043     } else {
00044         element->paintEditingHints( p, m_attributeManager );
00045     }
00046 
00047     // eventually paint all its children
00048     if( !element->childElements().isEmpty() && element->elementType() != Phantom ) {
00049         foreach( BasicElement* tmpElement, element->childElements() ) {
00050             paintElement( p, tmpElement, hints );
00051         }
00052     }
00053 
00054     p.restore();
00055 }
00056 
00057 
00058 void FormulaRenderer::layoutElement( BasicElement* element )
00059 {
00060     int i = 0;
00061     element->setDisplayStyle( m_attributeManager->boolOf("displaystyle", element));
00062     foreach( BasicElement* tmp, element->childElements() ) {
00063     int scale = m_attributeManager->scriptLevel( element, i++ ); 
00064         tmp->setScaleLevel( scale );
00065         layoutElement( tmp );              // first layout all children
00066     }
00067     element->layout( m_attributeManager );      // actually layout the element
00068     element->stretch();
00069 }
00070 
00071 void FormulaRenderer::update( QPainter& p, BasicElement* element )
00072 {
00073     updateElementLayout( element );              // relayout the changed element
00074     paintElement( p, m_dirtyElement );     // and then repaint as much as needed
00075 }
00076 
00077 void FormulaRenderer::updateElementLayout( BasicElement* element )
00078 {
00079     QRectF tmpBoundingRect;
00080     bool parentLayoutAffected = true;
00081     BasicElement* tmpElement = element;
00082     while( parentLayoutAffected )
00083     {
00084         tmpBoundingRect = tmpElement->boundingRect();   // cache the former boundingRect
00085         tmpElement->layout( m_attributeManager );       // layout the element
00086 
00087         // check whether the new layout affects the parent element's layout
00088         if( tmpBoundingRect == tmpElement->boundingRect() )
00089         {
00090             parentLayoutAffected = false;               // stop the layouting
00091             m_dirtyElement = tmpElement;
00092         }
00093         else
00094             tmpElement = tmpElement->parentElement();   // prepare layouting the parent
00095     }
00096 }
00097 
00098 double FormulaRenderer::elementScaleFactor( BasicElement* element ) const
00099 {
00100     Q_UNUSED(element)
00101     AttributeManager am;
00102     return -1;  // FIXME!
00103 }