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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
|
//===-- qlogo/mainwindow.cpp - MainWindow class implementation --*- C++ -*-===//
//
// Copyright 2017-2024 Jason Sikes
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the conditions specified in the
// license found in the LICENSE file in the project root.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the implementation of the MainWindow class, which is the
/// main window portion of the user interface.
///
//===----------------------------------------------------------------------===//
#include "gui/mainwindow.h"
#include "gui/canvas.h"
#include "gui/editorwindow.h"
#include "sharedconstants.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QFontDatabase>
#include <QKeyEvent>
#include <QMessageBox>
#include <QScrollBar>
#include <QThread>
#include <QTimer>
/// @brief a pointer to the qlogo process.
static QProcess *logoProcess;
/// @brief Interface for sending messages to the qlogo process.
///
/// This class is used to send messages to the qlogo process. It presents a
/// QDataStream interface for "<<" stream operations and then the destructor will
/// send the message to the qlogo process.
struct message
{
message() : bufferStream(&buffer, QIODevice::WriteOnly)
{
buffer.clear();
}
~message()
{
qint64 datawritten;
qint64 datalen = buffer.size();
buffer.prepend((const char *)&datalen, sizeof(qint64));
datawritten = logoProcess->write(buffer);
Q_ASSERT(datawritten == buffer.size());
}
template <class T>
message &operator<<(const T &x)
{
bufferStream << x;
return *this;
}
private:
QByteArray buffer;
QDataStream bufferStream;
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
windowMode = windowMode_noWait;
}
void MainWindow::show()
{
QMainWindow::show();
ui->mainConsole->setFocus();
startLogo();
}
MainWindow::~MainWindow()
{
delete ui;
}
QString MainWindow::findQlogoExe()
{
// Windows executables have 'exe' extension.
#ifdef WIN32
QString filename("qlogo.exe");
#else
QString filename("qlogo");
#endif
// Build a list of candidate locations to try.
QStringList candidates;
// The qlogo directory relative to wherever the app binary is.
candidates << QCoreApplication::applicationDirPath() + QDir::separator() + ".." + QDir::separator() + "qlogo" +
QDir::separator() + filename;
// The same directory as the app binary.
candidates << QCoreApplication::applicationDirPath() + QDir::separator() + filename;
for (auto &c : candidates)
{
// qDebug() << "Checking: " << c;
if (QFileInfo::exists(c))
return c;
}
// TODO: How do we handle this gracefully?
return QString();
}
int MainWindow::startLogo()
{
QString command = findQlogoExe();
QStringList arguments;
arguments << "--QLogoGUI";
logoProcess = new QProcess(this);
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(ui->mainConsole, &Console::sendRawlineSignal, this, &MainWindow::sendRawlineSlot);
connect(ui->mainConsole, &Console::sendCharSignal, this, &MainWindow::sendCharSlot);
connect(ui->splitter, &QSplitter::splitterMoved, this, &MainWindow::splitterHasMovedSlot);
connect(ui->mainCanvas, &Canvas::sendMouseclickedSignal, this, &MainWindow::mouseclickedSlot);
connect(ui->mainCanvas, &Canvas::sendMousemovedSignal, this, &MainWindow::mousemovedSlot);
connect(ui->mainCanvas, &Canvas::sendMouseReleasedSignal, this, &MainWindow::mousereleasedSlot);
logoProcess->start(command, arguments);
return 0;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
qint64 pid = logoProcess->processId();
// Tell the process to die, then ignore.
// Because when the process dies another signal will be sent to close the application.
if (pid > 0)
{
message() << (message_t)S_SYSTEM;
logoProcess->closeWriteChannel();
event->ignore();
}
else
{
event->accept();
}
}
void MainWindow::initialize()
{
QFont defaultFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
ui->mainConsole->setTextFontSize(defaultFont.pointSizeF());
ui->mainConsole->setTextFontName(defaultFont.family());
ui->mainCanvas->setLabelFontSize(defaultFont.pointSizeF());
ui->mainCanvas->setLabelFontName(defaultFont.family());
setSplitterforMode(initScreenMode);
message() << (message_t)W_INITIALIZE << QFontDatabase::families() << defaultFont.family()
<< (double)defaultFont.pointSizeF();
}
void MainWindow::fileDialogModal()
{
QString startingDir = QDir::homePath();
QString filePath = QFileDialog::getOpenFileName(this, tr("Choose file"), startingDir);
message() << (message_t)W_FILE_DIALOG_GET_PATH << filePath;
}
void MainWindow::openEditorWindow(const QString startingText)
{
if (editWindow == NULL)
{
editWindow = new EditorWindow;
connect(editWindow, SIGNAL(editingHasEndedSignal(QString)), this, SLOT(editingHasEndedSlot(QString)));
}
editWindow->setTextFormat(ui->mainConsole->getFont());
editWindow->setContents(startingText);
editWindow->show();
editWindow->activateWindow();
editWindow->setFocus();
}
void MainWindow::editingHasEndedSlot(QString text)
{
message() << (message_t)C_CONSOLE_END_EDIT_TEXT << text;
}
void MainWindow::introduceCanvas()
{
if (hasShownCanvas)
return;
hasShownCanvas = true;
setSplitterforMode(splitScreenMode);
}
void MainWindow::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
if (exitStatus != QProcess::NormalExit)
{
QMessageBox msgBox;
msgBox.setText(tr("qlogo has reached an unstable state and will be terminated."));
msgBox.exec();
}
QApplication::exit(0);
}
void MainWindow::readStandardOutput()
{
qint64 datalen;
forever
{
// If a message is complete then it was already sent,
// and we can start a new one.
if (readBuffer.size() == readBufferLen)
{
int readResult = logoProcess->read((char *)&datalen, sizeof(qint64));
if (readResult != sizeof(qint64))
return;
readBufferLen = datalen;
readBuffer = logoProcess->read(readBufferLen);
}
else
{
// We are appending the incoming message to the buffer.
qint64 remain = readBufferLen - readBuffer.size();
QByteArray post = logoProcess->read(remain);
Q_ASSERT(!post.isEmpty());
readBuffer.append(post);
}
// If we don't have all of the message yet, keep what we have,
// and wait for the next signal to come back later.
if (readBuffer.size() < readBufferLen)
return;
// We do have a complete message.
processReadBuffer();
}
}
void MainWindow::processReadBuffer()
{
QDataStream dataStream = QDataStream(readBuffer);
message_t header;
dataStream >> header;
switch (header)
{
case W_ZERO:
// This only exists to help catch errors.
qDebug() << "Zero!";
break;
case W_INITIALIZE:
{
initialize();
break;
}
case W_CLOSE_PIPE:
{
logoProcess->closeWriteChannel();
break;
}
case W_SET_SCREENMODE:
{
ScreenModeEnum newMode;
dataStream >> newMode;
setSplitterforMode(newMode);
break;
}
case W_FILE_DIALOG_GET_PATH:
{
fileDialogModal();
break;
}
case C_CONSOLE_PRINT_STRING:
{
QString text;
dataStream >> text;
ui->mainConsole->printString(text);
break;
}
case C_CONSOLE_SET_FONT_NAME:
{
QString name;
dataStream >> name;
ui->mainConsole->setTextFontName(name);
break;
}
case C_CONSOLE_SET_FONT_SIZE:
{
qreal aSize;
dataStream >> aSize;
ui->mainConsole->setTextFontSize(aSize);
break;
}
case C_CONSOLE_REQUEST_LINE:
{
QString prompt;
dataStream >> prompt;
beginReadRawlineWithPrompt(prompt);
break;
}
case C_CONSOLE_REQUEST_CHAR:
beginReadChar();
break;
case C_CONSOLE_BEGIN_EDIT_TEXT:
{
QString startingText;
dataStream >> startingText;
openEditorWindow(startingText);
break;
}
case C_CONSOLE_TEXT_CURSOR_POS:
{
sendConsoleCursorPosition();
break;
}
case C_CONSOLE_SET_TEXT_CURSOR_POS:
{
int row, col;
dataStream >> row >> col;
ui->mainConsole->setTextCursorPosition(row, col);
break;
}
case C_CONSOLE_SET_CURSOR_MODE:
{
bool mode;
dataStream >> mode;
ui->mainConsole->setOverwriteMode(mode);
break;
}
case C_CONSOLE_SET_TEXT_COLOR:
{
QColor foreground;
QColor background;
dataStream >> foreground >> background;
ui->mainConsole->setTextFontColor(foreground, background);
break;
}
case C_CONSOLE_CLEAR_SCREEN_TEXT:
ui->mainConsole->setPlainText("");
break;
case C_CANVAS_UPDATE_TURTLE_POS:
{
QTransform matrix;
dataStream >> matrix;
ui->mainCanvas->setTurtleMatrix(matrix);
introduceCanvas();
break;
}
case C_CANVAS_SET_TURTLE_IS_VISIBLE:
{
bool isVisible;
dataStream >> isVisible;
ui->mainCanvas->setTurtleIsVisible(isVisible);
introduceCanvas();
break;
}
case C_CANVAS_EMIT_VERTEX:
{
ui->mainCanvas->emitVertex();
introduceCanvas();
break;
}
case C_CANVAS_SET_FOREGROUND_COLOR:
{
QColor color;
dataStream >> color;
ui->mainCanvas->setForegroundColor(color);
introduceCanvas();
break;
}
case C_CANVAS_SET_BACKGROUND_COLOR:
{
QColor color;
dataStream >> color;
ui->mainCanvas->setBackgroundColor(color);
introduceCanvas();
break;
}
case C_CANVAS_SET_BACKGROUND_IMAGE:
{
QImage image;
dataStream >> image;
ui->mainCanvas->setBackgroundImage(image);
introduceCanvas();
break;
}
case C_CANVAS_BEGIN_POLYGON:
{
QColor color;
dataStream >> color;
ui->mainCanvas->beginPolygon(color);
break;
}
case C_CANVAS_END_POLYGON:
{
ui->mainCanvas->endPolygon();
break;
}
case C_CANVAS_CLEAR_SCREEN:
ui->mainCanvas->clearScreen();
introduceCanvas();
break;
case C_CANVAS_SETBOUNDS:
{
qreal x, y;
dataStream >> x >> y;
ui->mainCanvas->setBounds(x, y);
break;
}
case C_CANVAS_SET_IS_BOUNDED:
{
bool isBounded;
dataStream >> isBounded;
ui->mainCanvas->setIsBounded(isBounded);
break;
}
case C_CANVAS_SET_FONT_NAME:
{
QString name;
dataStream >> name;
ui->mainCanvas->setLabelFontName(name);
break;
}
case C_CANVAS_SET_FONT_SIZE:
{
qreal aSize;
dataStream >> aSize;
ui->mainCanvas->setLabelFontSize(aSize);
break;
}
case C_CANVAS_DRAW_LABEL:
{
QString aString;
dataStream >> aString;
ui->mainCanvas->addLabel(aString);
introduceCanvas();
break;
}
case C_CANVAS_DRAW_ARC:
{
qreal angle;
qreal radius;
dataStream >> angle >> radius;
ui->mainCanvas->addArc(angle, radius);
introduceCanvas();
break;
}
case C_CANVAS_SET_PENSIZE:
{
qreal newSize;
dataStream >> newSize;
ui->mainCanvas->setPensize(newSize);
break;
}
case C_CANVAS_SET_PENMODE:
{
PenModeEnum newMode;
dataStream >> newMode;
ui->mainCanvas->setPenmode(newMode);
break;
}
case C_CANVAS_SET_PENUPDOWN:
{
bool penIsDown;
dataStream >> penIsDown;
ui->mainCanvas->setPenIsDown(penIsDown);
break;
}
case C_CANVAS_GET_IMAGE:
{
sendCanvasImage();
break;
}
case C_CANVAS_GET_SVG:
{
sendCanvasSvg();
break;
}
default:
qDebug() << "was not expecting" << header;
break;
}
}
void MainWindow::setSplitterforMode(ScreenModeEnum mode)
{
float canvasSize, consoleSize;
switch (mode)
{
case initScreenMode:
canvasSize = Config::get().initScreenSize;
break;
case textScreenMode:
canvasSize = Config::get().textScreenSize;
break;
case fullScreenMode:
canvasSize = Config::get().fullScreenSize;
break;
case splitScreenMode:
canvasSize = Config::get().splitScreenSize;
break;
}
QList<int> sizes = ui->splitter->sizes();
float splitterSize = sizes[0] + sizes[1];
canvasSize = canvasSize * splitterSize;
consoleSize = splitterSize - canvasSize;
ui->splitter->setSizes(QList<int>() << (int)canvasSize << (int)consoleSize);
}
void MainWindow::readStandardError()
{
QByteArray ary = logoProcess->readAllStandardError();
qDebug() << "stderr: " << QString(ary);
}
void MainWindow::beginReadRawlineWithPrompt(const QString prompt)
{
windowMode = windowMode_waitForRawline;
ui->mainConsole->requestRawlineWithPrompt(prompt);
}
void MainWindow::beginReadChar()
{
windowMode = windowMode_waitForChar;
ui->mainConsole->requestChar();
}
void MainWindow::mouseclickedSlot(QPointF position, int buttonID)
{
message() << (message_t)C_CANVAS_MOUSE_BUTTON_DOWN << position << buttonID;
}
void MainWindow::mousemovedSlot(QPointF position)
{
message() << (message_t)C_CANVAS_MOUSE_MOVED << position;
}
void MainWindow::mousereleasedSlot()
{
message() << (message_t)C_CANVAS_MOUSE_BUTTON_UP;
}
void MainWindow::sendCharSlot(QChar c)
{
message() << (message_t)C_CONSOLE_CHAR_READ << c;
}
void MainWindow::sendRawlineSlot(const QString &line)
{
message() << (message_t)C_CONSOLE_RAWLINE_READ << line;
}
void MainWindow::sendConsoleCursorPosition()
{
int row = 0;
int col = 0;
ui->mainConsole->getCursorPos(row, col);
message() << (message_t)C_CONSOLE_TEXT_CURSOR_POS << row << col;
}
void MainWindow::sendCanvasImage()
{
QImage image(ui->mainCanvas->getImage());
message() << (message_t)C_CANVAS_GET_IMAGE << image;
}
void MainWindow::sendCanvasSvg()
{
QByteArray svg = ui->mainCanvas->getSvg();
message() << (message_t)C_CANVAS_GET_SVG << svg;
}
void MainWindow::splitterHasMovedSlot(int, int)
{
hasShownCanvas = true;
}
|