Commit e179d6a4 authored by hugo's avatar hugo
Browse files

add format with color

parent 75b38d68
......@@ -12,8 +12,10 @@ MainWindow::MainWindow( QWidget *parent )
, ui_ptr_{ new Ui::MainWindow }
, directory_{ fs::temp_directory_path() / "prototypator" }
, process_{ parent }
, plain_text_{ nullptr }
{
ui_ptr_->setupUi( this );
plain_text_.reset( ui_ptr_->plainTextEdit_compilation );
connect( &process_, &QProcess::readyReadStandardOutput, this, &MainWindow::update_std_out );
connect( &process_, &QProcess::readyReadStandardError, this, &MainWindow::update_std_err );
connect( &process_, SIGNAL(finished(int)), this, SLOT(finished(int)));
......@@ -65,7 +67,6 @@ void MainWindow::run()
{
// show compilation tab
ui_ptr_->tabWidget->setCurrentIndex( 0 );
const auto build_directory = directory_ / "build";
try {
......@@ -74,6 +75,9 @@ void MainWindow::run()
prepare();
}
// set plain text
plain_text_.reset( ui_ptr_->plainTextEdit_io );
// copy code
auto fos = std::ofstream{};
fos.exceptions( std::ifstream::failbit );
......@@ -108,21 +112,19 @@ void MainWindow::run()
void MainWindow::update_std_out()
{
const auto &text = process_.readAllStandardOutput();
ui_ptr_->plainTextEdit_io->textCursor().insertText( text );
append_text( process_.readAllStandardOutput().data(), Qt::black );
}
void MainWindow::update_std_err()
{
const auto &text = process_.readAllStandardError();
ui_ptr_->plainTextEdit_io->textCursor().insertText( text );
append_text( process_.readAllStandardError().data(), Qt::darkRed );
}
void MainWindow::finished( int exit_code )
{
auto os = std::ostringstream{};
os << tr( "Process finished. Exit code: " ).toStdString() << exit_code;
ui_ptr_->plainTextEdit_io->appendPlainText( QString::fromStdString( os.str() ) );
os << tr( "exited with code " ).toStdString() << exit_code;
append_text( os.str(), Qt::darkGreen );
}
MainWindow::~MainWindow() = default;
......@@ -147,3 +149,12 @@ void MainWindow::restore_settings()
ui_ptr_->textEdit_code->setText( settings.value( "code" ).toString() );
settings.endGroup();
}
void MainWindow::append_text( std::string_view text, const QColor &color )
{
auto format = QTextCharFormat{};
format.setForeground( QBrush{ color } );
auto cursor = plain_text_->textCursor();
cursor.movePosition( QTextCursor::End );
cursor.insertText( text.data(), format );
}
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <filesystem>
#include <memory>
#include <QMainWindow>
#include <QProcess>
#include <experimental/memory>
#include <filesystem>
#include <memory>
namespace Ui {
class MainWindow;
}
class QPlainTextEdit;
class MainWindow
: public QMainWindow
{
......@@ -31,12 +34,14 @@ protected:
private:
void restore_settings();
void append_text( std::string_view text, const QColor &color );
private:
std::unique_ptr<Ui::MainWindow> ui_ptr_;
bool is_prepared_ = false;
std::filesystem::path directory_;
QProcess process_;
std::experimental::observer_ptr<QPlainTextEdit> plain_text_;
};
#endif // MAINWINDOW_HPP
......@@ -34,8 +34,8 @@
<translation></translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.cpp" line="124"/>
<source>Process finished. Exit code: </source>
<location filename="../src/prototypator/mainwindow.cpp" line="126"/>
<source>exited with code </source>
<translation></translation>
</message>
</context>
......
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="fr_FR">
<TS version="2.1" language="fr_FR">
<context>
<name>MainWindow</name>
<message>
......@@ -34,9 +34,9 @@
<translation>Entrée/sortie</translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.cpp" line="124"/>
<source>Process finished. Exit code: </source>
<translation>Processus terminé. Code d&apos;erreur : </translation>
<location filename="../src/prototypator/mainwindow.cpp" line="126"/>
<source>exited with code </source>
<translation>terminé avec le code </translation>
</message>
</context>
</TS>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment