1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
/*
* Copyright (C) 2015 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LOMIRIUTIL_TIMER_H
#define LOMIRIUTIL_TIMER_H
#include "ElapsedTimer.h"
#include <QObject>
#include <QPointer>
#include <QTimer>
namespace LomiriUtil {
/** Defines an interface for a Timer. Useful for tests. */
class AbstractTimer : public QObject
{
Q_OBJECT
public:
AbstractTimer(QObject *parent) : QObject(parent) {}
virtual int interval() const = 0;
virtual void setInterval(int msecs) = 0;
virtual void start() = 0;
virtual void stop() = 0;
virtual bool isRunning() const = 0;
virtual bool isSingleShot() const = 0;
virtual void setSingleShot(bool value) = 0;
Q_SIGNALS:
void timeout();
};
/** A QTimer wrapper */
class Timer : public AbstractTimer
{
Q_OBJECT
public:
Timer(QObject *parent = nullptr);
int interval() const override;
void setInterval(int msecs) override;
void start() override;
void stop() override;
bool isRunning() const override;
bool isSingleShot() const override;
void setSingleShot(bool value) override;
private:
QTimer m_timer;
};
class AbstractTimerFactory
{
public:
virtual ~AbstractTimerFactory() {}
virtual AbstractTimer *create(QObject *parent = nullptr) = 0;
};
class TimerFactory : public AbstractTimerFactory
{
public:
AbstractTimer *create(QObject *parent = nullptr) override { return new Timer(parent); }
};
} // namespace LomiriUtil
#endif // LOMIRIUTIL_TIMER_H
|