Finished chess animator.
[tagua/yd.git] / src / engine.cpp
blob913a21e32c5639f24e1a1b2775766bc5ecc894d5
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "engine.h"
13 #include <QTextStream>
15 #define ENGINE_DEBUG
16 #ifdef ENGINE_DEBUG
17 #include <iostream>
18 #include "common.h"
19 #endif
21 using namespace boost;
23 Engine::Engine(const QString& path, const QStringList& arguments)
24 : m_path(path)
25 , m_arguments(arguments) {
26 connect(&m_engine, SIGNAL(readyRead()), this, SLOT(processInput()));
27 connect(&m_engine, SIGNAL(finished(int, QProcess::ExitStatus)),
28 this, SIGNAL(lostConnection()));
31 Engine::~Engine() { }
33 void Engine::start() {
34 if (!m_workPath.isNull())
35 m_engine.setWorkingDirectory(m_workPath);
36 m_engine.start(m_path, m_arguments);
37 initializeEngine();
40 void Engine::sendCommand(const QString& command, bool echo) {
41 QTextStream os(&m_engine);
42 os << command << "\n";
43 if (echo && m_console)
44 m_console->echo(command);
45 #ifdef ENGINE_DEBUG
46 std::cout << "> " << command << std::endl;
47 #endif
50 void Engine::processInput() {
51 // process only full lines
52 while (m_engine.canReadLine()) {
53 QString line = m_engine.readLine();
54 line.remove("\n").remove("\r");
55 #ifdef ENGINE_DEBUG
56 std::cout << "< " << line << std::endl;
57 #endif
58 if (m_console)
59 m_console->displayText(line + "\n", 0);
60 emit receivedCommand(line);
64 void Engine::textNotify(const QString& text) {
65 sendCommand(text, false);
69 void Engine::setNotifier(const shared_ptr<EngineNotifier>& notifier) {
70 m_notifier = notifier;
73 void Engine::setConsole(const shared_ptr<Console>& console) {
74 m_console = console;
76 #include "engine.moc"