]> git.lyx.org Git - lyx.git/blob - src/support/InGuiThread.cpp
add generic helper class for calling functions in gui thread
[lyx.git] / src / support / InGuiThread.cpp
1 /**
2  * \file InGuiThread.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Peter Kümmel
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include "InGuiThread.h"
12
13 #include <QThread>
14 #include <QEventLoop>
15 #include <QApplication>
16
17 namespace lyx {
18 namespace frontend {
19
20
21 IntoGuiThreadMover::IntoGuiThreadMover()
22 {
23         moveToThread(QApplication::instance()->thread());
24         connect(this, SIGNAL(triggerCall()), this, SLOT(doFunctionCall()),
25                 Qt::QueuedConnection);
26 }
27
28
29 void IntoGuiThreadMover::callInGuiThread()
30 {
31         if (QThread::currentThread() == QApplication::instance()->thread()) {
32                 synchronousFunctionCall();
33         } else {
34                 QEventLoop loop;
35                 connect(this, SIGNAL(called()), &loop, SLOT(quit()));
36                 Q_EMIT triggerCall();
37                 loop.exec();
38         }
39 }
40
41
42 void IntoGuiThreadMover::doFunctionCall()
43 {
44         synchronousFunctionCall();
45         Q_EMIT called();
46 }
47
48
49 } // namespace frontend
50 } // namespace lyx
51
52