Example #1
0
/*!
  Adds the Qt Script function \a qtscriptFunction (fully qualified)
  as a transient signal handler for the C++ signal \a signal of the
  object \a sender.

  Example:
  \code
  interpreter->addTransientSignalHandler( myButton, SIGNAL( clicked() ), "classA.obj.calculate" );
  \endcode

  \sa removeTransientSignalHandler()
*/
void QSInterpreter::addTransientSignalHandler(QObject *sender,
                                              const char *signal,
                                              const char *qtscriptFunction)
{
  QuickInterpreter *ip = interpreter();
  QString func = QString::fromLatin1(qtscriptFunction);
  func.left(func.find('('));
  QSObject senderObj = ip->wrap(sender);

  QSObject obj = ip->object(func);
  if (!obj.isFunction()) {
    qDebug("QSInterpreter::addTransientSignalHandler(): '%s' not a function",
           qtscriptFunction);
    return;
  }

  QSObject base = QSFuncRefClass::refBase(obj);
  QSMember member = QSFuncRefClass::refMember(obj);
  QSWrapperShared *sh = ip->wrapperClass()->shared(&senderObj);
  if (!sh->setEventHandler(ip, QString::fromLatin1(signal + 1), 0, member.name(), base)) {
#if defined( QT_CHECK_STATE )
    qWarning("QSInterpreter::addTransientSignalHandler(), "
             "failed to add signal handler: '%s' to '%s'", signal + 1, qtscriptFunction);
#endif
  }
}
QSObject QuickInterpreter::wrap( QObject *o )
{
    if( !o ) {
	QSList list;
	return env()->nullClass()->construct( list );
    }

#if defined (QT_THREAD_SUPPORT) && QT_VERSION >= 0x030300
    if (qt_get_application_thread_id() != QThread::currentThread()
	&& o->inherits("QWidget")) {
	qWarning("QuickInterpreter::wrap(), GUI object (%s [%s]) cannot be used in non GUI thread",
		 o->name(), o->className());
	return QSObject();
    }
#endif

    QSUserData *udata = (QSUserData*) o->userData( userDataId() );
    // set user data in QObject if it's not there, yet
    if( !udata ) {
	udata = new QSUserData( 0 );
	o->setUserData( userDataId(), udata );
    }
    QSWrapperShared *shared = udata->data();
    const QSWrapperClass *cl;
    if ( shared ) {
	// wrapper is already there, reuse it
	cl = shared->wrapperClass();
    } else {
	// create & remember wrapper
	cl = new QSWrapperClass( wrapperClass() );
	shared = cl->createShared( o );
	shared->setObjectType( QSWrapperShared::GlobalObject );
	env()->registerShared( shared );
	wrapperShared->append( shared );
    }
    shared->setUserData( udata );
    udata->setData( shared );
    shared->ref(); // additional ref by either QObject or QSObject
    QSObject obj( cl );
    obj.setVal( shared ); // no ownership needs to be transferred anymore
    return obj;
}
Example #3
0
QSObject QuickInterpreter::wrap(QObject *o)
{
    if(!o) {
	QSList list;
	return env()->nullClass()->construct(list);
    }

    if (qsa_is_non_gui_thread() && o->isWidgetType()) {
	qWarning("QuickInterpreter::wrap(), GUI object (%s [%s]) cannot be used in non GUI thread",
		 o->objectName().toLatin1().constData(), o->metaObject()->className());
	return QSObject();
    }

    QSUserData *udata = (QSUserData*) o->userData(userDataId());
    // set user data in QObject if it's not there, yet
    if(!udata) {
	udata = new QSUserData(0);
	o->setUserData(userDataId(), udata);
    }
    QSWrapperShared *shared = udata->data();
    const QSWrapperClass *cl;
    if (shared) {
	// wrapper is already there, reuse it
	cl = shared->wrapperClass();
    } else {
	// create & remember wrapper
	cl = new QSWrapperClass(wrapperClass());
	shared = cl->createShared(o);
	shared->setObjectType(QSWrapperShared::GlobalObject);
	env()->registerShared(shared);
	wrapperShared->append(shared);
    }
    shared->setUserData(udata);
    udata->setData(shared);
    shared->ref(); // additional ref by either QObject or QSObject
    QSObject obj(cl);
    obj.setVal(shared); // no ownership needs to be transferred anymore
    return obj;
}