File indexing completed on 2024-04-14 03:40:42

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     SPDX-FileCopyrightText: 2006 David Saxton <david@bluehaze.org>
0006 
0007     This file is part of the KDE Project.
0008     KmPlot is part of the KDE-EDU Project.
0009 
0010     SPDX-License-Identifier: GPL-2.0-or-later
0011 
0012 */
0013 
0014 #ifndef CONSTANT_H
0015 #define CONSTANT_H
0016 
0017 #include "function.h"
0018 
0019 #include <QMap>
0020 #include <QObject>
0021 
0022 /**
0023  * Stores the details of a constant other than its name.
0024  * \author David Saxton
0025  */
0026 class Constant
0027 {
0028 public:
0029     Constant();
0030 
0031     /**
0032      * The scope of the constant.
0033      */
0034     enum Type {
0035         Document = 0x1, ///< The constant is part of the document
0036         Global = 0x2, ///< The constant is to be saved globally in the application settings
0037         All = 0x4 - 1
0038     };
0039 
0040     /**
0041      * The actual value of the constant.
0042      */
0043     Value value;
0044     /**
0045      * The OR'ed types.
0046      */
0047     int type;
0048 };
0049 
0050 typedef QMap<QString, Constant> ConstantList;
0051 
0052 /**
0053  * @short Manages a list of constants.
0054  */
0055 class Constants : public QObject
0056 {
0057     Q_OBJECT
0058 
0059 public:
0060     Constants();
0061     virtual ~Constants();
0062 
0063     /**
0064      * Load the constants at the start.
0065      */
0066     void load();
0067     /**
0068      * Save the constants when closing the program.
0069      */
0070     void save();
0071     /**
0072      * \return if the constant name is valid.
0073      */
0074     bool isValidName(const QString &name) const;
0075     /**
0076      * \return the value of the constant with the given name. This will
0077      * return a default Value if the constant does not exist; use
0078      * Constants::have to check for existence.
0079      */
0080     Value value(const QString &name) const;
0081     /**
0082      * Removes the constant with the given name from the constants list.
0083      */
0084     void remove(const QString &name);
0085     /**
0086      * Adds the constant to the internal list (overwriting any previous
0087      * constant with the same name).
0088      */
0089     void add(const QString &name, const Constant &constant);
0090     /**
0091      * \return whether the constant with the given name exists.
0092      */
0093     bool have(const QString &name) const;
0094     /**
0095      * \return a unique (i.e. unused) constant name.
0096      */
0097     QString generateUniqueName() const;
0098     /**
0099      * \param type OR'ed list of Constant::Type
0100      * \return a copy of the list of constants.
0101      */
0102     ConstantList list(int type) const;
0103     /**
0104      * \return a list of the constant names.
0105      */
0106     QStringList names() const
0107     {
0108         return m_constants.keys();
0109     }
0110 
0111 signals:
0112     /**
0113      * Emitted when a constant is added or removed, or the value of an
0114      * existing constant has changed.
0115      */
0116     void constantsChanged();
0117 
0118 protected:
0119     ConstantList m_constants;
0120 };
0121 
0122 #endif // CONSTANT_H