Mercurial > hg > octave-terminal
changeset 13335:609facb07bad
Refactored code, split clients into their own threads.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Tue, 05 Apr 2011 20:45:27 +0200 |
parents | 677d14fe5d8e |
children | 45fa9f424737 |
files | gui//client.cpp gui//client.h gui//mainwindow.cpp gui//octaveterminal.cpp gui//octaveterminal.h |
diffstat | 5 files changed, 26 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/gui//client.cpp +++ b/gui//client.cpp @@ -1,11 +1,15 @@ #include "client.h" Client::Client(QString command) - : m_command(command) { + : QObject(), + m_command(command) { + m_thread.start(); + moveToThread(&m_thread); + m_process.start(m_command, QProcess::ReadWrite); - connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(reemitDataAvailable())); connect(&m_process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(handleProcessStatusChange(QProcess::ProcessState))); connect(&m_process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(handleProcessFinished(int,QProcess::ExitStatus))); + connect(&m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(reemitDataAvailable())); connect(&m_process, SIGNAL(readyReadStandardError()), this, SLOT(reemitErrorAvailable())); } @@ -13,22 +17,12 @@ m_process.write(content.toLocal8Bit()); } -QString Client::fetch() { - QString fetchedInput(m_process.readAllStandardOutput()); - return fetchedInput; -} - -QString Client::errorMessage() { - QString error(m_process.readAllStandardError()); - return error; -} - void Client::reemitDataAvailable() { - emit dataAvailable(); + emit dataAvailable(m_process.readAllStandardOutput()); } void Client::reemitErrorAvailable() { - emit errorAvailable(); + emit errorAvailable(m_process.readAllStandardError()); } void Client::handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
--- a/gui//client.h +++ b/gui//client.h @@ -4,20 +4,18 @@ #include "clientmanager.h" #include <QProcess> #include <QObject> -#include <QMutex> +#include <QThread> class Client : public QObject { Q_OBJECT friend class ClientManager; -public: +public slots: void send(QString content); - QString fetch(); - QString errorMessage(); signals: - void dataAvailable(); - void errorAvailable(); + void dataAvailable(QString data); + void errorAvailable(QString error); void lostConnection(); protected: @@ -32,7 +30,7 @@ private: QString m_command; QProcess m_process; - QMutex m_clientInRequest; + QThread m_thread; }; #endif // CLIENT_H
--- a/gui//mainwindow.cpp +++ b/gui//mainwindow.cpp @@ -1,11 +1,12 @@ -#include "mainwindow.h" -#include "octaveterminal.h" #include <QMenuBar> #include <QMenu> #include <QAction> #include <QtWebKit/QWebView> +#include "mainwindow.h" +#include "octaveterminal.h" + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { showMaximized();
--- a/gui//octaveterminal.cpp +++ b/gui//octaveterminal.cpp @@ -36,7 +36,9 @@ m_octaveOutput->setFontUnderline(true); m_octaveOutput->append(command); command.append("\n"); - m_client->send(command); + + QMetaObject::invokeMethod(m_client, "send", Q_ARG(QString, command)); + m_commandLine->clear(); } @@ -51,8 +53,8 @@ void OctaveTerminal::assignClient(Client *client) { m_client = client; - connect(client, SIGNAL(dataAvailable()), this, SLOT(fetchDataFromClient())); - connect(client, SIGNAL(errorAvailable()), this, SLOT(fetchErrorFromClient())); + connect(client, SIGNAL(dataAvailable(QString)), this, SLOT(handleDataFromClient(QString))); + connect(client, SIGNAL(errorAvailable(QString)), this, SLOT(handleErrorFromClient(QString))); allowUserInput(); } @@ -60,14 +62,12 @@ m_client->send("who\n"); } -void OctaveTerminal::fetchDataFromClient() { - QString fetchedData = m_client->fetch(); +void OctaveTerminal::handleDataFromClient(QString data) { m_octaveOutput->setFontUnderline(false); - m_octaveOutput->append(fetchedData); + m_octaveOutput->append(data); } -void OctaveTerminal::fetchErrorFromClient() { - QString error = m_client->errorMessage(); +void OctaveTerminal::handleErrorFromClient(QString error) { m_octaveOutput->setFontUnderline(false); m_octaveOutput->append(error); }