signalslot.hpp 3.28 KB
Newer Older
1 2 3 4 5 6 7
#ifndef CVVISUAL_SIGNALEMITTER_HPP
#define CVVISUAL_SIGNALEMITTER_HPP

// std
#include <functional>
#include <stdexcept>

Maksim Shabunin's avatar
Maksim Shabunin committed
8
#include <opencv2/core.hpp>
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

// QT
#include <QObject>
#include <QString>


namespace cvv
{
namespace qtutil
{

/**
 * @brief The Signal class can be used for privat or static signals and in case
 * of
 * conflicts between templates and Q_OBJECT.
 */
class Signal : public QObject
{
	Q_OBJECT
      public:
	/**
	 * @brief Constructor
	 * @param parent The parent
	 */
	Signal(QObject *parent = nullptr) : QObject{ parent }
	{
	}

	/**
	 * @brief Emits the signal.
	 * @param args The arguments
	 */
	void emitSignal() const
	{
		emit signal();
	}
signals:
	/**
	 * @brief The signal emited by emitSignal.
	 */
	void signal() const;
};

/**
 * @brief The Slot class can be used for static slots and in case of conflicts
 * between
 * templates and Q_OBJECT.
 */
class Slot : public QObject
{
	Q_OBJECT
      public:
	/**
	 * @brief Constructor
	 * @param f Function called by the slot slot()
	 * @throw std::invalid_argument If f is invalide
	 * @param parent The parent
	 */
	Slot(const std::function<void()> &f, QObject *parent = nullptr)
	    : QObject{ parent }, function_{ f }
	{
		if (!f)
			throw std::invalid_argument{ "invalid function" };
	}

      public
slots:
	/**
	 * @brief The slot calling function()
	 */
	void slot() const
	{
		function_();
	}

      private:
	/**
	 * @brief The function called by the slot slot()
	 */
	std::function<void()> function_;
};

// ///////////////////////////////////////////////////////////////
// manual "templating" for classes Signal and Slot
// ///////////////////////////////////////////////////////////////

/**
 * @brief Similar to Signal (difference: it accepts a QStriang).
 */
class SignalQString : public QObject
{
	Q_OBJECT
      public:
	SignalQString(QObject *parent = nullptr) : QObject{ parent }
	{
	}

	void emitSignal(const QString &t) const
	{
		emit signal(t);
	}
signals:
	void signal(QString t) const;
};

/**
 * @brief Similar to Slot (difference: it accepts a QString).
 */
class SlotQString : public QObject
{
	Q_OBJECT
      public:
	SlotQString(const std::function<void(QString)> &f,
	            QObject *parent = nullptr)
	    : QObject{ parent }, function_{ f }
	{
		if (!f)
			throw std::invalid_argument{ "invalide function" };
	}

	~SlotQString()
	{
	}

      public
slots:
	void slot(QString t) const
	{
		function_(t);
	}

      private:
	std::function<void(QString)> function_;
};

/**
 * @brief Similar to Signal (difference: it accepts a cv::Mat&).
 */
class SignalMatRef : public QObject
{
	Q_OBJECT
      public:
	SignalMatRef(QObject *parent = nullptr) : QObject{ parent }
	{
	}

	void emitSignal(cv::Mat &mat) const
	{
		emit signal(mat);
	}
signals:
	/**
	 * @brief The signal emited by emitSignal.
	 */
	void signal(cv::Mat &mat) const;
};

/**
 * @brief Similar to Slot (difference: it accepts a bool).
 */
class SlotBool : public QObject
{
	Q_OBJECT
      public:
	SlotBool(const std::function<void(bool)> &f, QObject *parent = nullptr)
	    : QObject{ parent }, function_{ f }
	{
		if (!f)
			throw std::invalid_argument{ "invalide function" };
	}

      public
slots:
	void slot(bool t) const
	{
		function_(t);
	}

      private:
	std::function<void(bool)> function_;
};
}
} // end namespaces qtutil, cvv
#endif // CVVISUAL_SIGNALEMITTER_HPP