Take into account that Qt doesn't allow GUI operations outside the main thread.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@31386 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Enrico Forestieri 2009-09-13 15:07:28 +00:00
parent 6494fc15c1
commit 08fa11894f
2 changed files with 63 additions and 53 deletions

View File

@ -44,34 +44,6 @@
#include "server_monitor.h"
class ReadPipe : public QThread {
public:
///
ReadPipe(LyXServerMonitor * monitor) : lyxmonitor(monitor) {}
///
void run() { lyxmonitor->readPipe(); }
private:
///
LyXServerMonitor * lyxmonitor;
};
class DeleteThread : public QEvent {
public:
///
DeleteThread(ReadPipe * thread)
: QEvent(QEvent::User), pipethread(thread)
{}
///
ReadPipe * pipeThread() const { return pipethread; }
private:
///
ReadPipe * pipethread;
};
LyXServerMonitor::LyXServerMonitor()
: pipein(-1), pipeout(-1), thread_exit(false), lyx_listen(false)
{
@ -190,8 +162,7 @@ void LyXServerMonitor::readPipe()
if (fromLyX.contains("bye")) {
qWarning() << "monitor: LyX has closed "
"connection!";
infoLB->clear();
notifyLB->setText(fromLyX);
pipethread->emitNotice(fromLyX);
notified = true;
break;
}
@ -202,13 +173,10 @@ void LyXServerMonitor::readPipe()
submitCommandPB->setDisabled(false);
}
}
if (fromLyX[0] == QLatin1Char('I')) {
infoLB->setText(fromLyX);
notifyLB->clear();
} else {
infoLB->clear();
notifyLB->setText(fromLyX);
}
if (fromLyX[0] == QLatin1Char('I'))
pipethread->emitInfo(fromLyX);
else
pipethread->emitNotice(fromLyX);
#ifdef _WIN32
// On Windows, we have to close and reopen
// the pipe after each use.
@ -218,9 +186,8 @@ void LyXServerMonitor::readPipe()
O_RDONLY);
if (pipeout < 0) {
perror("monitor");
infoLB->clear();
notifyLB->setText("An error occurred, "
"closing pipes");
pipethread->emitNotice("An error occurred, "
"closing pipes");
notified = true;
break;
}
@ -241,8 +208,7 @@ void LyXServerMonitor::readPipe()
}
#endif
perror("monitor");
infoLB->clear();
notifyLB->setText("An error occurred, closing pipes");
pipethread->emitNotice("An error occurred, closing pipes");
notified = true;
break;
} else
@ -252,35 +218,46 @@ void LyXServerMonitor::readPipe()
if (!notified) {
if (thread_exit) {
qWarning() << "monitor: Closing pipes";
infoLB->clear();
notifyLB->setText("Closing pipes");
pipethread->emitNotice("Closing pipes");
} else {
qWarning() << "monitor: LyX has closed connection!";
infoLB->clear();
notifyLB->setText("LyX has closed connection!");
pipethread->emitNotice("LyX has closed connection!");
}
}
DeleteThread * event = new DeleteThread(pipethread);
QCoreApplication::postEvent(this, static_cast<QEvent *>(event));
QEvent * event = new QEvent(QEvent::User);
QCoreApplication::postEvent(this, event);
lyx_listen = false;
closePipes();
if (!thread_exit)
pipethread->emitClosing();
}
bool LyXServerMonitor::event(QEvent * e)
{
if (e->type() == QEvent::User) {
ReadPipe * pipeThread =
static_cast<DeleteThread *>(e)->pipeThread();
pipeThread->wait();
pipethread->wait();
thread_exit = false;
delete pipeThread;
delete pipethread;
return true;
}
return QDialog::event(e);
}
void LyXServerMonitor::showInfo(QString const & msg)
{
infoLB->setText(msg);
notifyLB->clear();
}
void LyXServerMonitor::showNotice(QString const & msg)
{
infoLB->clear();
notifyLB->setText(msg);
}
void LyXServerMonitor::openPipes()
{
if (pipein == -1) {
@ -307,6 +284,12 @@ void LyXServerMonitor::openPipes()
closePipes();
return;
}
connect(pipethread, SIGNAL(info(QString const &)),
this, SLOT(showInfo(QString const &)));
connect(pipethread, SIGNAL(notice(QString const &)),
this, SLOT(showNotice(QString const &)));
connect(pipethread, SIGNAL(closing()),
this, SLOT(closePipes()));
openPipesPB->setDisabled(true);
closePipesPB->setDisabled(false);
// greet LyX

View File

@ -58,6 +58,8 @@ public Q_SLOTS:
void openPipes();
void closePipes();
void submitCommand();
void showInfo(QString const &);
void showNotice(QString const &);
private:
void createCmdsGroupBox();
@ -87,4 +89,29 @@ private:
ReadPipe * pipethread;
};
class ReadPipe : public QThread
{
Q_OBJECT
public:
ReadPipe(LyXServerMonitor * monitor) : lyxmonitor(monitor) {}
///
void run() { lyxmonitor->readPipe(); }
///
void emitInfo(QString const & msg) { emit info(msg); }
///
void emitNotice(QString const & msg) { emit notice(msg); }
///
void emitClosing() { emit closing(); }
signals:
void info(QString const &);
void notice(QString const &);
void closing();
private:
LyXServerMonitor * lyxmonitor;
};
#endif