Logo Search packages:      
Sourcecode: qtjambi version File versions  Download package

QVariant.java

/****************************************************************************
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of Qt Jambi.
**
** ** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.

**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

package com.trolltech.qt;

import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
import java.util.List;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

/**
The QVariant class acts like a union for the most common Qt data types.
<p>
Because C++ forbids unions from including types that have
non-default constructors or destructors, most interesting Qt
classes cannot be used in unions. Without QVariant, this would be
a problem for QObject::property() and for database work, etc.
<p>
A QVariant object holds a single value of a single type() at a
time. (Some type()s are multi-valued, for example a string list.)
You can find out what type, T, the variant holds, convert it to a
different type using convert(), get its value using one of the
toT() functions (e.g., toSize()) and check whether the type can
be converted to a particular type using canConvert().
<p>
The methods named toT() (e.g., toInt(), toString()) are const. If
you ask for the stored type, they return a copy of the stored
object. If you ask for a type that can be generated from the
stored type, toT() copies and converts and leaves the object
itself unchanged. If you ask for a type that cannot be generated
from the stored type, the result depends on the type; see the
function documentation for details.
<p>
Here is some example code to demonstrate the use of QVariant:
<p>
<code>
<pre>
    QDataStream out(...);
    QVariant v(123);                // The variant now contains an int
    int x = v.toInt();              // x = 123
    out &lt;&lt; v;                       // Writes a type tag and an int to out
    v = QVariant("hello");          // The variant now contains a QByteArray
    v = QVariant(tr("hello"));      // The variant now contains a QString
    int y = v.toInt();              // y = 0 since v cannot be converted to an int
    QString s = v.toString();       // s = tr("hello")  (see QObject::tr())
    out &lt;&lt;v;                  // Writes a type tag and a QString to out
    ...
    QDataStream in(...);            // (opening the previously written stream)
    in &gt;&gt; v;                  // Reads an Int variant
    int z = v.toInt();              // z = 123
    qDebug("Type is %s",            // prints "Type is int"
            v.typeName());
    v = v.toInt() + 100;            // The variant now hold the value 223
    v = QVariant(QStringList());
</pre>
</code>
<p>
You can even store QList<QVariant> and QMap<QString, QVariant>
values in a variant, so you can easily construct arbitrarily
complex data structures of arbitrary types. This is very powerful
and versatile, but may prove less memory and speed efficient than
storing specific types in standard data structures.
<p>
QVariant also supports the notion of null values, where you have
a defined type with no value set.
<p>
<code>
<pre>
    QVariant x, y(QString()), z(QString(""));
    x.convert(QVariant::Int);
    // x.isNull() == true
    // y.isNull() == true, z.isNull() == false
    // y.isEmpty() == true, z.isEmpty() == true
</pre>
</code
<p>
QVariant can be extended to support other types than those
mentioned in the \l Type enum. See the \l QMetaType documentation
for details.
<p>
<h2>A Note on GUI Types</h2>
<p>
Because QVariant is part of the QtCore library, it cannot provide
conversion functions to data types defined in QtGui, such as
QColor, QImage, and QPixmap. In other words, there is no \c
toColor() function. Instead, you can use the QVariant::value() or
the qVariantValue() template function. For example:
<p>
<code>
<pre>
    QVariant variant;
    ...
    QColor color = variant.value<QColor>();
</pre>
</code>
<p>
The inverse conversion (e.g., from QColor to QVariant) is
automatic for all data types supported by QVariant, including
GUI-related types:
<p>
<code>
<pre>
    QColor color = palette().background().color();
    QVariant variant = color;
</pre>
</code>
<p>
Because of Java's introspection, you should only use the QVariant
class when dealing with Qt Jambi classes that requires them.
*/
00135 public class QVariant extends QtJambiObject {

    // Java types
    /** An invalid Java data type*/
00139     public static final int Invalid = 0;
    /** Java Double */
00141     public static final int Double = 6;
    /** Java String */
00143     public static final int String = 10;
    /** Java Boolean */
00145     public static final int Boolean = 1;
    /** Java ByteArray */
00147     public static final int ByteArray = 12;
    /** Java BitArray */
00149     public static final int BitArray = 13;
    /** Java Char */
00151     public static final int Char = 7;
    /** Java Date */
00153     public static final int Date = 14;
    /** Java DateTime */
00155     public static final int DateTime = 16;
    /** Java Int */
00157     public static final int Int = 2;
    /** Java Line */
00159     public static final int Line = 23;
    /** Java LineF */
00161     public static final int LineF = 24;
    /** Java Locale */
00163     public static final int Locale = 18;
    /** Java Long */
00165     public static final int Long = 4;
    /** Java Point */
00167     public static final int Point = 25;
    /** Java PointF */
00169     public static final int PointF = 26;
    /** Java Rect */
00171     public static final int Rect = 19;
    /** Java RectF */
00173     public static final int RectF = 20;
    /** Java RegExp */
00175     public static final int RegExp = 27;
    /** Java Size */
00177     public static final int Size = 21;
    /** Java SizeF */
00179     public static final int SizeF = 22;
    /** Java StringList */
00181     public static final int StringList = 11;
    /** Java Time */
00183     public static final int Time = 15; 

    // Gui types
    /** Java Font */
00187     public static final int Font = 64;
    /** Java Pixmap */
00189     public static final int Pixmap = 65;
    /** Java Brush */
00191     public static final int Brush = 66;
    /** Java Color */
00193     public static final int Color = 67;
    /** Java Palette */
00195     public static final int Palette = 68;
    /** Java Icon */
00197     public static final int Icon = 69;
    /** Java Image */
00199     public static final int Image = 70;
    /** Java Polygon */
00201     public static final int Polygon = 71;
    /** Java Region */
00203     public static final int Region = 72;
    /** Java Bitmap */
00205     public static final int Bitmap = 73;
    /** Java Cursor */
00207     public static final int Cursor = 74;
    /** Java SizePolicy */
00209     public static final int SizePolicy = 75;
    /** Java KeySequence */
00211     public static final int KeySequence = 76;
    /** Java Pen */
00213     public static final int Pen = 77;
    /** Java TextLength */
00215     public static final int TextLength = 78;
    /** Java TextFormat */
00217     public static final int TextFormat = 79;
    /** Java Usertype */
00219     public static final int UserType = 127;

    
    /** This enum describes the data types of QVariant. */
00223     enum Types {
        Invalid(0),
        Double(6),
        String(10),
        Boolean(1),
        ByteArray(12),
        BitArray(13),
        Char(7),
        Date(14),
        DateTime(16),
        Int(2),
        Line(23),
        LineF(24),
        Locale(18),
        Long(4),
        Point(25),
        PointF(26),
        Rect(19),
        RectF(20),
        RegExp(27),
        Size(21),
        SizeF(22),
        StringList(11),
        Time(15); 
        
        private int val;
        private Types(int val) { this.val = val; }
        public int value() { return val; }
    };
        
    private static native Object __qt_convert(int type, Object obj);
    private static native boolean __qt_canConvert(Object obj, int type);
    
    private static void setOk(Boolean ok[], boolean isOk) 
    {
        if (ok != null && ok.length > 0)
            ok[0] = isOk;
    }
     
    public static boolean canConvertToDouble(Object obj)
    {
        if (obj instanceof Number)
            return true;
        else if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Double.value());
        else
            return false;
    }
    public static double toDouble(Object obj) { return toDouble(obj, (Boolean[]) null); }    
    public static double toDouble(Object obj, Boolean ok[])
    {
        if (obj instanceof Number) {
            setOk(ok, true);
            return ((Number) obj).doubleValue();
        } else if (obj instanceof String || obj instanceof QtJambiObject) {
            return __qt_toDouble(obj, ok);        
        } else {
            setOk(ok, false);
            return 0.0;
        }
    }
    private static native double __qt_toDouble(Object variant, Boolean ok[]);
   
    public static boolean canConvertToString(Object obj)
    {
        return obj != null;
    }

    /**
     * {@inheritDoc}
     */
00294     public static String toString(Object obj)
    {
        if (obj instanceof QtJambiObject) {
            return __qt_toString(obj);
        }            
        return obj != null ? obj.toString() : "";        
    }
    private static native String __qt_toString(Object variant);
       
    public static boolean canConvertToBoolean(Object obj)
    {
        if (obj instanceof Boolean || obj instanceof Number)
            return true;
        else if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Boolean.value());
        else
            return false;
    }
    public static boolean toBoolean(Object obj)
    {
        if (obj instanceof Boolean) {
            return (Boolean) obj;
        } else if (obj instanceof Number) {
            return ((Number) obj).intValue() != 0;        
        } else if (obj instanceof QtJambiObject || obj instanceof String) {            
            return __qt_toBoolean(obj);
        } else {
            return false;        
        }
    }
    private static native boolean __qt_toBoolean(Object obj);    
    
    public static boolean canConvertToByteArray(Object obj) 
    {
        if (obj instanceof QtJambiObject)
            return __qt_canConvert(obj, Types.ByteArray.value());
        else
            return false;
    }
    public static QByteArray toByteArray(Object obj)
    {
        if (obj instanceof QtJambiObject) {
            QByteArray returned = (QByteArray) __qt_convert(Types.ByteArray.value(), obj);
            if (returned != null) {
                return returned;
            }                
        }
        
        return new QByteArray();
    }
    
    public static boolean canConvertToBitArray(Object obj)
    {
        if (obj instanceof QtJambiObject)
            return __qt_canConvert(obj, Types.BitArray.value());
        else
            return false;
    }
    public static QBitArray toBitArray(Object obj)
    {
        if (obj instanceof QtJambiObject) {
            QBitArray returned = (QBitArray) __qt_convert(Types.BitArray.value(), obj);
            if (returned != null) {
                return returned;
            }                       
        }
        
        return new QBitArray();        
    }
        
    public static boolean canConvertToChar(Object obj) 
    {
        if (obj instanceof Character)
            return true;
        else if (obj instanceof QtJambiObject)
            return __qt_canConvert(obj, Types.Char.value());
        else
            return false;
    }
    public static char toChar(Object obj)
    {
        if (obj instanceof Character)
            return (Character) obj;
        else if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_toChar(obj);
        else        
            return 0;        
    }
    private static native char __qt_toChar(Object obj);
    
    public static boolean canConvertToDate(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Date.value());
        else
            return false;
    }
    public static QDate toDate(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QDate returned = (QDate) __qt_convert(Types.Date.value(), obj);
            if (returned != null) {
                return returned;
            }            
        } 
        return new QDate();
    }
    
    public static boolean canConvertToDateTime(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.DateTime.value());
        else
            return false;
    }    
    public static QDateTime toDateTime(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QDateTime returned = (QDateTime) __qt_convert(Types.DateTime.value(), obj);
            if (returned != null) {
                return returned;
            }

        } 
        return new QDateTime();
    }
    
    public static boolean canConvertToInt(Object obj)
    {
        if (obj instanceof Number)
            return true;
        else if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Int.value());
        else
            return false;
            
    }
    public static int toInt(Object obj) { return toInt(obj, null); }
    public static int toInt(Object obj, Boolean ok[])
    {
        if (obj instanceof Number) {
            setOk(ok, true);
            return ((Number) obj).intValue();
        } else if (obj instanceof QtJambiObject || obj instanceof String) {
            return __qt_toInt(obj, ok);
        } else {
            setOk(ok, false);
            return 0;
        }
    }
    private static native int __qt_toInt(Object obj, Boolean ok[]);
     
    public static boolean canConvertToLine(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Line.value());
        else
            return false;
    }
    public static QLine toLine(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QLine returned = (QLine) __qt_convert(Types.Line.value(), obj);
            if (returned != null) {
                return returned;
            }

        } 
        return new QLine();
    }
    public static boolean canConvertToLineF(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.LineF.value());
        else
            return false;
    }    
    public static QLineF toLineF(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QLineF returned = (QLineF) __qt_convert(Types.LineF.value(), obj);
            if (returned != null) {
                return returned;
            }

        } 
        return new QLineF();
    }

    public static boolean canConvertToLocale(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Locale.value());
        else
            return false;
    }
    public static QLocale toLocale(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QLocale returned = (QLocale) __qt_convert(Types.Locale.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QLocale();
    }
    
    public static boolean canConvertToPoint(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Point.value());
        else
            return false;
    }    
    public static QPoint toPoint(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QPoint returned = (QPoint) __qt_convert(Types.Point.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QPoint();
    }

    public static boolean canConvertToPointF(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.PointF.value());
        else
            return false;
    }    
    public static QPointF toPointF(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QPointF returned = (QPointF) __qt_convert(Types.PointF.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QPointF();
    }
    
    public static boolean canConvertToRect(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Rect.value());
        else
            return false;
    }    
    public static QRect toRect(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QRect returned = (QRect) __qt_convert(Types.Rect.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QRect();
    }

    public static boolean canConvertToRectF(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.RectF.value());
        else
            return false;
    }    
    public static QRectF toRectF(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QRectF returned = (QRectF) __qt_convert(Types.RectF.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QRectF();
    }

    public static boolean canConvertToRegExp(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.RegExp.value());
        else
            return false;
    }    
    public static QRegExp toRegExp(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QRegExp returned = (QRegExp) __qt_convert(Types.RegExp.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QRegExp();
    }

    public static boolean canConvertToSize(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Size.value());
        else
            return false;
    }    
    public static QSize toSize(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QSize returned = (QSize) __qt_convert(Types.Size.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QSize();
    }
    
    public static boolean canConvertToSizeF(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.SizeF.value());
        else
            return false;
    }    
    public static QSizeF toSizeF(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QSizeF returned = (QSizeF) __qt_convert(Types.SizeF.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QSizeF();
    }

    public static boolean canConvertToTime(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Time.value());
        else
            return false;
    }    
    public static QTime toTime(Object obj)
    {
        if (obj instanceof QtJambiObject || obj instanceof String) {
            QTime returned = (QTime) __qt_convert(Types.Time.value(), obj);
            if (returned != null) {
                return returned;
            }
        } 
        return new QTime();         
    }
    
    public static boolean canConvertToLong(Object obj)
    {
        if (obj instanceof Number)
            return true;
        else if (obj instanceof QtJambiObject || obj instanceof String)
            return __qt_canConvert(obj, Types.Long.value());
        else
            return false;
    }
    public static long toLong(Object obj) { return toLong(obj, null); }
    public static long toLong(Object obj, Boolean ok[])
    {
        if (obj instanceof Number) {
            setOk(ok, true);
            return ((Number) obj).longValue();
        } else if (obj instanceof QtJambiObject || obj instanceof String) {
            return __qt_toLong(obj, ok);
        } else {
            setOk(ok, false);
            return 0;
        }
    }
    private static native long __qt_toLong(Object obj, Boolean ok[]); 
    
    public static boolean canConvertToList(Object obj)
    {
        return (obj instanceof Collection);
    }
    public static List<Object> toList(Object obj)
    {
        List<Object> list = new ArrayList<Object>();
        if (obj instanceof Collection)
            list.addAll((List<?>) obj);
        return list;
    }
    
    public static boolean canConvertToMap(Object obj)
    {
        return (obj instanceof Map);
    }
    public static Map<String, Object> toMap(Object obj)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        if (obj instanceof Map) {
            Map<?, ?> otherMap = (Map<?, ?>) obj;
            for (Map.Entry e : otherMap.entrySet())
                map.put(e.getKey().toString(), e.getValue());                                
        }         
        return map; 
    }
       
}

Generated by  Doxygen 1.6.0   Back to index