00001 /****************************************************************************** 00002 * Copyright (C) 2015 by Ralf Kaestner * 00003 * ralf.kaestner@gmail.com * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the Lesser GNU General Public License as published by* 00007 * the Free Software Foundation; either version 3 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * Lesser GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the Lesser GNU General Public License * 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 00017 ******************************************************************************/ 00018 00019 #include <QApplication> 00020 #include <QTimerEvent> 00021 00022 #include "rqt_multiplot/ThreadedTimer.h" 00023 00024 namespace rqt_multiplot { 00025 00026 /*****************************************************************************/ 00027 /* Constructors and Destructor */ 00028 /*****************************************************************************/ 00029 00030 ThreadedTimer::ThreadedTimer(QObject* parent) : 00031 QThread(parent), 00032 timer_(new QTimer()) { 00033 timer_->moveToThread(this); 00034 00035 connect(timer_, SIGNAL(timeout()), this, SLOT(timerTimeout())); 00036 } 00037 00038 ThreadedTimer::~ThreadedTimer() { 00039 quit(); 00040 wait(); 00041 00042 delete timer_; 00043 } 00044 00045 /*****************************************************************************/ 00046 /* Accessors */ 00047 /*****************************************************************************/ 00048 00049 int ThreadedTimer::getTimerId() const { 00050 return timer_->timerId(); 00051 } 00052 00053 void ThreadedTimer::setRate(double rate) { 00054 timer_->setInterval(1e3/rate); 00055 } 00056 00057 double ThreadedTimer::getRate() const { 00058 return 1e3/timer_->interval(); 00059 } 00060 00061 /*****************************************************************************/ 00062 /* Methods */ 00063 /*****************************************************************************/ 00064 00065 void ThreadedTimer::run() { 00066 timer_->start(); 00067 00068 QThread::exec(); 00069 } 00070 00071 /*****************************************************************************/ 00072 /* Methods */ 00073 /*****************************************************************************/ 00074 00075 void ThreadedTimer::timerTimeout() { 00076 if (parent()) 00077 QApplication::postEvent(parent(), new QTimerEvent(timer_->timerId())); 00078 } 00079 00080 }