7 #include "fwGuiQt/WorkerQt.hpp" 9 #include "fwGuiQt/App.hpp" 10 #include "fwGuiQt/util/FuncSlot.hpp" 12 #include <fwRuntime/profile/Profile.hpp> 14 #include <fwServices/registry/ActiveWorkers.hpp> 16 #include <fwThread/Timer.hpp> 17 #include <fwThread/Worker.hpp> 23 #include <QSharedPointer> 24 #include <QStringList> 35 WorkerQtTask( const ::fwThread::Worker::TaskType& handler ) :
36 QEvent( static_cast< QEvent::Type >(s_WORKER_QT_TASK_EVENT_TYPE) ),
39 SLM_ASSERT(
"QApplication should be instantiated", qApp );
47 static const int s_WORKER_QT_TASK_EVENT_TYPE;
51 ::fwThread::Worker::TaskType m_handler;
54 const int WorkerQtTask::s_WORKER_QT_TASK_EVENT_TYPE = QEvent::registerEventType();
64 void init(
int& argc,
char** argv,
bool guiEnabled =
true );
70 void post(TaskType handler);
72 ::fwThread::Worker::FutureType getFuture();
74 virtual ::fwThread::ThreadIdType getThreadId()
const;
76 virtual void processTasks();
78 virtual void processTasks(PeriodType maxtime);
84 QSharedPointer< QApplication > m_app;
94 ::fwThread::ThreadIdType m_threadId;
99 ::fwThread::Worker::sptr getQtWorker(
int& argc,
char** argv,
bool guiEnabled)
101 SPTR(
WorkerQt) workerQt = std::make_shared< WorkerQt >();
102 workerQt->init(argc, argv, guiEnabled);
129 void setDuration(TimeDurationType duration);
134 ::fwCore::mt::ScopedLock lock(m_mutex);
135 return m_timerQt->isSingleShot();
141 ::fwCore::mt::ScopedLock lock(m_mutex);
142 m_timerQt->setSingleShot(oneShot);
148 ::fwCore::mt::ScopedLock lock(m_mutex);
149 return m_timerQt->isActive();
164 void updatedFunction();
166 QPointer< QTimer > m_timerQt;
168 QPointer< ::fwGuiQt::util::FuncSlot > m_qtFunc;
175 WorkerQt::WorkerQt() :
184 void WorkerQt::init(
int& argc,
char** argv,
bool guiEnabled )
189 QDir pluginDir(
"./bin/qt5/plugins");
191 QDir pluginDir(
"./lib/qt5/plugins");
193 if (pluginDir.exists())
195 QCoreApplication::setLibraryPaths(QStringList(pluginDir.absolutePath()));
199 m_app = QSharedPointer< QApplication > ( new ::fwGuiQt::App( m_argc, argv, guiEnabled ) );
206 WorkerQt::~WorkerQt()
215 if (!m_future.valid() )
217 SLM_ASSERT(
"WorkerQt loop shall be created and ran from main thread ",
218 !m_future.valid() && ::fwThread::getCurrentThreadId() == this->getThreadId() );
220 std::packaged_task< ExitReturnType() > task( std::bind(&QApplication::exec) );
222 std::future< ExitReturnType > ufuture = task.get_future();
224 m_future = std::move(ufuture);
243 this->postTask<void>(&QApplication::quit).wait();
249 return std::make_shared< TimerQt >();
256 QApplication::postEvent( qApp,
new WorkerQtTask(handler) );
263 QApplication::sendPostedEvents(0, ::fwGuiQt::WorkerQtTask::s_WORKER_QT_TASK_EVENT_TYPE);
270 QCoreApplication::processEvents(QEventLoop::AllEvents,
int(maxtime));
276 m_timerQt( new QTimer(qApp) )
278 m_qtFunc = new ::fwGuiQt::util::FuncSlot();
279 QObject::connect(m_timerQt, SIGNAL(timeout()), m_qtFunc, SLOT(trigger()));
286 QObject::disconnect(m_timerQt, SIGNAL(timeout()), m_qtFunc, SLOT(trigger()));
296 ::fwCore::mt::ScopedLock lock(m_mutex);
297 m_timerQt->setInterval( static_cast<int>(
298 std::chrono::duration_cast< std::chrono::milliseconds >(duration).count())
306 ::fwCore::mt::ScopedLock lock(m_mutex);
314 ::fwCore::mt::ScopedLock lock(m_mutex);
void stop()
Stops the timer and cancel all pending operations.
Private implementation of fwThread::Worker using boost::asio.
void updatedFunction()
This method is triggered when Timer's function is changed.
FWTHREAD_API ThreadIdType getCurrentThreadId()
Returns the current thread id.
virtual void processTasks()
Processes all worker pending tasks for the calling thread until there are no more tasks to process...
#define OSLM_TRACE(message)
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
FunctionType m_function
Function object to execute each time the timer expires.
void post(TaskType handler)
Requests invocation of the given task handler and returns immediately.
bool isOneShot() const
Returns if the timer mode is 'one shot'.
TimerQt()
Constructs a TimerQt from given io_service.
::fwThread::Worker::FutureType getFuture()
Returns a std::shared_future associated with the execution of Worker's loop.
void start()
Starts or restarts the timer.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
The Timer class provides single-shot or repetitive timers. A Timer triggers a function once after a d...
Private Timer implementation using Qt.
void setOneShot(bool oneShot)
Sets timer mode.
bool isRunning() const
Returns true if the timer is currently running.
This class creates and manages a task loop. The default implementation create a loop in a new thread...
void setDuration(TimeDurationType duration)
Sets time duration.
void stop()
Waits for the last task to be processed and stops the loop.
virtual ::fwThread::ThreadIdType getThreadId() const
Returns the worker's thread id.