#include "qtimerwidget.h"
#include <QPainter>
#include <QTimerEvent>


QTimerWidget::QTimerWidget(QWidget * parent): QWidget(parent) {
	enabled_ = true;
	bdebug_ = once_ = false;
	id_emit = id_set = timer = -1;
	int_ = 1000;
}


QTimerWidget::~QTimerWidget() {
}


void QTimerWidget::paintEvent(QPaintEvent * e) {
	QWidget::paintEvent(e);
	QPainter p(this);
	p.translate(width() / 2, height() / 2);
	int sz = qMin(width() / 2, height() / 2);
	p.setRenderHint(QPainter::SmoothPixmapTransform);
	p.drawImage(QRect(-sz, -sz, sz*2, sz*2), dimage_);
}


void QTimerWidget::timerEvent(QTimerEvent * e) {
	if (e->timerId() == timer) {
		if (enabled_) {
			emit tick();
			emit tickID(id_emit);
		}
		if (once_) stop();
		return;
	}
	QObject::timerEvent(e);
}


void QTimerWidget::start() {
	if (timer >= 0) stop();
	once_ = false;
	timer = startTimer(int_);
}


void QTimerWidget::startOnce() {
	if (timer >= 0) stop();
	once_ = true;
	timer = startTimer(int_);
}


void QTimerWidget::stop() {
	if (timer < 0) return;
	killTimer(timer);
	timer = -1;
}
