interruptibletime.cpp 859 Bytes
#include "interruptibletime.h"

InterruptibleTime::InterruptibleTime(QObject *parent) : QObject(parent)
{
    elapsed_ = -1;
}

int InterruptibleTime::elapsed()
{
    if (isNull())
        return -1;

    if (time.isValid())
        return elapsed_ + time.elapsed();

    return elapsed_;
}

bool InterruptibleTime::isValid()
{
    return elapsed_ != -1;
}

bool InterruptibleTime::isNull()
{
    return elapsed_ == -1;
}

void InterruptibleTime::start()
{
    elapsed_ = 0;
    time.start();
}

void InterruptibleTime::pause()
{
    if (isNull())
        return;

    elapsed_ += time.elapsed();
    time = QTime();
}

void InterruptibleTime::resume()
{
    if (isNull())
        return;

    time.start();
}

int InterruptibleTime::restart()
{
    if (isNull())
        return -1;

    int t = elapsed();
    elapsed_ = 0;
    time.start();
    return t;
}