kmathml
KoFormulaShape
KoFormulaShape is a flake shape ( derived from KoShape ) that implements the functionality to edit and show a mathematical formula. Its structure is highly related to the MathML markup language which is the ODF standard format for formula markup. At the moment the KoFormulaShape implements only parts of the MathML presentation markup, content markup is not supported.
Every formula is stored as a tree of element objects that more or less are equivalent to the XML structure of the MathML they were loaded from. The element objects are classes that derive all from the BasicElement class. They all implement a special interface which is used to alter and navigate through them.
The element tree's highest element is an object of the class FormulaElement. This object is a private member of the FormulaShape class which implements the public interface of a flake shape. The according flake tool is implemented in FormulaTool and contains an instance of FormulaCursor which is a class that emulates the behaviour of a normal text cursor for the formula. Through the FormulaCursor all editing is done and apart from loading the formula tree's structure is never changed in a different way.
The complex system of attributes which can be hold by the single elements is completely implemented in the AttributeManager class. The attributes are generically loaded and stored inside the elements and obtain and parse their values the elements use AttributeManager which also deals with heritage and default values.
MathML specifies a scaling mechanism that uses the scriptlevel attribute. This mechanism is implemented in BasicElement using the m_scalingFactor. This is the factor the element is scaled in relation to its parent element. The value is determined in during layouting in FormulaRenderer class.
There are a number of elements that do not inherit BasicElement directly but instead through RowElement. These classes are referred to as the inferred row elements - see MathML spec 3.1.3.1.
The MathML specification lists default values for operator's layouting. These values are different for every element class so each of them implements a defaultAttributeValue() method that returns the hardcoded default value of the passed attribute.
An other complex topic is user interaction while editing a formula. There is the FormulaCursor class which is quite important for user interaction and the FormulaToolOptions class.
The latter one implements most of the docker and the gui stuff and also does the template loading which provides the user with predefined elements to be inserted quickly.
The FormulaCursor class implements the cursor which can be moved around in the formula. The main methods for this are the moveLeft/Right/Up/Down methods of the FormulaCursor class and the acceptCursor() method that is implemented by each of element classes. The main idea behind this that the cursor is passed by the elements either to one of the children or the parent till one element accepts the cursor and sets it to itsself.
Implementation wise this is realised with a loop and the acceptCursor method just has to specify for the 8 directions which one has a special behaviour. For example the FractionElement wants that when the cursor enters the fraction from a parent by a right movement, that the cursor goes to the numerator and not the denominator. Therefore the FractionElement returns its numerator element when the acceptCursor is called with a RightFromParent.
KDE 4.4 API Reference