/calligra-2.4.92/plugins/formulashape/elements/BasicElement.h

# · C Header · 346 lines · 91 code · 72 blank · 183 comment · 0 complexity · ebc192897405d809ede382253a77267c MD5 · raw file

  1. /* This file is part of the KDE project
  2. Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
  3. Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
  4. Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net>
  5. Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
  6. 2009 Jeremias Epperlein <jeeree@web.de>
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public License
  16. along with this library; see the file COPYING.LIB. If not, write to
  17. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. Boston, MA 02110-1301, USA.
  19. */
  20. #ifndef BASICELEMENT_H
  21. #define BASICELEMENT_H
  22. #include "kformula_export.h"
  23. #include "ElementFactory.h"
  24. #include <QHash>
  25. #include <QList>
  26. #include <QString>
  27. #include <QRectF>
  28. #include <QLineF>
  29. class QPainter;
  30. class QVariant;
  31. class KoXmlWriter;
  32. #include "KoXmlReaderForward.h"
  33. class AttributeManager;
  34. class FormulaCursor;
  35. class QPainterPath;
  36. class TableDataElement;
  37. #define DEBUGID 40000
  38. /**
  39. * @short The base class for all elements of a formula
  40. *
  41. * The BasicElement class is constructed with a parent and normally an element in a
  42. * formula has a parent. The only exception is FormulaElement which is the root of
  43. * the element tree and has no parent element.
  44. * Most of the elements have children but the number of it can be fixed or variable
  45. * and the type of child element is not certain. So with the childElements() method you
  46. * can obtain a list of all direct children of an element. Note that the returned list
  47. * can be empty when the element is eg a token. This is also the reason why each class
  48. * inheriting BasicElement has to implement the childElements() method on its own.
  49. * With the childElementAt method you can test if the given point is in the element.
  50. * This method is generically implemented for all element types only once in
  51. * BasicElement.
  52. * The BasicElement knows its size and position in the formula. This data is normally
  53. * only used for drawing and stored in the m_boundingRect attribute.
  54. * To adapt both variables, size and coordinates, to fit in the formula each and every
  55. * BasicElement derived class has to implement layoutElement().
  56. *
  57. * For cursor movement, an element has to implement elementBefore, elementAfter,
  58. * lastCursorPosition and positionOfChild as well as
  59. * moveCursor (keyboard navigation) and setCursorTo (cursor placement by clicking).
  60. */
  61. class KOFORMULA_EXPORT BasicElement {
  62. public:
  63. /*
  64. * The standard constructor
  65. * @param parent pointer to the BasicElement's parent
  66. */
  67. BasicElement( BasicElement* parent = 0 );
  68. /// The standard destructor
  69. virtual ~BasicElement();
  70. /**
  71. * Get the element of the formula at the given point
  72. * @param p the point to look for
  73. * @return a pointer to a BasicElement
  74. */
  75. BasicElement* childElementAt( const QPointF& p );
  76. /**
  77. * Obtain a list of all child elements of this element - sorted in saving order
  78. * @return a QList with pointers to all child elements
  79. */
  80. virtual const QList<BasicElement*> childElements() const;
  81. /**
  82. * Replace a child element
  83. * @param oldelement the child to replace
  84. * @param newelement the child @p oldelement is replaced with
  85. */
  86. virtual bool replaceChild( BasicElement* oldelement, BasicElement* newelement );
  87. /**
  88. * Render the element to the given QPainter
  89. * @param painter The QPainter to paint the element to
  90. * @param am AttributeManager containing style info
  91. */
  92. virtual void paint( QPainter& painter, AttributeManager* am );
  93. /**
  94. * Render the editing hints of the element to the given QPainter
  95. * @param painter The QPainter to paint the element to
  96. * @param am AttributeManager containing style info
  97. */
  98. virtual void paintEditingHints( QPainter& painter, AttributeManager* am );
  99. /**
  100. * Calculate the minimum size of the element and the positions of its children
  101. *
  102. * Laying out the items is done in two parts.
  103. *
  104. * First layout() is called for the topmost element, which in turn calls
  105. * layout() for its children, and so on. This sets the minimum size of all elements.
  106. *
  107. * Then stretch() is called for the topmost element, which in turn calls
  108. * stretch() for its children, and so on. This stretches elements that
  109. * are stretchable, up to their maximum size.
  110. *
  111. * @param am The AttributeManager providing information about attributes values
  112. */
  113. virtual void layout( const AttributeManager* am );
  114. /**
  115. * Calculate the stretched size of the element. This is called after layouting.
  116. */
  117. virtual void stretch();
  118. /**
  119. * Implement the cursor behaviour for the element
  120. * @param cursor the cursor we test
  121. * @return true, if the element accepts the cursor
  122. */
  123. virtual bool acceptCursor( const FormulaCursor& cursor );
  124. /**
  125. * Return the coordinates of the line, where the cursor should be drawn
  126. * in coordinates relative to the formula element (or the flake shape)
  127. * @param cursor The FormulaCursor specifying the position
  128. * @return the cursor line
  129. */
  130. virtual QLineF cursorLine(int position) const;
  131. virtual QPainterPath selectionRegion(const int pos1, const int pos2) const;
  132. virtual QList<BasicElement*> elementsBetween(int pos1, int pos2) const;
  133. /**
  134. * Move the cursor in the direction specified in cursor
  135. * @param newcursor the cursor we move around
  136. * @param oldcursor the former cursor position
  137. * @return true, if we moved the cursor
  138. */
  139. virtual bool moveCursor(FormulaCursor& newcursor, FormulaCursor& oldcursor);
  140. /// @return The element's ElementType
  141. virtual ElementType elementType() const;
  142. /// Set the element's width to @p width
  143. void setWidth( qreal width );
  144. /// @return The width of the element
  145. qreal width() const;
  146. /// Set the element's height to @p height
  147. void setHeight( qreal height );
  148. /// @return The height of the element
  149. qreal height() const;
  150. /// @return The bounding rectangle of the element
  151. const QRectF& boundingRect() const;
  152. /// @return The absoulte bounding rectangle of the element
  153. const QRectF absoluteBoundingRect() const;
  154. /**
  155. * place the cursor at the the given point
  156. * the point should be placed a the position in the element
  157. * (or it's child) that is closest to the point
  158. * in particular the point doesn't have to be within
  159. * boundingBox()
  160. * @param cursor The FormulaCursor to modify
  161. * @param point The point in coordinates relative to the elements local coordinate system
  162. * @return true, iff the cursor could be placed
  163. **/
  164. virtual bool setCursorTo(FormulaCursor& cursor, QPointF point);
  165. /// @return The bounding rectangle of the children, relative to the element
  166. const QRectF& childrenBoundingRect() const;
  167. /// Set the bounding rectangle of the children, relative to the element
  168. void setChildrenBoundingRect(const QRectF &rect);
  169. /// Set the element's baseline to @p baseLine
  170. void setBaseLine( qreal baseLine );
  171. /// @return The baseline of the element
  172. qreal baseLine() const;
  173. /// Set the element's origin inside the m_parentElement to @p origin
  174. void setOrigin( QPointF origin );
  175. /// @return The element's origin
  176. QPointF origin() const;
  177. /// Set the element's m_parentElement to @p parent
  178. void setParentElement( BasicElement* parent );
  179. /// @return The parent element of this BasicElement
  180. BasicElement* parentElement() const;
  181. /// @return The last cusor position (number of available cursor positions - 1)
  182. virtual int endPosition() const;
  183. /**
  184. * @return the cursor position before the child in this element and -1 if it isn't a child
  185. * @param child the childelement we are looking for
  186. */
  187. virtual int positionOfChild(BasicElement* child) const;
  188. /// @return the element right before the cursor position @p position and 0 if there is none
  189. virtual BasicElement* elementBefore(int position) const;
  190. /// @return the element right after the cursor position @p position and 0 if there is none
  191. virtual BasicElement* elementAfter(int position) const;
  192. /// Set the element's m_scaleFactor to @p scaleFactor
  193. void setScaleFactor( qreal scaleFactor );
  194. /// @return The elements scale factor
  195. qreal scaleFactor() const;
  196. /// Set the elements scale level and sets the scale factor
  197. void setScaleLevel( int scaleLevel );
  198. /// @return The elements scale level
  199. int scaleLevel() const;
  200. /**
  201. * Set an attribute's value
  202. * @param name The name of the attribute to be set
  203. * @param value The value to set for the attribute
  204. */
  205. void setAttribute( const QString& name, const QVariant& value );
  206. /// @return The value of the attribute if it is set for this element
  207. QString attribute( const QString& attribute ) const;
  208. /// @return The value of the attribute if it is inherited
  209. virtual QString inheritsAttribute( const QString& attribute ) const;
  210. /// @return The default value of the attribute for this element
  211. virtual QString attributesDefaultValue( const QString& attribute ) const;
  212. /// Whether displaystyle is set
  213. bool displayStyle() const;
  214. /// Whether displaystyle is set. This is updated by FormulaRenderer
  215. void setDisplayStyle(bool displayStyle);
  216. /// Read the element from MathML
  217. bool readMathML( const KoXmlElement& element );
  218. /// Save the element to MathML
  219. void writeMathML( KoXmlWriter* writer, const QString& ns = "math" ) const;
  220. /// @return true, if @p other is a descendant of this element
  221. bool hasDescendant(BasicElement* other) const;
  222. /// @return first empty element, that is a descendant of this element, if there is one
  223. BasicElement* emptyDescendant();
  224. /// @return true, when the element is empty
  225. virtual bool isEmpty() const;
  226. /// @return true, if the element is an inferred mrow
  227. virtual bool isInferredRow() const;
  228. /// @return the formula element that is a descendant of this element
  229. BasicElement* formulaElement();
  230. /** writes the child element tree to kDebug()
  231. * only for debugging purpose
  232. * @param wrong indicates, if the parent is set wrong
  233. * @param indent indention level
  234. */
  235. virtual void writeElementTree(int indent=0, bool wrong=false) const;
  236. /// return the content of the element to kDebug(), only for debugging
  237. virtual const QString writeElementContent() const;
  238. /// @return the first TableDataElement among the elements ancestors or 0 if there is none
  239. TableDataElement* parentTableData();
  240. protected:
  241. /// Read all attributes loaded and add them to the m_attributes map
  242. virtual bool readMathMLAttributes( const KoXmlElement& element );
  243. /// Read all content from the node - reimplemented by child elements
  244. virtual bool readMathMLContent( const KoXmlElement& element );
  245. /// Write all attributes of m_attributes to @p writer
  246. virtual void writeMathMLAttributes( KoXmlWriter* writer ) const;
  247. /// Write all content to the KoXmlWriter - reimplemented by the child elements
  248. virtual void writeMathMLContent( KoXmlWriter* writer, const QString& ns ) const;
  249. static void cleanElementTree(BasicElement* element);
  250. private:
  251. /// The element's parent element - might not be null except of FormulaElement
  252. BasicElement* m_parentElement;
  253. /// A hash map of all attributes where attribute name is assigned to a value
  254. QHash<QString,QString> m_attributes;
  255. /// The boundingRect storing the element's width, height, x and y
  256. QRectF m_boundingRect;
  257. /** The boundingRect storing the childrens element's width, height, x and y
  258. * The bottomRight hand corner will always be small that then size of
  259. * m_boundingRect
  260. */
  261. QRectF m_childrenBoundingRect;
  262. /// The position of our base line from the upper border
  263. qreal m_baseLine;
  264. /// Factor with which this element is scaled down by
  265. qreal m_scaleFactor;
  266. /// Scale level with which this element is scaled down by
  267. qreal m_scaleLevel;
  268. /// Indicates whether this element has displaystyle set
  269. bool m_displayStyle;
  270. };
  271. #endif // BASICELEMENT_H