Example #1
0
int QTimerProto::timerId() const
{
  QTimer *item = qscriptvalue_cast<QTimer*>(thisObject());
  if (item)
    return item->timerId();
  return -1;
}
Example #2
0
void CoordinationMessageModule::onAddNewMessage(const Data::Message &msg, bool needAck, int resendInterval, int ackCode)
{
    emit sigMessageSendCMM(msg);
    if ((needAck) && (ackCode != 0) && (resendInterval != 0)) {
        QTimer *msgTimer = new QTimer();
        msgTimer->setInterval(resendInterval);
        msgTimer->start();
        int timerId = msgTimer->timerId();
        messages.insert(timerId, ackCode, msg);
        connect(msgTimer, SIGNAL(timeout()), messageTimeoutMapper, SLOT(map()));
        messageTimeoutMapper->setMapping(msgTimer, timerId);
        messageQTimerHash.insert(timerId, msgTimer);
    }else{
        //delete &msg;
    }
}
bool ScriptEngine::unsetTimer(int timerId)
{
    QHashIterator <QTimer*, QScriptValue> it (timerEvents);
    while (it.hasNext()) {
        it.next();
        QTimer *timer = it.key();

        if (timer->timerId() == timerId) {
            timer->stop();
            timer->blockSignals(true);

            timerEvents.remove(timer);
            timer->deleteLater();
            return true; // Timer found.
        }
    }
    warn ("unsetTimer(timerId)", "no timer with that id");
    return false; // No timer found.
}
int ScriptEngine::setTimer(const QScriptValue &code, int delay, bool repeats)
{
    if (delay <= 0) {
        return -1;
    }
    if ((!code.isFunction()) && (!code.isString())) {
        warn("setTimer(code, delay, repeats)", "code must be string or function.");
        return -1;
    }

    QTimer *t = new QTimer();

    timerEvents[t] = code;
    t->setSingleShot(!repeats);
    t->start(delay);
    connect(t, SIGNAL(timeout()), SLOT(timer()), Qt::DirectConnection);

    return t->timerId();
}