| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 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 examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include "domimage.h"
|
|---|
| 42 |
|
|---|
| 43 | #include <QVariant>
|
|---|
| 44 |
|
|---|
| 45 | #include <qscriptcontext.h>
|
|---|
| 46 |
|
|---|
| 47 | QScriptValue DomImage::s_self;
|
|---|
| 48 |
|
|---|
| 49 | DomImage::DomImage()
|
|---|
| 50 | {
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | int DomImage::width() const
|
|---|
| 55 | {
|
|---|
| 56 | return m_image.width();
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 | int DomImage::height() const
|
|---|
| 61 | {
|
|---|
| 62 | return m_image.height();
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | QString DomImage::src() const
|
|---|
| 67 | {
|
|---|
| 68 | return m_src;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void DomImage::setSrc(const QString &src)
|
|---|
| 72 | {
|
|---|
| 73 | m_src = src;
|
|---|
| 74 | m_image = QImage(m_src);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | QString DomImage::name() const
|
|---|
| 79 | {
|
|---|
| 80 | return m_src;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | static QScriptValue Image(QScriptContext *context, QScriptEngine *env)
|
|---|
| 84 | {
|
|---|
| 85 | QScriptValue val = context->thisObject();
|
|---|
| 86 | DomImage *image = new DomImage();
|
|---|
| 87 | QScriptValue klass = env->newVariant(qVariantFromValue(image));
|
|---|
| 88 | klass.setPrototype(DomImage::s_self);
|
|---|
| 89 | return klass;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | static QScriptValue width(QScriptContext *context, QScriptEngine *)
|
|---|
| 94 | {
|
|---|
| 95 | QScriptValue val = context->thisObject();
|
|---|
| 96 |
|
|---|
| 97 | DomImage *image = qvariant_cast<DomImage*> (val.toVariant());
|
|---|
| 98 | if (image)
|
|---|
| 99 | return image->width();
|
|---|
| 100 |
|
|---|
| 101 | return 0;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | static QScriptValue height(QScriptContext *context, QScriptEngine *)
|
|---|
| 106 | {
|
|---|
| 107 | QScriptValue val = context->thisObject();
|
|---|
| 108 |
|
|---|
| 109 | DomImage *image = qvariant_cast<DomImage*> (val.toVariant());
|
|---|
| 110 | if (image)
|
|---|
| 111 | return image->height();
|
|---|
| 112 |
|
|---|
| 113 | return 0;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | static QScriptValue setSrc(QScriptContext *context, QScriptEngine *env)
|
|---|
| 118 | {
|
|---|
| 119 | QScriptValue val = context->thisObject();
|
|---|
| 120 | QString src = context->argument(0).toString();
|
|---|
| 121 |
|
|---|
| 122 | DomImage *image = qvariant_cast<DomImage*> (val.toVariant());
|
|---|
| 123 | if (image)
|
|---|
| 124 | image->setSrc(src);
|
|---|
| 125 |
|
|---|
| 126 | return env->undefinedValue();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 | static QScriptValue name(QScriptContext *context, QScriptEngine *)
|
|---|
| 131 | {
|
|---|
| 132 | QScriptValue val = context->thisObject();
|
|---|
| 133 |
|
|---|
| 134 | DomImage *image = qvariant_cast<DomImage*> (val.toVariant());
|
|---|
| 135 | if (image)
|
|---|
| 136 | return image->name();
|
|---|
| 137 |
|
|---|
| 138 | return QString();
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | void DomImage::setup(QScriptEngine *e)
|
|---|
| 143 | {
|
|---|
| 144 | qRegisterMetaType<DomImage>();
|
|---|
| 145 |
|
|---|
| 146 | e->globalObject().setProperty("Image",
|
|---|
| 147 | e->newFunction(::Image, 0));
|
|---|
| 148 |
|
|---|
| 149 | s_self = e->newObject();
|
|---|
| 150 | s_self.setProperty("setSrc", e->newFunction(&::setSrc, 1));
|
|---|
| 151 | s_self.setProperty("width", e->newFunction(&::width));
|
|---|
| 152 | s_self.setProperty("height", e->newFunction(&::height));
|
|---|
| 153 | s_self.setProperty("name", e->newFunction(&::name));
|
|---|
| 154 |
|
|---|
| 155 | e->setDefaultPrototype(qMetaTypeId<DomImage>(), s_self);
|
|---|
| 156 | }
|
|---|