#include "brainywindow.h" #include "ui_editorwindow.h" #include <QFileDialog> #include <QFontDialog> #include <QFile> #include <QTextStream> #include <QFontMetrics> #include <iostream> EditorWindow::EditorWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::EditorWindow) { ui->setupUi(this); sh = new SyntaxHighlighter(ui->textEdit->document()); ui->actionOpen_2->connect(ui->actionOpen_2, &QAction::triggered, this, openFile); ui->actionSave_2->connect(ui->actionSave_2, &QAction::triggered, this, saveFile); ui->actionEditor_Font->connect(ui->actionEditor_Font, &QAction::triggered, this, [this] () { bool ok; QFont font = QFontDialog::getFont(&ok, ui->textEdit->font(), this); if (ok) ui->textEdit->setFont(font); }); } EditorWindow::~EditorWindow() { delete ui; } void EditorWindow::openFile() { auto filename = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Brainfuck File (*.bf);; Any File (*.*)")); openedFile = new QFile(filename); if (!openedFile->open(QIODevice::ReadWrite | QFile::Text)) return; QTextStream in(openedFile); ui->textEdit->setText(in.readAll()); } void EditorWindow::saveFile() { if (!openedFile->isOpen()) { openedFile->open(QIODevice::WriteOnly | QFile::Text | QFile::Truncate); } if (openedFile->isOpen()) { openedFile->write(ui->textEdit->document()->toPlainText().toUtf8().data()); openedFile->close(); } } bool EditorWindow::changesToSave() const { return changedAnything; }