]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/InGuiThread.cpp
only connect when needed
[lyx.git] / src / frontends / qt4 / 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 <config.h>
12
13 #include "InGuiThread.h"
14
15 #include <QThread>
16 #include <QEventLoop>
17 #include <QApplication>
18
19 namespace lyx {
20 namespace frontend {
21
22
23 IntoGuiThreadMover::IntoGuiThreadMover()
24 {
25 }
26
27
28 void IntoGuiThreadMover::callInGuiThread()
29 {
30         QThread* gui_thread = QApplication::instance()->thread();
31         if (QThread::currentThread() == gui_thread) {
32                 synchronousFunctionCall();
33         } else {
34                 moveToThread(gui_thread);
35                 connect(this, SIGNAL(triggerCall()), this, SLOT(doFunctionCall()),
36                         Qt::QueuedConnection);
37                 // TODO try with condition, it's maybe cheaper
38                 QEventLoop loop;
39                 connect(this, SIGNAL(called()), &loop, SLOT(quit()));
40                 Q_EMIT triggerCall();
41                 loop.exec();
42         }
43 }
44
45
46 void IntoGuiThreadMover::doFunctionCall()
47 {
48         synchronousFunctionCall();
49         Q_EMIT called();
50 }
51
52
53 } // namespace frontend
54 } // namespace lyx
55
56 #include "moc_InGuiThread.cpp"