File indexing completed on 2024-05-12 16:33:59

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net> 
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef ATTRIBUTEMANAGER_H
0021 #define ATTRIBUTEMANAGER_H
0022 
0023 #include <QFont>
0024 #include "koformula_export.h"
0025 
0026 class KoViewConverter;
0027 class BasicElement;
0028 
0029 /** Enum encoding all possibilities to align */
0030 enum Align {
0031     Left /**< Align to the left*/,
0032     Center /**< Align to the center*/,
0033     Right /**< Align to the right*/,
0034     Top /**< Align to the top*/,
0035     Bottom /**< Align to the bottom*/,
0036     BaseLine /**< Align to the baseline*/,
0037     Axis /**< Align to the axis*/,
0038     InvalidAlign
0039 };
0040 
0041 class Length {
0042   public:
0043     enum Unit {
0044         Em, /**< ems (font-relative unit traditionally used for horizontal lengths) */
0045         Ex,     /**< exs (font-relative unit traditionally used for vertical lengths) */
0046         Px,     /**< pixels, or pixel size of a "typical computer display" */
0047         In,     /**< inches (1 inch = 2.54 centimeters) */
0048         Cm,     /**< centimeters */
0049         Mm,     /**< millimeters */
0050         Pt,     /**< points (1 point = 1/72 inch) */
0051         Pc,     /**< picas (1 pica = 12 points) */
0052         Percentage, /**< percentage of default value */
0053         None    /**< For when no unit has been specified */
0054     };
0055 
0056     enum UnitType {
0057         NoType,
0058         Relative,
0059         Absolute,
0060         Pixel
0061     };
0062 
0063     Length() : value(0), unit(None), type(NoType) {};
0064     qreal value;
0065     Unit unit;
0066     UnitType type;
0067 };
0068 
0069 /**
0070  * @short manages all the attributes, used by the elements to obtain attribute values
0071  *
0072  * The AttributeManager is the central point dealing with the MathML attributes and
0073  * their values. It is in fact something like a StyleManager. As the normal elements
0074  * only have a general hash list of the elements they hold, there is the need for a
0075  * class that manages conversion and heritage of the attributes during the painting
0076  * phase. These are the two main tasks of AttributeManager.
0077  * The AttributeManager is always called when an element needs a value to work with.
0078  * The AttributeManager looks for that value first in the asking element's own
0079  * attribute list. If nothing is found the AttributeManager looks for a parent element
0080  * that might inherit the right value. If AttributeManager has no success again it
0081  * returns the default value for that attribute.
0082  *
0083  * @author Martin Pfeiffer <hubipete@gmx.net>
0084  */
0085 class KOFORMULA_EXPORT AttributeManager {
0086 public:
0087     /// The constructor
0088     AttributeManager();
0089 
0090     /// The destructor
0091     ~AttributeManager();
0092 
0093     /**
0094      * Obtain the @p attribute's value as color
0095      * @param attribute A string with the attribute to look up
0096      * @param element The element the value is looked up for
0097      * @return The value that was looked up
0098      */
0099     QColor colorOf( const QString& attribute, const BasicElement* element  ) const;
0100 
0101     /**
0102      * Obtain the @p attribute's value as boolean
0103      * @param attribute A string with the attribute to look up
0104      * @param element The element the value is looked up for
0105      * @return The value that was looked up
0106      */
0107     bool boolOf( const QString& attribute, const BasicElement* element ) const;
0108 
0109     /**
0110      * Obtain the @p attribute's value as qreal
0111      * @param attribute A string with the attribute to look up
0112      * @param element The element the value is looked up for
0113      * @return The value that was looked up
0114      */
0115     qreal doubleOf( const QString& attribute, const BasicElement* element ) const;
0116 
0117     /**
0118      * Obtain the @p attribute's value as list of qreals
0119      * @param attribute A string with the attribute to look up
0120      * @param element The element the value is looked up for
0121      * @return The value that was looked up
0122      */
0123     QList<qreal> doubleListOf( const QString& attribute,
0124                                 const BasicElement* element ) const;
0125 
0126     /**
0127      * Obtain the @p attribute's value as string
0128      * @param attribute A string with the attribute to look up
0129      * @param element The element the value is looked up for
0130      * @return The value that was looked up
0131      */
0132     QString stringOf( const QString& attribute, const BasicElement* element ) const;
0133 
0134     /**
0135      * Obtain the @p attribute's value as align
0136      * @param attribute A string with the attribute to look up
0137      * @param element The element the value is looked up for
0138      * @return The value that was looked up
0139      */
0140     Align alignOf( const QString& attribute, const BasicElement* element ) const;
0141 
0142     /**
0143      * Obtain the @p attribute's value as list of aligns
0144      * @param attribute A string with the attribute to look up
0145      * @param element The element the value is looked up for
0146      * @return The value that was looked up
0147      */
0148     QList<Align> alignListOf( const QString& attribute, const BasicElement* element ) const;
0149 
0150     /**
0151      * Obtain the @p attribute's value as Qt::PenStyle
0152      * @param attribute A string with the attribute to look up
0153      * @param element The element the value is looked up for
0154      * @return The value that was looked up
0155      */
0156     Qt::PenStyle penStyleOf( const QString& attribute, const BasicElement* element ) const;
0157 
0158     /**
0159      * Obtain the @p attribute's value as list of Qt::PenStyles
0160      * @param attribute A string with the attribute to look up
0161      * @param element The element the value is looked up for
0162      * @return The valuefont  that was looked up
0163      */
0164     QList<Qt::PenStyle> penStyleListOf( const QString& attribute,
0165                                         const BasicElement* element ) const;
0166 
0167     /**
0168      * Obtain the scaling level for @p parent 's child element at index @p index
0169      * Usually the first child is treated different.  For example in 
0170      * @verbatim
0171      * <msup><mi>b</mi><mn>2</mn></msup>
0172      * @endverbatim
0173      * to represent  b^2,  the 2 is smaller
0174      * than the b.
0175      * @param parent The parent of the element for which scaling is determined
0176      * @param index The index of the child element for which scaling is determined
0177      * @return The scaling level for the font size
0178      */
0179     int scriptLevel( const BasicElement* parent, int index ) const;
0180     
0181     /// @return Line thickness for mfrac, mroot etc lines
0182     qreal lineThickness( const BasicElement* element ) const;
0183 
0184     /// @return A value used for spacing tasks during layouting
0185     qreal layoutSpacing( const BasicElement* element ) const;
0186 
0187     /**
0188      * Determine the maximal height of an given element's child elements
0189      * @param element The element whose children are used
0190      * @return The maximal height
0191      */
0192     qreal maxHeightOfChildren( const BasicElement* element ) const;
0193 
0194     /**
0195      * Determine the maximal height of an given element's child elements
0196      * @param element The element whose children are used
0197      * @return The maximal height
0198      */
0199     qreal maxWidthOfChildren( const BasicElement* element ) const;
0200 
0201     /// @return The Align value that was passed as QString @p value
0202     Align parseAlign( const QString& value ) const;
0203 
0204     /// @return The font that is set for @p element 
0205     QFont font( const BasicElement* element ) const;
0206 
0207     /// Set the KoViewConverter to use
0208     void setViewConverter( KoViewConverter* converter );
0209 
0210     /// @return The parsed the @p value into a Qt::PenStyle
0211     Qt::PenStyle parsePenStyle( const QString& value ) const;
0212 
0213     /// @return The parsed @p value which is given with a unit
0214     Length parseUnit( const QString& value, const BasicElement* element ) const;
0215 
0216     //// @return The given Length converted to units of pixels
0217     qreal lengthToPixels( Length length, const BasicElement* element, const QString &attribute) const;
0218 
0219     /// Find a value for @p attribute that applies to @p element
0220     QString findValue( const QString& attribute, const BasicElement* element ) const;
0221 
0222     /// Convert a math space string, such as "thinmathspace", to a size in pixels
0223     qreal parseMathSpace( const QString& value, const BasicElement *element ) const;
0224 
0225     /// The KoViewConverter used to determine the point values of pixels
0226     KoViewConverter* m_viewConverter;
0227 };
0228 
0229 #endif // ATTRIBUTEMANAGER_H