File: mainwindow.cpp

package info (click to toggle)
qlogo 0.92%2Bgit.1.29a5ca2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,556 kB
  • sloc: cpp: 15,101; python: 5,098; sh: 30; makefile: 4
file content (239 lines) | stat: -rw-r--r-- 6,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

//===-- qlogo/mainwindow.cpp - MainWindow class implementation -------*- C++
//-*-===//
//
// This file is part of QLogo.
//
// QLogo 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 2 of the License, or
// (at your option) any later version.
//
// QLogo 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 QLogo.  If not, see <http://www.gnu.org/licenses/>.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the implementation of the MainWindow class, which is the
/// main window portion of the user interface.
///
//===----------------------------------------------------------------------===//

#include "mainwindow.h"
#include "canvas.h"
#include "ui_mainwindow.h"
#include "constants.h"
#include <QDebug>
#include <QKeyEvent>
#include <QScrollBar>
#include <QTimer>
#include <QMessageBox>
#include <QDir>
#include <QThread>

// Wrapper function for sending data to the logo interpreter
void MainWindow::sendMessage(std::function<void (QDataStream*)> func)
{
    qint64 datawritten;
    QByteArray buffer;
    QDataStream bufferStream(&buffer, QIODevice::WriteOnly);
    func(&bufferStream);
    qint64 datalen = buffer.size();
    buffer.prepend((const char*)&datalen, sizeof(qint64));
    datawritten = logoProcess->write(buffer);
    Q_ASSERT(datawritten == buffer.size());
}


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);

  // There seems to be a bug involving window Maximize with an OpenGL widget.
  // So disable Maximize.
  setWindowFlags(Qt::Window | Qt::WindowMinimizeButtonHint |
                 Qt::WindowCloseButtonHint);

  windowMode = windowMode_noWait;
}

void MainWindow::show()
{
  QMainWindow::show();
  ui->mainConsole->setFocus();

  startLogo();
}

MainWindow::~MainWindow()
{
    delete ui;
}


int MainWindow::startLogo()
{
  QString command = QCoreApplication::applicationDirPath().append("/QLogo-cli");
  QStringList arguments;
  arguments << "--QLogoGUI";

  logoProcess = new QProcess(this);

  // TODO: maybe call setWorkingDirectory()

  connect(logoProcess, &QProcess::started,
          this, &MainWindow::processStarted);

  connect(logoProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
          this, &MainWindow::processFinished);

  connect(logoProcess, &QProcess::readyReadStandardOutput,
          this, &MainWindow::readStandardOutput);

  connect(logoProcess, &QProcess::readyReadStandardError,
          this, &MainWindow::readStandardError);

  connect(logoProcess, &QProcess::errorOccurred,
          this, &MainWindow::errorOccurred);

  connect(ui->mainConsole, &Console::sendRawlineSignal,
          this, &MainWindow::sendRawlineSlot);

  connect(ui->mainConsole, &Console::sendCharSignal,
          this, &MainWindow::sendCharSlot);

  logoProcess->start(command, arguments);
  return 0;
}



void MainWindow::processStarted()
{
  qDebug() <<"ProcessStarted()";
}


void MainWindow::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
  qDebug() <<"processFinished()" <<exitCode << exitStatus;

}

// TODO: rename this. It sounds confusing.
void MainWindow::readStandardOutput()
{
    int readResult;
    do {
        qint64 datalen;
        message_t header;
        QByteArray buffer;
        QDataStream inDataStream;
        readResult = logoProcess->read((char*)&datalen, sizeof(qint64));
        if (readResult == 0) break;
        Q_ASSERT(readResult == sizeof(qint64));

        buffer.resize(datalen);
        readResult = logoProcess->read(buffer.data(), datalen);
        Q_ASSERT(readResult == (int)datalen);
        QDataStream *dataStream = new QDataStream(&buffer, QIODevice::ReadOnly);

        *dataStream >> header;
        switch(header)
        {
        case W_ZERO:
            qDebug() <<"Zero!";
            break;
        case C_CONSOLE_PRINT_STRING: // 1
        {
            QString text;
            *dataStream >> text;
            ui->mainConsole->printString(text);
            break;
        }
        case C_CONSOLE_REQUEST_LINE: // 2
            beginReadRawline();
            break;
        case C_CONSOLE_REQUEST_CHAR: // 3
            beginReadChar();
            break;
        case C_CANVAS_UPDATE_TURTLE_POS: // 6
            {
              QMatrix4x4 matrix;
              *dataStream >> matrix;
              ui->mainCanvas->setTurtleMatrix(matrix);
              break;
            }
          case C_CANVAS_DRAW_LINE: // 7
            {
              QVector3D a, b;
              QColor color;
              *dataStream
                  >> a
                  >> b
                  >> color;
              ui->mainCanvas->addLine(a, b, color);
              break;
            }
        case C_CANVAS_CLEAR_SCREEN: // 8
            ui->mainCanvas->clearScreen();
            break;
        default:
            qDebug() <<"was not expecting" <<header;
            break;

        }
        delete dataStream;
    } while (1);
}


void MainWindow::readStandardError()
{
    QByteArray ary = logoProcess->readAllStandardError();
    qDebug() <<"stderr: " <<ary;
//  QMessageBox msgBox;
//  msgBox.setText(ary);
//  msgBox.exec();
}

void MainWindow::errorOccurred(QProcess::ProcessError error)
{
    qDebug() <<"Error occurred" <<error;
}


void MainWindow::beginReadRawline()
{
    windowMode = windowMode_waitForRawline;
    ui->mainConsole->requestRawline();
}


void MainWindow::beginReadChar()
{
    windowMode = windowMode_waitForChar;
    ui->mainConsole->requestChar();
}


void MainWindow::sendCharSlot(QChar c)
{
    sendMessage([&](QDataStream *out) {
        *out << (message_t)C_CONSOLE_CHAR_READ << c;
    });
}


void MainWindow::sendRawlineSlot(const QString &line)
{
    sendMessage([&](QDataStream *out) {
        *out << (message_t)C_CONSOLE_RAWLINE_READ << line;
    });
}