# HG changeset patch # User Jacob Dawid # Date 1302167719 -7200 # Node ID 9562e5252fa65797a6a3b989968ab73b9e5109c5 # Parent 5329898fbe051e354f2173eeca731cf587246d76 Removed piped terminal windows. diff -r 5329898fbe05 -r 9562e5252fa6 gui//Quint.pro --- a/gui//Quint.pro Thu Apr 07 11:01:55 2011 +0200 +++ b/gui//Quint.pro Thu Apr 07 11:15:19 2011 +0200 @@ -12,18 +12,10 @@ SOURCES += main.cpp\ mainwindow.cpp \ - octaveterminal.cpp \ - clientmanager.cpp \ - client.cpp \ - terminalhighlighter.cpp \ - terminal.cpp + terminal.cpp -HEADERS += mainwindow.h \ - octaveterminal.h \ - clientmanager.h \ - client.h \ - terminalhighlighter.h \ - terminal.h +HEADERS += mainwindow.h \ + terminal.h LIBS += ../Quint/qtermwidget/libqtermwidget.a diff -r 5329898fbe05 -r 9562e5252fa6 gui//client.cpp --- a/gui//client.cpp Thu Apr 07 11:01:55 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -/* Quint - A graphical user interface for Octave - * Copyright (C) 2011 Jacob Dawid - * jacob.dawid@googlemail.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "client.h" - -Client::Client(QString command) - : QObject(), - m_command(command) { - m_thread.start(); - moveToThread(&m_thread); - - m_process.start(m_command, QProcess::ReadWrite); - 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())); -} - -QMutex *Client::accessMutex() { - return &m_access; -} - -void Client::send(QString content) { - m_process.write(content.toLocal8Bit()); -} - -void Client::reemitDataAvailable() { - emit dataAvailable(m_process.readAllStandardOutput()); -} - -void Client::reemitErrorAvailable() { - emit errorAvailable(m_process.readAllStandardError()); -} - -void Client::handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) { - m_process.start(m_command, QProcess::ReadWrite); -} diff -r 5329898fbe05 -r 9562e5252fa6 gui//client.h --- a/gui//client.h Thu Apr 07 11:01:55 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,131 +0,0 @@ -/* Quint - A graphical user interface for Octave - * Copyright (C) 2011 Jacob Dawid - * jacob.dawid@googlemail.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef CLIENT_H -#define CLIENT_H - -#include "clientmanager.h" -#include -#include -#include -#include - -/** - * \class Client - * - * A client is a representation of another external unit Quint is communicating with. - * Usually this will be the octave process. A client is launched by providing a command - * that will be executed. It will handle the process itself. - */ -class Client : public QObject { - Q_OBJECT - friend class ClientManager; - -public: - QMutex *accessMutex(); - -public slots: - void send(QString content); - -signals: - void dataAvailable(QString data); - void errorAvailable(QString error); - void lostConnection(); - -protected: - /** Clients may only be created by the client manager. */ - Client(QString command); - -private slots: - void reemitDataAvailable(); - void reemitErrorAvailable(); - void handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); - -private: - QString m_command; - QProcess m_process; - QThread m_thread; - QMutex m_access; -}; - -/** - * \class PendingRequest - * - * The PendingRequest class helps to communicate with clients. It automatically locks - * the client until the PendingRequest is deleted. As soon as the request is being - * answered, it will send out the answered()-Signal. It buffers all input and delivers - * it by offering the fetchData() and fetchError()-methods. A request is considered - * as answered, when the termination string is found in the buffer. - */ -class PendingRequest : public QObject { - Q_OBJECT -public: - PendingRequest(Client *client) - : QObject(), - m_client(client), - m_dataBuffer(""), - m_errorBuffer("") { - client->accessMutex()->lock(); - connect(client, SIGNAL(dataAvailable(QString)), this, SLOT(receiveData(QString))); - connect(client, SIGNAL(errorAvailable(QString)), this, SLOT(receiveError(QString))); - } - - virtual ~PendingRequest() { - m_client->accessMutex()->unlock(); - } - - QString fetchData() { - QString content = m_dataBuffer, m_dataBuffer = ""; - return content; - } - - QString fetchError() { - QString content = m_errorBuffer, m_errorBuffer = ""; - return content; - } - - void query(QString request, QRegExp terminator = QRegExp("(\r\n | \n\r | \n)+")) { - m_terminator = terminator; - QMetaObject::invokeMethod(m_client, "send", Q_ARG(QString, request)); - } - -signals: - void answered(); - -private slots: - void receiveData(QString data) { - m_dataBuffer += data; - if(m_terminator.indexIn(m_dataBuffer) != -1) - emit answered(); - } - - void receiveError(QString error) { - m_errorBuffer += error; - if(m_terminator.indexIn(m_dataBuffer) != -1) - emit answered(); - } - -private: - Client *m_client; - QString m_dataBuffer; - QString m_errorBuffer; - QString m_request; - QRegExp m_terminator; -}; - -#endif // CLIENT_H diff -r 5329898fbe05 -r 9562e5252fa6 gui//clientmanager.cpp --- a/gui//clientmanager.cpp Thu Apr 07 11:01:55 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -/* Quint - A graphical user interface for Octave - * Copyright (C) 2011 Jacob Dawid - * jacob.dawid@googlemail.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "clientmanager.h" - -ClientManager ClientManager::m_clientManager; - -ClientManager::ClientManager() { -} - -ClientManager& ClientManager::clientManager() { - return ClientManager::m_clientManager; -} - -Client *ClientManager::startProcess(QString command) { - Client *client = new Client(command); - m_activeClients.push_back(client); - return client; -} diff -r 5329898fbe05 -r 9562e5252fa6 gui//clientmanager.h --- a/gui//clientmanager.h Thu Apr 07 11:01:55 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -/* Quint - A graphical user interface for Octave - * Copyright (C) 2011 Jacob Dawid - * jacob.dawid@googlemail.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef CLIENTMANAGER_H -#define CLIENTMANAGER_H - -#include -#include -#include "client.h" - -/** - * \class ClientManager - * - * The client manager is a singleton that keeps track of all current clients. - */ -class Client; -class ClientManager : public QObject { - Q_OBJECT -public: - static ClientManager& clientManager(); - - /** Factory function to produce new clients. */ - Client *startProcess(QString command); - -private: - ClientManager(); - static ClientManager m_clientManager; - QList m_activeClients; -}; - -#endif // CLIENTMANAGER_H diff -r 5329898fbe05 -r 9562e5252fa6 gui//mainwindow.cpp --- a/gui//mainwindow.cpp Thu Apr 07 11:01:55 2011 +0200 +++ b/gui//mainwindow.cpp Thu Apr 07 11:15:19 2011 +0200 @@ -33,7 +33,7 @@ //addOctaveTerminal(); loadWebPage("Online Manual", "http://www.gnu.org/software/octave/doc/interpreter/"); addTerminalWindow(); - + addTerminalWindow(); m_mdiArea->setViewMode(QMdiArea::SubWindowView); showMaximized(); } diff -r 5329898fbe05 -r 9562e5252fa6 gui//octaveterminal.cpp --- a/gui//octaveterminal.cpp Thu Apr 07 11:01:55 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -/* Quint - A graphical user interface for Octave - * Copyright (C) 2011 Jacob Dawid - * jacob.dawid@googlemail.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "octaveterminal.h" -#include -#include - -OctaveTerminal::OctaveTerminal(QWidget *parent) : - QMdiSubWindow(parent), - m_client(0) { - setWindowTitle("Octave Terminal"); - - setWidget(new QWidget(this)); - m_mainToolBar = new QToolBar(widget()); - m_octaveOutput = new QTextBrowser(widget()); - m_commandLine = new TerminalCommandLine(widget()); - - QVBoxLayout *layout = new QVBoxLayout(); - layout->addWidget(m_mainToolBar); - layout->addWidget(m_octaveOutput); - layout->addWidget(m_commandLine); - widget()->setLayout(layout); - - QPushButton *showEnvironmentButton = new QPushButton("Show Environment (who)"); - m_mainToolBar->addWidget(showEnvironmentButton); - - m_octaveOutput->setFontFamily("Monospace"); - m_octaveOutput->setReadOnly(true); - - blockUserInput(); - connect(m_commandLine, SIGNAL(claimCommand(QString)), this, SLOT(sendCommand(QString))); - connect(showEnvironmentButton, SIGNAL(clicked()), this, SLOT(showEnvironment())); - - m_terminalHighlighter = new TerminalHighlighter(m_octaveOutput->document()); -} - -void OctaveTerminal::sendCommand(QString command) { - m_octaveOutput->append(">" + command); - addRequest(command + "\n"); -} - -void OctaveTerminal::blockUserInput() { - m_commandLine->setEnabled(false); -} - -void OctaveTerminal::allowUserInput() { - m_commandLine->setEnabled(true); - m_commandLine->setFocus(); -} - -void OctaveTerminal::assignClient(Client *client) { - m_client = client; - allowUserInput(); - - // Sends an empty command to make the welcome message show up. - addRequest("\n"); -} - -void OctaveTerminal::showEnvironment() { - addRequest("who\n"); -} - - -void OctaveTerminal::addRequest(QString command) { - blockUserInput(); - m_pendingRequest = new PendingRequest(m_client); - connect(m_pendingRequest, SIGNAL(answered()), this, SLOT(handleAnsweredRequest())); - m_pendingRequest->query(command, QRegExp("octave:[0-9]+>")); -} - -void OctaveTerminal::handleAnsweredRequest() { - allowUserInput(); - QString data = m_pendingRequest->fetchData(); - QString error = m_pendingRequest->fetchError(); - m_octaveOutput->append(data); - m_octaveOutput->append(error); - delete m_pendingRequest; -} diff -r 5329898fbe05 -r 9562e5252fa6 gui//octaveterminal.h --- a/gui//octaveterminal.h Thu Apr 07 11:01:55 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,161 +0,0 @@ -/* Quint - A graphical user interface for Octave - * Copyright (C) 2011 Jacob Dawid - * jacob.dawid@googlemail.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef OCTAVETERMINAL_H -#define OCTAVETERMINAL_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "client.h" -#include "terminalhighlighter.h" - -/** - * \class TerminalCommandLine - * - * Extends the QLineEdit by a history function. - */ -class TerminalCommandLine : public QLineEdit { - Q_OBJECT -public: - TerminalCommandLine(QWidget *parent = 0) - : QLineEdit(parent), - m_commandHistoryIndex(0) { - } - -signals: - void claimCommand(QString command); - -protected: - void focusOutEvent(QFocusEvent *focusEvent) { - setFocus(); - } - - bool event(QEvent *event) - { - if (event->type() == QEvent::KeyPress) { - QKeyEvent *ke = static_cast(event); - if (ke->key() == Qt::Key_Tab) { - emit claimCommand("\t"); - return true; - } - } - - return QWidget::event(event); - } - - void keyPressEvent(QKeyEvent *keyEvent) { - QString command; - switch(keyEvent->key()) { - case Qt::Key_Return: - command = text(); - emit claimCommand(command); - m_commandHistory.append(command); - m_commandHistoryIndex = m_commandHistory.size(); - m_currentlyEditedCommand = ""; - setText(""); - break; - - case Qt::Key_Up: - if(!m_commandHistory.empty()) - { - if(m_commandHistoryIndex == m_commandHistory.size()) - m_currentlyEditedCommand = text(); - - m_commandHistoryIndex--; - if(m_commandHistoryIndex < 0) - m_commandHistoryIndex = m_commandHistory.size(); - - if(m_commandHistoryIndex == m_commandHistory.size()) - setText(m_currentlyEditedCommand); - else - setText(m_commandHistory.at(m_commandHistoryIndex)); - } - break; - - case Qt::Key_Down: - if(!m_commandHistory.empty()) - { - if(m_commandHistoryIndex == m_commandHistory.size()) - m_currentlyEditedCommand = text(); - - m_commandHistoryIndex++; - if(m_commandHistoryIndex > m_commandHistory.size()) - m_commandHistoryIndex = 0; - - if(m_commandHistoryIndex == m_commandHistory.size()) - setText(m_currentlyEditedCommand); - else - setText(m_commandHistory.at(m_commandHistoryIndex)); - } - break; - - default: - QLineEdit::keyPressEvent(keyEvent); - break; - } - } - -private: - QList m_commandHistory; - QString m_currentlyEditedCommand; - int m_commandHistoryIndex; -}; - -/** - * \class OctaveTerminal - * - * Provides a complete OctaveTerminal. Unless there is no octave client assigned to - * this terminal it will be not functional. - */ -class OctaveTerminal : public QMdiSubWindow { - Q_OBJECT -public: - explicit OctaveTerminal(QWidget *parent = 0); - -signals: - -public slots: - void sendCommand(QString command); - void blockUserInput(); - void allowUserInput(); - - void assignClient(Client* client); - void showEnvironment(); - void handleAnsweredRequest(); - -private: - void addRequest(QString command); - - QToolBar *m_mainToolBar; - QTextBrowser *m_octaveOutput; - TerminalCommandLine *m_commandLine; - Client *m_client; - TerminalHighlighter *m_terminalHighlighter; - PendingRequest *m_pendingRequest; -}; - -#endif // OCTAVETERMINAL_H diff -r 5329898fbe05 -r 9562e5252fa6 gui//terminal.cpp --- a/gui//terminal.cpp Thu Apr 07 11:01:55 2011 +0200 +++ b/gui//terminal.cpp Thu Apr 07 11:15:19 2011 +0200 @@ -18,5 +18,6 @@ m_terminalWidget->setShellProgram(programName); m_terminalWidget->startShellProgram(); setFocus(); + connect(m_terminalWidget, SIGNAL(finished()), this, SLOT(launchTerminal())); } diff -r 5329898fbe05 -r 9562e5252fa6 gui//terminalhighlighter.cpp --- a/gui//terminalhighlighter.cpp Thu Apr 07 11:01:55 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* Quint - A graphical user interface for Octave - * Copyright (C) 2011 Jacob Dawid - * jacob.dawid@googlemail.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "terminalhighlighter.h" -#include - -TerminalHighlighter::TerminalHighlighter(QTextDocument *parent) - : QSyntaxHighlighter(parent) -{ - HighlightingRule rule; - - keywordFormat.setForeground(Qt::darkRed); - QStringList keywordPatterns; - keywordPatterns << "\\bOctave\\b" << "\\bGNU\\b"; - - foreach(const QString &pattern, keywordPatterns) { - rule.pattern = QRegExp(pattern); - rule.format = keywordFormat; - highlightingRules.append(rule); - } - - promptFormat.setForeground(Qt::darkGreen); - rule.pattern = QRegExp("\\boctave:[0-9]+>"); - rule.format = promptFormat; - highlightingRules.append(rule); - - errorFormat.setForeground(Qt::red); - errorFormat.setFontWeight(QFont::Bold); - rule.pattern = QRegExp("\\berror:"); - rule.format = errorFormat; - highlightingRules.append(rule); - - numberFormat.setForeground(Qt::darkGreen); - numberFormat.setFontWeight(QFont::Bold); - rule.pattern = QRegExp("\\b[0-9\\.]+[i]?\\b"); - rule.format = numberFormat; - highlightingRules.append(rule); - - urlFormat.setForeground(Qt::darkBlue); - rule.pattern = QRegExp("\\bhttp://[^\\s]+\\b"); - rule.format = urlFormat; - highlightingRules.append(rule); - - quotationFormat.setForeground(Qt::darkGreen); - rule.pattern = QRegExp("\".*\""); - rule.format = quotationFormat; - highlightingRules.append(rule); -} - -void TerminalHighlighter::highlightBlock(const QString &text) -{ - foreach(const HighlightingRule &rule, highlightingRules) { - QRegExp expression(rule.pattern); - int index = expression.indexIn(text); - while(index >= 0) { - int length = expression.matchedLength(); - setFormat(index, length, rule.format); - index = expression.indexIn(text, index + length); - } - } -} diff -r 5329898fbe05 -r 9562e5252fa6 gui//terminalhighlighter.h --- a/gui//terminalhighlighter.h Thu Apr 07 11:01:55 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/* Quint - A graphical user interface for Octave - * Copyright (C) 2011 Jacob Dawid - * jacob.dawid@googlemail.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef TERMINALHIGHLIGHTER_H -#define TERMINALHIGHLIGHTER_H - -#include - -/** - * \class TerminalHighlighter - * - * Subclass Qt's QSyntaxHighlighter-class to provide syntac highlighting. - */ -class QTextDocument; -class TerminalHighlighter : public QSyntaxHighlighter { - Q_OBJECT - -public: - TerminalHighlighter(QTextDocument *parent = 0); - -protected: - void highlightBlock(const QString &text); - -private: - struct HighlightingRule { - QRegExp pattern; - QTextCharFormat format; - }; - QVector highlightingRules; - - QTextCharFormat keywordFormat; - QTextCharFormat quotationFormat; - QTextCharFormat numberFormat; - QTextCharFormat urlFormat; - QTextCharFormat errorFormat; - QTextCharFormat promptFormat; -}; - -#endif // TERMINALHIGHLIGHTER_H