Warning, file /sdk/ktechlab/src/variant.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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