JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
jkqtmathtextnode.h
1/*
2 Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
3 with contributions from: Razi Alavizadeh
4
5
6
7 This software is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License (LGPL) as published by
9 the Free Software Foundation, either version 2.1 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License (LGPL) for more details.
16
17 You should have received a copy of the GNU Lesser General Public License (LGPL)
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
22
23
24#ifndef JKQTMATHTEXTNODE_H
25#define JKQTMATHTEXTNODE_H
26#include "jkqtmathtext/jkqtmathtext_imexport.h"
27#include "jkqtmathtext/jkqtmathtexttools.h"
28#include <QPainter>
29
30class JKQTMathText; // forward
31
32/** \brief subclass representing one node in the syntax tree
33 * \ingroup jkqtmathtext_items
34 *
35 * \image html jkqtmathtext/jkqtmathtext_node_geo.png
36 */
38 public:
39 explicit JKQTMathTextNode(JKQTMathText* parentMathText);
43 /** \brief determine the size of the node, calls getSizeInternal() implementation of the actual type \see getSizeInternal()
44 *
45 * \param painter painter to use for determining the size
46 * \param currentEv current environment object
47 *
48 * \return all important box size parameters packed as JKQTMathTextNodeSize
49 *
50 */
51 JKQTMathTextNodeSize getSize(QPainter& painter, JKQTMathTextEnvironment currentEv) const;
52 /** \brief calculates the x-size-difference between the given (probably) italic (width externally calculated: \a width_potentiallyitalic, \a ev_potentiallyitalic) and the non-italic version of \a child */
53 static double getNonItalicXCorretion(QPainter &painter, double width_potentiallyitalic, const JKQTMathTextEnvironment &ev_potentiallyitalic, const JKQTMathTextNode* child) ;
54 /** \brief draw the contents at the designated position
55 *
56 * \param painter QPainter to use
57 * \param x x-position, where the drawing starts [Pixel]
58 * \param y Y-position of the baseline, where the drawing starts [Pixel]
59 * \param currentEv JKQTMathTextEnvironment object describing the current drawing environment/settings
60 *
61 * \return the x position which to use for the next part of the text
62 */
63 virtual double draw(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv) const=0;
64 /** \brief convert node to HTML and returns \c true on success
65 * \param[out] html new HTML code is APPENDED to this string
66 * \param currentEv JKQTMathTextEnvironment object describing the current drawing environment/settings
67 * \param defaultEv JKQTMathTextEnvironment object describing the default drawing environment/settings when starting to interpret a node tree
68 * \return \c true on success
69 */
70 virtual bool toHtml(QString& html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) const;
71
72 /** \brief returns the drawing of colored boxes (for DEBUGGING) around the actual output of the node is enabled */
73 bool getDrawBoxes() const;
74 /** \brief enables the drawing of colored boxes (for DEBUGGING) around the actual output of the node */
75 virtual void setDrawBoxes(bool draw);
76 /** \brief return the name of this class as a string */
77 virtual QString getTypeName() const;
78 /** \copydoc parentNode */
80 /** \copydoc parentNode */
82 /** \copydoc parentNode */
84 /** \copydoc subSuperscriptAboveBelowNode */
86 /** \copydoc subSuperscriptAboveBelowNode */
88
89 protected:
90 /** \brief determine the size of the node, overwrite this function in derived classes
91 *
92 * \param painter painter to use for determining the size
93 * \param currentEv current environment object
94 *
95 * \return all important box size parameters packed as JKQTMathTextNodeSize
96 *
97 */
98 virtual JKQTMathTextNodeSize getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv) const =0;
99
100 /** \brief parent JKQTMathText object (required for several drawing operations */
102 /** \brief parent node of this node (i.e. one level up, ode \c nullptr ) */
104 /** \brief enables the drawing of colored boxes (for DEBUGGING) around the actual output of the node */
106 /** \brief if \c true then following sub- and/or superscripts are placed below and above the node, not besides it.
107 * This is activated when \c \\sum\\limits_{sub}^{sup} is used in LaTeX instead of simply \c \\sum_{sub}^{sup}
108 *
109 * The default is c false
110 */
112 /** \brief draws colored boxes (for DEBUGGING) around the actual output of the node
113 *
114 * \param painter QPainter to use
115 * \param x x-position, where the drawing starts [Pixel]
116 * \param y Y-position of the baseline, where the drawing starts [Pixel]
117 * \param size size of the node, result of getSize(), see JKQTMathTextNodeSize
118 */
119 void doDrawBoxes(QPainter& painter, double x, double y, const JKQTMathTextNodeSize& size) const;
120 /** \brief draws colored boxes (for DEBUGGING) around the actual output of the node
121 *
122 * \param painter QPainter to use
123 * \param x x-position, where the drawing starts [Pixel]
124 * \param y Y-position of the baseline, where the drawing starts [Pixel]
125 * \param currentEv JKQTMathTextEnvironment object describing the current drawing environment/settings
126 *
127 * \note This version of the function calls getSize() internally. There is a second variant that
128 * skips this call and expects the node size info as parameter. This can be used in
129 * draw() implementations that call getSize() themselves to speed up drawing.
130 */
131 void doDrawBoxes(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv) const;
132
133
134 /** \brief returns the list of parent, parent-of-parent, ... that can be cast to type \a T */
135 template<class T>
136 inline QList<T*> getParents() {
137 QList<T*> lst;
138 JKQTMathTextNode* p=getParentNode();
139 T* pT=dynamic_cast<T*>(p);
140 while (p!=nullptr) {
141 if (pT!=nullptr) lst.append(pT);
142 p=p->getParentNode();
143 pT=dynamic_cast<T*>(p);
144 }
145 return lst;
146 }
147
148 /** \brief returns the list of parent, parent-of-parent, ... that can be cast to type \a T */
149 template<class T>
150 inline QList<const T*> getParents() const {
151 QList<const T*> lst;
152 const JKQTMathTextNode* p=getParentNode();
153 const T* pT=dynamic_cast<const T*>(p);
154 while (p!=nullptr) {
155 if (pT!=nullptr) lst.append(pT);
156 p=p->getParentNode();
157 pT=dynamic_cast<const T*>(p);
158 }
159 return lst;
160 }
161
162 /** \brief adds a new error to the JKQTMathText referenced by parentMathText
163 *
164 * \see JKQTMathText::addToErrorList()
165 */
166 void addToErrorList(const QString& error);
167
168};
169
170
171/** \brief subclass representing a node in the syntax tree, that has one child
172 * \ingroup jkqtmathtext_items
173 */
175 public:
178
179 /** \copydoc child */
181 /** \copydoc child */
183 /** \brief replaces the child node with the node \a newChild , returns the replaced old node */
185
186 /** \brief replaces the child node with the node \a newChild , deletes the replaced old node */
188
189 /** \brief returns \c true if the child is valie (\c !=nullptr ) */
190 bool hasChild() const;
191
192 /** \copydoc JKQTMathTextNode::setDrawBoxes() */
193 virtual void setDrawBoxes(bool draw) override;
194 protected:
195 /** \brief child node of this node */
197};
198
199/** \brief subclass representing a node in the syntax tree, that has two children
200 * \ingroup jkqtmathtext_items
201 */
203 public:
204 explicit JKQTMathTextMultiChildNode(JKQTMathText* parentMathText);
205 virtual ~JKQTMathTextMultiChildNode() override;
206
207 /** \brief returns a list of all child node */
208 virtual QList<JKQTMathTextNode*> getChildren();
209 /** \brief returns a list of all child node */
210 virtual QList<const JKQTMathTextNode*> getChildren() const;
211 /** \brief returns the i-th child node */
212 virtual JKQTMathTextNode* getChild(int i)=0;
213 /** \brief returns the i-th child node */
214 virtual const JKQTMathTextNode* getChild(int i) const=0;
215
216 /** \brief returns the first child node */
218 /** \brief returns the first child node */
219 virtual const JKQTMathTextNode* getFirstChild() const;
220 /** \brief returns the last child node */
222 /** \brief returns the last child node */
223 virtual const JKQTMathTextNode* getLastChild() const;
224
225 /** \brief replaces the i-th child node with the node \a newChild , returns the replaced old node */
226 virtual JKQTMathTextNode* replaceChild(int i, JKQTMathTextNode* newChild) =0;
227
228 /** \brief replaces the i-th child node with the node \a newChild , deletes the replaced old node */
229 virtual void replaceAndDeleteChild(int i, JKQTMathTextNode* newChild);
230
231 /** \brief returns the number of child nodes */
232 virtual int childCount() const =0;
233 /** \brief returns \c true if children exist */
234 bool hasChildren() const;
235 /** \brief returns \c true if there are no children */
236 bool isEmpty() const;
237
238 /** \brief clear all children, deleting them if \a deleteChildren==true */
239 virtual void clearChildren(bool deleteChildren=true) =0;
240 /** \brief delete the i-th child */
241 virtual void deleteChild(int i) =0;
242
243 /** \copydoc JKQTMathTextNode::setDrawBoxes() */
244 virtual void setDrawBoxes(bool draw) override;
245 protected:
246};
247
248/** \brief subclass representing a node in the syntax tree, that has two children
249 * \ingroup jkqtmathtext_items
250 */
252 public:
253 explicit JKQTMathTextDualChildNode(JKQTMathTextNode* _child1, JKQTMathTextNode* _child2, JKQTMathText* parentMathText);
254 virtual ~JKQTMathTextDualChildNode() override;
255
256 /** \copydoc child1 */
258 /** \copydoc child1 */
260 /** \copydoc child2 */
262 /** \copydoc child2 */
264
265
266 /** \copydoc JKQTMathTextMultiChildNode::getChild() */
267 virtual JKQTMathTextNode* getChild(int i) override;
268 /** \copydoc JKQTMathTextMultiChildNode::getChild() */
269 virtual const JKQTMathTextNode* getChild(int i) const override;
270 /** \copydoc JKQTMathTextMultiChildNode::getChild() */
271 virtual JKQTMathTextNode* replaceChild(int i, JKQTMathTextNode* newChild) override;
272 /** \copydoc JKQTMathTextMultiChildNode::getChild() */
273 virtual int childCount() const override;
274 /** \copydoc JKQTMathTextMultiChildNode::getChild() */
275 virtual void clearChildren(bool deleteChildren=true) override;
276 /** \copydoc JKQTMathTextMultiChildNode::deleteChild() */
277 virtual void deleteChild(int i) override;
278
279
280 protected:
281 /** \brief first child node of this node */
283 /** \brief second child node of this node */
285};
286
287
288
289
290/** \brief base class for all derived classes that do not draw anything
291 * \ingroup jkqtmathtext_items
292 *
293 * This class finalizes draw() with no drawing actions and and getSizeInternal(), which
294 * return a size 0.
295 */
297 public:
300 /** \copydoc JKQTMathTextNode::draw() */
301 virtual double draw(QPainter& painter, double x, double y, JKQTMathTextEnvironment currentEv) const override final;
302 /** \copydoc JKQTMathTextNode::toHtml() */
303 virtual bool toHtml(QString& html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) const override final;
304 protected:
305 /** \copydoc JKQTMathTextNode::getSizeInternal() */
306 virtual JKQTMathTextNodeSize getSizeInternal(QPainter& painter, JKQTMathTextEnvironment currentEv) const override final;
307
308};
309
310
311/** \brief mixin extending a node that does not produce any output,
312 * but provides a function modifyEnvironment() that modifies the current
313 * JKQTMathTextEnvironment. Deriving classes are used to represent
314 * instructions like \c \\bf or \c \\color{...} that alter the text
315 * formatting for all further nodes in the current block.
316 * \ingroup jkqtmathtext_items
317 *
318 * Classes derived from this require a parent node that executes the additional method
319 * modifyEnvironment(). In the context of JKQTMathText, this is done by JKQTMathTextHorizontalListNode .
320 * Therefor the effect of the node end with the last nod in the parent list node.
321 */
323 public:
325 /** \brief modifies the fiven JKQTMathTextEnvironment \a currrentEv */
326 virtual void modifyEnvironment(JKQTMathTextEnvironment& currentEv) const=0;
327 protected:
328
329};
330
331
332
333#endif // JKQTMATHTEXTNODE_H
subclass representing a node in the syntax tree, that has two children
Definition jkqtmathtextnode.h:251
JKQTMathTextNode * getChild1()
first child node of this node
virtual void clearChildren(bool deleteChildren=true) override
returns the i-th child node
virtual void deleteChild(int i) override
delete the i-th child
const JKQTMathTextNode * getChild2() const
second child node of this node
virtual JKQTMathTextNode * replaceChild(int i, JKQTMathTextNode *newChild) override
returns the i-th child node
JKQTMathTextNode * getChild2()
second child node of this node
virtual const JKQTMathTextNode * getChild(int i) const override
returns the i-th child node
JKQTMathTextNode * child2
second child node of this node
Definition jkqtmathtextnode.h:284
JKQTMathTextDualChildNode(JKQTMathTextNode *_child1, JKQTMathTextNode *_child2, JKQTMathText *parentMathText)
JKQTMathTextNode * child1
first child node of this node
Definition jkqtmathtextnode.h:282
const JKQTMathTextNode * getChild1() const
first child node of this node
virtual ~JKQTMathTextDualChildNode() override
virtual int childCount() const override
returns the i-th child node
virtual JKQTMathTextNode * getChild(int i) override
returns the i-th child node
this class parses a mathematical markup string and can then draw the contained text/equation onto a Q...
Definition jkqtmathtext.h:192
mixin extending a node that does not produce any output, but provides a function modifyEnvironment() ...
Definition jkqtmathtextnode.h:322
virtual void modifyEnvironment(JKQTMathTextEnvironment &currentEv) const =0
modifies the fiven JKQTMathTextEnvironment currrentEv
virtual ~JKQTMathTextModifyEnvironmentNodeMixIn()
Definition jkqtmathtextnode.h:324
subclass representing a node in the syntax tree, that has two children
Definition jkqtmathtextnode.h:202
virtual void setDrawBoxes(bool draw) override
enables the drawing of colored boxes (for DEBUGGING) around the actual output of the node
virtual const JKQTMathTextNode * getLastChild() const
returns the last child node
bool hasChildren() const
returns true if children exist
virtual ~JKQTMathTextMultiChildNode() override
virtual JKQTMathTextNode * replaceChild(int i, JKQTMathTextNode *newChild)=0
replaces the i-th child node with the node newChild , returns the replaced old node
virtual QList< const JKQTMathTextNode * > getChildren() const
returns a list of all child node
virtual int childCount() const =0
returns the number of child nodes
virtual JKQTMathTextNode * getChild(int i)=0
returns the i-th child node
virtual const JKQTMathTextNode * getFirstChild() const
returns the first child node
virtual const JKQTMathTextNode * getChild(int i) const =0
returns the i-th child node
virtual JKQTMathTextNode * getLastChild()
returns the last child node
JKQTMathTextMultiChildNode(JKQTMathText *parentMathText)
virtual void deleteChild(int i)=0
delete the i-th child
virtual void replaceAndDeleteChild(int i, JKQTMathTextNode *newChild)
replaces the i-th child node with the node newChild , deletes the replaced old node
bool isEmpty() const
returns true if there are no children
virtual QList< JKQTMathTextNode * > getChildren()
returns a list of all child node
virtual void clearChildren(bool deleteChildren=true)=0
clear all children, deleting them if deleteChildren==true
virtual JKQTMathTextNode * getFirstChild()
returns the first child node
subclass representing one node in the syntax tree
Definition jkqtmathtextnode.h:37
virtual JKQTMathTextNodeSize getSizeInternal(QPainter &painter, JKQTMathTextEnvironment currentEv) const =0
determine the size of the node, overwrite this function in derived classes
JKQTMathTextNode(JKQTMathText *parentMathText)
QList< const T * > getParents() const
returns the list of parent, parent-of-parent, ... that can be cast to type T
Definition jkqtmathtextnode.h:150
JKQTMathTextNode(const JKQTMathTextNode &)=delete
void doDrawBoxes(QPainter &painter, double x, double y, JKQTMathTextEnvironment currentEv) const
draws colored boxes (for DEBUGGING) around the actual output of the node
virtual void setDrawBoxes(bool draw)
enables the drawing of colored boxes (for DEBUGGING) around the actual output of the node
JKQTMathTextNode & operator=(const JKQTMathTextNode &)=delete
JKQTMathText * parentMathText
parent JKQTMathText object (required for several drawing operations
Definition jkqtmathtextnode.h:101
virtual bool toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) const
convert node to HTML and returns true on success
virtual QString getTypeName() const
return the name of this class as a string
JKQTMathTextNode * parentNode
parent node of this node (i.e. one level up, ode nullptr )
Definition jkqtmathtextnode.h:103
virtual double draw(QPainter &painter, double x, double y, JKQTMathTextEnvironment currentEv) const =0
draw the contents at the designated position
bool drawBoxes
enables the drawing of colored boxes (for DEBUGGING) around the actual output of the node
Definition jkqtmathtextnode.h:105
void addToErrorList(const QString &error)
adds a new error to the JKQTMathText referenced by parentMathText
bool getDrawBoxes() const
returns the drawing of colored boxes (for DEBUGGING) around the actual output of the node is enabled
bool isSubSuperscriptAboveBelowNode() const
if true then following sub- and/or superscripts are placed below and above the node,...
void doDrawBoxes(QPainter &painter, double x, double y, const JKQTMathTextNodeSize &size) const
draws colored boxes (for DEBUGGING) around the actual output of the node
JKQTMathTextNode * getParentNode()
parent node of this node (i.e. one level up, ode nullptr )
const JKQTMathTextNode * getParentNode() const
parent node of this node (i.e. one level up, ode nullptr )
QList< T * > getParents()
returns the list of parent, parent-of-parent, ... that can be cast to type T
Definition jkqtmathtextnode.h:136
static double getNonItalicXCorretion(QPainter &painter, double width_potentiallyitalic, const JKQTMathTextEnvironment &ev_potentiallyitalic, const JKQTMathTextNode *child)
calculates the x-size-difference between the given (probably) italic (width externally calculated: wi...
virtual ~JKQTMathTextNode()
void setParentNode(JKQTMathTextNode *node)
parent node of this node (i.e. one level up, ode nullptr )
JKQTMathTextNodeSize getSize(QPainter &painter, JKQTMathTextEnvironment currentEv) const
determine the size of the node, calls getSizeInternal() implementation of the actual type
void setSubSuperscriptAboveBelowNode(bool __value)
if true then following sub- and/or superscripts are placed below and above the node,...
bool subSuperscriptAboveBelowNode
if true then following sub- and/or superscripts are placed below and above the node,...
Definition jkqtmathtextnode.h:111
base class for all derived classes that do not draw anything
Definition jkqtmathtextnode.h:296
virtual double draw(QPainter &painter, double x, double y, JKQTMathTextEnvironment currentEv) const override final
draw the contents at the designated position
JKQTMathTextNonDrawingBaseNode(JKQTMathText *parent)
virtual JKQTMathTextNodeSize getSizeInternal(QPainter &painter, JKQTMathTextEnvironment currentEv) const override final
determine the size of the node, overwrite this function in derived classes
virtual ~JKQTMathTextNonDrawingBaseNode() override
virtual bool toHtml(QString &html, JKQTMathTextEnvironment currentEv, JKQTMathTextEnvironment defaultEv) const override final
convert node to HTML and returns true on success
subclass representing a node in the syntax tree, that has one child
Definition jkqtmathtextnode.h:174
void replaceAndDeleteChild(JKQTMathTextNode *newChild)
replaces the child node with the node newChild , deletes the replaced old node
const JKQTMathTextNode * getChild() const
child node of this node
JKQTMathTextSingleChildNode(JKQTMathTextNode *_child, JKQTMathText *parentMathText)
JKQTMathTextNode * getChild()
child node of this node
JKQTMathTextNode * replaceChild(JKQTMathTextNode *newChild)
replaces the child node with the node newChild , returns the replaced old node
virtual ~JKQTMathTextSingleChildNode() override
bool hasChild() const
returns true if the child is valie (!=nullptr )
JKQTMathTextNode * child
child node of this node
Definition jkqtmathtextnode.h:196
virtual void setDrawBoxes(bool draw) override
enables the drawing of colored boxes (for DEBUGGING) around the actual output of the node
#define JKQTMATHTEXT_LIB_EXPORT
Definition jkqtmathtext_imexport.h:108
describes the current drawing environment (base fontname ...)
Definition jkqtmathtexttools.h:304
beschreibt die Größe(n) eines Knotens
Definition jkqtmathtexttools.h:393