Krita Source Code Documentation
Loading...
Searching...
No Matches
EnhancedPathFormula.h
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#ifndef KOENHANCEDPATHFORMULA_H
8#define KOENHANCEDPATHFORMULA_H
9
10#include <QString>
11#include <QVariant>
12
14
16{
17public:
26
38
40 explicit FormulaToken(Type type = TypeUnknown, const QString &text = QString(), int position = -1);
41
43 FormulaToken(const FormulaToken &token);
44
46 FormulaToken &operator=(const FormulaToken &token);
47
49 Type type() const
50 {
51 return m_type;
52 }
54 QString text() const
55 {
56 return m_text;
57 }
59 int position() const
60 {
61 return m_position;
62 }
63
65 bool isNumber() const
66 {
67 return m_type == TypeNumber;
68 }
70 bool isOperator() const
71 {
72 return m_type == TypeOperator;
73 }
75 bool isFunction() const
76 {
77 return m_type == TypeFunction;
78 }
80 bool isReference() const
81 {
82 return m_type == TypeReference;
83 }
84
86 qreal asNumber() const;
88 Operator asOperator() const;
89private:
91 QString m_text;
92 int m_position {-1};
93};
94
96
97class Opcode;
98
100{
101public:
119
128
130 EnhancedPathFormula(const QString &text, EnhancedPathShape *parent);
131
134
141 qreal evaluate();
142
144 Error error() const
145 {
146 return m_error;
147 }
148
150 QString toString() const;
151private:
153 TokenList scan(const QString &formula) const;
154
156 bool compile(const TokenList &tokens);
157
164 qreal evaluateFunction(Function function, const QList<qreal> &arguments) const;
165
167 void debugTokens(const TokenList &tokens);
169 void debugOpcodes();
170
171 bool m_valid;
174 QString m_text;
178};
179
180#endif // KOENHANCEDPATHFORMULA_H
QList< FormulaToken > TokenList
EnhancedPathShape * m_parent
QString m_text
the formula text representation
QString toString() const
returns string representation of the formula
~EnhancedPathFormula()
Destroys the formula.
QList< QVariant > m_constants
constant values
qreal evaluateFunction(Function function, const QList< qreal > &arguments) const
bool m_compiled
flag that shows if function was compiled
bool m_valid
flag that shows if function is valid, i.e the function was compiled successfully
QList< Opcode > m_codes
the compiled byte code
void debugTokens(const TokenList &tokens)
Prints token list.
Error m_error
the last occurred error
TokenList scan(const QString &formula) const
Separates the given formula text into tokens.
EnhancedPathFormula(const QString &text, EnhancedPathShape *parent)
Constructs a new formula from the specified string representation.
Error
The possible error code returned by error()
@ ErrorValue
error when converting value
@ ErrorCompile
compiling error
@ ErrorName
invalid function name value
Error error() const
Returns the last occurred error.
Function
predefined functions
void debugOpcodes()
Prints byte code.
bool compile(const TokenList &tokens)
Compiles the formula tokens into byte code.
Type type() const
Returns the type of the token.
FormulaToken & operator=(const FormulaToken &token)
assignment operator
QString text() const
Returns the text representation of the token.
bool isFunction() const
Returns if token is a function.
FormulaToken(Type type=TypeUnknown, const QString &text=QString(), int position=-1)
Constructs token with given type, text and position.
Operator
operator types
@ OperatorLeftPar
(left parentheses
@ OperatorInvalid
invalid operator
@ OperatorRightPar
) right parentheses
@ OperatorDiv
/ division
bool isOperator() const
Returns if the token is a operator, OperatorInvalid if token is no operator.
Operator asOperator() const
Returns the token as operator.
qreal asNumber() const
Returns the token converted to qreal.
QString m_text
the token text representation
bool isReference() const
Returns if token is a reference.
Type m_type
the token type
@ TypeReference
function reference, modifier reference or named variable
@ TypeOperator
+, *, /, -
@ TypeNumber
14, 3, 1977, 3.141592, 1e10, 5.9e-7
@ TypeUnknown
unknown type
@ TypeFunction
function name
bool isNumber() const
Returns if the token is a number.
int position() const
Returns the position of the token.
int m_position
the tokens position