File indexing completed on 2024-05-12 16:01:30
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Dmitry Kazakov <dimula73@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef __KIS_DEFERRED_SIGNAL_H 0008 #define __KIS_DEFERRED_SIGNAL_H 0009 0010 #include <QObject> 0011 0012 #include <functional> 0013 0014 /** 0015 * \class KisDeferredSignal 0016 * \brief This class is used for calling a specified callback 0017 * function (which is a std::function) after a specified time 0018 * delay. The callback is called from the QTimer event, so the 0019 * usage of the class does not block the Qt's event loop. 0020 * 0021 * Usage: 0022 * 0023 * \code{.cpp} 0024 * 0025 * // prepare the callback function 0026 * std::function<void ()> callback( 0027 * std::bind(&KisCanvas2::setMonitorProfile, this, 0028 * monitorProfile, renderingIntent, conversionFlags)); 0029 * 0030 * // create the timer connected to the function 0031 * KisDeferredSignal::deferSignal(1000, callback); 0032 * 0033 * \endcode 0034 * 0035 * TODO: rename KisDeferredSignal -> KisDeferredCallback 0036 */ 0037 class KisDeferredSignal : public QObject 0038 { 0039 Q_OBJECT 0040 public: 0041 using CallbackFunction = std::function<void ()>; 0042 0043 public: 0044 /** 0045 * Creates a timer which will call \p function after \p delay 0046 * milliseconds 0047 */ 0048 static void deferSignal(int delay, CallbackFunction function); 0049 0050 private Q_SLOTS: 0051 void timeout(); 0052 0053 private: 0054 KisDeferredSignal(int delay, CallbackFunction function); 0055 0056 private: 0057 CallbackFunction m_function; 0058 }; 0059 0060 #endif /* __KIS_DEFERRED_SIGNAL_H */