Skip to content

Commit

Permalink
Refactor driver for continuous compilation by a background process
Browse files Browse the repository at this point in the history
  • Loading branch information
IgKh committed Jun 15, 2024
1 parent ae63c95 commit 021328b
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 67 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(hunspell REQUIRED IMPORTED_TARGET hunspell)

set(SOURCES
katvan_compileroutput.cpp
katvan_editor.cpp
katvan_editorsettings.cpp
katvan_highlighter.cpp
Expand Down
43 changes: 43 additions & 0 deletions src/katvan_compileroutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of Katvan
* Copyright (c) 2024 Igor Khanin
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "katvan_compileroutput.h"

#include <QFontDatabase>

namespace katvan {

CompilerOutput::CompilerOutput(QWidget* parent)
: QPlainTextEdit(parent)
{
setReadOnly(true);
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
}

void CompilerOutput::setOutputLines(const QStringList& output)
{
clear();
QTextCursor cursor(document());

QString outputString = "";
for (const auto& line : output) {
cursor.insertText(line);
cursor.insertBlock();
}
}

}
36 changes: 36 additions & 0 deletions src/katvan_compileroutput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of Katvan
* Copyright (c) 2024 Igor Khanin
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once

#include <QList>
#include <QPlainTextEdit>

namespace katvan {

class CompilerOutput : public QPlainTextEdit
{
Q_OBJECT

public:
CompilerOutput(QWidget* parent = nullptr);

public slots:
void setOutputLines(const QStringList& output);
};

}
33 changes: 10 additions & 23 deletions src/katvan_mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "katvan_compileroutput.h"
#include "katvan_editor.h"
#include "katvan_editorsettings.h"
#include "katvan_mainwindow.h"
Expand All @@ -34,7 +35,6 @@
#include <QInputDialog>
#include <QMenuBar>
#include <QMessageBox>
#include <QPlainTextEdit>
#include <QSettings>
#include <QStatusBar>
#include <QTextEdit>
Expand All @@ -57,15 +57,16 @@ MainWindow::MainWindow()
connect(d_recentFiles, &RecentFiles::fileSelected, this, &MainWindow::openNamedFile);

d_driver = new TypstDriver(this);
connect(d_driver, &TypstDriver::previewReady, this, &MainWindow::updatePreview);
connect(d_driver, &TypstDriver::compilationFailed, this, &MainWindow::compilationFailed);

setupUI();
setupActions();
setupStatusBar();

connect(d_driver, &TypstDriver::previewReady, this, &MainWindow::updatePreview);
connect(d_driver, &TypstDriver::outputReady, d_compilerOutput, &CompilerOutput::setOutputLines);
connect(d_driver, &TypstDriver::compilationFailed, d_compilerOutputDock, &QDockWidget::show);

readSettings();
setCurrentFile(QString());
cursorPositionChanged();

if (!d_driver->compilerFound()) {
Expand All @@ -86,7 +87,7 @@ void MainWindow::setupUI()
d_editor = new Editor();
connect(d_editor, &Editor::contentModified, d_driver, &TypstDriver::updatePreview);
connect(d_editor, &QTextEdit::cursorPositionChanged, this, &MainWindow::cursorPositionChanged);
connect(d_editor->document(), &QTextDocument::modificationChanged, this, &QMainWindow::setWindowModified);
connect(d_editor->document(), &QTextDocument::modificationChanged, this, &QMainWindow::setWindowModified, Qt::QueuedConnection);

d_searchBar = new SearchBar(d_editor);
d_searchBar->setVisible(false);
Expand All @@ -103,13 +104,7 @@ void MainWindow::setupUI()
connect(d_editorSettingsDialog, &QDialog::accepted, this, &MainWindow::editorSettingsDialogAccepted);

d_previewer = new Previewer();

QFont monospaceFont { "Monospace" };
monospaceFont.setStyleHint(QFont::Monospace);

d_compilerOutput = new QPlainTextEdit();
d_compilerOutput->setReadOnly(true);
d_compilerOutput->setFont(monospaceFont);
d_compilerOutput = new CompilerOutput();

setDockOptions(QMainWindow::AnimatedDocks);

Expand Down Expand Up @@ -380,13 +375,13 @@ void MainWindow::setCurrentFile(const QString& fileName)

d_editor->checkForModelines();

d_editor->document()->setModified(false);
setWindowModified(false);

setWindowTitle(QString("%1[*] - %2").arg(
QCoreApplication::applicationName(),
d_currentFileShortName));

d_editor->document()->setModified(false);
setWindowModified(false);

d_driver->resetInputFile(fileName);
d_searchBar->resetSearchRange();
d_searchBar->hide();
Expand Down Expand Up @@ -692,18 +687,10 @@ void MainWindow::updatePreview(const QString& pdfFile)
return;
}

d_compilerOutput->clear();

if (d_exportPdfPending) {
QTimer::singleShot(0, this, &MainWindow::exportPdf);
d_exportPdfPending = false;
}
}

void MainWindow::compilationFailed(const QString& output)
{
d_compilerOutput->setPlainText(output);
d_compilerOutputDock->show();
}

}
11 changes: 5 additions & 6 deletions src/katvan_mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
#include <QMainWindow>

QT_BEGIN_NAMESPACE
class QPdfDocument;
class QPlainTextEdit;
class QSettings;
class QToolButton;
QT_END_NAMESPACE

namespace katvan
{

class CompilerOutput;
class Editor;
class EditorSettingsDialog;
class TypstDriver;
Expand All @@ -45,8 +44,10 @@ class MainWindow : public QMainWindow

void loadFile(const QString& fileName);

private slots:
public slots:
void newFile();

private slots:
void openFile();
void openNamedFile(const QString& fileName);
bool saveFile();
Expand All @@ -61,7 +62,6 @@ private slots:
void toggleCursorMovementStyle();
void editorSettingsDialogAccepted();
void updatePreview(const QString& pdfFile);
void compilationFailed(const QString& output);

private:
void setupUI();
Expand All @@ -83,12 +83,11 @@ private slots:

RecentFiles* d_recentFiles;
TypstDriver* d_driver;
QPdfDocument* d_previewDocument;

Editor* d_editor;
SearchBar* d_searchBar;
Previewer* d_previewer;
QPlainTextEdit* d_compilerOutput;
CompilerOutput* d_compilerOutput;

EditorSettingsDialog* d_editorSettingsDialog;

Expand Down
Loading

0 comments on commit 021328b

Please sign in to comment.