File indexing completed on 2024-04-28 07:29:29

0001 /*
0002     KmPlot - a math. function plotter for the KDE-Desktop
0003 
0004     SPDX-FileCopyrightText: 1998, 1999, 2000, 2002 Klaus-Dieter Möller <kd.moeller@t-online.de>
0005 
0006     This file is part of the KDE Project.
0007     KmPlot is part of the KDE-EDU Project.
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 
0011 */
0012 
0013 #ifndef KMPLOTIO_H
0014 #define KMPLOTIO_H
0015 
0016 #include <QGradient>
0017 #include <QUrl>
0018 
0019 class Equation;
0020 class Function;
0021 class QDomDocument;
0022 class QDomElement;
0023 class QString;
0024 
0025 /** @short This class manages the file operations load and save.
0026  *
0027  * @author Klaus-Dieter Möller & Matthias Meßmer
0028  */
0029 class KmPlotIO
0030 {
0031 public:
0032     /// Nothing to do here; only static functions needed.
0033     KmPlotIO();
0034     /// Empty.
0035     ~KmPlotIO();
0036 
0037     /**
0038      * Store all information about the current saved plot in a xml file
0039      * with the .fkt extension in the filename file.
0040      * @param url Name (URL) of the file which will be saved.
0041      */
0042     bool save(const QUrl &url);
0043 
0044     /**
0045      * @return a QDomDocument describing the current KmPlot state (settings
0046      * and functions).
0047      */
0048     QDomDocument currentState();
0049 
0050     /**
0051      * Read a kmpdoc xml file to restore the settings of a previously saved
0052      * plot.
0053      * @param url Name (URL) of file which will be opened
0054      */
0055     bool load(const QUrl &url);
0056 
0057     /**
0058      * Restore KmPlot to the state described in the given QDomDocument.
0059      * @return success status
0060      */
0061     bool restore(const QDomDocument &doc);
0062 
0063     /**
0064      * Adds a QDomElement for \p function to the given \p document
0065      */
0066     void addFunction(QDomDocument &doc, QDomElement &root, Function *function);
0067     /**
0068      * Adds a QDomElement for the Constants (stored in Parser) to \p document
0069      */
0070     void addConstants(QDomDocument &doc, QDomElement &root);
0071     /**
0072      * Reads function parameters from the node @a n.
0073      * @param n Node containing the options.
0074      * @param allowRename whether to check function names for uniqueness
0075      */
0076     void parseFunction(const QDomElement &n, bool allowRename = false);
0077 
0078 private:
0079     /** Esay way to add a tag to the Dom tree
0080      * @param &doc The document.
0081      * @param parentTag The parent tag to support encapsulated tags.
0082      * @param tagName The Name of the tag.
0083      * @param tagValue The data between the opening and closing tag.
0084      * @return The QDomElement that was created.
0085      */
0086     QDomElement addTag(QDomDocument &doc, QDomElement &parentTag, const QString &tagName, const QString &tagValue);
0087     /// Reads axes parameters from the node @a n.
0088     /// @param n Node containing the options.
0089     void parseAxes(const QDomElement &n);
0090     /// Reads grid parameters from the node @a n.
0091     /// @param n Node containing the options.
0092     void parseGrid(const QDomElement &n);
0093     /// Reads scale parameters from the node @a n.
0094     /// @param n Node containing the options.
0095     void parseScale(const QDomElement &n);
0096     /**
0097      * Reads in a constant from the node \p n.
0098      */
0099     void parseConstant(const QDomElement &n);
0100     /// Reads parameter values for a function from the node @a n.
0101     /// @param parser points to the parser instance.
0102     /// @param n Node containing the options.
0103     /// @param ix Function index in the parser instance
0104     void parseParameters(const QDomElement &n, Function *function);
0105     /**
0106      * Initializes \p equation from the dom element.
0107      */
0108     void parseDifferentialStates(const QDomElement &e, Equation *equation);
0109 
0110     /// For KDE <3.3
0111     ///  This is the same as parseFunction but is made for old KmPlot-files
0112     void oldParseFunction(const QDomElement &n);
0113     /**
0114      * For KDE <4.0
0115      * Reads function parameters from the node @a n.
0116      * @param n Node containing the options.
0117      * @param allowRename whether to check function names for uniqueness
0118      */
0119     void oldParseFunction2(const QDomElement &n);
0120 
0121     double lengthScaler; ///< for reading in lengths
0122     /**
0123      * version of the file currently being opened (0,1,2,3,4)
0124      * \li < 3 is for pre-kde4
0125      * \li 4 is for kde4
0126      */
0127     int version;
0128     QString parametricXEquation; ///< Used when reading in the x part of a parametric equation
0129 
0130     /**
0131      * Converts \p stops to a string representation for saving in a file.
0132      */
0133     static QString gradientToString(const QGradientStops &stops);
0134     /**
0135      * Inverse of gradientToString function.
0136      */
0137     static QGradientStops stringToGradient(const QString &string);
0138 };
0139 
0140 #endif