File indexing completed on 2024-04-21 05:44:06

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2004 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef VARIANT_H
0012 #define VARIANT_H
0013 
0014 #include <QObject>
0015 #include <QStringList>
0016 #include <QVariant>
0017 
0018 #include "filefilters.h"
0019 
0020 /// \todo Replace "Variant" with "Property"
0021 class Variant;
0022 typedef Variant Property;
0023 
0024 class QColor;
0025 class QString;
0026 
0027 typedef QMap<QString, QString> QStringMap;
0028 
0029 /**
0030 For information:
0031 QVariant::type() returns an enum for the current data type
0032 contained. e.g. returns QVariant::Color or QVariant::Rect
0033 @author Daniel Clarke
0034 @author David Saxton
0035 */
0036 class Variant : public QObject
0037 {
0038     Q_OBJECT
0039 public:
0040     class Type
0041     {
0042     public:
0043         enum Value {
0044             None,
0045             Int,          // Integer
0046             Raw,          // QByteArray
0047             Double,       // Real number
0048             String,       // Editable string
0049             Multiline,    // String that may contain linebreaks
0050             RichText,     // HTML formatted text
0051             Select,       // Selection of strings
0052             Combo,        // Editable combination of strings
0053             FileName,     // Filename on local filesystem
0054             Color,        // Color
0055             Bool,         // Boolean
0056             VarName,      // Variable name
0057             Port,         // Port name
0058             Pin,          // Pin name
0059             PenStyle,     // Pen Style
0060             PenCapStyle,  // Pen Cap Style
0061             SevenSegment, // Pin Map for Seven Segment Display
0062             KeyPad        // Pin Map for Keypad
0063         };
0064     };
0065 
0066     Variant(const QString &id, Type::Value type);
0067     ~Variant() override;
0068 
0069     QString id() const
0070     {
0071         return m_id;
0072     }
0073 
0074     /**
0075      * Returns the type of Variant (see Variant::Type::Value)
0076      */
0077     Variant::Type::Value type() const
0078     {
0079         return m_type;
0080     }
0081     /**
0082      * Sets the variant type
0083      */
0084     void setType(Type::Value type);
0085     /**
0086      * Returns the filters used for file dialogs (if this is of type Type::FileName)
0087      */
0088     FileFilters fileFilters() const
0089     {
0090         return m_fileFilters;
0091     }
0092     void setFileFilters(const FileFilters &fileFilters)
0093     {
0094         m_fileFilters = fileFilters;
0095     }
0096     /**
0097      * The selection of colours to be used in the combo box - e.g.
0098      * ColorCombo::LED.
0099      * @see ColorCombo::ColorScheme
0100      */
0101     int colorScheme() const
0102     {
0103         return m_colorScheme;
0104     }
0105     void setColorScheme(int colorScheme)
0106     {
0107         m_colorScheme = colorScheme;
0108     }
0109     /**
0110      * This function is for convenience; it sets both the toolbar and editor
0111      * caption.
0112      */
0113     void setCaption(const QString &caption)
0114     {
0115         setToolbarCaption(caption);
0116         setEditorCaption(caption);
0117     }
0118     /**
0119      * This text is displayed to the left of the entry widget in the toolbar
0120      */
0121     QString toolbarCaption() const
0122     {
0123         return m_toolbarCaption;
0124     }
0125     void setToolbarCaption(const QString &caption)
0126     {
0127         m_toolbarCaption = caption;
0128     }
0129     /**
0130      * This text is displayed to the left of the entry widget in the item editor
0131      */
0132     QString editorCaption() const
0133     {
0134         return m_editorCaption;
0135     }
0136     void setEditorCaption(const QString &caption)
0137     {
0138         m_editorCaption = caption;
0139     }
0140     /**
0141      * Unit of number, (e.g. V (volts) / F (farads))
0142      */
0143     QString unit() const
0144     {
0145         return m_unit;
0146     }
0147     void setUnit(const QString &unit)
0148     {
0149         m_unit = unit;
0150     }
0151     /**
0152      * The smallest (as in negative, not absoluteness) value that the user can
0153      * set this to.
0154      */
0155     double minValue() const
0156     {
0157         return m_minValue;
0158     }
0159     void setMinValue(double value);
0160     /**
0161      * The largest (as in positive, not absoluteness) value that the user can
0162      * set this to.
0163      */
0164     double maxValue() const
0165     {
0166         return m_maxValue;
0167     }
0168     void setMaxValue(double value);
0169     /**
0170      * The smallest absolute value that the user can set this to, before the
0171      * value is considered zero.
0172      */
0173     double minAbsValue() const
0174     {
0175         return m_minAbsValue;
0176     }
0177     void setMinAbsValue(double val);
0178     QVariant defaultValue() const
0179     {
0180         return m_defaultValue;
0181     }
0182     /**
0183      * If this data is marked as advanced, it will only display in the item
0184      * editor (and not in the toolbar)
0185      */
0186     void setAdvanced(bool advanced)
0187     {
0188         m_bAdvanced = advanced;
0189     }
0190     bool isAdvanced() const
0191     {
0192         return m_bAdvanced;
0193     }
0194     /**
0195      * If this data is marked as hidden, it will not be editable from anywhere
0196      * in the user interface
0197      */
0198     void setHidden(bool hidden)
0199     {
0200         m_bHidden = hidden;
0201     }
0202     bool isHidden() const
0203     {
0204         return m_bHidden;
0205     }
0206     /**
0207      * Returns the best possible attempt at representing the data in a string
0208      * for display. Used by the properties list view.
0209      */
0210     QString displayString() const;
0211     /**
0212      * The list of values that the data is allowed to take (if it is string)
0213      * that is displayed to the user.
0214      */
0215     QStringList allowed() const
0216     {
0217         return m_allowed.values();
0218     }
0219     /**
0220      * @param allowed A list of pairs of (id, i18n-name) of allowed values.
0221      */
0222     void setAllowed(const QStringMap &allowed)
0223     {
0224         m_allowed = allowed;
0225     }
0226     void setAllowed(const QStringList &allowed);
0227     void appendAllowed(const QString &id, const QString &i18nName);
0228     void appendAllowed(const QString &allowed);
0229     /**
0230      * @return whether the current value is different to the default value.
0231      */
0232     bool changed() const;
0233     QVariant value() const
0234     {
0235         return m_value;
0236     }
0237     void setValue(QVariant val);
0238 
0239 signals:
0240     /**
0241      * Emitted when the value changes.
0242      * NOTE: The order of data given is the new value, and then the old value
0243      * This is done so that slots that don't care about the old value don't
0244      * have to accept it
0245      */
0246     void valueChanged(QVariant newValue, QVariant oldValue);
0247     /**
0248      * Emitted for variants of string-like type.
0249      */
0250     void valueChanged(const QString &newValue);
0251     /**
0252      * Emitted for variants of string-like type.
0253      * This signal is needed for updating values in KComboBox-es, see KComboBox::setCurrentItem(),
0254      *   second bool parameter, insert.
0255      */
0256     void valueChangedStrAndTrue(const QString &newValue, bool trueBool);
0257     /**
0258      * Emitted for variants of int-like type.
0259      */
0260     void valueChanged(int newValue);
0261     /**
0262      * Emitted for variants of double-like type.
0263      */
0264     void valueChanged(double newValue);
0265     /**
0266      * Emitted for variants of color-like type.
0267      */
0268     void valueChanged(const QColor &newValue);
0269     /**
0270      * Emitted for variants of bool-like type.
0271      */
0272     void valueChanged(bool newValue);
0273 
0274 private:
0275     QVariant m_value; // the actual data
0276     QVariant m_defaultValue;
0277     QString m_unit;
0278     const QString m_id;
0279     double m_minAbsValue;
0280     double m_minValue;
0281     double m_maxValue;
0282     QString m_toolbarCaption; // Short description shown in e.g. properties dialog
0283     QString m_editorCaption;  // Text displayed before the data entry widget in the toolbar
0284     bool m_bAdvanced;         // If advanced, only display data in item editor
0285     bool m_bHidden;           // If hidden, do not allow user to change data
0286     FileFilters m_fileFilters; // If type() == Type::FileName these are the filters used in file dialogs.
0287     bool m_bSetDefault;       // If false, then the default will be set to the first thing this variant is set to
0288     Type::Value m_type;
0289     QStringMap m_allowed;
0290     int m_colorScheme;
0291 };
0292 
0293 #endif