qtmobility/plugins/declarative/multimedia/qmetadatacontrolmetaobject.cpp
changeset 14 6fbed849b4f4
child 15 1f895d8a5b2b
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qmetadatacontrolmetaobject_p.h"
       
    43 #include <qmetadatareadercontrol.h>
       
    44 
       
    45 
       
    46 QT_BEGIN_NAMESPACE
       
    47 
       
    48 // copied from qmetaobject.cpp
       
    49 // do not touch without touching the moc as well
       
    50 enum PropertyFlags  {
       
    51     Invalid = 0x00000000,
       
    52     Readable = 0x00000001,
       
    53     Writable = 0x00000002,
       
    54     Resettable = 0x00000004,
       
    55     EnumOrFlag = 0x00000008,
       
    56     StdCppSet = 0x00000100,
       
    57 //    Override = 0x00000200,
       
    58     Designable = 0x00001000,
       
    59     ResolveDesignable = 0x00002000,
       
    60     Scriptable = 0x00004000,
       
    61     ResolveScriptable = 0x00008000,
       
    62     Stored = 0x00010000,
       
    63     ResolveStored = 0x00020000,
       
    64     Editable = 0x00040000,
       
    65     ResolveEditable = 0x00080000,
       
    66     User = 0x00100000,
       
    67     ResolveUser = 0x00200000,
       
    68     Notify = 0x00400000,
       
    69     Dynamic = 0x00800000
       
    70 };
       
    71 
       
    72 enum MethodFlags  {
       
    73     AccessPrivate = 0x00,
       
    74     AccessProtected = 0x01,
       
    75     AccessPublic = 0x02,
       
    76     AccessMask = 0x03, //mask
       
    77 
       
    78     MethodMethod = 0x00,
       
    79     MethodSignal = 0x04,
       
    80     MethodSlot = 0x08,
       
    81     MethodConstructor = 0x0c,
       
    82     MethodTypeMask = 0x0c,
       
    83 
       
    84     MethodCompatibility = 0x10,
       
    85     MethodCloned = 0x20,
       
    86     MethodScriptable = 0x40
       
    87 };
       
    88 
       
    89 struct QMetaObjectPrivate
       
    90 {
       
    91     int revision;
       
    92     int className;
       
    93     int classInfoCount, classInfoData;
       
    94     int methodCount, methodData;
       
    95     int propertyCount, propertyData;
       
    96     int enumeratorCount, enumeratorData;
       
    97     int constructorCount, constructorData;
       
    98     int flags;
       
    99 };
       
   100 
       
   101 static inline const QMetaObjectPrivate *priv(const uint* m_data)
       
   102 { return reinterpret_cast<const QMetaObjectPrivate*>(m_data); }
       
   103 // end of copied lines from qmetaobject.cpp
       
   104 
       
   105 namespace
       
   106 {
       
   107     struct MetaDataKey
       
   108     {
       
   109         QtMultimedia::MetaData key;
       
   110         const char *name;
       
   111     };
       
   112 
       
   113     const MetaDataKey qt_metaDataKeys[] =
       
   114     {
       
   115         { QtMultimedia::Title, "title" },
       
   116         { QtMultimedia::SubTitle, "subTitle" },
       
   117         { QtMultimedia::Author, "author" },
       
   118         { QtMultimedia::Comment, "comment" },
       
   119         { QtMultimedia::Description, "description" },
       
   120         { QtMultimedia::Category, "category" },
       
   121         { QtMultimedia::Genre, "genre" },
       
   122         { QtMultimedia::Year, "year" },
       
   123         { QtMultimedia::Date, "date" },
       
   124         { QtMultimedia::UserRating, "userRating" },
       
   125         { QtMultimedia::Keywords, "keywords" },
       
   126         { QtMultimedia::Language, "language" },
       
   127         { QtMultimedia::Publisher, "publisher" },
       
   128         { QtMultimedia::Copyright, "copyright" },
       
   129         { QtMultimedia::ParentalRating, "parentalRating" },
       
   130         { QtMultimedia::RatingOrganisation, "ratingOrganisation" },
       
   131 
       
   132         // Media
       
   133         { QtMultimedia::Size, "size" },
       
   134         { QtMultimedia::MediaType, "mediaType" },
       
   135 //        { QtMultimedia::Duration, "duration" },
       
   136 
       
   137         // Audio
       
   138         { QtMultimedia::AudioBitRate, "audioBitRate" },
       
   139         { QtMultimedia::AudioCodec, "audioCodec" },
       
   140         { QtMultimedia::AverageLevel, "averageLevel" },
       
   141         { QtMultimedia::ChannelCount, "channelCount" },
       
   142         { QtMultimedia::PeakValue, "peakValue" },
       
   143         { QtMultimedia::SampleRate, "sampleRate" },
       
   144 
       
   145         // Music
       
   146         { QtMultimedia::AlbumTitle, "albumTitle" },
       
   147         { QtMultimedia::AlbumArtist, "albumArtist" },
       
   148         { QtMultimedia::ContributingArtist, "contributingArtist" },
       
   149         { QtMultimedia::Composer, "composer" },
       
   150         { QtMultimedia::Conductor, "conductor" },
       
   151         { QtMultimedia::Lyrics, "lyrics" },
       
   152         { QtMultimedia::Mood, "mood" },
       
   153         { QtMultimedia::TrackNumber, "trackNumber" },
       
   154         { QtMultimedia::TrackCount, "trackCount" },
       
   155 
       
   156         { QtMultimedia::CoverArtUrlSmall, "coverArtUrlSmall" },
       
   157         { QtMultimedia::CoverArtUrlLarge, "coverArtUrlLarge" },
       
   158 
       
   159         // Image/Video
       
   160         { QtMultimedia::Resolution, "resolution" },
       
   161         { QtMultimedia::PixelAspectRatio, "pixelAspectRatio" },
       
   162 
       
   163         // Video
       
   164         { QtMultimedia::VideoFrameRate, "videoFrameRate" },
       
   165         { QtMultimedia::VideoBitRate, "videoBitRate" },
       
   166         { QtMultimedia::VideoCodec, "videoCodec" },
       
   167 
       
   168         { QtMultimedia::PosterUrl, "posterUrl" },
       
   169 
       
   170         // Movie
       
   171         { QtMultimedia::ChapterNumber, "chapterNumber" },
       
   172         { QtMultimedia::Director, "director" },
       
   173         { QtMultimedia::LeadPerformer, "leadPerformer" },
       
   174         { QtMultimedia::Writer, "writer" },
       
   175 
       
   176         // Photos
       
   177         { QtMultimedia::CameraManufacturer, "cameraManufacturer" },
       
   178         { QtMultimedia::CameraModel, "cameraModel" },
       
   179         { QtMultimedia::Event, "event" },
       
   180         { QtMultimedia::Subject, "subject" },
       
   181         { QtMultimedia::Orientation, "orientation" },
       
   182         { QtMultimedia::ExposureTime, "exposureTime" },
       
   183         { QtMultimedia::FNumber, "fNumber" },
       
   184         { QtMultimedia::ExposureProgram, "exposureProgram" },
       
   185         { QtMultimedia::ISOSpeedRatings, "isoSpeedRatings" },
       
   186         { QtMultimedia::ExposureBiasValue, "exposureBiasValue" },
       
   187         { QtMultimedia::DateTimeOriginal, "dateTimeOriginal" },
       
   188         { QtMultimedia::DateTimeDigitized, "dateTimeDigitized" },
       
   189         { QtMultimedia::SubjectDistance, "subjectDistance" },
       
   190         { QtMultimedia::MeteringMode, "meteringMode" },
       
   191         { QtMultimedia::LightSource, "lightSource" },
       
   192         { QtMultimedia::Flash, "flash" },
       
   193         { QtMultimedia::FocalLength, "focalLength" },
       
   194         { QtMultimedia::ExposureMode, "exposureMode" },
       
   195         { QtMultimedia::WhiteBalance, "whiteBalance" },
       
   196         { QtMultimedia::DigitalZoomRatio, "digitalZoomRatio" },
       
   197         { QtMultimedia::FocalLengthIn35mmFilm, "focalLengthIn35mmFilm" },
       
   198         { QtMultimedia::SceneCaptureType, "sceneCaptureType" },
       
   199         { QtMultimedia::GainControl, "gainControl" },
       
   200         { QtMultimedia::Contrast, "contrast" },
       
   201         { QtMultimedia::Saturation, "saturation" },
       
   202         { QtMultimedia::Sharpness, "sharpness" },
       
   203         { QtMultimedia::DeviceSettingDescription, "deviceSettingDescription" }
       
   204     };
       
   205 
       
   206     class QMetaDataControlObject : public QObject
       
   207     {
       
   208     public:
       
   209         inline QObjectData *data() { return d_ptr.data(); }
       
   210     };
       
   211 }
       
   212 
       
   213 QMetaDataControlMetaObject::QMetaDataControlMetaObject(QMetaDataReaderControl *control, QObject *object)
       
   214     : m_control(control)
       
   215     , m_object(object)
       
   216     , m_string(0)
       
   217     , m_data(0)
       
   218     , m_propertyOffset(0)
       
   219     , m_signalOffset(0)
       
   220 {
       
   221     const QMetaObject *superClass = m_object->metaObject();
       
   222 
       
   223     const int propertyCount =  sizeof(qt_metaDataKeys) / sizeof(MetaDataKey);
       
   224     const int dataSize = sizeof(uint)
       
   225             * (13                   // QMetaObjectPrivate members.
       
   226             + 5                     // 5 members per signal.
       
   227             + 4 * propertyCount     // 3 members per property + 1 notify signal per property.
       
   228             + 1);                   // Terminating value.
       
   229 
       
   230     m_data = reinterpret_cast<uint *>(qMalloc(dataSize));
       
   231 
       
   232     QMetaObjectPrivate *pMeta = reinterpret_cast<QMetaObjectPrivate *>(m_data);
       
   233 
       
   234     pMeta->revision = 3;
       
   235     pMeta->className = 0;
       
   236     pMeta->classInfoCount = 0;
       
   237     pMeta->classInfoData = 0;
       
   238     pMeta->methodCount = 1;
       
   239     pMeta->methodData = 13;
       
   240     pMeta->propertyCount = propertyCount;
       
   241     pMeta->propertyData = 18;
       
   242     pMeta->enumeratorCount = 0;
       
   243     pMeta->enumeratorData = 0;
       
   244     pMeta->constructorCount = 0;
       
   245     pMeta->constructorData = 0;
       
   246     pMeta->flags = 0x01;    // Dynamic meta object flag.
       
   247 
       
   248     const int classNameSize = qstrlen(superClass->className()) + 1;
       
   249 
       
   250     int stringIndex = classNameSize + 1;
       
   251 
       
   252     // __metaDataChanged() signal.
       
   253     static const char *changeSignal = "__metaDataChanged()";
       
   254     const int changeSignalSize = qstrlen(changeSignal) + 1;
       
   255 
       
   256     m_data[13] = stringIndex;                             // Signature.
       
   257     m_data[14] = classNameSize;                           // Parameters.
       
   258     m_data[15] = classNameSize;                           // Type.
       
   259     m_data[16] = classNameSize;                           // Tag.
       
   260     m_data[17] = MethodSignal | AccessProtected;          // Flags.
       
   261 
       
   262     stringIndex += changeSignalSize;
       
   263 
       
   264     const char *qvariantName = "QVariant";
       
   265     const int qvariantSize = qstrlen(qvariantName) + 1;
       
   266     const int qvariantIndex = stringIndex;
       
   267 
       
   268     stringIndex += qvariantSize;
       
   269 
       
   270     // Properties.
       
   271     for (int i = 0; i < propertyCount; ++i) {       
       
   272         m_data[18 + 3 * i] = stringIndex;                                       // Name.
       
   273         m_data[19 + 3 * i] = qvariantIndex;                                     // Type.
       
   274         m_data[20 + 3 * i] 
       
   275                 = Readable | Notify | Dynamic | (0xffffffff << 24);  // Flags.
       
   276         m_data[18 + propertyCount * 3 + i] = 0;                                 // Notify signal.
       
   277 
       
   278         stringIndex += qstrlen(qt_metaDataKeys[i].name) + 1;
       
   279     }
       
   280 
       
   281     // Terminating value.
       
   282     m_data[18 + propertyCount * 4] = 0;
       
   283 
       
   284     // Build string.
       
   285     m_string = reinterpret_cast<char *>(qMalloc(stringIndex + 1));
       
   286 
       
   287     // Class name.
       
   288     qMemCopy(m_string, superClass->className(), classNameSize);
       
   289 
       
   290     stringIndex = classNameSize;
       
   291 
       
   292     // Null m_string.
       
   293     m_string[stringIndex] = '\0';
       
   294     stringIndex += 1;
       
   295 
       
   296     // __metaDataChanged() signal.
       
   297     qMemCopy(m_string + stringIndex, changeSignal, changeSignalSize);
       
   298     stringIndex += changeSignalSize;
       
   299 
       
   300     qMemCopy(m_string + stringIndex, qvariantName, qvariantSize);
       
   301     stringIndex += qvariantSize;
       
   302 
       
   303     // Properties.
       
   304     for (int i = 0; i < propertyCount; ++i) {
       
   305         const int propertyNameSize = qstrlen(qt_metaDataKeys[i].name) + 1;
       
   306 
       
   307         qMemCopy(m_string + stringIndex, qt_metaDataKeys[i].name, propertyNameSize);
       
   308         stringIndex += propertyNameSize;
       
   309     }
       
   310 
       
   311     // Terminating character.
       
   312     m_string[stringIndex] = '\0';
       
   313 
       
   314     d.superdata = superClass;
       
   315     d.stringdata = m_string;
       
   316     d.data = m_data;
       
   317     d.extradata = 0;
       
   318 
       
   319     static_cast<QMetaDataControlObject *>(m_object)->data()->metaObject = this;
       
   320 
       
   321     m_propertyOffset = propertyOffset();
       
   322     m_signalOffset = methodOffset();
       
   323 }
       
   324 
       
   325 QMetaDataControlMetaObject::~QMetaDataControlMetaObject()
       
   326 {
       
   327     static_cast<QMetaDataControlObject *>(m_object)->data()->metaObject = 0;
       
   328 
       
   329     qFree(m_data);
       
   330     qFree(m_string);
       
   331 }
       
   332 
       
   333 int QMetaDataControlMetaObject::metaCall(QMetaObject::Call c, int id, void **a)
       
   334 {
       
   335     if (c == QMetaObject::ReadProperty && id >= m_propertyOffset) {
       
   336         int propId = id - m_propertyOffset;
       
   337 
       
   338         *reinterpret_cast<QVariant *>(a[0]) = m_control->metaData(qt_metaDataKeys[propId].key);
       
   339 
       
   340         return -1;
       
   341     } else {
       
   342         return m_object->qt_metacall(c, id, a);
       
   343     }
       
   344 }
       
   345 
       
   346 int QMetaDataControlMetaObject::createProperty(const char *, const char *)
       
   347 {
       
   348     return -1;
       
   349 }
       
   350 
       
   351 void QMetaDataControlMetaObject::metaDataChanged()
       
   352 {
       
   353     activate(m_object, m_signalOffset, 0);
       
   354 }
       
   355 
       
   356 QT_END_NAMESPACE