1 /***************************************************************************
2  *   Copyright (C) 2003-2004 by David Saxton                               *
3  *   david@bluehaze.org                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10 
11 #ifndef VARIANT_H
12 #define VARIANT_H
13 
14 #include <QObject>
15 #include <QVariant>
16 #include <QStringList>
17 
18 /// \todo Replace "Variant" with "Property"
19 class Variant;
20 typedef Variant Property;
21 
22 class QColor;
23 class QString;
24 
25 typedef QMap< QString, QString > QStringMap;
26 
27 /**
28 For information:
29 QVariant::type() returns an enum for the current data type
30 contained. e.g. returns QVariant::Color or QVariant::Rect
31 @author Daniel Clarke
32 @author David Saxton
33 */
34 class Variant : public QObject
35 {
36 Q_OBJECT
37 public:
38 	class Type
39 	{
40 		public:
41 			enum Value
42 			{
43 				None,
44 				Int, // Integer
45 				Raw, // QByteArray
46 				Double, // Real number
47 				String, // Editable string
48 				Multiline, // String that may contain linebreaks
49 				RichText, // HTML formatted text
50 				Select, // Selection of strings
51 				Combo, // Editable combination of strings
52 				FileName, // Filename on local filesystem
53 				Color, // Color
54 				Bool, // Boolean
55 				VarName, // Variable name
56 				Port, // Port name
57 				Pin, // Pin name
58 				PenStyle, // Pen Style
59 				PenCapStyle, // Pen Cap Style
60 				SevenSegment, // Pin Map for Seven Segment Display
61 				KeyPad // Pin Map for Keypad
62 			};
63 	};
64 
65 	Variant( const QString & id, Type::Value type );
66 	~Variant() override;
67 
id()68 	QString id() const { return m_id; }
69 
70 	/**
71 	 * Returns the type of Variant (see Variant::Type::Value)
72 	 */
type()73 	Variant::Type::Value type() const { return m_type; }
74 	/**
75 	 * Sets the variant type
76 	 */
77 	void setType( Type::Value type );
78 	/**
79 	 * Returns the filter used for file dialogs (if this is of type Type::FileName)
80 	 */
filter()81 	QString filter() const { return m_filter; }
setFilter(const QString & filter)82 	void setFilter( const QString & filter ) { m_filter = filter; }
83 	/**
84 	 * The selection of colours to be used in the combo box - e.g.
85 	 * ColorCombo::LED.
86 	 * @see ColorCombo::ColorScheme
87 	 */
colorScheme()88 	int colorScheme() const { return m_colorScheme; }
setColorScheme(int colorScheme)89 	void setColorScheme( int colorScheme ) { m_colorScheme = colorScheme; }
90 	/**
91 	 * This function is for convenience; it sets both the toolbar and editor
92 	 * caption.
93 	 */
setCaption(const QString & caption)94 	void setCaption( const QString & caption ) { setToolbarCaption(caption); setEditorCaption(caption); }
95 	/**
96 	 * This text is displayed to the left of the entry widget in the toolbar
97 	 */
toolbarCaption()98 	QString toolbarCaption() const { return m_toolbarCaption; }
setToolbarCaption(const QString & caption)99 	void setToolbarCaption( const QString & caption ) { m_toolbarCaption = caption; }
100 	/**
101 	 * This text is displayed to the left of the entry widget in the item editor
102 	 */
editorCaption()103 	QString editorCaption() const { return m_editorCaption; }
setEditorCaption(const QString & caption)104 	void setEditorCaption( const QString & caption ) { m_editorCaption = caption; }
105 	/**
106 	 * Unit of number, (e.g. V (volts) / F (farads))
107 	 */
unit()108 	QString unit() const { return m_unit; }
setUnit(const QString & unit)109 	void setUnit( const QString & unit ) { m_unit = unit; }
110 	/**
111 	 * The smallest (as in negative, not absoluteness) value that the user can
112 	 * set this to.
113 	 */
minValue()114 	double minValue() const { return m_minValue; }
115 	void setMinValue( double value );
116 	/**
117 	 * The largest (as in positive, not absoluteness) value that the user can
118 	 * set this to.
119 	 */
maxValue()120 	double maxValue() const { return m_maxValue; }
121 	void setMaxValue( double value );
122 	/**
123 	 * The smallest absolute value that the user can set this to, before the
124 	 * value is considered zero.
125 	 */
minAbsValue()126 	double minAbsValue() const { return m_minAbsValue; }
127 	void setMinAbsValue( double val );
defaultValue()128 	QVariant defaultValue() const { return m_defaultValue; }
129 	/**
130 	 * If this data is marked as advanced, it will only display in the item
131 	 * editor (and not in the toolbar)
132 	 */
setAdvanced(bool advanced)133 	void setAdvanced( bool advanced ) { m_bAdvanced = advanced; }
isAdvanced()134 	bool isAdvanced() const { return m_bAdvanced; }
135 	/**
136 	 * If this data is marked as hidden, it will not be editable from anywhere
137 	 * in the user interface
138 	 */
setHidden(bool hidden)139 	void setHidden( bool hidden ) { m_bHidden = hidden; }
isHidden()140 	bool isHidden() const { return m_bHidden; }
141 	/**
142 	 * Returns the best possible attempt at representing the data in a string
143 	 * for display. Used by the properties list view.
144 	 */
145 	QString displayString() const;
146 	/**
147 	 * The list of values that the data is allowed to take (if it is string)
148 	 * that is displayed to the user.
149 	 */
allowed()150 	QStringList allowed() const { return m_allowed.values(); }
151 	/**
152 	 * @param allowed A list of pairs of (id, i18n-name) of allowed values.
153 	 */
setAllowed(const QStringMap & allowed)154 	void setAllowed( const QStringMap & allowed ) { m_allowed = allowed; }
155 	void setAllowed( const QStringList & allowed );
156 	void appendAllowed( const QString & id, const QString & i18nName );
157 	void appendAllowed( const QString & allowed );
158 	/**
159 	 * @return whether the current value is different to the default value.
160 	 */
161 	bool changed() const;
value()162 	QVariant value() const { return m_value; }
163 	void setValue( QVariant val );
164 
165 signals:
166 	/**
167 	 * Emitted when the value changes.
168 	 * NOTE: The order of data given is the new value, and then the old value
169 	 * This is done so that slots that don't care about the old value don't
170 	 * have to accept it
171 	 */
172 	void valueChanged( QVariant newValue, QVariant oldValue );
173 	/**
174 	 * Emitted for variants of string-like type.
175 	 */
176 	void valueChanged( const QString & newValue );
177     /**
178      * Emitted for variants of string-like type.
179      * This signal is needed for updating values in KComboBox-es, see KComboBox::setCurrentItem(),
180      *   second bool parameter, insert.
181      */
182     void valueChangedStrAndTrue( const QString &newValue, bool trueBool);
183 	/**
184 	 * Emitted for variants of int-like type.
185 	 */
186 	void valueChanged( int newValue );
187 	/**
188 	 * Emitted for variants of double-like type.
189 	 */
190 	void valueChanged( double newValue );
191 	/**
192 	 * Emitted for variants of color-like type.
193 	 */
194 	void valueChanged( const QColor & newValue );
195 	/**
196 	 * Emitted for variants of bool-like type.
197 	 */
198 	void valueChanged( bool newValue );
199 
200 private:
201 	QVariant m_value; // the actual data
202 	QVariant m_defaultValue;
203 	QString m_unit;
204 	const QString m_id;
205 	double m_minAbsValue;
206 	double m_minValue;
207 	double m_maxValue;
208 	QString m_toolbarCaption; // Short description shown in e.g. properties dialog
209 	QString m_editorCaption; // Text displayed before the data entry widget in the toolbar
210 	bool m_bAdvanced; // If advanced, only display data in item editor
211 	bool m_bHidden; // If hidden, do not allow user to change data
212 	QString m_filter; // If type() == Type::FileName this is the filter used in file dialogs.
213 	bool m_bSetDefault; // If false, then the default will be set to the first thing this variant is set to
214 	Type::Value m_type;
215 	QStringMap m_allowed;
216 	int m_colorScheme;
217 };
218 
219 #endif
220