//! [0]
      #include <QtScript>
//! [0]


//! [1]
      QT += script
//! [1]


//! [2]
  function myInterestingScriptFunction() { ... }
  ...
  myQObject.somethingChanged.connect(myInterestingScriptFunction);
//! [2]


//! [3]
  myQObject.somethingChanged.connect(myOtherQObject.doSomething);
//! [3]


//! [4]
  myQObject.somethingChanged.disconnect(myInterestingFunction);
  myQObject.somethingChanged.disconnect(myOtherQObject.doSomething);
//! [4]


//! [5]
  var obj = { x: 123 };
  var fun = function() { print(this.x); };
  myQObject.somethingChanged.connect(obj, fun);
//! [5]


//! [6]
  myQObject.somethingChanged.disconnect(obj, fun);
//! [6]


//! [7]
  var obj = { x: 123, fun: function() { print(this.x); } };
  myQObject.somethingChanged.connect(obj, "fun");
//! [7]


//! [8]
  myQObject.somethingChanged.disconnect(obj, "fun");
//! [8]


//! [9]
  try {
      myQObject.somethingChanged.connect(myQObject, "slotThatDoesntExist");
  } catch (e) {
      print(e);
  }
//! [9]


//! [10]
  myQObject.somethingChanged("hello");
//! [10]


//! [11]
  myQObject.myOverloadedSlot(10);   // will call the int overload
  myQObject.myOverloadedSlot("10"); // will call the QString overload
//! [11]


//! [12]
  myQObject['myOverloadedSlot(int)']("10");   // call int overload; the argument is converted to an int
  myQObject['myOverloadedSlot(QString)'](10); // call QString overload; the argument is converted to a string
//! [12]


//! [13]
  Q_PROPERTY(bool enabled READ enabled WRITE setEnabled)
//! [13]


//! [14]
  myQObject.enabled = true;

  ...

  myQObject.enabled = !myQObject.enabled;
//! [14]


//! [15]
  myDialog.okButton
//! [15]


//! [16]
  myDialog.okButton.objectName = "cancelButton";
  // from now on, myDialog.cancelButton references the button
//! [16]


//! [17]
  var okButton = myDialog.findChild("okButton");
  if (okButton != null) {
     // do something with the OK button
  }

  var buttons = myDialog.findChildren(RegExp("button[0-9]+"));
  for (var i = 0; i < buttons.length; ++i) {
     // do something with buttons[i]
  }
//! [17]


//! [18]
  QScriptValue myQObjectConstructor(QScriptContext *context, QScriptEngine *engine)
  {
    // let the engine manage the new object's lifetime.
    return engine->newQObject(new MyQObject(), QScriptEngine::ScriptOwnership);
  }
//! [18]


//! [19]
    class MyObject : public QObject
    {
        Q_OBJECT

    public:
        MyObject( ... );

        void aNonScriptableFunction();

    public slots: // these functions (slots) will be available in QtScript
        void calculate( ... );
        void setEnabled( bool enabled );
        bool isEnabled() const;

    private:
       ....

    };
//! [19]


//! [20]
  class MyObject : public QObject
  {
      Q_OBJECT

      public:
      Q_INVOKABLE void thisMethodIsInvokableInQtScript();
      void thisMethodIsNotInvokableInQtScript();

      ...
  };
//! [20]


//! [21]
    var obj = new MyObject;
    obj.setEnabled( true );
    print( "obj is enabled: " + obj.isEnabled() );
//! [21]


//! [22]
    var obj = new MyObject;
    obj.enabled = true;
    print( "obj is enabled: " + obj.enabled );
//! [22]


//! [23]
    class MyObject : public QObject
    {
        Q_OBJECT
        // define the enabled property
        Q_PROPERTY( bool enabled WRITE setEnabled READ isEnabled )

    public:
        MyObject( ... );

        void aNonScriptableFunction();

    public slots: // these functions (slots) will be available in QtScript
        void calculate( ... );
        void setEnabled( bool enabled );
        bool isEnabled() const;

    private:
       ....

    };
//! [23]


//! [24]
  Q_PROPERTY(int nonScriptableProperty READ foo WRITE bar SCRIPTABLE false)
//! [24]


//! [25]
    class MyObject : public QObject
    {
        Q_OBJECT
        // define the enabled property
        Q_PROPERTY( bool enabled WRITE setEnabled READ isEnabled )

    public:
        MyObject( ... );

        void aNonScriptableFunction();

    public slots: // these functions (slots) will be available in QtScript
        void calculate( ... );
        void setEnabled( bool enabled );
        bool isEnabled() const;

    signals: // the signals
        void enabledChanged( bool newState );

    private:
       ....

    };
//! [25]


//! [26]
    function enabledChangedHandler( b )
    {
        print( "state changed to: " + b );
    }

    function init()
    {
        var obj = new MyObject();
        // connect a script function to the signal
        obj["enabledChanged(bool)"].connect(enabledChangedHandler);
        obj.enabled = true;
        print( "obj is enabled: " + obj.enabled );
    }
//! [26]


//! [27]
  var o = new Object();
  o.foo = 123;
  print(o.hasOwnProperty('foo')); // true
  print(o.hasOwnProperty('bar')); // false
  print(o); // calls o.toString(), which returns "[object Object]"
//! [27]


//! [28]
  function Person(name)
  {
    this.name = name;
  }
//! [28]


//! [29]
  Person.prototype.toString = function() { return "Person(name: " + this.name + ")"; }
//! [29]


//! [30]
  var p1 = new Person("John Doe");
  var p2 = new Person("G.I. Jane");
  print(p1); // "Person(name: John Doe)"
  print(p2); // "Person(name: G.I. Jane)"
//! [30]


//! [31]
  print(p1.hasOwnProperty('name')); // 'name' is an instance variable, so this returns true
  print(p1.hasOwnProperty('toString')); // returns false; inherited from prototype
  print(p1 instanceof Person); // true
  print(p1 instanceof Object); // true
//! [31]


//! [32]
  function Employee(name, salary)
  {
    Person.call(this, name); // call base constructor

    this.salary = salary;
  }

  // set the prototype to be an instance of the base class
  Employee.prototype = new Person();

  // initialize prototype
  Employee.prototype.toString = function() { ... }
//! [32]


//! [33]
  var e = new Employee("Johnny Bravo", 5000000);
  print(e instanceof Employee); // true
  print(e instanceof Person);   // true
  print(e instanceof Object);   // true
  print(e instanceof Array);    // false
//! [33]


//! [34]
  QScriptValue Person_ctor(QScriptContext *context, QScriptEngine *engine)
  {
    QString name = context->argument(0).toString();
    context->thisObject().setProperty("name", QScriptValue(engine, name));
    return engine->undefinedValue();
  }
//! [34]


//! [35]
  QScriptValue Person_prototype_toString(QScriptContext *context, QScriptEngine *engine)
  {
    QString name = context->thisObject().property("name").toString();
    QString result = QString::fromLatin1("Person(name: %0)").arg(name);
    return QScriptValue(engine, result);
  }
//! [35]


//! [36]
  QScriptEngine engine;
  QScriptValue ctor = engine.newFunction(Person_ctor);
  ctor.property("prototype").setProperty("toString", engine.newFunction(Person_prototype_toString));
  QScriptValue global = engine.globalObject();
  global.setProperty("Person", ctor);
//! [36]


//! [37]
  QScriptValue Employee_ctor(QScriptContext *context, QScriptEngine *engine)
  {
    QScriptValue super = context->callee().property("prototype").property("constructor");
    super.call(context->thisObject(), QScriptValueList() << context->argument(0));
    context->thisObject().setProperty("salary", context->argument(1));
    return engine->undefinedValue();
  }
//! [37]


//! [38]
  QScriptValue empCtor = engine.newFunction(Employee_ctor);
  empCtor.setProperty("prototype", global.property("Person").construct());
  global.setProperty("Employee", empCtor);
//! [38]


//! [39]
  Q_DECLARE_METATYPE(QPointF)
  Q_DECLARE_METATYPE(QPointF*)

  QScriptValue QPointF_prototype_x(QScriptContext *context, QScriptEngine *engine)
  {
    // Since the point is not to be modified, it's OK to cast to a value here
      QPointF point = qscriptvalue_cast<QPointF>(context->thisObject());
      return QScriptValue(engine, point.x());
  }

  QScriptValue QPointF_prototype_setX(QScriptContext *context, QScriptEngine *engine)
  {
      // Cast to a pointer to be able to modify the underlying C++ value
      QPointF *point = qscriptvalue_cast<QPointF*>(context->thisObject());
      if (!point)
          return context->throwError(QScriptContext::TypeError, "QPointF.prototype.setX: this object is not a QPointF");
      point->setX(context->argument(0).toNumber());
      return engine->undefinedValue();
  }
//! [39]


//! [40]
    var o = new Object();
    (o.__proto__ === Object.prototype); // this evaluates to true
//! [40]


//! [41]
    var o = new Object();
    o.__defineGetter__("x", function() { return 123; });
    var y = o.x; // 123
//! [41]


//! [42]
    var o = new Object();
    o.__defineSetter__("x", function(v) { print("and the value is:", v); });
    o.x = 123; // will print "and the value is: 123"
//! [42]


//! [43]
    class MyObject : public QObject
    {
        Q_OBJECT
        ...
    };

    Q_DECLARE_METATYPE(MyObject*)

    QScriptValue myObjectToScriptValue(QScriptEngine *engine, MyObject* const &in)
    { return engine->newQObject(in); }

    void myObjectFromScriptValue(const QScriptValue &object, MyObject* &out)
    { out = qobject_cast<MyObject*>(object.toQObject()); }

    ...

    qScriptRegisterMetaType(&engine, myObjectToScriptValue, myObjectFromScriptValue);
//! [43]

//! [44]
    QScriptValue QPoint_ctor(QScriptContext *context, QScriptEngine *engine)
    {
        int x = context->argument(0).toInt32();
        int y = context->argument(1).toInt32();
        return engine->toScriptValue(QPoint(x, y));
    }

    ...

    engine.globalObject().setProperty("QPoint", engine.newFunction(QPoint_ctor));
//! [44]

//! [45]
    QScriptValue myPrintFunction(QScriptContext *context, QScriptEngine *engine)
    {
        QString result;
        for (int i = 0; i < context->argumentCount(); ++i) {
            if (i > 0)
                result.append(" ");
            result.append(context->argument(i).toString());
        }

        QScriptValue calleeData = context->callee().data();
        QPlainTextEdit *edit = qobject_cast<QPlainTextEdit*>(calleeData.toQObject());
        edit->appendPlainText(result);

        return engine->undefinedValue();
    }
//! [45]

//! [46]
int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QScriptEngine eng;
    QPlainTextEdit edit;

    QScriptValue fun = eng.newFunction(myPrintFunction);
    fun.setData(eng.newQObject(&edit));
    eng.globalObject().setProperty("print", fun);

    eng.evaluate("print('hello', 'world')");

    edit.show();
    return app.exec();
}
//! [46]
